Skip to content

Commit

Permalink
Code refactor and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ravichdev committed Mar 18, 2021
1 parent b169173 commit 54e873a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
17 changes: 15 additions & 2 deletions plugin/php/blocks/class-posts-list-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Expand Down Expand Up @@ -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
)
);
}
}
3 changes: 2 additions & 1 deletion plugin/php/class-block-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
2 changes: 1 addition & 1 deletion plugin/php/rest/class-posts-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down

0 comments on commit 54e873a

Please sign in to comment.