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/clone/wp-inclu.../rest-api/endpoint...
File: class-wp-rest-attachments-controller.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* REST API: WP_REST_Attachments_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 controller used to access attachments via the REST API.
[10] Fix | Delete
*
[11] Fix | Delete
* @since 4.7.0
[12] Fix | Delete
*
[13] Fix | Delete
* @see WP_REST_Posts_Controller
[14] Fix | Delete
*/
[15] Fix | Delete
class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* Whether the controller supports batching.
[19] Fix | Delete
*
[20] Fix | Delete
* @since 5.9.0
[21] Fix | Delete
* @var false
[22] Fix | Delete
*/
[23] Fix | Delete
protected $allow_batch = false;
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Registers the routes for attachments.
[27] Fix | Delete
*
[28] Fix | Delete
* @since 5.3.0
[29] Fix | Delete
*
[30] Fix | Delete
* @see register_rest_route()
[31] Fix | Delete
*/
[32] Fix | Delete
public function register_routes() {
[33] Fix | Delete
parent::register_routes();
[34] Fix | Delete
register_rest_route(
[35] Fix | Delete
$this->namespace,
[36] Fix | Delete
'/' . $this->rest_base . '/(?P<id>[\d]+)/post-process',
[37] Fix | Delete
array(
[38] Fix | Delete
'methods' => WP_REST_Server::CREATABLE,
[39] Fix | Delete
'callback' => array( $this, 'post_process_item' ),
[40] Fix | Delete
'permission_callback' => array( $this, 'post_process_item_permissions_check' ),
[41] Fix | Delete
'args' => array(
[42] Fix | Delete
'id' => array(
[43] Fix | Delete
'description' => __( 'Unique identifier for the attachment.' ),
[44] Fix | Delete
'type' => 'integer',
[45] Fix | Delete
),
[46] Fix | Delete
'action' => array(
[47] Fix | Delete
'type' => 'string',
[48] Fix | Delete
'enum' => array( 'create-image-subsizes' ),
[49] Fix | Delete
'required' => true,
[50] Fix | Delete
),
[51] Fix | Delete
),
[52] Fix | Delete
)
[53] Fix | Delete
);
[54] Fix | Delete
register_rest_route(
[55] Fix | Delete
$this->namespace,
[56] Fix | Delete
'/' . $this->rest_base . '/(?P<id>[\d]+)/edit',
[57] Fix | Delete
array(
[58] Fix | Delete
'methods' => WP_REST_Server::CREATABLE,
[59] Fix | Delete
'callback' => array( $this, 'edit_media_item' ),
[60] Fix | Delete
'permission_callback' => array( $this, 'edit_media_item_permissions_check' ),
[61] Fix | Delete
'args' => $this->get_edit_media_item_args(),
[62] Fix | Delete
)
[63] Fix | Delete
);
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
/**
[67] Fix | Delete
* Determines the allowed query_vars for a get_items() response and
[68] Fix | Delete
* prepares for WP_Query.
[69] Fix | Delete
*
[70] Fix | Delete
* @since 4.7.0
[71] Fix | Delete
*
[72] Fix | Delete
* @param array $prepared_args Optional. Array of prepared arguments. Default empty array.
[73] Fix | Delete
* @param WP_REST_Request $request Optional. Request to prepare items for.
[74] Fix | Delete
* @return array Array of query arguments.
[75] Fix | Delete
*/
[76] Fix | Delete
protected function prepare_items_query( $prepared_args = array(), $request = null ) {
[77] Fix | Delete
$query_args = parent::prepare_items_query( $prepared_args, $request );
[78] Fix | Delete
[79] Fix | Delete
if ( empty( $query_args['post_status'] ) ) {
[80] Fix | Delete
$query_args['post_status'] = 'inherit';
[81] Fix | Delete
}
[82] Fix | Delete
[83] Fix | Delete
$media_types = $this->get_media_types();
[84] Fix | Delete
[85] Fix | Delete
if ( ! empty( $request['media_type'] ) && isset( $media_types[ $request['media_type'] ] ) ) {
[86] Fix | Delete
$query_args['post_mime_type'] = $media_types[ $request['media_type'] ];
[87] Fix | Delete
}
[88] Fix | Delete
[89] Fix | Delete
if ( ! empty( $request['mime_type'] ) ) {
[90] Fix | Delete
$parts = explode( '/', $request['mime_type'] );
[91] Fix | Delete
if ( isset( $media_types[ $parts[0] ] ) && in_array( $request['mime_type'], $media_types[ $parts[0] ], true ) ) {
[92] Fix | Delete
$query_args['post_mime_type'] = $request['mime_type'];
[93] Fix | Delete
}
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
// Filter query clauses to include filenames.
[97] Fix | Delete
if ( isset( $query_args['s'] ) ) {
[98] Fix | Delete
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
return $query_args;
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
/**
[105] Fix | Delete
* Checks if a given request has access to create an attachment.
[106] Fix | Delete
*
[107] Fix | Delete
* @since 4.7.0
[108] Fix | Delete
*
[109] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[110] Fix | Delete
* @return true|WP_Error Boolean true if the attachment may be created, or a WP_Error if not.
[111] Fix | Delete
*/
[112] Fix | Delete
public function create_item_permissions_check( $request ) {
[113] Fix | Delete
$ret = parent::create_item_permissions_check( $request );
[114] Fix | Delete
[115] Fix | Delete
if ( ! $ret || is_wp_error( $ret ) ) {
[116] Fix | Delete
return $ret;
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
if ( ! current_user_can( 'upload_files' ) ) {
[120] Fix | Delete
return new WP_Error(
[121] Fix | Delete
'rest_cannot_create',
[122] Fix | Delete
__( 'Sorry, you are not allowed to upload media on this site.' ),
[123] Fix | Delete
array( 'status' => 400 )
[124] Fix | Delete
);
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
// Attaching media to a post requires ability to edit said post.
[128] Fix | Delete
if ( ! empty( $request['post'] ) && ! current_user_can( 'edit_post', (int) $request['post'] ) ) {
[129] Fix | Delete
return new WP_Error(
[130] Fix | Delete
'rest_cannot_edit',
[131] Fix | Delete
__( 'Sorry, you are not allowed to upload media to this post.' ),
[132] Fix | Delete
array( 'status' => rest_authorization_required_code() )
[133] Fix | Delete
);
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
return true;
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
/**
[140] Fix | Delete
* Creates a single attachment.
[141] Fix | Delete
*
[142] Fix | Delete
* @since 4.7.0
[143] Fix | Delete
*
[144] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[145] Fix | Delete
* @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure.
[146] Fix | Delete
*/
[147] Fix | Delete
public function create_item( $request ) {
[148] Fix | Delete
if ( ! empty( $request['post'] ) && in_array( get_post_type( $request['post'] ), array( 'revision', 'attachment' ), true ) ) {
[149] Fix | Delete
return new WP_Error(
[150] Fix | Delete
'rest_invalid_param',
[151] Fix | Delete
__( 'Invalid parent type.' ),
[152] Fix | Delete
array( 'status' => 400 )
[153] Fix | Delete
);
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
$insert = $this->insert_attachment( $request );
[157] Fix | Delete
[158] Fix | Delete
if ( is_wp_error( $insert ) ) {
[159] Fix | Delete
return $insert;
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
$schema = $this->get_item_schema();
[163] Fix | Delete
[164] Fix | Delete
// Extract by name.
[165] Fix | Delete
$attachment_id = $insert['attachment_id'];
[166] Fix | Delete
$file = $insert['file'];
[167] Fix | Delete
[168] Fix | Delete
if ( isset( $request['alt_text'] ) ) {
[169] Fix | Delete
update_post_meta( $attachment_id, '_wp_attachment_image_alt', sanitize_text_field( $request['alt_text'] ) );
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) {
[173] Fix | Delete
$thumbnail_update = $this->handle_featured_media( $request['featured_media'], $attachment_id );
[174] Fix | Delete
[175] Fix | Delete
if ( is_wp_error( $thumbnail_update ) ) {
[176] Fix | Delete
return $thumbnail_update;
[177] Fix | Delete
}
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
[181] Fix | Delete
$meta_update = $this->meta->update_value( $request['meta'], $attachment_id );
[182] Fix | Delete
[183] Fix | Delete
if ( is_wp_error( $meta_update ) ) {
[184] Fix | Delete
return $meta_update;
[185] Fix | Delete
}
[186] Fix | Delete
}
[187] Fix | Delete
[188] Fix | Delete
$attachment = get_post( $attachment_id );
[189] Fix | Delete
$fields_update = $this->update_additional_fields_for_object( $attachment, $request );
[190] Fix | Delete
[191] Fix | Delete
if ( is_wp_error( $fields_update ) ) {
[192] Fix | Delete
return $fields_update;
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
$terms_update = $this->handle_terms( $attachment_id, $request );
[196] Fix | Delete
[197] Fix | Delete
if ( is_wp_error( $terms_update ) ) {
[198] Fix | Delete
return $terms_update;
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
$request->set_param( 'context', 'edit' );
[202] Fix | Delete
[203] Fix | Delete
/**
[204] Fix | Delete
* Fires after a single attachment is completely created or updated via the REST API.
[205] Fix | Delete
*
[206] Fix | Delete
* @since 5.0.0
[207] Fix | Delete
*
[208] Fix | Delete
* @param WP_Post $attachment Inserted or updated attachment object.
[209] Fix | Delete
* @param WP_REST_Request $request Request object.
[210] Fix | Delete
* @param bool $creating True when creating an attachment, false when updating.
[211] Fix | Delete
*/
[212] Fix | Delete
do_action( 'rest_after_insert_attachment', $attachment, $request, true );
[213] Fix | Delete
[214] Fix | Delete
wp_after_insert_post( $attachment, false, null );
[215] Fix | Delete
[216] Fix | Delete
if ( wp_is_serving_rest_request() ) {
[217] Fix | Delete
/*
[218] Fix | Delete
* Set a custom header with the attachment_id.
[219] Fix | Delete
* Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
[220] Fix | Delete
*/
[221] Fix | Delete
header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
[222] Fix | Delete
}
[223] Fix | Delete
[224] Fix | Delete
// Include media and image functions to get access to wp_generate_attachment_metadata().
[225] Fix | Delete
require_once ABSPATH . 'wp-admin/includes/media.php';
[226] Fix | Delete
require_once ABSPATH . 'wp-admin/includes/image.php';
[227] Fix | Delete
[228] Fix | Delete
/*
[229] Fix | Delete
* Post-process the upload (create image sub-sizes, make PDF thumbnails, etc.) and insert attachment meta.
[230] Fix | Delete
* At this point the server may run out of resources and post-processing of uploaded images may fail.
[231] Fix | Delete
*/
[232] Fix | Delete
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
[233] Fix | Delete
[234] Fix | Delete
$response = $this->prepare_item_for_response( $attachment, $request );
[235] Fix | Delete
$response = rest_ensure_response( $response );
[236] Fix | Delete
$response->set_status( 201 );
[237] Fix | Delete
$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $attachment_id ) ) );
[238] Fix | Delete
[239] Fix | Delete
return $response;
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
/**
[243] Fix | Delete
* Inserts the attachment post in the database. Does not update the attachment meta.
[244] Fix | Delete
*
[245] Fix | Delete
* @since 5.3.0
[246] Fix | Delete
*
[247] Fix | Delete
* @param WP_REST_Request $request
[248] Fix | Delete
* @return array|WP_Error
[249] Fix | Delete
*/
[250] Fix | Delete
protected function insert_attachment( $request ) {
[251] Fix | Delete
// Get the file via $_FILES or raw data.
[252] Fix | Delete
$files = $request->get_file_params();
[253] Fix | Delete
$headers = $request->get_headers();
[254] Fix | Delete
[255] Fix | Delete
$time = null;
[256] Fix | Delete
[257] Fix | Delete
// Matches logic in media_handle_upload().
[258] Fix | Delete
if ( ! empty( $request['post'] ) ) {
[259] Fix | Delete
$post = get_post( $request['post'] );
[260] Fix | Delete
// The post date doesn't usually matter for pages, so don't backdate this upload.
[261] Fix | Delete
if ( $post && 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) {
[262] Fix | Delete
$time = $post->post_date;
[263] Fix | Delete
}
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
if ( ! empty( $files ) ) {
[267] Fix | Delete
$file = $this->upload_from_file( $files, $headers, $time );
[268] Fix | Delete
} else {
[269] Fix | Delete
$file = $this->upload_from_data( $request->get_body(), $headers, $time );
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
if ( is_wp_error( $file ) ) {
[273] Fix | Delete
return $file;
[274] Fix | Delete
}
[275] Fix | Delete
[276] Fix | Delete
$name = wp_basename( $file['file'] );
[277] Fix | Delete
$name_parts = pathinfo( $name );
[278] Fix | Delete
$name = trim( substr( $name, 0, -( 1 + strlen( $name_parts['extension'] ) ) ) );
[279] Fix | Delete
[280] Fix | Delete
$url = $file['url'];
[281] Fix | Delete
$type = $file['type'];
[282] Fix | Delete
$file = $file['file'];
[283] Fix | Delete
[284] Fix | Delete
// Include image functions to get access to wp_read_image_metadata().
[285] Fix | Delete
require_once ABSPATH . 'wp-admin/includes/image.php';
[286] Fix | Delete
[287] Fix | Delete
// Use image exif/iptc data for title and caption defaults if possible.
[288] Fix | Delete
$image_meta = wp_read_image_metadata( $file );
[289] Fix | Delete
[290] Fix | Delete
if ( ! empty( $image_meta ) ) {
[291] Fix | Delete
if ( empty( $request['title'] ) && trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
[292] Fix | Delete
$request['title'] = $image_meta['title'];
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
if ( empty( $request['caption'] ) && trim( $image_meta['caption'] ) ) {
[296] Fix | Delete
$request['caption'] = $image_meta['caption'];
[297] Fix | Delete
}
[298] Fix | Delete
}
[299] Fix | Delete
[300] Fix | Delete
$attachment = $this->prepare_item_for_database( $request );
[301] Fix | Delete
[302] Fix | Delete
$attachment->post_mime_type = $type;
[303] Fix | Delete
$attachment->guid = $url;
[304] Fix | Delete
[305] Fix | Delete
// If the title was not set, use the original filename.
[306] Fix | Delete
if ( empty( $attachment->post_title ) && ! empty( $files['file']['name'] ) ) {
[307] Fix | Delete
// Remove the file extension (after the last `.`)
[308] Fix | Delete
$tmp_title = substr( $files['file']['name'], 0, strrpos( $files['file']['name'], '.' ) );
[309] Fix | Delete
[310] Fix | Delete
if ( ! empty( $tmp_title ) ) {
[311] Fix | Delete
$attachment->post_title = $tmp_title;
[312] Fix | Delete
}
[313] Fix | Delete
}
[314] Fix | Delete
[315] Fix | Delete
// Fall back to the original approach.
[316] Fix | Delete
if ( empty( $attachment->post_title ) ) {
[317] Fix | Delete
$attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) );
[318] Fix | Delete
}
[319] Fix | Delete
[320] Fix | Delete
// $post_parent is inherited from $attachment['post_parent'].
[321] Fix | Delete
$id = wp_insert_attachment( wp_slash( (array) $attachment ), $file, 0, true, false );
[322] Fix | Delete
[323] Fix | Delete
if ( is_wp_error( $id ) ) {
[324] Fix | Delete
if ( 'db_update_error' === $id->get_error_code() ) {
[325] Fix | Delete
$id->add_data( array( 'status' => 500 ) );
[326] Fix | Delete
} else {
[327] Fix | Delete
$id->add_data( array( 'status' => 400 ) );
[328] Fix | Delete
}
[329] Fix | Delete
[330] Fix | Delete
return $id;
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
$attachment = get_post( $id );
[334] Fix | Delete
[335] Fix | Delete
/**
[336] Fix | Delete
* Fires after a single attachment is created or updated via the REST API.
[337] Fix | Delete
*
[338] Fix | Delete
* @since 4.7.0
[339] Fix | Delete
*
[340] Fix | Delete
* @param WP_Post $attachment Inserted or updated attachment
[341] Fix | Delete
* object.
[342] Fix | Delete
* @param WP_REST_Request $request The request sent to the API.
[343] Fix | Delete
* @param bool $creating True when creating an attachment, false when updating.
[344] Fix | Delete
*/
[345] Fix | Delete
do_action( 'rest_insert_attachment', $attachment, $request, true );
[346] Fix | Delete
[347] Fix | Delete
return array(
[348] Fix | Delete
'attachment_id' => $id,
[349] Fix | Delete
'file' => $file,
[350] Fix | Delete
);
[351] Fix | Delete
}
[352] Fix | Delete
[353] Fix | Delete
/**
[354] Fix | Delete
* Determines the featured media based on a request param.
[355] Fix | Delete
*
[356] Fix | Delete
* @since 6.5.0
[357] Fix | Delete
*
[358] Fix | Delete
* @param int $featured_media Featured Media ID.
[359] Fix | Delete
* @param int $post_id Post ID.
[360] Fix | Delete
* @return bool|WP_Error Whether the post thumbnail was successfully deleted, otherwise WP_Error.
[361] Fix | Delete
*/
[362] Fix | Delete
protected function handle_featured_media( $featured_media, $post_id ) {
[363] Fix | Delete
$post_type = get_post_type( $post_id );
[364] Fix | Delete
$thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' );
[365] Fix | Delete
[366] Fix | Delete
// Similar check as in wp_insert_post().
[367] Fix | Delete
if ( ! $thumbnail_support && get_post_mime_type( $post_id ) ) {
[368] Fix | Delete
if ( wp_attachment_is( 'audio', $post_id ) ) {
[369] Fix | Delete
$thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
[370] Fix | Delete
} elseif ( wp_attachment_is( 'video', $post_id ) ) {
[371] Fix | Delete
$thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
[372] Fix | Delete
}
[373] Fix | Delete
}
[374] Fix | Delete
[375] Fix | Delete
if ( $thumbnail_support ) {
[376] Fix | Delete
return parent::handle_featured_media( $featured_media, $post_id );
[377] Fix | Delete
}
[378] Fix | Delete
[379] Fix | Delete
return new WP_Error(
[380] Fix | Delete
'rest_no_featured_media',
[381] Fix | Delete
sprintf(
[382] Fix | Delete
/* translators: %s: attachment mime type */
[383] Fix | Delete
__( 'This site does not support post thumbnails on attachments with MIME type %s.' ),
[384] Fix | Delete
get_post_mime_type( $post_id )
[385] Fix | Delete
),
[386] Fix | Delete
array( 'status' => 400 )
[387] Fix | Delete
);
[388] Fix | Delete
}
[389] Fix | Delete
[390] Fix | Delete
/**
[391] Fix | Delete
* Updates a single attachment.
[392] Fix | Delete
*
[393] Fix | Delete
* @since 4.7.0
[394] Fix | Delete
*
[395] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[396] Fix | Delete
* @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure.
[397] Fix | Delete
*/
[398] Fix | Delete
public function update_item( $request ) {
[399] Fix | Delete
if ( ! empty( $request['post'] ) && in_array( get_post_type( $request['post'] ), array( 'revision', 'attachment' ), true ) ) {
[400] Fix | Delete
return new WP_Error(
[401] Fix | Delete
'rest_invalid_param',
[402] Fix | Delete
__( 'Invalid parent type.' ),
[403] Fix | Delete
array( 'status' => 400 )
[404] Fix | Delete
);
[405] Fix | Delete
}
[406] Fix | Delete
[407] Fix | Delete
$attachment_before = get_post( $request['id'] );
[408] Fix | Delete
$response = parent::update_item( $request );
[409] Fix | Delete
[410] Fix | Delete
if ( is_wp_error( $response ) ) {
[411] Fix | Delete
return $response;
[412] Fix | Delete
}
[413] Fix | Delete
[414] Fix | Delete
$response = rest_ensure_response( $response );
[415] Fix | Delete
$data = $response->get_data();
[416] Fix | Delete
[417] Fix | Delete
if ( isset( $request['alt_text'] ) ) {
[418] Fix | Delete
update_post_meta( $data['id'], '_wp_attachment_image_alt', $request['alt_text'] );
[419] Fix | Delete
}
[420] Fix | Delete
[421] Fix | Delete
$attachment = get_post( $request['id'] );
[422] Fix | Delete
[423] Fix | Delete
if ( ! empty( $schema['properties']['featured_media'] ) && isset( $request['featured_media'] ) ) {
[424] Fix | Delete
$thumbnail_update = $this->handle_featured_media( $request['featured_media'], $attachment->ID );
[425] Fix | Delete
[426] Fix | Delete
if ( is_wp_error( $thumbnail_update ) ) {
[427] Fix | Delete
return $thumbnail_update;
[428] Fix | Delete
}
[429] Fix | Delete
}
[430] Fix | Delete
[431] Fix | Delete
$fields_update = $this->update_additional_fields_for_object( $attachment, $request );
[432] Fix | Delete
[433] Fix | Delete
if ( is_wp_error( $fields_update ) ) {
[434] Fix | Delete
return $fields_update;
[435] Fix | Delete
}
[436] Fix | Delete
[437] Fix | Delete
$request->set_param( 'context', 'edit' );
[438] Fix | Delete
[439] Fix | Delete
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */
[440] Fix | Delete
do_action( 'rest_after_insert_attachment', $attachment, $request, false );
[441] Fix | Delete
[442] Fix | Delete
wp_after_insert_post( $attachment, true, $attachment_before );
[443] Fix | Delete
[444] Fix | Delete
$response = $this->prepare_item_for_response( $attachment, $request );
[445] Fix | Delete
$response = rest_ensure_response( $response );
[446] Fix | Delete
[447] Fix | Delete
return $response;
[448] Fix | Delete
}
[449] Fix | Delete
[450] Fix | Delete
/**
[451] Fix | Delete
* Performs post processing on an attachment.
[452] Fix | Delete
*
[453] Fix | Delete
* @since 5.3.0
[454] Fix | Delete
*
[455] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[456] Fix | Delete
* @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure.
[457] Fix | Delete
*/
[458] Fix | Delete
public function post_process_item( $request ) {
[459] Fix | Delete
switch ( $request['action'] ) {
[460] Fix | Delete
case 'create-image-subsizes':
[461] Fix | Delete
require_once ABSPATH . 'wp-admin/includes/image.php';
[462] Fix | Delete
wp_update_image_subsizes( $request['id'] );
[463] Fix | Delete
break;
[464] Fix | Delete
}
[465] Fix | Delete
[466] Fix | Delete
$request['context'] = 'edit';
[467] Fix | Delete
[468] Fix | Delete
return $this->prepare_item_for_response( get_post( $request['id'] ), $request );
[469] Fix | Delete
}
[470] Fix | Delete
[471] Fix | Delete
/**
[472] Fix | Delete
* Checks if a given request can perform post processing on an attachment.
[473] Fix | Delete
*
[474] Fix | Delete
* @since 5.3.0
[475] Fix | Delete
*
[476] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[477] Fix | Delete
* @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise.
[478] Fix | Delete
*/
[479] Fix | Delete
public function post_process_item_permissions_check( $request ) {
[480] Fix | Delete
return $this->update_item_permissions_check( $request );
[481] Fix | Delete
}
[482] Fix | Delete
[483] Fix | Delete
/**
[484] Fix | Delete
* Checks if a given request has access to editing media.
[485] Fix | Delete
*
[486] Fix | Delete
* @since 5.5.0
[487] Fix | Delete
*
[488] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[489] Fix | Delete
* @return true|WP_Error True if the request has read access, WP_Error object otherwise.
[490] Fix | Delete
*/
[491] Fix | Delete
public function edit_media_item_permissions_check( $request ) {
[492] Fix | Delete
if ( ! current_user_can( 'upload_files' ) ) {
[493] Fix | Delete
return new WP_Error(
[494] Fix | Delete
'rest_cannot_edit_image',
[495] Fix | Delete
__( 'Sorry, you are not allowed to upload media on this site.' ),
[496] Fix | Delete
array( 'status' => rest_authorization_required_code() )
[497] Fix | Delete
);
[498] Fix | Delete
}
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function