__( 'Image Gallery', 'image-gallery' ), 'labels' => [ 'name' => __( 'Image Gallery', 'image-gallery' ), 'singular_name' => __( 'Image Gallery', 'image-gallery' ), 'add_new' => __( 'Add New', 'image-gallery' ), 'add_new_item' => __( 'Add New Image Gallery', 'image-gallery' ), 'edit_item' => __( 'Edit Image Gallery', 'image-gallery' ), 'new_item' => __( 'New Image Gallery', 'image-gallery' ), 'view_item' => __( 'View Image Gallery', 'image-gallery' ), 'view_items' => __( 'View Image Gallery', 'image-gallery' ), 'search_items' => __( 'Search Image Gallery', 'image-gallery' ), 'not_found' => __( 'No Image Gallery found.', 'image-gallery' ), 'not_found_in_trash' => __( 'No Image Gallery found in Trash.', 'image-gallery' ), 'all_items' => __( 'All Image Gallery', 'image-gallery' ), 'archives' => __( 'Image Gallery Archives', 'image-gallery' ), 'item_published' => __( 'Image Gallery Published', 'image-gallery' ), 'item_updated' => __( 'Image Gallery Updated', 'image-gallery' ), ], 'show_in_rest' => true, 'public' => true, 'menu_icon' => 'dashicons-format-gallery', 'menu_position' => 25, // A gallery post is a container to be placed by shortcode, not a // page to be visited. Without this it would answer on its own URL // as a bare gallery with no theme around it, and land in search // results as a page nobody meant to publish. 'publicly_queryable' => false, 'has_archive' => false, 'template' => [ [ self::BLOCK ] ], 'template_lock' => 'all', ] ); } /** * `[image_gallery id="12"]` * * @param array $atts Shortcode attributes. * @return string */ public function shortcode( $atts ) { $atts = shortcode_atts( [ 'id' => 0 ], $atts, 'image_gallery' ); $post_id = absint( $atts['id'] ); if ( ! $post_id ) { return ''; } $post = get_post( $post_id ); if ( ! $post || self::POST_TYPE !== $post->post_type ) { return ''; } if ( post_password_required( $post ) ) { return get_the_password_form( $post ); } /* * The shortcode obeys the post's status, which is the whole reason for * the switch rather than a single `'publish' === $post->post_status`. * A draft gallery placed on a live page must not show to visitors, but * must show to the person editing it -- otherwise there is no way to * preview one before publishing. */ switch ( $post->post_status ) { case 'publish': return $this->displayContent( $post ); case 'private': return current_user_can( 'read_post', $post_id ) ? $this->displayContent( $post ) : ''; case 'draft': case 'pending': case 'future': return current_user_can( 'edit_post', $post_id ) ? $this->displayContent( $post ) : ''; default: return ''; } } /** * Render the gallery block a post holds. * * The block name is checked and `render_block()` does the work, so there is * exactly one rendering path: whatever the block does in a page, it does * here. Running the output through `wp_kses_post()` instead would be worse * than useless -- render.php escapes everything it prints, and kses allows * no `