Skip to content

Commit

Permalink
Move REST API functions to rest-api.php
Browse files Browse the repository at this point in the history
  • Loading branch information
huubl committed Apr 23, 2024
1 parent 785b0dd commit 376178f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 45 deletions.
50 changes: 50 additions & 0 deletions lib/compat/wordpress-6.6/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,53 @@ function wp_api_template_access_controller( $args, $post_type ) {
}
}
add_filter( 'register_post_type_args', 'wp_api_template_access_controller', 10, 2 );

/**
* Registers the REST route for retrieving the post classes of a post.
*/
function gutenberg_register_post_class_rest_route() {
register_rest_route(
'wp/v2',
'/postclass/(?P<id>\d+)',
array(
'methods' => WP_REST_Server::READABLE,
'permission_callback' => '__return_true',
'callback' => 'gutenberg_get_post_classes',
)
);
}
add_action( 'rest_api_init', 'gutenberg_register_post_class_rest_route' );

/**
* Callback function for the REST route.
*
* @param WP_REST_Request $request Current request.
*/
function gutenberg_get_post_classes( WP_REST_Request $request ) {
$id = (int) $request['id'];
$post = get_post( $id );
$post_type = get_post_type( $post );
$valid_post_types = get_post_types(
array(
'public' => true,
'show_in_rest' => true,
)
);

if ( ! in_array( $post_type, $valid_post_types, true ) ) {
return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
}

$controller = new WP_REST_Posts_Controller( $post_type );

$check = $controller->get_item_permissions_check( $request );

if ( $check !== true ) {

Check failure on line 73 in lib/compat/wordpress-6.6/rest-api.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Use Yoda Condition checks, you must.
return $check;
}

// Get the post classes and return them
$post_classes = get_post_class( '', $id );

return $post_classes;
}
45 changes: 0 additions & 45 deletions packages/block-library/src/post-template/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,48 +160,3 @@ function register_block_core_post_template() {
);
}
add_action( 'init', 'register_block_core_post_template' );

/**
* Registers the REST route for retrieving the post classes of a post.
*
* @since 6.6.0?
*/
function register_post_class_rest_route() {
register_rest_route(
'wp/v2',
'/postclass/(?P<id>\d+)',
array(
'methods' => WP_REST_Server::READABLE,
'permission_callback' => '__return_true',
'callback' => function ( WP_REST_Request $request ) {
$id = (int) $request['id'];
$post = get_post( $id );
$post_type = get_post_type( $post );
$valid_post_types = get_post_types(
array(
'public' => true,
'show_in_rest' => true,
)
);

if ( ! in_array( $post_type, $valid_post_types, true ) ) {
return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) );
}

$controller = new WP_REST_Posts_Controller( $post_type );

$check = $controller->get_item_permissions_check( $request );

if ( $check !== true ) {
return $check;
}

// Get the post classes and return them
$post_classes = get_post_class( '', $id );

return $post_classes;
},
)
);
}
add_action( 'rest_api_init', 'register_post_class_rest_route' );

0 comments on commit 376178f

Please sign in to comment.