<?php
/**
 * Post_Type_Person Class
 *
 * @package dvcrs-core
 */

namespace DVCRS\Core;

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

/**
 * Post_Type_Person Class
 */
class Post_Type_Person {

	/**
	 * Post_Type_Person constuctor
	 */
	public function __construct() {

		\add_action( 'init', [ $this, 'register_post_type' ], 1 );

		\add_filter( 'manage_person_posts_columns', [ $this, 'person_columns' ], 10, 1 );
		\add_filter( 'manage_person_posts_custom_column', [ $this, 'person_columns_data' ], 10, 2 );
		\add_filter( 'manage_edit-person_sortable_columns', [ $this, 'person_columns_sort' ], 10, 1 );

	}

	/**
	 * Registers the custom post type
	 */
	public function register_post_type() {

		$labels = [
			'name'                  => _x( 'Staff', 'Post Type General Name', 'dvcrs-core' ),
			'singular_name'         => _x( 'Person', 'Post Type Singular Name', 'dvcrs-core' ),
			'menu_name'             => __( 'Staff', 'dvcrs-core' ),
			'name_admin_bar'        => __( 'Person', 'dvcrs-core' ),
			'archives'              => __( 'Person Archives', 'dvcrs-core' ),
			'attributes'            => __( 'Person Attributes', 'dvcrs-core' ),
			'parent_item_colon'     => __( 'Parent Person:', 'dvcrs-core' ),
			'all_items'             => __( 'All Staff', 'dvcrs-core' ),
			'add_new_item'          => __( 'Add New Person', 'dvcrs-core' ),
			'add_new'               => __( 'Add New', 'dvcrs-core' ),
			'new_item'              => __( 'New Person', 'dvcrs-core' ),
			'edit_item'             => __( 'Edit Person', 'dvcrs-core' ),
			'update_item'           => __( 'Update Person', 'dvcrs-core' ),
			'view_item'             => __( 'View Person', 'dvcrs-core' ),
			'view_items'            => __( 'View Staff', 'dvcrs-core' ),
			'search_items'          => __( 'Search Staff', 'dvcrs-core' ),
			'not_found'             => __( 'Not found', 'dvcrs-core' ),
			'not_found_in_trash'    => __( 'Not found in Trash', 'dvcrs-core' ),
			'featured_image'        => __( 'Fetaured Image', 'dvcrs-core' ),
			'set_featured_image'    => __( 'Set featured image', 'dvcrs-core' ),
			'remove_featured_image' => __( 'Remove featured image', 'dvcrs-core' ),
			'use_featured_image'    => __( 'Use as featured image', 'dvcrs-core' ),
			'insert_into_item'      => __( 'Add to Person', 'dvcrs-core' ),
			'uploaded_to_this_item' => __( 'Uploaded to this Person', 'dvcrs-core' ),
			'items_list'            => __( 'Person list', 'dvcrs-core' ),
			'items_list_navigation' => __( 'Person list navigation', 'dvcrs-core' ),
			'filter_items_list'     => __( 'Filter Person list', 'dvcrs-core' ),
		];
		$args   = [
			'label'                         => __( 'Staff', 'dvcrs-core' ),
			'description'                   => __( 'Staff at DVC Rental Store', 'dvcrs-core' ),
			'labels'                        => $labels,
			'supports'                      => [ 'title', 'editor', 'excerpt', 'thumbnail', 'page-attributes' ],
			'hierarchical'                  => true,
			'menu_icon'                     => 'dashicons-id',
			'public'                        => true,
			'show_in_rest'                  => true,
			'has_archive'                   => true,
			'has_post_type_page'            => true,
			'post_type_page_disable_editor' => false,
		];

		register_post_type( 'person', $args );
	}

	/**
	 * Set custom person columns in admin
	 *
	 * @param array $columns An associative array of column headings.
	 *
	 * @return array
	 */
	public function person_columns( $columns ) {

		$cb    = array_slice( $columns, 0, 1 );
		$title = array_slice( $columns, 1, 1 );
		$etc   = array_slice( $columns, 2 );

		$person_columns['menu_order'] = __( 'Order', 'dvcrs-core' );

		$columns = array_merge( $cb, $title, $person_columns, $etc );

		return $columns;
	}

	/**
	 * Set custom person column data in admin
	 *
	 * @param string $column The name of the column to display.
	 * @param int    $post_id The current post ID.
	 */
	public function person_columns_data( $column, $post_id ) {

		switch ( $column ) {
			case 'menu_order':
				echo esc_html( get_post_field( $column, $post_id ) );
				break;
		}

	}

	/**
	 * Set sortable columns
	 *
	 * @param array $columns An array of sortable columns.
	 *
	 * @return array
	 */
	public function person_columns_sort( $columns ) {

		$columns['menu_order'] = 'menu_order';

		return $columns;
	}
}

new Post_Type_Person();
