* @since 3.0.0 */ namespace Beyondwords\Wordpress\Component\Post\Panel\Inspect; use Beyondwords\Wordpress\Component\Post\PostMetaUtils; use Beyondwords\Wordpress\Component\Settings\SettingsUtils; /** * Inspect setup * * @since 3.0.0 */ class Inspect { /** * Constructor */ public function __construct() { add_action('admin_enqueue_scripts', array($this, 'adminEnqueueScripts')); add_action("add_meta_boxes", array($this, 'addMetaBox')); add_filter('default_hidden_meta_boxes', array($this, 'hideMetaBox')); add_action('wp_loaded', function () { $postTypes = SettingsUtils::getSupportedPostTypes(); if (is_array($postTypes)) { foreach ($postTypes as $postType) { add_action("save_post_{$postType}", array($this, 'save'), 5); } } }); } /** * Enqueue JS for Inspect feature. */ public function adminEnqueueScripts($hook) { // Only enqueue for Post screens if ($hook === 'post.php' || $hook === 'post-new.php') { // Clipboard.js dependency wp_enqueue_script( 'clipboard', 'https://cdn.jsdelivr.net/npm/clipboard@2.0.10/dist/clipboard.min.js', array('jquery'), null, true ); // Our script for the "Copy" button wp_enqueue_script( 'beyondwords-inspect', BEYONDWORDS__PLUGIN_URI . 'src/Component/Post/Panel/Inspect/js/inspect.js', array('jquery', 'clipboard'), BEYONDWORDS__PLUGIN_VERSION, true ); } } /** * Hides the metabox by default. * * @param string[] $hidden An array of IDs of meta boxes hidden by default. */ public function hideMetaBox($hidden) { $hidden[] = 'beyondwords__inspect'; return $hidden; } /** * Adds the meta box container for the Classic Editor. * * The Block Editor UI is handled using JavaScript. * * @param $postType */ public function addMetaBox($postType) { $postTypes = SettingsUtils::getSupportedPostTypes(); if (is_array($postTypes) && ! in_array($postType, $postTypes)) { return; } add_meta_box( 'beyondwords__inspect', __('BeyondWords', 'speechkit') . ': ' . __('Inspect', 'speechkit'), array($this, 'renderMetaBoxContent'), $postType, 'advanced', 'low', [ '__back_compat_meta_box' => true, ] ); } /** * Render Meta Box content. * * @param WP_Post $post The post object. */ public function renderMetaBoxContent($post) { $metadata = PostMetaUtils::getAllBeyondwordsMetadata($post->ID); $this->postMetaTable($metadata); ?>
formatPostMetaValue($item['meta_value']); ?>
formatPostMetaValue($m['meta_value']); } $lines[] = "=== " . __('Copied using the Classic Editor', 'speechkit') . " ===\r\n\r\n"; return implode("\r\n\r\n", $lines); } /** * Optionally remove the data when a post is saved. * * @since 4.0.7 * * @param int $postId The ID of the post being saved. */ public function save($postId) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $postId; } // "save_post" can be triggered at other times, so verify this request came from the our component if ( ! isset($_POST['beyondwords_inspect_remove_nonce']) || ! wp_verify_nonce( sanitize_text_field($_POST['beyondwords_inspect_remove_nonce']), 'beyondwords_inspect_remove' ) ) { return $postId; } if (isset($_POST['beyondwords_inspect_remove'])) { PostMetaUtils::removeAllBeyondwordsMetadata($postId); } return $postId; } }