<?php
if ( ! class_exists( 'NISHIKI_PRO_POST_META_NOFOLLOW' ) ) {
	/**
	 * Add Post Meta(nofollow)
	 *
	 * @author     AnimaGate, Inc.
	 * @link       https://support.animagate.com/product/wp-nishiki-pro/
	 */
	class NISHIKI_PRO_POST_META_NOFOLLOW {
		/**
		 * Name
		 *
		 * @var string
		 */
		private $name = 'nishiki_pro_meta_box_nofollow';

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

		public function register_post_meta_fields() {
			$post_types = nishiki_pro_get_all_post_types();
			foreach ( $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 = get_post_type( $object_id );
			if ( ! $post_type ) { return false; }
			$post_type_object = get_post_type_object( $post_type );
			if ( ! $post_type_object ) { return false; }
			return current_user_can( $post_type_object->cap->edit_post, $object_id );
		}

	}
}

new NISHIKI_PRO_POST_META_NOFOLLOW();
