<?php
/**
 * Testimonial custom post type (admin-managed library).
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

/**
 * Rsmspark_Testimonials_CPT
 */
class Rsmspark_Testimonials_CPT {

	const CPT       = 'rsmspark_testimonial';
	const META_ROLE = '_rsmspark_testimonial_role';

	/**
	 * @return void
	 */
	public static function register() {
		register_post_type(
			self::CPT,
			array(
				'labels'              => array(
					'name'               => __( 'Testimonials', 'rsm-content-widgets' ),
					'singular_name'      => __( 'Testimonial', 'rsm-content-widgets' ),
					'add_new'            => __( 'Add New', 'rsm-content-widgets' ),
					'add_new_item'       => __( 'Add New Testimonial', 'rsm-content-widgets' ),
					'edit_item'          => __( 'Edit Testimonial', 'rsm-content-widgets' ),
					'new_item'           => __( 'New Testimonial', 'rsm-content-widgets' ),
					'view_item'          => __( 'View Testimonial', 'rsm-content-widgets' ),
					'search_items'       => __( 'Search Testimonials', 'rsm-content-widgets' ),
					'not_found'          => __( 'No testimonials found.', 'rsm-content-widgets' ),
					'not_found_in_trash' => __( 'No testimonials found in Trash.', 'rsm-content-widgets' ),
					'menu_name'          => __( 'Testimonials', 'rsm-content-widgets' ),
				),
				'public'              => false,
				'show_ui'             => true,
				'show_in_menu'        => false,
				'show_in_rest'        => true,
				'capability_type'     => 'post',
				'map_meta_cap'        => true,
				'hierarchical'        => false,
				'supports'            => array( 'title', 'editor', 'excerpt', 'thumbnail', 'page-attributes' ),
				'has_archive'         => false,
				'rewrite'             => false,
				'delete_with_user'    => false,
			)
		);
	}

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

		return self::CPT === get_post_type( $post_id );
	}

	/**
	 * @param int $post_id Post ID.
	 * @return string
	 */
	public static function get_role( $post_id ) {
		return sanitize_text_field( (string) get_post_meta( absint( $post_id ), self::META_ROLE, true ) );
	}

	/**
	 * @param int    $post_id Post ID.
	 * @param string $role    Role / title.
	 * @return void
	 */
	public static function set_role( $post_id, $role ) {
		update_post_meta( absint( $post_id ), self::META_ROLE, sanitize_text_field( $role ) );
	}
}
