* @since 6.3.0 */ namespace Beyondwords\Wordpress\Component\Post\ContentId; use Beyondwords\Wordpress\Component\Post\PostMetaUtils; use Beyondwords\Wordpress\Component\Settings\SettingsUtils; use Beyondwords\Wordpress\Core\CoreUtils; /** * ContentId * * @since 6.3.0 */ defined('ABSPATH') || exit; class ContentId { /** * Init. * * @since 6.3.0 */ public static function init() { add_action('admin_enqueue_scripts', [self::class, 'adminEnqueueScripts']); add_action('wp_loaded', function (): void { $postTypes = SettingsUtils::getCompatiblePostTypes(); if (is_array($postTypes)) { foreach ($postTypes as $postType) { add_action("save_post_{$postType}", [self::class, 'save'], 10); } } }); } /** * HTML output for this component. * * @since 6.3.0 * * @param \WP_Post $post The post object. */ public static function element($post) { $contentId = PostMetaUtils::getContentId($post->ID) ?: ''; $projectId = PostMetaUtils::getProjectId($post->ID) ?: get_option('beyondwords_project_id', ''); $postType = get_post_type($post); $postTypeObj = $postType ? get_post_type_object($postType) : null; $restBase = ($postTypeObj && ! empty($postTypeObj->rest_base)) ? $postTypeObj->rest_base : $postType; wp_nonce_field('beyondwords_content_id', 'beyondwords_content_id_nonce'); ?>

wp_create_nonce('wp_rest'), 'root' => esc_url_raw(rest_url()), ] ); wp_enqueue_script('beyondwords-metabox--content-id'); } } /** * Save the meta when the post is saved. * * @since 6.3.0 * * @param int $postId The ID of the post being saved. */ public static function save($postId) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $postId; } if ( ! isset($_POST['beyondwords_content_id_nonce']) || ! wp_verify_nonce( sanitize_key($_POST['beyondwords_content_id_nonce']), 'beyondwords_content_id' ) ) { return $postId; } if (isset($_POST['beyondwords_content_id'])) { update_post_meta( $postId, 'beyondwords_content_id', sanitize_text_field(wp_unslash($_POST['beyondwords_content_id'])) ); } return $postId; } }