$label ) { $ordered[ $key ] = $label; if ( 'title' === $key ) { $ordered['cvp_shortcode'] = __( 'Shortcode', 'camelplug-video-player' ); } } return $ordered; } /** * Prints the shortcode of a row as a small click-to-copy button. * * @param string $column Column key. * @param int $post_id Row post ID. */ public function list_column_content( $column, $post_id ) { if ( 'cvp_shortcode' !== $column ) { return; } $shortcode = sprintf( '[%s id="%d"]', self::TAG, $post_id ); printf( '', esc_attr( $shortcode ), esc_attr__( 'Click to copy', 'camelplug-video-player' ), esc_html( $shortcode ) ); } /** * Styles and click-to-copy behaviour of the Shortcode column. */ public function list_copy_script() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || 'edit-' . self::POST_TYPE !== $screen->id ) { return; } ?> post_type ) { echo ''; } } /** * Registers the shortcode and its post type. */ public function register() { add_shortcode( self::TAG, array( $this, 'render' ) ); register_post_type( self::POST_TYPE, array( 'labels' => array( 'name' => __( 'Video Player Shortcodes', 'camelplug-video-player' ), 'singular_name' => __( 'Video Player Shortcode', 'camelplug-video-player' ), 'add_new' => __( 'Add New Shortcode', 'camelplug-video-player' ), 'add_new_item' => __( 'Add New Shortcode', 'camelplug-video-player' ), 'edit_item' => __( 'Edit Shortcode', 'camelplug-video-player' ), 'new_item' => __( 'New Shortcode', 'camelplug-video-player' ), 'search_items' => __( 'Search Shortcodes', 'camelplug-video-player' ), 'not_found' => __( 'No shortcodes found.', 'camelplug-video-player' ), 'not_found_in_trash' => __( 'No shortcodes found in Trash.', 'camelplug-video-player' ), 'item_published' => __( 'Shortcode published.', 'camelplug-video-player' ), 'item_updated' => __( 'Shortcode updated.', 'camelplug-video-player' ), ), 'public' => false, 'show_ui' => true, 'show_in_menu' => false, 'show_in_rest' => true, 'rewrite' => false, 'supports' => array( 'title', 'editor' ), // Exactly one Video Player block; nothing can be added, moved or removed. 'template' => array( array( self::BLOCK_NAME ) ), 'template_lock' => 'all', ) ); } /** * Only the Video Player block is available in the shortcode editor. * * @param bool|string[] $allowed Allowed block types. * @param WP_Block_Editor_Context $context Editor context. * @return bool|string[] Allowed block types. */ public function allowed_blocks( $allowed, $context ) { if ( isset( $context->post ) && $context->post instanceof WP_Post && self::POST_TYPE === $context->post->post_type ) { return array( self::BLOCK_NAME ); } return $allowed; } /** * Renders the shortcode. * * @param array|string $atts Shortcode attributes. * @return string Block markup, or an empty string when it cannot be shown. */ public function render( $atts ) { $atts = shortcode_atts( array( 'id' => 0, ), $atts, self::TAG ); $post_id = absint( $atts['id'] ); return $post_id ? $this->render_post( $post_id ) : ''; } /** * Renders a published shortcode post (or Synced Pattern) that contains the block. * * @param int $post_id Shortcode post ID. * @return string Block markup, or an empty string when it cannot be shown. */ private function render_post( $post_id ) { $post = get_post( $post_id ); if ( ! $post || ! in_array( $post->post_type, self::SOURCE_TYPES, true ) || 'publish' !== $post->post_status || post_password_required( $post ) || ! has_block( self::BLOCK_NAME, $post ) || in_array( $post_id, self::$rendering, true ) ) { return ''; } self::$rendering[] = $post_id; $output = do_blocks( $post->post_content ); array_pop( self::$rendering ); return $output; } } }