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-autosaves-controller.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* REST API: WP_REST_Autosaves_Controller class.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage REST_API
[5] Fix | Delete
* @since 5.0.0
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Core class used to access autosaves via the REST API.
[10] Fix | Delete
*
[11] Fix | Delete
* @since 5.0.0
[12] Fix | Delete
*
[13] Fix | Delete
* @see WP_REST_Revisions_Controller
[14] Fix | Delete
* @see WP_REST_Controller
[15] Fix | Delete
*/
[16] Fix | Delete
class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller {
[17] Fix | Delete
[18] Fix | Delete
/**
[19] Fix | Delete
* Parent post type.
[20] Fix | Delete
*
[21] Fix | Delete
* @since 5.0.0
[22] Fix | Delete
* @var string
[23] Fix | Delete
*/
[24] Fix | Delete
private $parent_post_type;
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* Parent post controller.
[28] Fix | Delete
*
[29] Fix | Delete
* @since 5.0.0
[30] Fix | Delete
* @var WP_REST_Controller
[31] Fix | Delete
*/
[32] Fix | Delete
private $parent_controller;
[33] Fix | Delete
[34] Fix | Delete
/**
[35] Fix | Delete
* Revision controller.
[36] Fix | Delete
*
[37] Fix | Delete
* @since 5.0.0
[38] Fix | Delete
* @var WP_REST_Revisions_Controller
[39] Fix | Delete
*/
[40] Fix | Delete
private $revisions_controller;
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* The base of the parent controller's route.
[44] Fix | Delete
*
[45] Fix | Delete
* @since 5.0.0
[46] Fix | Delete
* @var string
[47] Fix | Delete
*/
[48] Fix | Delete
private $parent_base;
[49] Fix | Delete
[50] Fix | Delete
/**
[51] Fix | Delete
* Constructor.
[52] Fix | Delete
*
[53] Fix | Delete
* @since 5.0.0
[54] Fix | Delete
*
[55] Fix | Delete
* @param string $parent_post_type Post type of the parent.
[56] Fix | Delete
*/
[57] Fix | Delete
public function __construct( $parent_post_type ) {
[58] Fix | Delete
$this->parent_post_type = $parent_post_type;
[59] Fix | Delete
$post_type_object = get_post_type_object( $parent_post_type );
[60] Fix | Delete
$parent_controller = $post_type_object->get_rest_controller();
[61] Fix | Delete
[62] Fix | Delete
if ( ! $parent_controller ) {
[63] Fix | Delete
$parent_controller = new WP_REST_Posts_Controller( $parent_post_type );
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
$this->parent_controller = $parent_controller;
[67] Fix | Delete
[68] Fix | Delete
$revisions_controller = $post_type_object->get_revisions_rest_controller();
[69] Fix | Delete
if ( ! $revisions_controller ) {
[70] Fix | Delete
$revisions_controller = new WP_REST_Revisions_Controller( $parent_post_type );
[71] Fix | Delete
}
[72] Fix | Delete
$this->revisions_controller = $revisions_controller;
[73] Fix | Delete
$this->rest_base = 'autosaves';
[74] Fix | Delete
$this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
[75] Fix | Delete
$this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2';
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
/**
[79] Fix | Delete
* Registers the routes for autosaves.
[80] Fix | Delete
*
[81] Fix | Delete
* @since 5.0.0
[82] Fix | Delete
*
[83] Fix | Delete
* @see register_rest_route()
[84] Fix | Delete
*/
[85] Fix | Delete
public function register_routes() {
[86] Fix | Delete
register_rest_route(
[87] Fix | Delete
$this->namespace,
[88] Fix | Delete
'/' . $this->parent_base . '/(?P<id>[\d]+)/' . $this->rest_base,
[89] Fix | Delete
array(
[90] Fix | Delete
'args' => array(
[91] Fix | Delete
'parent' => array(
[92] Fix | Delete
'description' => __( 'The ID for the parent of the autosave.' ),
[93] Fix | Delete
'type' => 'integer',
[94] Fix | Delete
),
[95] Fix | Delete
),
[96] Fix | Delete
array(
[97] Fix | Delete
'methods' => WP_REST_Server::READABLE,
[98] Fix | Delete
'callback' => array( $this, 'get_items' ),
[99] Fix | Delete
'permission_callback' => array( $this, 'get_items_permissions_check' ),
[100] Fix | Delete
'args' => $this->get_collection_params(),
[101] Fix | Delete
),
[102] Fix | Delete
array(
[103] Fix | Delete
'methods' => WP_REST_Server::CREATABLE,
[104] Fix | Delete
'callback' => array( $this, 'create_item' ),
[105] Fix | Delete
'permission_callback' => array( $this, 'create_item_permissions_check' ),
[106] Fix | Delete
'args' => $this->parent_controller->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
[107] Fix | Delete
),
[108] Fix | Delete
'schema' => array( $this, 'get_public_item_schema' ),
[109] Fix | Delete
)
[110] Fix | Delete
);
[111] Fix | Delete
[112] Fix | Delete
register_rest_route(
[113] Fix | Delete
$this->namespace,
[114] Fix | Delete
'/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base . '/(?P<id>[\d]+)',
[115] Fix | Delete
array(
[116] Fix | Delete
'args' => array(
[117] Fix | Delete
'parent' => array(
[118] Fix | Delete
'description' => __( 'The ID for the parent of the autosave.' ),
[119] Fix | Delete
'type' => 'integer',
[120] Fix | Delete
),
[121] Fix | Delete
'id' => array(
[122] Fix | Delete
'description' => __( 'The ID for the autosave.' ),
[123] Fix | Delete
'type' => 'integer',
[124] Fix | Delete
),
[125] Fix | Delete
),
[126] Fix | Delete
array(
[127] Fix | Delete
'methods' => WP_REST_Server::READABLE,
[128] Fix | Delete
'callback' => array( $this, 'get_item' ),
[129] Fix | Delete
'permission_callback' => array( $this->revisions_controller, 'get_item_permissions_check' ),
[130] Fix | Delete
'args' => array(
[131] Fix | Delete
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
[132] Fix | Delete
),
[133] Fix | Delete
),
[134] Fix | Delete
'schema' => array( $this, 'get_public_item_schema' ),
[135] Fix | Delete
)
[136] Fix | Delete
);
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
/**
[140] Fix | Delete
* Get the parent post.
[141] Fix | Delete
*
[142] Fix | Delete
* @since 5.0.0
[143] Fix | Delete
*
[144] Fix | Delete
* @param int $parent_id Supplied ID.
[145] Fix | Delete
* @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
[146] Fix | Delete
*/
[147] Fix | Delete
protected function get_parent( $parent_id ) {
[148] Fix | Delete
return $this->revisions_controller->get_parent( $parent_id );
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
/**
[152] Fix | Delete
* Checks if a given request has access to get autosaves.
[153] Fix | Delete
*
[154] Fix | Delete
* @since 5.0.0
[155] Fix | Delete
*
[156] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[157] Fix | Delete
* @return true|WP_Error True if the request has read access, WP_Error object otherwise.
[158] Fix | Delete
*/
[159] Fix | Delete
public function get_items_permissions_check( $request ) {
[160] Fix | Delete
$parent = $this->get_parent( $request['id'] );
[161] Fix | Delete
if ( is_wp_error( $parent ) ) {
[162] Fix | Delete
return $parent;
[163] Fix | Delete
}
[164] Fix | Delete
[165] Fix | Delete
if ( ! current_user_can( 'edit_post', $parent->ID ) ) {
[166] Fix | Delete
return new WP_Error(
[167] Fix | Delete
'rest_cannot_read',
[168] Fix | Delete
__( 'Sorry, you are not allowed to view autosaves of this post.' ),
[169] Fix | Delete
array( 'status' => rest_authorization_required_code() )
[170] Fix | Delete
);
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
return true;
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
/**
[177] Fix | Delete
* Checks if a given request has access to create an autosave revision.
[178] Fix | Delete
*
[179] Fix | Delete
* Autosave revisions inherit permissions from the parent post,
[180] Fix | Delete
* check if the current user has permission to edit the post.
[181] Fix | Delete
*
[182] Fix | Delete
* @since 5.0.0
[183] Fix | Delete
*
[184] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[185] Fix | Delete
* @return true|WP_Error True if the request has access to create the item, WP_Error object otherwise.
[186] Fix | Delete
*/
[187] Fix | Delete
public function create_item_permissions_check( $request ) {
[188] Fix | Delete
$id = $request->get_param( 'id' );
[189] Fix | Delete
[190] Fix | Delete
if ( empty( $id ) ) {
[191] Fix | Delete
return new WP_Error(
[192] Fix | Delete
'rest_post_invalid_id',
[193] Fix | Delete
__( 'Invalid item ID.' ),
[194] Fix | Delete
array( 'status' => 404 )
[195] Fix | Delete
);
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
return $this->parent_controller->update_item_permissions_check( $request );
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
/**
[202] Fix | Delete
* Creates, updates or deletes an autosave revision.
[203] Fix | Delete
*
[204] Fix | Delete
* @since 5.0.0
[205] Fix | Delete
*
[206] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[207] Fix | Delete
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
[208] Fix | Delete
*/
[209] Fix | Delete
public function create_item( $request ) {
[210] Fix | Delete
[211] Fix | Delete
if ( ! defined( 'WP_RUN_CORE_TESTS' ) && ! defined( 'DOING_AUTOSAVE' ) ) {
[212] Fix | Delete
define( 'DOING_AUTOSAVE', true );
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
$post = $this->get_parent( $request['id'] );
[216] Fix | Delete
[217] Fix | Delete
if ( is_wp_error( $post ) ) {
[218] Fix | Delete
return $post;
[219] Fix | Delete
}
[220] Fix | Delete
[221] Fix | Delete
$prepared_post = $this->parent_controller->prepare_item_for_database( $request );
[222] Fix | Delete
$prepared_post->ID = $post->ID;
[223] Fix | Delete
$user_id = get_current_user_id();
[224] Fix | Delete
[225] Fix | Delete
// We need to check post lock to ensure the original author didn't leave their browser tab open.
[226] Fix | Delete
if ( ! function_exists( 'wp_check_post_lock' ) ) {
[227] Fix | Delete
require_once ABSPATH . 'wp-admin/includes/post.php';
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
$post_lock = wp_check_post_lock( $post->ID );
[231] Fix | Delete
$is_draft = 'draft' === $post->post_status || 'auto-draft' === $post->post_status;
[232] Fix | Delete
[233] Fix | Delete
if ( $is_draft && (int) $post->post_author === $user_id && ! $post_lock ) {
[234] Fix | Delete
/*
[235] Fix | Delete
* Draft posts for the same author: autosaving updates the post and does not create a revision.
[236] Fix | Delete
* Convert the post object to an array and add slashes, wp_update_post() expects escaped array.
[237] Fix | Delete
*/
[238] Fix | Delete
$autosave_id = wp_update_post( wp_slash( (array) $prepared_post ), true );
[239] Fix | Delete
} else {
[240] Fix | Delete
// Non-draft posts: create or update the post autosave. Pass the meta data.
[241] Fix | Delete
$autosave_id = $this->create_post_autosave( (array) $prepared_post, (array) $request->get_param( 'meta' ) );
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
if ( is_wp_error( $autosave_id ) ) {
[245] Fix | Delete
return $autosave_id;
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
$autosave = get_post( $autosave_id );
[249] Fix | Delete
$request->set_param( 'context', 'edit' );
[250] Fix | Delete
[251] Fix | Delete
$response = $this->prepare_item_for_response( $autosave, $request );
[252] Fix | Delete
$response = rest_ensure_response( $response );
[253] Fix | Delete
[254] Fix | Delete
return $response;
[255] Fix | Delete
}
[256] Fix | Delete
[257] Fix | Delete
/**
[258] Fix | Delete
* Get the autosave, if the ID is valid.
[259] Fix | Delete
*
[260] Fix | Delete
* @since 5.0.0
[261] Fix | Delete
*
[262] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[263] Fix | Delete
* @return WP_Post|WP_Error Revision post object if ID is valid, WP_Error otherwise.
[264] Fix | Delete
*/
[265] Fix | Delete
public function get_item( $request ) {
[266] Fix | Delete
$parent_id = (int) $request->get_param( 'parent' );
[267] Fix | Delete
[268] Fix | Delete
if ( $parent_id <= 0 ) {
[269] Fix | Delete
return new WP_Error(
[270] Fix | Delete
'rest_post_invalid_id',
[271] Fix | Delete
__( 'Invalid post parent ID.' ),
[272] Fix | Delete
array( 'status' => 404 )
[273] Fix | Delete
);
[274] Fix | Delete
}
[275] Fix | Delete
[276] Fix | Delete
$autosave = wp_get_post_autosave( $parent_id );
[277] Fix | Delete
[278] Fix | Delete
if ( ! $autosave ) {
[279] Fix | Delete
return new WP_Error(
[280] Fix | Delete
'rest_post_no_autosave',
[281] Fix | Delete
__( 'There is no autosave revision for this post.' ),
[282] Fix | Delete
array( 'status' => 404 )
[283] Fix | Delete
);
[284] Fix | Delete
}
[285] Fix | Delete
[286] Fix | Delete
$response = $this->prepare_item_for_response( $autosave, $request );
[287] Fix | Delete
return $response;
[288] Fix | Delete
}
[289] Fix | Delete
[290] Fix | Delete
/**
[291] Fix | Delete
* Gets a collection of autosaves using wp_get_post_autosave.
[292] Fix | Delete
*
[293] Fix | Delete
* Contains the user's autosave, for empty if it doesn't exist.
[294] Fix | Delete
*
[295] Fix | Delete
* @since 5.0.0
[296] Fix | Delete
*
[297] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[298] Fix | Delete
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
[299] Fix | Delete
*/
[300] Fix | Delete
public function get_items( $request ) {
[301] Fix | Delete
$parent = $this->get_parent( $request['id'] );
[302] Fix | Delete
if ( is_wp_error( $parent ) ) {
[303] Fix | Delete
return $parent;
[304] Fix | Delete
}
[305] Fix | Delete
[306] Fix | Delete
$response = array();
[307] Fix | Delete
$parent_id = $parent->ID;
[308] Fix | Delete
$revisions = wp_get_post_revisions( $parent_id, array( 'check_enabled' => false ) );
[309] Fix | Delete
[310] Fix | Delete
foreach ( $revisions as $revision ) {
[311] Fix | Delete
if ( str_contains( $revision->post_name, "{$parent_id}-autosave" ) ) {
[312] Fix | Delete
$data = $this->prepare_item_for_response( $revision, $request );
[313] Fix | Delete
$response[] = $this->prepare_response_for_collection( $data );
[314] Fix | Delete
}
[315] Fix | Delete
}
[316] Fix | Delete
[317] Fix | Delete
return rest_ensure_response( $response );
[318] Fix | Delete
}
[319] Fix | Delete
[320] Fix | Delete
[321] Fix | Delete
/**
[322] Fix | Delete
* Retrieves the autosave's schema, conforming to JSON Schema.
[323] Fix | Delete
*
[324] Fix | Delete
* @since 5.0.0
[325] Fix | Delete
*
[326] Fix | Delete
* @return array Item schema data.
[327] Fix | Delete
*/
[328] Fix | Delete
public function get_item_schema() {
[329] Fix | Delete
if ( $this->schema ) {
[330] Fix | Delete
return $this->add_additional_fields_schema( $this->schema );
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
$schema = $this->revisions_controller->get_item_schema();
[334] Fix | Delete
[335] Fix | Delete
$schema['properties']['preview_link'] = array(
[336] Fix | Delete
'description' => __( 'Preview link for the post.' ),
[337] Fix | Delete
'type' => 'string',
[338] Fix | Delete
'format' => 'uri',
[339] Fix | Delete
'context' => array( 'edit' ),
[340] Fix | Delete
'readonly' => true,
[341] Fix | Delete
);
[342] Fix | Delete
[343] Fix | Delete
$this->schema = $schema;
[344] Fix | Delete
[345] Fix | Delete
return $this->add_additional_fields_schema( $this->schema );
[346] Fix | Delete
}
[347] Fix | Delete
[348] Fix | Delete
/**
[349] Fix | Delete
* Creates autosave for the specified post.
[350] Fix | Delete
*
[351] Fix | Delete
* From wp-admin/post.php.
[352] Fix | Delete
*
[353] Fix | Delete
* @since 5.0.0
[354] Fix | Delete
* @since 6.4.0 The `$meta` parameter was added.
[355] Fix | Delete
*
[356] Fix | Delete
* @param array $post_data Associative array containing the post data.
[357] Fix | Delete
* @param array $meta Associative array containing the post meta data.
[358] Fix | Delete
* @return mixed The autosave revision ID or WP_Error.
[359] Fix | Delete
*/
[360] Fix | Delete
public function create_post_autosave( $post_data, array $meta = array() ) {
[361] Fix | Delete
[362] Fix | Delete
$post_id = (int) $post_data['ID'];
[363] Fix | Delete
$post = get_post( $post_id );
[364] Fix | Delete
[365] Fix | Delete
if ( is_wp_error( $post ) ) {
[366] Fix | Delete
return $post;
[367] Fix | Delete
}
[368] Fix | Delete
[369] Fix | Delete
// Only create an autosave when it is different from the saved post.
[370] Fix | Delete
$autosave_is_different = false;
[371] Fix | Delete
$new_autosave = _wp_post_revision_data( $post_data, true );
[372] Fix | Delete
[373] Fix | Delete
foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
[374] Fix | Delete
if ( normalize_whitespace( $new_autosave[ $field ] ) !== normalize_whitespace( $post->$field ) ) {
[375] Fix | Delete
$autosave_is_different = true;
[376] Fix | Delete
break;
[377] Fix | Delete
}
[378] Fix | Delete
}
[379] Fix | Delete
[380] Fix | Delete
// Check if meta values have changed.
[381] Fix | Delete
if ( ! empty( $meta ) ) {
[382] Fix | Delete
$revisioned_meta_keys = wp_post_revision_meta_keys( $post->post_type );
[383] Fix | Delete
foreach ( $revisioned_meta_keys as $meta_key ) {
[384] Fix | Delete
// get_metadata_raw is used to avoid retrieving the default value.
[385] Fix | Delete
$old_meta = get_metadata_raw( 'post', $post_id, $meta_key, true );
[386] Fix | Delete
$new_meta = isset( $meta[ $meta_key ] ) ? $meta[ $meta_key ] : '';
[387] Fix | Delete
[388] Fix | Delete
if ( $new_meta !== $old_meta ) {
[389] Fix | Delete
$autosave_is_different = true;
[390] Fix | Delete
break;
[391] Fix | Delete
}
[392] Fix | Delete
}
[393] Fix | Delete
}
[394] Fix | Delete
[395] Fix | Delete
$user_id = get_current_user_id();
[396] Fix | Delete
[397] Fix | Delete
// Store one autosave per author. If there is already an autosave, overwrite it.
[398] Fix | Delete
$old_autosave = wp_get_post_autosave( $post_id, $user_id );
[399] Fix | Delete
[400] Fix | Delete
if ( ! $autosave_is_different && $old_autosave ) {
[401] Fix | Delete
// Nothing to save, return the existing autosave.
[402] Fix | Delete
return $old_autosave->ID;
[403] Fix | Delete
}
[404] Fix | Delete
[405] Fix | Delete
if ( $old_autosave ) {
[406] Fix | Delete
$new_autosave['ID'] = $old_autosave->ID;
[407] Fix | Delete
$new_autosave['post_author'] = $user_id;
[408] Fix | Delete
[409] Fix | Delete
/** This filter is documented in wp-admin/post.php */
[410] Fix | Delete
do_action( 'wp_creating_autosave', $new_autosave );
[411] Fix | Delete
[412] Fix | Delete
// wp_update_post() expects escaped array.
[413] Fix | Delete
$revision_id = wp_update_post( wp_slash( $new_autosave ) );
[414] Fix | Delete
} else {
[415] Fix | Delete
// Create the new autosave as a special post revision.
[416] Fix | Delete
$revision_id = _wp_put_post_revision( $post_data, true );
[417] Fix | Delete
}
[418] Fix | Delete
[419] Fix | Delete
if ( is_wp_error( $revision_id ) || 0 === $revision_id ) {
[420] Fix | Delete
return $revision_id;
[421] Fix | Delete
}
[422] Fix | Delete
[423] Fix | Delete
// Attached any passed meta values that have revisions enabled.
[424] Fix | Delete
if ( ! empty( $meta ) ) {
[425] Fix | Delete
foreach ( $revisioned_meta_keys as $meta_key ) {
[426] Fix | Delete
if ( isset( $meta[ $meta_key ] ) ) {
[427] Fix | Delete
update_metadata( 'post', $revision_id, $meta_key, wp_slash( $meta[ $meta_key ] ) );
[428] Fix | Delete
}
[429] Fix | Delete
}
[430] Fix | Delete
}
[431] Fix | Delete
[432] Fix | Delete
return $revision_id;
[433] Fix | Delete
}
[434] Fix | Delete
[435] Fix | Delete
/**
[436] Fix | Delete
* Prepares the revision for the REST response.
[437] Fix | Delete
*
[438] Fix | Delete
* @since 5.0.0
[439] Fix | Delete
* @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
[440] Fix | Delete
*
[441] Fix | Delete
* @param WP_Post $item Post revision object.
[442] Fix | Delete
* @param WP_REST_Request $request Request object.
[443] Fix | Delete
* @return WP_REST_Response Response object.
[444] Fix | Delete
*/
[445] Fix | Delete
public function prepare_item_for_response( $item, $request ) {
[446] Fix | Delete
// Restores the more descriptive, specific name for use within this method.
[447] Fix | Delete
$post = $item;
[448] Fix | Delete
[449] Fix | Delete
$response = $this->revisions_controller->prepare_item_for_response( $post, $request );
[450] Fix | Delete
$fields = $this->get_fields_for_response( $request );
[451] Fix | Delete
[452] Fix | Delete
if ( in_array( 'preview_link', $fields, true ) ) {
[453] Fix | Delete
$parent_id = wp_is_post_autosave( $post );
[454] Fix | Delete
$preview_post_id = false === $parent_id ? $post->ID : $parent_id;
[455] Fix | Delete
$preview_query_args = array();
[456] Fix | Delete
[457] Fix | Delete
if ( false !== $parent_id ) {
[458] Fix | Delete
$preview_query_args['preview_id'] = $parent_id;
[459] Fix | Delete
$preview_query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $parent_id );
[460] Fix | Delete
}
[461] Fix | Delete
[462] Fix | Delete
$response->data['preview_link'] = get_preview_post_link( $preview_post_id, $preview_query_args );
[463] Fix | Delete
}
[464] Fix | Delete
[465] Fix | Delete
$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
[466] Fix | Delete
$response->data = $this->add_additional_fields_to_object( $response->data, $request );
[467] Fix | Delete
$response->data = $this->filter_response_by_context( $response->data, $context );
[468] Fix | Delete
[469] Fix | Delete
/**
[470] Fix | Delete
* Filters a revision returned from the REST API.
[471] Fix | Delete
*
[472] Fix | Delete
* Allows modification of the revision right before it is returned.
[473] Fix | Delete
*
[474] Fix | Delete
* @since 5.0.0
[475] Fix | Delete
*
[476] Fix | Delete
* @param WP_REST_Response $response The response object.
[477] Fix | Delete
* @param WP_Post $post The original revision object.
[478] Fix | Delete
* @param WP_REST_Request $request Request used to generate the response.
[479] Fix | Delete
*/
[480] Fix | Delete
return apply_filters( 'rest_prepare_autosave', $response, $post, $request );
[481] Fix | Delete
}
[482] Fix | Delete
[483] Fix | Delete
/**
[484] Fix | Delete
* Retrieves the query params for the autosaves collection.
[485] Fix | Delete
*
[486] Fix | Delete
* @since 5.0.0
[487] Fix | Delete
*
[488] Fix | Delete
* @return array Collection parameters.
[489] Fix | Delete
*/
[490] Fix | Delete
public function get_collection_params() {
[491] Fix | Delete
return array(
[492] Fix | Delete
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
[493] Fix | Delete
);
[494] Fix | Delete
}
[495] Fix | Delete
}
[496] Fix | Delete
[497] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function