post_type, __( 'Help & Demos - Advanced Posts', 'advanced-post-block' ), '' . esc_html__( 'Help & Demos', 'advanced-post-block' ) . '', 'manage_options', 'advanced-post-block', [ \APBPlugin::class, 'renderDashboard' ] ); } /** * Enqueues scripts and styles for the admin area. * * @param string $hook The current admin page hook. * @return void */ public function adminEnqueueScripts( $hook ){ if( 'edit.php' === $hook || 'post.php' === $hook ){ wp_enqueue_style( 'apb-admin-post', APB_DIR_URL . 'build/admin/post.css', [], APB_VERSION ); wp_enqueue_script( 'apb-admin-post', APB_DIR_URL . 'build/admin/post.js', [], APB_VERSION, true ); wp_set_script_translations( 'apb-admin-post', 'advanced-post-block', APB_DIR_PATH . 'languages' ); } } /** * Registers the 'apb' custom post type. * * @return void */ public function onInit(){ $menuIcon = ""; register_post_type( $this->post_type, [ 'labels' => [ 'name' => __( 'Advanced Posts', 'advanced-post-block' ), 'singular_name' => __( 'ShortCode', 'advanced-post-block' ), 'all_items' => __( 'ShortCode Generator', 'advanced-post-block' ), 'add_new' => __( 'Add New ShortCode', 'advanced-post-block' ), 'add_new_item' => __( 'Add New ShortCode', 'advanced-post-block' ), 'edit_item' => __( 'Edit ShortCode', 'advanced-post-block' ), 'new_item' => __( 'New ShortCode', 'advanced-post-block' ), 'view_item' => __( 'View ShortCode', 'advanced-post-block' ), 'search_items' => __( 'Search ShortCode', 'advanced-post-block' ), 'not_found' => __( 'Sorry, we couldn\'t find the ShortCode you are looking for.', 'advanced-post-block' ) ], 'public' => false, 'show_ui' => true, 'show_in_rest' => true, 'publicly_queryable' => false, 'exclude_from_search' => true, 'menu_position' => 14, 'menu_icon' => 'data:image/svg+xml;base64,' . base64_encode( $menuIcon ), 'has_archive' => false, 'hierarchical' => false, 'capability_type' => 'page', 'rewrite' => [ 'slug' => 'apb' ], 'supports' => [ 'title', 'editor' ], 'template' => [ ['ap-block/posts'] ], 'template_lock' => 'all' ] ); // Register Post Type } /** * Renders the 'apb' shortcode. * * @param array $atts Shortcode attributes. * @return string Rendered shortcode content. */ public function onAddShortcode( $atts ) { $atts = shortcode_atts( [ 'id' => 0 ], $atts, 'apb' ); $post_id = absint( $atts['id'] ); if ( ! $post_id ) { return ''; } $post = get_post( $post_id ); if ( !$post ) { return ''; } if ( post_password_required( $post ) ) { return get_the_password_form( $post ); } switch ( $post->post_status ) { case 'publish': return $this->displayContent( $post ); case 'private': if ( current_user_can( 'read_private_posts' ) ) { return $this->displayContent( $post ); } return ''; case 'draft': case 'pending': case 'future': if ( current_user_can( 'edit_post', $post_id ) ) { return $this->displayContent( $post ); } return ''; default: return ''; } } /** * Parses and renders the first block of the post content. * * @param \WP_Post $post The post object. * @return string Rendered block content. */ public function displayContent( $post ){ $blocks = parse_blocks( $post->post_content ); global $allowedposttags; $allowed_html = wp_parse_args( ['style' => [] ], $allowedposttags ); return wp_kses( render_block( $blocks[0] ), $allowed_html ); } /** * Modifies the columns for the 'apb' post type list table. * * @param array $defaults The default columns. * @return array Modified columns. */ public function manageAPBPostsColumns( $defaults ) { unset( $defaults['date'] ); $defaults['shortcode'] = __( 'ShortCode', 'advanced-post-block' ); $defaults['date'] = __( 'Date', 'advanced-post-block' ); return $defaults; } /** * Renders custom column content for the 'apb' post type. * * @param string $column_name The name of the column. * @param int $post_ID The ID of the post. * @return void */ public function manageAPBPostsCustomColumns( $column_name, $post_ID ) { if ( 'shortcode' === $column_name ) { echo '
' . esc_html__( 'Copy To Clipboard', 'advanced-post-block' ) . '
'; } } /** * Forces the use of the block editor for the 'apb' post type. * * @param bool $use Whether to use the block editor. * @param \WP_Post $post The post object. * @return bool True if it's the 'apb' post type, otherwise the original value. */ public function useBlockEditorForPost( $use, $post ){ if ( is_object( $post ) && isset( $post->post_type ) && $this->post_type === $post->post_type ) { return true; } return $use; } } new CPT();