From 54e873a52e89323396d04d6d2aa49ecaeca15eb2 Mon Sep 17 00:00:00 2001 From: Ravi Chandra Date: Thu, 18 Mar 2021 17:22:46 +0530 Subject: [PATCH] Code refactor and cleanup --- plugin/php/blocks/class-posts-list-block.php | 17 +++++++++++++++-- plugin/php/class-block-types.php | 3 ++- plugin/php/rest/class-posts-rest-controller.php | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/plugin/php/blocks/class-posts-list-block.php b/plugin/php/blocks/class-posts-list-block.php index 1bc4020d..eedc3265 100644 --- a/plugin/php/blocks/class-posts-list-block.php +++ b/plugin/php/blocks/class-posts-list-block.php @@ -72,7 +72,7 @@ public function init() { * Register hooks. */ public function register_hooks() { - foreach ( self::get_supported_post_types() as $post_type ) { + foreach ( array_keys( self::get_supported_post_types() ) as $post_type ) { add_filter( "rest_prepare_$post_type", [ $this, 'add_extra_post_meta' ], 10, 3 ); } } @@ -213,7 +213,20 @@ public function render_block( $attributes ) { public static function get_supported_post_types() { return apply_filters( 'material_design_query_post_types', - get_post_types( [ 'show_in_rest' => true ] ) + array_filter( + get_post_types( [ 'show_in_rest' => true ], 'objects' ), + function( $post_type ) { + // Ignore attachment and wp_block post types. + return ! array_key_exists( + $post_type, + [ + 'attachment' => 1, + 'wp_block' => 1, + ] + ); + }, + ARRAY_FILTER_USE_KEY + ) ); } } diff --git a/plugin/php/class-block-types.php b/plugin/php/class-block-types.php index 5e3d4825..77c7aeb2 100644 --- a/plugin/php/class-block-types.php +++ b/plugin/php/class-block-types.php @@ -185,7 +185,8 @@ public function enqueue_block_editor_assets() { ]; // Only find post types that are available in_rest, and parse them into expected data structure. - $post_type_objects = get_post_types( [ 'show_in_rest' => true ], 'objects' ); + $post_type_objects = Posts_List_Block::get_supported_post_types(); + foreach ( $post_type_objects as $slug => $post_type ) { $controller = $this->get_rest_controller( $post_type ); diff --git a/plugin/php/rest/class-posts-rest-controller.php b/plugin/php/rest/class-posts-rest-controller.php index 13a71761..151e993a 100644 --- a/plugin/php/rest/class-posts-rest-controller.php +++ b/plugin/php/rest/class-posts-rest-controller.php @@ -92,7 +92,7 @@ public function register_routes() { * @param \WP_REST_Request $request Request object. */ public function query_multiple_post_types( \WP_REST_Request $request ) { - $post_types = Posts_List_Block::get_supported_post_types(); + $post_types = array_keys( Posts_List_Block::get_supported_post_types() ); $post_type = $request->get_param( 'post_type' ); if ( 'posts-pages' !== $post_type && ! in_array( $post_type, (array) $post_types, true ) ) {