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-templates-controller.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* REST API: WP_REST_Templates_Controller class
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage REST_API
[5] Fix | Delete
* @since 5.8.0
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Base Templates REST API Controller.
[10] Fix | Delete
*
[11] Fix | Delete
* @since 5.8.0
[12] Fix | Delete
*
[13] Fix | Delete
* @see WP_REST_Controller
[14] Fix | Delete
*/
[15] Fix | Delete
class WP_REST_Templates_Controller extends WP_REST_Controller {
[16] Fix | Delete
[17] Fix | Delete
/**
[18] Fix | Delete
* Post type.
[19] Fix | Delete
*
[20] Fix | Delete
* @since 5.8.0
[21] Fix | Delete
* @var string
[22] Fix | Delete
*/
[23] Fix | Delete
protected $post_type;
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Constructor.
[27] Fix | Delete
*
[28] Fix | Delete
* @since 5.8.0
[29] Fix | Delete
*
[30] Fix | Delete
* @param string $post_type Post type.
[31] Fix | Delete
*/
[32] Fix | Delete
public function __construct( $post_type ) {
[33] Fix | Delete
$this->post_type = $post_type;
[34] Fix | Delete
$obj = get_post_type_object( $post_type );
[35] Fix | Delete
$this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
[36] Fix | Delete
$this->namespace = ! empty( $obj->rest_namespace ) ? $obj->rest_namespace : 'wp/v2';
[37] Fix | Delete
}
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Registers the controllers routes.
[41] Fix | Delete
*
[42] Fix | Delete
* @since 5.8.0
[43] Fix | Delete
* @since 6.1.0 Endpoint for fallback template content.
[44] Fix | Delete
*/
[45] Fix | Delete
public function register_routes() {
[46] Fix | Delete
// Lists all templates.
[47] Fix | Delete
register_rest_route(
[48] Fix | Delete
$this->namespace,
[49] Fix | Delete
'/' . $this->rest_base,
[50] Fix | Delete
array(
[51] Fix | Delete
array(
[52] Fix | Delete
'methods' => WP_REST_Server::READABLE,
[53] Fix | Delete
'callback' => array( $this, 'get_items' ),
[54] Fix | Delete
'permission_callback' => array( $this, 'get_items_permissions_check' ),
[55] Fix | Delete
'args' => $this->get_collection_params(),
[56] Fix | Delete
),
[57] Fix | Delete
array(
[58] Fix | Delete
'methods' => WP_REST_Server::CREATABLE,
[59] Fix | Delete
'callback' => array( $this, 'create_item' ),
[60] Fix | Delete
'permission_callback' => array( $this, 'create_item_permissions_check' ),
[61] Fix | Delete
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
[62] Fix | Delete
),
[63] Fix | Delete
'schema' => array( $this, 'get_public_item_schema' ),
[64] Fix | Delete
)
[65] Fix | Delete
);
[66] Fix | Delete
[67] Fix | Delete
// Get fallback template content.
[68] Fix | Delete
register_rest_route(
[69] Fix | Delete
$this->namespace,
[70] Fix | Delete
'/' . $this->rest_base . '/lookup',
[71] Fix | Delete
array(
[72] Fix | Delete
array(
[73] Fix | Delete
'methods' => WP_REST_Server::READABLE,
[74] Fix | Delete
'callback' => array( $this, 'get_template_fallback' ),
[75] Fix | Delete
'permission_callback' => array( $this, 'get_item_permissions_check' ),
[76] Fix | Delete
'args' => array(
[77] Fix | Delete
'slug' => array(
[78] Fix | Delete
'description' => __( 'The slug of the template to get the fallback for' ),
[79] Fix | Delete
'type' => 'string',
[80] Fix | Delete
'required' => true,
[81] Fix | Delete
),
[82] Fix | Delete
'is_custom' => array(
[83] Fix | Delete
'description' => __( 'Indicates if a template is custom or part of the template hierarchy' ),
[84] Fix | Delete
'type' => 'boolean',
[85] Fix | Delete
),
[86] Fix | Delete
'template_prefix' => array(
[87] Fix | Delete
'description' => __( 'The template prefix for the created template. This is used to extract the main template type, e.g. in `taxonomy-books` extracts the `taxonomy`' ),
[88] Fix | Delete
'type' => 'string',
[89] Fix | Delete
),
[90] Fix | Delete
),
[91] Fix | Delete
),
[92] Fix | Delete
)
[93] Fix | Delete
);
[94] Fix | Delete
[95] Fix | Delete
// Lists/updates a single template based on the given id.
[96] Fix | Delete
register_rest_route(
[97] Fix | Delete
$this->namespace,
[98] Fix | Delete
// The route.
[99] Fix | Delete
sprintf(
[100] Fix | Delete
'/%s/(?P<id>%s%s)',
[101] Fix | Delete
$this->rest_base,
[102] Fix | Delete
/*
[103] Fix | Delete
* Matches theme's directory: `/themes/<subdirectory>/<theme>/` or `/themes/<theme>/`.
[104] Fix | Delete
* Excludes invalid directory name characters: `/:<>*?"|`.
[105] Fix | Delete
*/
[106] Fix | Delete
'([^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)',
[107] Fix | Delete
// Matches the template name.
[108] Fix | Delete
'[\/\w%-]+'
[109] Fix | Delete
),
[110] Fix | Delete
array(
[111] Fix | Delete
'args' => array(
[112] Fix | Delete
'id' => array(
[113] Fix | Delete
'description' => __( 'The id of a template' ),
[114] Fix | Delete
'type' => 'string',
[115] Fix | Delete
'sanitize_callback' => array( $this, '_sanitize_template_id' ),
[116] Fix | Delete
),
[117] Fix | Delete
),
[118] Fix | Delete
array(
[119] Fix | Delete
'methods' => WP_REST_Server::READABLE,
[120] Fix | Delete
'callback' => array( $this, 'get_item' ),
[121] Fix | Delete
'permission_callback' => array( $this, 'get_item_permissions_check' ),
[122] Fix | Delete
'args' => array(
[123] Fix | Delete
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
[124] Fix | Delete
),
[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
'schema' => array( $this, 'get_public_item_schema' ),
[145] Fix | Delete
)
[146] Fix | Delete
);
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
/**
[150] Fix | Delete
* Returns the fallback template for the given slug.
[151] Fix | Delete
*
[152] Fix | Delete
* @since 6.1.0
[153] Fix | Delete
* @since 6.3.0 Ignore empty templates.
[154] Fix | Delete
*
[155] Fix | Delete
* @param WP_REST_Request $request The request instance.
[156] Fix | Delete
* @return WP_REST_Response|WP_Error
[157] Fix | Delete
*/
[158] Fix | Delete
public function get_template_fallback( $request ) {
[159] Fix | Delete
$hierarchy = get_template_hierarchy( $request['slug'], $request['is_custom'], $request['template_prefix'] );
[160] Fix | Delete
[161] Fix | Delete
do {
[162] Fix | Delete
$fallback_template = resolve_block_template( $request['slug'], $hierarchy, '' );
[163] Fix | Delete
array_shift( $hierarchy );
[164] Fix | Delete
} while ( ! empty( $hierarchy ) && empty( $fallback_template->content ) );
[165] Fix | Delete
[166] Fix | Delete
// To maintain original behavior, return an empty object rather than a 404 error when no template is found.
[167] Fix | Delete
$response = $fallback_template ? $this->prepare_item_for_response( $fallback_template, $request ) : new stdClass();
[168] Fix | Delete
[169] Fix | Delete
return rest_ensure_response( $response );
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
/**
[173] Fix | Delete
* Checks if the user has permissions to make the request.
[174] Fix | Delete
*
[175] Fix | Delete
* @since 5.8.0
[176] Fix | Delete
*
[177] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[178] Fix | Delete
* @return true|WP_Error True if the request has read access, WP_Error object otherwise.
[179] Fix | Delete
*/
[180] Fix | Delete
protected function permissions_check( $request ) {
[181] Fix | Delete
/*
[182] Fix | Delete
* Verify if the current user has edit_theme_options capability.
[183] Fix | Delete
* This capability is required to edit/view/delete templates.
[184] Fix | Delete
*/
[185] Fix | Delete
if ( ! current_user_can( 'edit_theme_options' ) ) {
[186] Fix | Delete
return new WP_Error(
[187] Fix | Delete
'rest_cannot_manage_templates',
[188] Fix | Delete
__( 'Sorry, you are not allowed to access the templates on this site.' ),
[189] Fix | Delete
array(
[190] Fix | Delete
'status' => rest_authorization_required_code(),
[191] Fix | Delete
)
[192] Fix | Delete
);
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
return true;
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
/**
[199] Fix | Delete
* Requesting this endpoint for a template like 'twentytwentytwo//home'
[200] Fix | Delete
* requires using a path like /wp/v2/templates/twentytwentytwo//home. There
[201] Fix | Delete
* are special cases when WordPress routing corrects the name to contain
[202] Fix | Delete
* only a single slash like 'twentytwentytwo/home'.
[203] Fix | Delete
*
[204] Fix | Delete
* This method doubles the last slash if it's not already doubled. It relies
[205] Fix | Delete
* on the template ID format {theme_name}//{template_slug} and the fact that
[206] Fix | Delete
* slugs cannot contain slashes.
[207] Fix | Delete
*
[208] Fix | Delete
* @since 5.9.0
[209] Fix | Delete
* @see https://core.trac.wordpress.org/ticket/54507
[210] Fix | Delete
*
[211] Fix | Delete
* @param string $id Template ID.
[212] Fix | Delete
* @return string Sanitized template ID.
[213] Fix | Delete
*/
[214] Fix | Delete
public function _sanitize_template_id( $id ) {
[215] Fix | Delete
$id = urldecode( $id );
[216] Fix | Delete
[217] Fix | Delete
$last_slash_pos = strrpos( $id, '/' );
[218] Fix | Delete
if ( false === $last_slash_pos ) {
[219] Fix | Delete
return $id;
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
$is_double_slashed = substr( $id, $last_slash_pos - 1, 1 ) === '/';
[223] Fix | Delete
if ( $is_double_slashed ) {
[224] Fix | Delete
return $id;
[225] Fix | Delete
}
[226] Fix | Delete
return (
[227] Fix | Delete
substr( $id, 0, $last_slash_pos )
[228] Fix | Delete
. '/'
[229] Fix | Delete
. substr( $id, $last_slash_pos )
[230] Fix | Delete
);
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
/**
[234] Fix | Delete
* Checks if a given request has access to read templates.
[235] Fix | Delete
*
[236] Fix | Delete
* @since 5.8.0
[237] Fix | Delete
* @since 6.6.0 Allow users with edit_posts capability to read templates.
[238] Fix | Delete
*
[239] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[240] Fix | Delete
* @return true|WP_Error True if the request has read access, WP_Error object otherwise.
[241] Fix | Delete
*/
[242] Fix | Delete
public function get_items_permissions_check( $request ) {
[243] Fix | Delete
if ( current_user_can( 'edit_posts' ) ) {
[244] Fix | Delete
return true;
[245] Fix | Delete
}
[246] Fix | Delete
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
[247] Fix | Delete
if ( current_user_can( $post_type->cap->edit_posts ) ) {
[248] Fix | Delete
return true;
[249] Fix | Delete
}
[250] Fix | Delete
}
[251] Fix | Delete
[252] Fix | Delete
return new WP_Error(
[253] Fix | Delete
'rest_cannot_manage_templates',
[254] Fix | Delete
__( 'Sorry, you are not allowed to access the templates on this site.' ),
[255] Fix | Delete
array(
[256] Fix | Delete
'status' => rest_authorization_required_code(),
[257] Fix | Delete
)
[258] Fix | Delete
);
[259] Fix | Delete
}
[260] Fix | Delete
[261] Fix | Delete
/**
[262] Fix | Delete
* Returns a list of templates.
[263] Fix | Delete
*
[264] Fix | Delete
* @since 5.8.0
[265] Fix | Delete
*
[266] Fix | Delete
* @param WP_REST_Request $request The request instance.
[267] Fix | Delete
* @return WP_REST_Response
[268] Fix | Delete
*/
[269] Fix | Delete
public function get_items( $request ) {
[270] Fix | Delete
$query = array();
[271] Fix | Delete
if ( isset( $request['wp_id'] ) ) {
[272] Fix | Delete
$query['wp_id'] = $request['wp_id'];
[273] Fix | Delete
}
[274] Fix | Delete
if ( isset( $request['area'] ) ) {
[275] Fix | Delete
$query['area'] = $request['area'];
[276] Fix | Delete
}
[277] Fix | Delete
if ( isset( $request['post_type'] ) ) {
[278] Fix | Delete
$query['post_type'] = $request['post_type'];
[279] Fix | Delete
}
[280] Fix | Delete
[281] Fix | Delete
$templates = array();
[282] Fix | Delete
foreach ( get_block_templates( $query, $this->post_type ) as $template ) {
[283] Fix | Delete
$data = $this->prepare_item_for_response( $template, $request );
[284] Fix | Delete
$templates[] = $this->prepare_response_for_collection( $data );
[285] Fix | Delete
}
[286] Fix | Delete
[287] Fix | Delete
return rest_ensure_response( $templates );
[288] Fix | Delete
}
[289] Fix | Delete
[290] Fix | Delete
/**
[291] Fix | Delete
* Checks if a given request has access to read a single template.
[292] Fix | Delete
*
[293] Fix | Delete
* @since 5.8.0
[294] Fix | Delete
* @since 6.6.0 Allow users with edit_posts capability to read individual templates.
[295] Fix | Delete
*
[296] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[297] Fix | Delete
* @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
[298] Fix | Delete
*/
[299] Fix | Delete
public function get_item_permissions_check( $request ) {
[300] Fix | Delete
if ( current_user_can( 'edit_posts' ) ) {
[301] Fix | Delete
return true;
[302] Fix | Delete
}
[303] Fix | Delete
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
[304] Fix | Delete
if ( current_user_can( $post_type->cap->edit_posts ) ) {
[305] Fix | Delete
return true;
[306] Fix | Delete
}
[307] Fix | Delete
}
[308] Fix | Delete
[309] Fix | Delete
return new WP_Error(
[310] Fix | Delete
'rest_cannot_manage_templates',
[311] Fix | Delete
__( 'Sorry, you are not allowed to access the templates on this site.' ),
[312] Fix | Delete
array(
[313] Fix | Delete
'status' => rest_authorization_required_code(),
[314] Fix | Delete
)
[315] Fix | Delete
);
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
/**
[319] Fix | Delete
* Returns the given template
[320] Fix | Delete
*
[321] Fix | Delete
* @since 5.8.0
[322] Fix | Delete
*
[323] Fix | Delete
* @param WP_REST_Request $request The request instance.
[324] Fix | Delete
* @return WP_REST_Response|WP_Error
[325] Fix | Delete
*/
[326] Fix | Delete
public function get_item( $request ) {
[327] Fix | Delete
if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
[328] Fix | Delete
$template = get_block_file_template( $request['id'], $this->post_type );
[329] Fix | Delete
} else {
[330] Fix | Delete
$template = get_block_template( $request['id'], $this->post_type );
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
if ( ! $template ) {
[334] Fix | Delete
return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) );
[335] Fix | Delete
}
[336] Fix | Delete
[337] Fix | Delete
return $this->prepare_item_for_response( $template, $request );
[338] Fix | Delete
}
[339] Fix | Delete
[340] Fix | Delete
/**
[341] Fix | Delete
* Checks if a given request has access to write a single template.
[342] Fix | Delete
*
[343] Fix | Delete
* @since 5.8.0
[344] Fix | Delete
*
[345] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[346] Fix | Delete
* @return true|WP_Error True if the request has write access for the item, WP_Error object otherwise.
[347] Fix | Delete
*/
[348] Fix | Delete
public function update_item_permissions_check( $request ) {
[349] Fix | Delete
return $this->permissions_check( $request );
[350] Fix | Delete
}
[351] Fix | Delete
[352] Fix | Delete
/**
[353] Fix | Delete
* Updates a single template.
[354] Fix | Delete
*
[355] Fix | Delete
* @since 5.8.0
[356] Fix | Delete
*
[357] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[358] Fix | Delete
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
[359] Fix | Delete
*/
[360] Fix | Delete
public function update_item( $request ) {
[361] Fix | Delete
$template = get_block_template( $request['id'], $this->post_type );
[362] Fix | Delete
if ( ! $template ) {
[363] Fix | Delete
return new WP_Error( 'rest_template_not_found', __( 'No templates exist with that id.' ), array( 'status' => 404 ) );
[364] Fix | Delete
}
[365] Fix | Delete
[366] Fix | Delete
$post_before = get_post( $template->wp_id );
[367] Fix | Delete
[368] Fix | Delete
if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
[369] Fix | Delete
wp_delete_post( $template->wp_id, true );
[370] Fix | Delete
$request->set_param( 'context', 'edit' );
[371] Fix | Delete
[372] Fix | Delete
$template = get_block_template( $request['id'], $this->post_type );
[373] Fix | Delete
$response = $this->prepare_item_for_response( $template, $request );
[374] Fix | Delete
[375] Fix | Delete
return rest_ensure_response( $response );
[376] Fix | Delete
}
[377] Fix | Delete
[378] Fix | Delete
$changes = $this->prepare_item_for_database( $request );
[379] Fix | Delete
[380] Fix | Delete
if ( is_wp_error( $changes ) ) {
[381] Fix | Delete
return $changes;
[382] Fix | Delete
}
[383] Fix | Delete
[384] Fix | Delete
if ( 'custom' === $template->source ) {
[385] Fix | Delete
$update = true;
[386] Fix | Delete
$result = wp_update_post( wp_slash( (array) $changes ), false );
[387] Fix | Delete
} else {
[388] Fix | Delete
$update = false;
[389] Fix | Delete
$post_before = null;
[390] Fix | Delete
$result = wp_insert_post( wp_slash( (array) $changes ), false );
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
if ( is_wp_error( $result ) ) {
[394] Fix | Delete
if ( 'db_update_error' === $result->get_error_code() ) {
[395] Fix | Delete
$result->add_data( array( 'status' => 500 ) );
[396] Fix | Delete
} else {
[397] Fix | Delete
$result->add_data( array( 'status' => 400 ) );
[398] Fix | Delete
}
[399] Fix | Delete
return $result;
[400] Fix | Delete
}
[401] Fix | Delete
[402] Fix | Delete
$template = get_block_template( $request['id'], $this->post_type );
[403] Fix | Delete
$fields_update = $this->update_additional_fields_for_object( $template, $request );
[404] Fix | Delete
if ( is_wp_error( $fields_update ) ) {
[405] Fix | Delete
return $fields_update;
[406] Fix | Delete
}
[407] Fix | Delete
[408] Fix | Delete
$request->set_param( 'context', 'edit' );
[409] Fix | Delete
[410] Fix | Delete
$post = get_post( $template->wp_id );
[411] Fix | Delete
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
[412] Fix | Delete
do_action( "rest_after_insert_{$this->post_type}", $post, $request, false );
[413] Fix | Delete
[414] Fix | Delete
wp_after_insert_post( $post, $update, $post_before );
[415] Fix | Delete
[416] Fix | Delete
$response = $this->prepare_item_for_response( $template, $request );
[417] Fix | Delete
[418] Fix | Delete
return rest_ensure_response( $response );
[419] Fix | Delete
}
[420] Fix | Delete
[421] Fix | Delete
/**
[422] Fix | Delete
* Checks if a given request has access to create a template.
[423] Fix | Delete
*
[424] Fix | Delete
* @since 5.8.0
[425] Fix | Delete
*
[426] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[427] Fix | Delete
* @return true|WP_Error True if the request has access to create items, WP_Error object otherwise.
[428] Fix | Delete
*/
[429] Fix | Delete
public function create_item_permissions_check( $request ) {
[430] Fix | Delete
return $this->permissions_check( $request );
[431] Fix | Delete
}
[432] Fix | Delete
[433] Fix | Delete
/**
[434] Fix | Delete
* Creates a single template.
[435] Fix | Delete
*
[436] Fix | Delete
* @since 5.8.0
[437] Fix | Delete
*
[438] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[439] Fix | Delete
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
[440] Fix | Delete
*/
[441] Fix | Delete
public function create_item( $request ) {
[442] Fix | Delete
$prepared_post = $this->prepare_item_for_database( $request );
[443] Fix | Delete
[444] Fix | Delete
if ( is_wp_error( $prepared_post ) ) {
[445] Fix | Delete
return $prepared_post;
[446] Fix | Delete
}
[447] Fix | Delete
[448] Fix | Delete
$prepared_post->post_name = $request['slug'];
[449] Fix | Delete
$post_id = wp_insert_post( wp_slash( (array) $prepared_post ), true );
[450] Fix | Delete
if ( is_wp_error( $post_id ) ) {
[451] Fix | Delete
if ( 'db_insert_error' === $post_id->get_error_code() ) {
[452] Fix | Delete
$post_id->add_data( array( 'status' => 500 ) );
[453] Fix | Delete
} else {
[454] Fix | Delete
$post_id->add_data( array( 'status' => 400 ) );
[455] Fix | Delete
}
[456] Fix | Delete
[457] Fix | Delete
return $post_id;
[458] Fix | Delete
}
[459] Fix | Delete
$posts = get_block_templates( array( 'wp_id' => $post_id ), $this->post_type );
[460] Fix | Delete
if ( ! count( $posts ) ) {
[461] Fix | Delete
return new WP_Error( 'rest_template_insert_error', __( 'No templates exist with that id.' ), array( 'status' => 400 ) );
[462] Fix | Delete
}
[463] Fix | Delete
$id = $posts[0]->id;
[464] Fix | Delete
$post = get_post( $post_id );
[465] Fix | Delete
$template = get_block_template( $id, $this->post_type );
[466] Fix | Delete
$fields_update = $this->update_additional_fields_for_object( $template, $request );
[467] Fix | Delete
if ( is_wp_error( $fields_update ) ) {
[468] Fix | Delete
return $fields_update;
[469] Fix | Delete
}
[470] Fix | Delete
[471] Fix | Delete
/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
[472] Fix | Delete
do_action( "rest_after_insert_{$this->post_type}", $post, $request, true );
[473] Fix | Delete
[474] Fix | Delete
wp_after_insert_post( $post, false, null );
[475] Fix | Delete
[476] Fix | Delete
$response = $this->prepare_item_for_response( $template, $request );
[477] Fix | Delete
$response = rest_ensure_response( $response );
[478] Fix | Delete
[479] Fix | Delete
$response->set_status( 201 );
[480] Fix | Delete
$response->header( 'Location', rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $template->id ) ) );
[481] Fix | Delete
[482] Fix | Delete
return $response;
[483] Fix | Delete
}
[484] Fix | Delete
[485] Fix | Delete
/**
[486] Fix | Delete
* Checks if a given request has access to delete a single template.
[487] Fix | Delete
*
[488] Fix | Delete
* @since 5.8.0
[489] Fix | Delete
*
[490] Fix | Delete
* @param WP_REST_Request $request Full details about the request.
[491] Fix | Delete
* @return true|WP_Error True if the request has delete access for the item, WP_Error object otherwise.
[492] Fix | Delete
*/
[493] Fix | Delete
public function delete_item_permissions_check( $request ) {
[494] Fix | Delete
return $this->permissions_check( $request );
[495] Fix | Delete
}
[496] Fix | Delete
[497] Fix | Delete
/**
[498] Fix | Delete
* Deletes a single template.
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function