<?php
if ( ! class_exists( 'NISHIKI_PRO_POST_META_SHARE_BUTTON' ) ) {
	/**
	 * Post Meta 追加(シェアボタン)
	 *
	 * @author     AnimaGate, Inc.
	 * @link       https://support.animagate.com/product/wp-nishiki-pro/
	 */
	class NISHIKI_PRO_POST_META_SHARE_BUTTON {
		/**
		 * 名前
		 *
		 * @var string
		 */
		private $name = 'nishiki_pro_share_button_disable';

		/**
		 * 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 ) {
				register_post_meta(
					$post_type,
					'_' . $this->name . '_' . $post_type,
					array(
						'show_in_rest'      => true,
						'single'            => true,
						'type'              => 'boolean',
						'auth_callback'     => array( $this, 'auth_callback' ),
						'sanitize_callback' => 'rest_sanitize_boolean',
					)
				);
			}
		}

		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_SHARE_BUTTON();
