- Timestamp:
- 11/30/2021 12:22:30 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php
r52268 r52275 73 73 'args' => array( 74 74 'id' => array( 75 'description' => __( 'The id of a template' ), 76 'type' => 'string', 75 'description' => __( 'The id of a template' ), 76 'type' => 'string', 77 'sanitize_callback' => array( $this, '_sanitize_template_id' ), 77 78 ), 78 79 ), … … 133 134 134 135 /** 136 * Requesting this endpoint for a template like 'twentytwentytwo//home' 137 * requires using a path like /wp/v2/templates/twentytwentytwo//home. There 138 * are special cases when WordPress routing corrects the name to contain 139 * only a single slash like 'twentytwentytwo/home'. 140 * 141 * This method doubles the last slash if it's not already doubled. It relies 142 * on the template ID format {theme_name}//{template_slug} and the fact that 143 * slugs cannot contain slashes. 144 * 145 * @since 5.9.0 146 * @see https://core.trac.wordpress.org/ticket/54507 147 * 148 * @param string $id Template ID. 149 * @return string Sanitized template ID. 150 */ 151 public function _sanitize_template_id( $id ) { 152 $last_slash_pos = strrpos( $id, '/' ); 153 if ( false === $last_slash_pos ) { 154 return $id; 155 } 156 157 $is_double_slashed = substr( $id, $last_slash_pos - 1, 1 ) === '/'; 158 if ( $is_double_slashed ) { 159 return $id; 160 } 161 return ( 162 substr( $id, 0, $last_slash_pos ) 163 . '/' 164 . substr( $id, $last_slash_pos ) 165 ); 166 } 167 168 /** 135 169 * Checks if a given request has access to read templates. 136 170 * … … 244 278 245 279 $changes = $this->prepare_item_for_database( $request ); 280 281 if ( is_wp_error( $changes ) ) { 282 return $changes; 283 } 246 284 247 285 if ( 'custom' === $template->source ) { … … 299 337 public function create_item( $request ) { 300 338 $prepared_post = $this->prepare_item_for_database( $request ); 339 340 if ( is_wp_error( $prepared_post ) ) { 341 return $prepared_post; 342 } 343 301 344 $prepared_post->post_name = $request['slug']; 302 345 $post_id = wp_insert_post( wp_slash( (array) $prepared_post ), true ); … … 431 474 'wp_theme' => $template->theme, 432 475 ); 476 $changes->meta_input = array( 477 'origin' => $template->source, 478 ); 433 479 } else { 434 480 $changes->post_name = $template->slug; … … 470 516 } 471 517 518 if ( ! empty( $request['author'] ) ) { 519 $post_author = (int) $request['author']; 520 521 if ( get_current_user_id() !== $post_author ) { 522 $user_obj = get_userdata( $post_author ); 523 524 if ( ! $user_obj ) { 525 return new WP_Error( 526 'rest_invalid_author', 527 __( 'Invalid author ID.' ), 528 array( 'status' => 400 ) 529 ); 530 } 531 } 532 533 $changes->post_author = $post_author; 534 } 535 472 536 return $changes; 473 537 } … … 517 581 if ( rest_is_field_included( 'source', $fields ) ) { 518 582 $data['source'] = $template->source; 583 } 584 585 if ( rest_is_field_included( 'origin', $fields ) ) { 586 $data['origin'] = $template->origin; 519 587 } 520 588 … … 554 622 if ( rest_is_field_included( 'has_theme_file', $fields ) ) { 555 623 $data['has_theme_file'] = (bool) $template->has_theme_file; 624 } 625 626 if ( rest_is_field_included( 'is_custom', $fields ) && 'wp_template' === $template->type ) { 627 $data['is_custom'] = $template->is_custom; 628 } 629 630 if ( rest_is_field_included( 'author', $fields ) ) { 631 $data['author'] = (int) $template->author; 556 632 } 557 633 … … 704 780 'readonly' => true, 705 781 ), 782 'origin' => array( 783 'description' => __( 'Source of a customized template' ), 784 'type' => 'string', 785 'context' => array( 'embed', 'view', 'edit' ), 786 'readonly' => true, 787 ), 706 788 'content' => array( 707 789 'description' => __( 'Content of template.' ), … … 767 849 'readonly' => true, 768 850 ), 851 'author' => array( 852 'description' => __( 'The ID for the author of the template.' ), 853 'type' => 'integer', 854 'context' => array( 'view', 'edit', 'embed' ), 855 ), 769 856 ), 770 857 ); 858 859 if ( 'wp_template' === $this->post_type ) { 860 $schema['properties']['is_custom'] = array( 861 'description' => __( 'Whether a template is a custom template.' ), 862 'type' => 'bool', 863 'context' => array( 'embed', 'view', 'edit' ), 864 'readonly' => true, 865 ); 866 } 771 867 772 868 if ( 'wp_template_part' === $this->post_type ) {
Note: See TracChangeset
for help on using the changeset viewer.