<?php
/**
 * RSM Button custom post type.
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

/**
 * Rsmspark_Buttons_CPT
 */
class Rsmspark_Buttons_CPT {

	/**
	 * @return void
	 */
	public static function register() {
		register_post_type(
			Rsmspark_Buttons_Config::CPT,
			array(
				'labels'              => array(
					'name'          => __( 'RSM Buttons', 'rsm-content-widgets' ),
					'singular_name' => __( 'RSM Button', 'rsm-content-widgets' ),
					'add_new_item'  => __( 'Add New Button', 'rsm-content-widgets' ),
					'edit_item'     => __( 'Edit Button', 'rsm-content-widgets' ),
				),
				'public'              => false,
				'show_ui'             => false,
				'show_in_menu'        => false,
				'show_in_rest'        => false,
				'capability_type'     => 'post',
				'map_meta_cap'        => true,
				'hierarchical'        => false,
				'supports'            => array( 'title' ),
				'has_archive'         => false,
				'rewrite'             => false,
				'delete_with_user'    => false,
			)
		);
	}

	/**
	 * @return void
	 */
	public static function register_capabilities() {
		$roles = array( 'administrator', 'editor' );
		foreach ( $roles as $role_slug ) {
			$role = get_role( $role_slug );
			if ( $role ) {
				$role->add_cap( 'manage_rsm_buttons' );
			}
		}
	}

	/**
	 * @return array<int, string>
	 */
	public static function get_post_types() {
		$types = array(
			Rsmspark_Buttons_Config::CPT,
			'rsm_buttons',
		);

		/**
		 * @param array<int, string> $types Supported button post types.
		 */
		return apply_filters( 'rsmspark_button_post_types', array_values( array_unique( $types ) ) );
	}

	/**
	 * @param int $post_id Post ID.
	 * @return bool
	 */
	public static function is_button_post( $post_id ) {
		$post_id = absint( $post_id );
		if ( $post_id <= 0 ) {
			return false;
		}

		return in_array( get_post_type( $post_id ), self::get_post_types(), true );
	}
}
