<?php
/**
 * Directory profile custom post type.
 *
 * @package LanternSpanPeopleDirectory
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Registers and configures Lantern Span People Directory directory profiles.
 */
class LSPD_Post_Type {

	/**
	 * Post type key.
	 *
	 * @var string
	 */
	const POST_TYPE = 'lspd_person';

	/**
	 * Registers hooks.
	 */
	public function __construct() {
		add_action( 'init', array( __CLASS__, 'register_post_type' ), 20 );
		add_filter( 'enter_title_here', array( $this, 'filter_title_placeholder' ), 10, 2 );
		add_filter( 'post_updated_messages', array( $this, 'filter_updated_messages' ) );
		add_action( 'pre_get_posts', array( $this, 'order_admin_members' ) );
	}

	/**
	 * Registers the directory profile custom post type.
	 *
	 * @return void
	 */
	public static function register_post_type() {
		$labels = array(
			'name'                  => __( 'People', 'lantern-span-people-directory' ),
			'singular_name'         => __( 'Person', 'lantern-span-people-directory' ),
			'menu_name'             => __( 'People Directory', 'lantern-span-people-directory' ),
			'name_admin_bar'        => __( 'Person', 'lantern-span-people-directory' ),
			'add_new'               => __( 'Add New', 'lantern-span-people-directory' ),
			'add_new_item'          => __( 'Add New Person', 'lantern-span-people-directory' ),
			'new_item'              => __( 'New Person', 'lantern-span-people-directory' ),
			'edit_item'             => __( 'Edit Person', 'lantern-span-people-directory' ),
			'view_item'             => __( 'View Person', 'lantern-span-people-directory' ),
			'all_items'             => __( 'All People', 'lantern-span-people-directory' ),
			'search_items'          => __( 'Search People', 'lantern-span-people-directory' ),
			'not_found'             => __( 'No people found.', 'lantern-span-people-directory' ),
			'not_found_in_trash'    => __( 'No people found in Trash.', 'lantern-span-people-directory' ),
			'featured_image'        => __( 'Profile Photo', 'lantern-span-people-directory' ),
			'set_featured_image'    => __( 'Set profile photo', 'lantern-span-people-directory' ),
			'remove_featured_image' => __( 'Remove profile photo', 'lantern-span-people-directory' ),
			'use_featured_image'    => __( 'Use as profile photo', 'lantern-span-people-directory' ),
			'archives'              => __( 'Profile Archives', 'lantern-span-people-directory' ),
		);

		$capabilities = array(
			'edit_post'              => 'edit_lspd_person',
			'read_post'              => 'read_lspd_person',
			'delete_post'            => 'delete_lspd_person',
			'edit_posts'             => 'edit_lspd_people',
			'edit_others_posts'      => 'edit_others_lspd_people',
			'publish_posts'          => 'publish_lspd_people',
			'read_private_posts'     => 'read_private_lspd_people',
			'delete_posts'           => 'delete_lspd_people',
			'delete_private_posts'   => 'delete_private_lspd_people',
			'delete_published_posts' => 'delete_published_lspd_people',
			'delete_others_posts'    => 'delete_others_lspd_people',
			'edit_private_posts'     => 'edit_private_lspd_people',
			'edit_published_posts'   => 'edit_published_lspd_people',
			'create_posts'           => 'create_lspd_people',
		);

		register_post_type(
			self::POST_TYPE,
			array(
				'labels'              => $labels,
				'description'         => __( 'People shown in this photo and contact directory.', 'lantern-span-people-directory' ),
				'public'              => false,
				'publicly_queryable'  => false,
				'exclude_from_search' => true,
				'show_ui'             => true,
				'show_in_menu'        => true,
				'show_in_admin_bar'   => false,
				'show_in_nav_menus'   => false,
				'show_in_rest'        => false,
				'menu_position'       => 26,
				'menu_icon'           => 'dashicons-groups',
				'supports'            => array( 'title', 'editor', 'thumbnail' ),
				'has_archive'         => false,
				'rewrite'             => false,
				'query_var'           => false,
				'capability_type'     => array( 'lspd_person', 'lspd_people' ),
				'capabilities'        => $capabilities,
				'map_meta_cap'        => true,
			)
		);
	}

	/**
	 * Changes the title field placeholder for directory profiles.
	 *
	 * @param string  $placeholder Existing placeholder.
	 * @param WP_Post $post        Current post.
	 * @return string
	 */
	public function filter_title_placeholder( $placeholder, $post ) {
		if ( $post instanceof WP_Post && self::POST_TYPE === $post->post_type ) {
			return __( 'Enter the person’s full name', 'lantern-span-people-directory' );
		}

		return $placeholder;
	}

	/**
	 * Customizes post-updated messages for directory profiles.
	 *
	 * @param array<int, array<int, string>> $messages Existing messages.
	 * @return array<int, array<int, string>>
	 */
	public function filter_updated_messages( $messages ) {
		$messages[ self::POST_TYPE ] = array(
			0  => '',
			1  => __( 'Profile updated.', 'lantern-span-people-directory' ),
			2  => __( 'Custom field updated.', 'lantern-span-people-directory' ),
			3  => __( 'Custom field deleted.', 'lantern-span-people-directory' ),
			4  => __( 'Profile updated.', 'lantern-span-people-directory' ),
			5  => __( 'Profile restored to an earlier revision.', 'lantern-span-people-directory' ),
			6  => __( 'Profile published.', 'lantern-span-people-directory' ),
			7  => __( 'Profile saved.', 'lantern-span-people-directory' ),
			8  => __( 'Profile submitted.', 'lantern-span-people-directory' ),
			9  => __( 'Profile scheduled.', 'lantern-span-people-directory' ),
			10 => __( 'Profile draft updated.', 'lantern-span-people-directory' ),
		);

		return $messages;
	}

	/**
	 * Orders the admin profile list alphabetically by title.
	 *
	 * @param WP_Query $query Current query.
	 * @return void
	 */
	public function order_admin_members( $query ) {
		if ( ! is_admin() || ! $query->is_main_query() ) {
			return;
		}

		if ( self::POST_TYPE !== $query->get( 'post_type' ) || $query->get( 'orderby' ) ) {
			return;
		}

		$query->set( 'orderby', 'title' );
		$query->set( 'order', 'ASC' );
	}
}
