<?php
if ( ! class_exists( 'NISHIKI_PRO_POST_META_PR' ) ) {
	/**
	 * Post Meta 追加(パンくずリスト)
	 *
	 * @author     AnimaGate, Inc.
	 * @link       https://support.animagate.com/product/wp-nishiki-pro/
	 */
	class NISHIKI_PRO_POST_META_PR {
		/**
		 * 名前
		 *
		 * @var string
		 */
		private $name = 'nishiki_pro_meta_box_pr';

		/**
		 * Constructor.
		 */
		public function __construct() {
			add_action( 'init', array( $this, 'register_post_meta_fields' ), 30 );
		}

		public function register_post_meta_fields() {
			foreach ( nishiki_pro_get_all_post_types() as $post_type ) {
				// checkboxes
				foreach ( array( '_' . $this->name . '_label_', '_' . $this->name . '_before_content_' ) as $key ) {
					register_post_meta(
						$post_type,
						$key . $post_type,
						array(
							'show_in_rest'      => true,
							'single'            => true,
							'type'              => 'boolean',
							'auth_callback'     => array( $this, 'auth_callback' ),
							'sanitize_callback' => 'rest_sanitize_boolean',
						)
					);
				}

				// text fields
				foreach ( array( '_' . $this->name . '_custom_label_text_', '_' . $this->name . '_custom_before_content_text_' ) as $key ) {
					register_post_meta(
						$post_type,
						$key . $post_type,
						array(
							'show_in_rest'      => true,
							'single'            => true,
							'type'              => 'string',
							'auth_callback'     => array( $this, 'auth_callback' ),
							'sanitize_callback' => 'sanitize_text_field',
						)
					);
				}
			}
		}

		public function auth_callback( $allowed, $meta_key, $object_id ) {
			$post_type_object = get_post_type_object( get_post_type( $object_id ) );
			return $post_type_object ? current_user_can( $post_type_object->cap->edit_post, $object_id ) : false;
		}
	}
}

new NISHIKI_PRO_POST_META_PR();
