Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-inclu.../rest-api/endpoint...
File: class-wp-rest-posts-controller.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* REST API: WP_REST_Posts_Controller class
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage REST_API
[5] Fix | Delete
* @since 4.7.0
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Core class to access posts via the REST API.
[10] Fix | Delete
*
[11] Fix | Delete
* @since 4.7.0
[12] Fix | Delete
*
[13] Fix | Delete
* @see WP_REST_Controller
[14] Fix | Delete
*/
[15] Fix | Delete
class WP_REST_Posts_Controller extends WP_REST_Controller {
[16] Fix | Delete
/**
[17] Fix | Delete
* Post type.
[18] Fix | Delete
*
[19] Fix | Delete
* @since 4.7.0
[20] Fix | Delete
* @var string
[21] Fix | Delete
*/
[22] Fix | Delete
protected $post_type;
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* Instance of a post meta fields object.
[26] Fix | Delete
*
[27] Fix | Delete
* @since 4.7.0
[28] Fix | Delete
* @var WP_REST_Post_Meta_Fields
[29] Fix | Delete
*/
[30] Fix | Delete
protected $meta;
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* Passwordless post access permitted.
[34] Fix | Delete
*
[35] Fix | Delete
* @since 5.7.1
[36] Fix | Delete
* @var int[]
[37] Fix | Delete
*/
[38] Fix | Delete
protected $password_check_passed = array();
[39] Fix | Delete
[40] Fix | Delete
/**
[41] Fix | Delete
* Whether the controller supports batching.
[42] Fix | Delete
*
[43] Fix | Delete
* @since 5.9.0
[44] Fix | Delete
* @var array
[45] Fix | Delete
*/
[46] Fix | Delete
protected $allow_batch = array( 'v1' => true );
[47] Fix | Delete
[48] Fix | Delete
/**
[49] Fix | Delete
* Constructor.
[50] Fix | Delete
*
[51] Fix | Delete
* @since 4.7.0
[52] Fix | Delete
*
[53] Fix | Delete
* @param string $post_type Post type.
[54] Fix | Delete
*/
[55] Fix | Delete
public function __construct( $post_type ) {
[56] Fix | Delete
$this->post_type = $post_type;
[57] Fix | Delete
$obj = get_post_type_object( $post_type );
[58] Fix | Delete
$this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
[59] Fix | Delete
$this->namespace = ! empty( $obj->rest_namespace ) ? $obj->rest_namespace : 'wp/v2';
[60] Fix | Delete
[61] Fix | Delete
$this->meta = new WP_REST_Post_Meta_Fields( $this->post_type );
[62] Fix | Delete
}
[63] Fix | Delete
[64] Fix | Delete
/**
[65] Fix | Delete
* Registers the routes for posts.
[66] Fix | Delete
*
[67] Fix | Delete
* @since 4.7.0
[68] Fix | Delete
*
[69] Fix | Delete
* @see register_rest_route()
[70] Fix | Delete
*/
[71] Fix | Delete
public function register_routes() {
[72] Fix | Delete
[73] Fix | Delete
register_rest_route(
[74] Fix | Delete
$this->namespace,
[75] Fix | Delete
'/' . $this->rest_base,
[76] Fix | Delete
array(
[77] Fix | Delete
array(
[78] Fix | Delete
'methods' => WP_REST_Server::READABLE,
[79] Fix | Delete
'callback' => array( $this, 'get_items' ),
[80] Fix | Delete
'permission_callback' => array( $this, 'get_items_permissions_check' ),
[81] Fix | Delete
'args' => $this->get_collection_params(),
[82] Fix | Delete
),
[83] Fix | Delete
array(
[84] Fix | Delete
'methods' => WP_REST_Server::CREATABLE,
[85] Fix | Delete
'callback' => array( $this, 'create_item' ),
[86] Fix | Delete
'permission_callback' => array( $this, 'create_item_permissions_check' ),
[87] Fix | Delete
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
[88] Fix | Delete
),
[89] Fix | Delete
'allow_batch' => $this->allow_batch,
[90] Fix | Delete
'schema' => array( $this, 'get_public_item_schema' ),
[91] Fix | Delete
)
[92] Fix | Delete
);
[93] Fix | Delete
[94] Fix | Delete
$schema = $this->get_item_schema();
[95] Fix | Delete
$get_item_args = array(
[96] Fix | Delete
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
[97] Fix | Delete
);
[98] Fix | Delete
if ( isset( $schema['properties']['excerpt'] ) ) {
[99] Fix | Delete
$get_item_args['excerpt_length'] = array(
[100] Fix | Delete
'description' => __( 'Override the default excerpt length.' ),
[101] Fix | Delete
'type' => 'integer',
[102] Fix | Delete
);
[103] Fix | Delete
}
[104] Fix | Delete
if ( isset( $schema['properties']['password'] ) ) {
[105] Fix | Delete
$get_item_args['password'] = array(
[106] Fix | Delete
'description' => __( 'The password for the post if it is password protected.' ),
[107] Fix | Delete
'type' => 'string',
[108] Fix | Delete
);
[109] Fix | Delete
}
[110] Fix | Delete
register_rest_route(
[111] Fix | Delete
$this->namespace,
[112] Fix | Delete
'/' . $this->rest_base . '/(?P<id>[\d]+)',
[113] Fix | Delete
array(
[114] Fix | Delete
'args' => array(
[115] Fix | Delete
'id' => array(
[116] Fix | Delete
'description' => __( 'Unique identifier for the post.' ),
[117] Fix | Delete
'type' => 'integer',
[118] Fix | Delete
),
[119] Fix | Delete
),
[120] Fix | Delete
array(
[121] Fix | Delete
'methods' => WP_REST_Server::READABLE,
[122] Fix | Delete
'callback' => array( $this, 'get_item' ),
[123] Fix | Delete
'permission_callback' => array( $this, 'get_item_permissions_check' ),
[124] Fix | Delete
'args' => $get_item_args,
[125] Fix | Delete
),
[126] Fix | Delete
array(
[127] Fix | Delete
'methods' => WP_REST_Server::EDITABLE,
[128] Fix | Delete
'callback' => array( $this, 'update_item' ),
[129] Fix | Delete
'permission_callback' => array( $this, 'update_item_permissions_check' ),
[130] Fix | Delete
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
[131] Fix | Delete
),
[132] Fix | Delete
array(
[133] Fix | Delete
'methods' => WP_REST_Server::DELETABLE,
[134] Fix | Delete
'callback' => array( $this, 'delete_item' ),
[135] Fix | Delete
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
[136] Fix | Delete
'args' => array(
[137] Fix | Delete
'force' => array(
[138] Fix | Delete
'type' => 'boolean',
[139] Fix | Delete
'default' => false,
[140] Fix | Delete
'description' => __( 'Whether to bypass Trash and force deletion.' ),
[141] Fix | Delete
),
[142] Fix | Delete
),
[143] Fix | Delete
),
[144] Fix | Delete
'allow_batch' => $this->allow_batch,
[145] Fix | Delete
'schema' => array( $this, 'get_public_item_schema' ),
[146] Fix | Delete
)
[147] Fix | Delete
);
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
/**
[151] Fix | Delete
* Checks if a given request has access to read posts.
[152] Fix | Delete
*
[153] Fix | Delete
* @since 4.7.0
[154] Fix | Delete
*
[155] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[156] Fix | Delete
* @return true|WP_Error True if the request has read access, WP_Error object otherwise.
[157] Fix | Delete
*/
[158] Fix | Delete
public function get_items_permissions_check( $request ) {
[159] Fix | Delete
[160] Fix | Delete
$post_type = get_post_type_object( $this->post_type );
[161] Fix | Delete
[162] Fix | Delete
if ( 'edit' === $request['context'] && ! current_user_can( $post_type->cap->edit_posts ) ) {
[163] Fix | Delete
return new WP_Error(
[164] Fix | Delete
'rest_forbidden_context',
[165] Fix | Delete
__( 'Sorry, you are not allowed to edit posts in this post type.' ),
[166] Fix | Delete
array( 'status' => rest_authorization_required_code() )
[167] Fix | Delete
);
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
return true;
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
/**
[174] Fix | Delete
* Overrides the result of the post password check for REST requested posts.
[175] Fix | Delete
*
[176] Fix | Delete
* Allow users to read the content of password protected posts if they have
[177] Fix | Delete
* previously passed a permission check or if they have the `edit_post` capability
[178] Fix | Delete
* for the post being checked.
[179] Fix | Delete
*
[180] Fix | Delete
* @since 5.7.1
[181] Fix | Delete
*
[182] Fix | Delete
* @param bool $required Whether the post requires a password check.
[183] Fix | Delete
* @param WP_Post $post The post been password checked.
[184] Fix | Delete
* @return bool Result of password check taking in to account REST API considerations.
[185] Fix | Delete
*/
[186] Fix | Delete
public function check_password_required( $required, $post ) {
[187] Fix | Delete
if ( ! $required ) {
[188] Fix | Delete
return $required;
[189] Fix | Delete
}
[190] Fix | Delete
[191] Fix | Delete
$post = get_post( $post );
[192] Fix | Delete
[193] Fix | Delete
if ( ! $post ) {
[194] Fix | Delete
return $required;
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
if ( ! empty( $this->password_check_passed[ $post->ID ] ) ) {
[198] Fix | Delete
// Password previously checked and approved.
[199] Fix | Delete
return false;
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
return ! current_user_can( 'edit_post', $post->ID );
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
/**
[206] Fix | Delete
* Retrieves a collection of posts.
[207] Fix | Delete
*
[208] Fix | Delete
* @since 4.7.0
[209] Fix | Delete
*
[210] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[211] Fix | Delete
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
[212] Fix | Delete
*/
[213] Fix | Delete
public function get_items( $request ) {
[214] Fix | Delete
[215] Fix | Delete
// Ensure a search string is set in case the orderby is set to 'relevance'.
[216] Fix | Delete
if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] && empty( $request['search'] ) ) {
[217] Fix | Delete
return new WP_Error(
[218] Fix | Delete
'rest_no_search_term_defined',
[219] Fix | Delete
__( 'You need to define a search term to order by relevance.' ),
[220] Fix | Delete
array( 'status' => 400 )
[221] Fix | Delete
);
[222] Fix | Delete
}
[223] Fix | Delete
[224] Fix | Delete
// Ensure an include parameter is set in case the orderby is set to 'include'.
[225] Fix | Delete
if ( ! empty( $request['orderby'] ) && 'include' === $request['orderby'] && empty( $request['include'] ) ) {
[226] Fix | Delete
return new WP_Error(
[227] Fix | Delete
'rest_orderby_include_missing_include',
[228] Fix | Delete
__( 'You need to define an include parameter to order by include.' ),
[229] Fix | Delete
array( 'status' => 400 )
[230] Fix | Delete
);
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
// Retrieve the list of registered collection query parameters.
[234] Fix | Delete
$registered = $this->get_collection_params();
[235] Fix | Delete
$args = array();
[236] Fix | Delete
[237] Fix | Delete
/*
[238] Fix | Delete
* This array defines mappings between public API query parameters whose
[239] Fix | Delete
* values are accepted as-passed, and their internal WP_Query parameter
[240] Fix | Delete
* name equivalents (some are the same). Only values which are also
[241] Fix | Delete
* present in $registered will be set.
[242] Fix | Delete
*/
[243] Fix | Delete
$parameter_mappings = array(
[244] Fix | Delete
'author' => 'author__in',
[245] Fix | Delete
'author_exclude' => 'author__not_in',
[246] Fix | Delete
'exclude' => 'post__not_in',
[247] Fix | Delete
'include' => 'post__in',
[248] Fix | Delete
'menu_order' => 'menu_order',
[249] Fix | Delete
'offset' => 'offset',
[250] Fix | Delete
'order' => 'order',
[251] Fix | Delete
'orderby' => 'orderby',
[252] Fix | Delete
'page' => 'paged',
[253] Fix | Delete
'parent' => 'post_parent__in',
[254] Fix | Delete
'parent_exclude' => 'post_parent__not_in',
[255] Fix | Delete
'search' => 's',
[256] Fix | Delete
'search_columns' => 'search_columns',
[257] Fix | Delete
'slug' => 'post_name__in',
[258] Fix | Delete
'status' => 'post_status',
[259] Fix | Delete
);
[260] Fix | Delete
[261] Fix | Delete
/*
[262] Fix | Delete
* For each known parameter which is both registered and present in the request,
[263] Fix | Delete
* set the parameter's value on the query $args.
[264] Fix | Delete
*/
[265] Fix | Delete
foreach ( $parameter_mappings as $api_param => $wp_param ) {
[266] Fix | Delete
if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
[267] Fix | Delete
$args[ $wp_param ] = $request[ $api_param ];
[268] Fix | Delete
}
[269] Fix | Delete
}
[270] Fix | Delete
[271] Fix | Delete
// Check for & assign any parameters which require special handling or setting.
[272] Fix | Delete
$args['date_query'] = array();
[273] Fix | Delete
[274] Fix | Delete
if ( isset( $registered['before'], $request['before'] ) ) {
[275] Fix | Delete
$args['date_query'][] = array(
[276] Fix | Delete
'before' => $request['before'],
[277] Fix | Delete
'column' => 'post_date',
[278] Fix | Delete
);
[279] Fix | Delete
}
[280] Fix | Delete
[281] Fix | Delete
if ( isset( $registered['modified_before'], $request['modified_before'] ) ) {
[282] Fix | Delete
$args['date_query'][] = array(
[283] Fix | Delete
'before' => $request['modified_before'],
[284] Fix | Delete
'column' => 'post_modified',
[285] Fix | Delete
);
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
if ( isset( $registered['after'], $request['after'] ) ) {
[289] Fix | Delete
$args['date_query'][] = array(
[290] Fix | Delete
'after' => $request['after'],
[291] Fix | Delete
'column' => 'post_date',
[292] Fix | Delete
);
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
if ( isset( $registered['modified_after'], $request['modified_after'] ) ) {
[296] Fix | Delete
$args['date_query'][] = array(
[297] Fix | Delete
'after' => $request['modified_after'],
[298] Fix | Delete
'column' => 'post_modified',
[299] Fix | Delete
);
[300] Fix | Delete
}
[301] Fix | Delete
[302] Fix | Delete
// Ensure our per_page parameter overrides any provided posts_per_page filter.
[303] Fix | Delete
if ( isset( $registered['per_page'] ) ) {
[304] Fix | Delete
$args['posts_per_page'] = $request['per_page'];
[305] Fix | Delete
}
[306] Fix | Delete
[307] Fix | Delete
if ( isset( $registered['sticky'], $request['sticky'] ) ) {
[308] Fix | Delete
$sticky_posts = get_option( 'sticky_posts', array() );
[309] Fix | Delete
if ( ! is_array( $sticky_posts ) ) {
[310] Fix | Delete
$sticky_posts = array();
[311] Fix | Delete
}
[312] Fix | Delete
if ( $request['sticky'] ) {
[313] Fix | Delete
/*
[314] Fix | Delete
* As post__in will be used to only get sticky posts,
[315] Fix | Delete
* we have to support the case where post__in was already
[316] Fix | Delete
* specified.
[317] Fix | Delete
*/
[318] Fix | Delete
$args['post__in'] = $args['post__in'] ? array_intersect( $sticky_posts, $args['post__in'] ) : $sticky_posts;
[319] Fix | Delete
[320] Fix | Delete
/*
[321] Fix | Delete
* If we intersected, but there are no post IDs in common,
[322] Fix | Delete
* WP_Query won't return "no posts" for post__in = array()
[323] Fix | Delete
* so we have to fake it a bit.
[324] Fix | Delete
*/
[325] Fix | Delete
if ( ! $args['post__in'] ) {
[326] Fix | Delete
$args['post__in'] = array( 0 );
[327] Fix | Delete
}
[328] Fix | Delete
} elseif ( $sticky_posts ) {
[329] Fix | Delete
/*
[330] Fix | Delete
* As post___not_in will be used to only get posts that
[331] Fix | Delete
* are not sticky, we have to support the case where post__not_in
[332] Fix | Delete
* was already specified.
[333] Fix | Delete
*/
[334] Fix | Delete
$args['post__not_in'] = array_merge( $args['post__not_in'], $sticky_posts );
[335] Fix | Delete
}
[336] Fix | Delete
}
[337] Fix | Delete
[338] Fix | Delete
$args = $this->prepare_tax_query( $args, $request );
[339] Fix | Delete
[340] Fix | Delete
// Force the post_type argument, since it's not a user input variable.
[341] Fix | Delete
$args['post_type'] = $this->post_type;
[342] Fix | Delete
[343] Fix | Delete
/**
[344] Fix | Delete
* Filters WP_Query arguments when querying posts via the REST API.
[345] Fix | Delete
*
[346] Fix | Delete
* The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
[347] Fix | Delete
*
[348] Fix | Delete
* Possible hook names include:
[349] Fix | Delete
*
[350] Fix | Delete
* - `rest_post_query`
[351] Fix | Delete
* - `rest_page_query`
[352] Fix | Delete
* - `rest_attachment_query`
[353] Fix | Delete
*
[354] Fix | Delete
* Enables adding extra arguments or setting defaults for a post collection request.
[355] Fix | Delete
*
[356] Fix | Delete
* @since 4.7.0
[357] Fix | Delete
* @since 5.7.0 Moved after the `tax_query` query arg is generated.
[358] Fix | Delete
*
[359] Fix | Delete
* @link https://developer.wordpress.org/reference/classes/wp_query/
[360] Fix | Delete
*
[361] Fix | Delete
* @param array $args Array of arguments for WP_Query.
[362] Fix | Delete
* @param WP_REST_Request $request The REST API request.
[363] Fix | Delete
*/
[364] Fix | Delete
$args = apply_filters( "rest_{$this->post_type}_query", $args, $request );
[365] Fix | Delete
$query_args = $this->prepare_items_query( $args, $request );
[366] Fix | Delete
[367] Fix | Delete
$posts_query = new WP_Query();
[368] Fix | Delete
$query_result = $posts_query->query( $query_args );
[369] Fix | Delete
[370] Fix | Delete
// Allow access to all password protected posts if the context is edit.
[371] Fix | Delete
if ( 'edit' === $request['context'] ) {
[372] Fix | Delete
add_filter( 'post_password_required', array( $this, 'check_password_required' ), 10, 2 );
[373] Fix | Delete
}
[374] Fix | Delete
[375] Fix | Delete
$posts = array();
[376] Fix | Delete
[377] Fix | Delete
update_post_author_caches( $query_result );
[378] Fix | Delete
update_post_parent_caches( $query_result );
[379] Fix | Delete
[380] Fix | Delete
if ( post_type_supports( $this->post_type, 'thumbnail' ) ) {
[381] Fix | Delete
update_post_thumbnail_cache( $posts_query );
[382] Fix | Delete
}
[383] Fix | Delete
[384] Fix | Delete
foreach ( $query_result as $post ) {
[385] Fix | Delete
if ( 'edit' === $request['context'] ) {
[386] Fix | Delete
$permission = $this->check_update_permission( $post );
[387] Fix | Delete
} else {
[388] Fix | Delete
$permission = $this->check_read_permission( $post );
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
if ( ! $permission ) {
[392] Fix | Delete
continue;
[393] Fix | Delete
}
[394] Fix | Delete
[395] Fix | Delete
$data = $this->prepare_item_for_response( $post, $request );
[396] Fix | Delete
$posts[] = $this->prepare_response_for_collection( $data );
[397] Fix | Delete
}
[398] Fix | Delete
[399] Fix | Delete
// Reset filter.
[400] Fix | Delete
if ( 'edit' === $request['context'] ) {
[401] Fix | Delete
remove_filter( 'post_password_required', array( $this, 'check_password_required' ) );
[402] Fix | Delete
}
[403] Fix | Delete
[404] Fix | Delete
$page = (int) $query_args['paged'];
[405] Fix | Delete
$total_posts = $posts_query->found_posts;
[406] Fix | Delete
[407] Fix | Delete
if ( $total_posts < 1 && $page > 1 ) {
[408] Fix | Delete
// Out-of-bounds, run the query again without LIMIT for total count.
[409] Fix | Delete
unset( $query_args['paged'] );
[410] Fix | Delete
[411] Fix | Delete
$count_query = new WP_Query();
[412] Fix | Delete
$count_query->query( $query_args );
[413] Fix | Delete
$total_posts = $count_query->found_posts;
[414] Fix | Delete
}
[415] Fix | Delete
[416] Fix | Delete
$max_pages = (int) ceil( $total_posts / (int) $posts_query->query_vars['posts_per_page'] );
[417] Fix | Delete
[418] Fix | Delete
if ( $page > $max_pages && $total_posts > 0 ) {
[419] Fix | Delete
return new WP_Error(
[420] Fix | Delete
'rest_post_invalid_page_number',
[421] Fix | Delete
__( 'The page number requested is larger than the number of pages available.' ),
[422] Fix | Delete
array( 'status' => 400 )
[423] Fix | Delete
);
[424] Fix | Delete
}
[425] Fix | Delete
[426] Fix | Delete
$response = rest_ensure_response( $posts );
[427] Fix | Delete
[428] Fix | Delete
$response->header( 'X-WP-Total', (int) $total_posts );
[429] Fix | Delete
$response->header( 'X-WP-TotalPages', (int) $max_pages );
[430] Fix | Delete
[431] Fix | Delete
$request_params = $request->get_query_params();
[432] Fix | Delete
$collection_url = rest_url( rest_get_route_for_post_type_items( $this->post_type ) );
[433] Fix | Delete
$base = add_query_arg( urlencode_deep( $request_params ), $collection_url );
[434] Fix | Delete
[435] Fix | Delete
if ( $page > 1 ) {
[436] Fix | Delete
$prev_page = $page - 1;
[437] Fix | Delete
[438] Fix | Delete
if ( $prev_page > $max_pages ) {
[439] Fix | Delete
$prev_page = $max_pages;
[440] Fix | Delete
}
[441] Fix | Delete
[442] Fix | Delete
$prev_link = add_query_arg( 'page', $prev_page, $base );
[443] Fix | Delete
$response->link_header( 'prev', $prev_link );
[444] Fix | Delete
}
[445] Fix | Delete
if ( $max_pages > $page ) {
[446] Fix | Delete
$next_page = $page + 1;
[447] Fix | Delete
$next_link = add_query_arg( 'page', $next_page, $base );
[448] Fix | Delete
[449] Fix | Delete
$response->link_header( 'next', $next_link );
[450] Fix | Delete
}
[451] Fix | Delete
[452] Fix | Delete
return $response;
[453] Fix | Delete
}
[454] Fix | Delete
[455] Fix | Delete
/**
[456] Fix | Delete
* Gets the post, if the ID is valid.
[457] Fix | Delete
*
[458] Fix | Delete
* @since 4.7.2
[459] Fix | Delete
*
[460] Fix | Delete
* @param int $id Supplied ID.
[461] Fix | Delete
* @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
[462] Fix | Delete
*/
[463] Fix | Delete
protected function get_post( $id ) {
[464] Fix | Delete
$error = new WP_Error(
[465] Fix | Delete
'rest_post_invalid_id',
[466] Fix | Delete
__( 'Invalid post ID.' ),
[467] Fix | Delete
array( 'status' => 404 )
[468] Fix | Delete
);
[469] Fix | Delete
[470] Fix | Delete
if ( (int) $id <= 0 ) {
[471] Fix | Delete
return $error;
[472] Fix | Delete
}
[473] Fix | Delete
[474] Fix | Delete
$post = get_post( (int) $id );
[475] Fix | Delete
if ( empty( $post ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) {
[476] Fix | Delete
return $error;
[477] Fix | Delete
}
[478] Fix | Delete
[479] Fix | Delete
return $post;
[480] Fix | Delete
}
[481] Fix | Delete
[482] Fix | Delete
/**
[483] Fix | Delete
* Checks if a given request has access to read a post.
[484] Fix | Delete
*
[485] Fix | Delete
* @since 4.7.0
[486] Fix | Delete
*
[487] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[488] Fix | Delete
* @return bool|WP_Error True if the request has read access for the item, WP_Error object or false otherwise.
[489] Fix | Delete
*/
[490] Fix | Delete
public function get_item_permissions_check( $request ) {
[491] Fix | Delete
$post = $this->get_post( $request['id'] );
[492] Fix | Delete
if ( is_wp_error( $post ) ) {
[493] Fix | Delete
return $post;
[494] Fix | Delete
}
[495] Fix | Delete
[496] Fix | Delete
if ( 'edit' === $request['context'] && $post && ! $this->check_update_permission( $post ) ) {
[497] Fix | Delete
return new WP_Error(
[498] Fix | Delete
'rest_forbidden_context',
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function