Changeset 58041 for branches/6.5/src/wp-includes/block-template-utils.php
- Timestamp:
- 04/24/2024 12:00:29 PM (14 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/6.5/src/wp-includes/block-template-utils.php
r57947 r58041 724 724 725 725 /** 726 * Builds a unified template object based a post Object. 727 * 728 * @since 5.9.0 729 * @since 6.3.0 Added `modified` property to template objects. 730 * @since 6.4.0 Added support for a revision post to be passed to this function. 726 * Builds a block template object from a post object. 727 * 728 * This is a helper function that creates a block template object from a given post object. 729 * It is self-sufficient in that it only uses information passed as arguments; it does not 730 * query the database for additional information. 731 * 732 * @since 6.5.1 731 733 * @access private 732 734 * 733 * @param WP_Post $post Template post. 735 * @param WP_Post $post Template post. 736 * @param array $terms Additional terms to inform the template object. 737 * @param array $meta Additional meta fields to inform the template object. 734 738 * @return WP_Block_Template|WP_Error Template or error object. 735 739 */ 736 function _build_block_template_result_from_post( $post ) { 740 function _build_block_template_object_from_post_object( $post, $terms = array(), $meta = array() ) { 741 if ( empty( $terms['wp_theme'] ) ) { 742 return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) ); 743 } 744 $theme = $terms['wp_theme']; 745 737 746 $default_template_types = get_default_block_template_types(); 738 747 739 $post_id = wp_is_post_revision( $post );740 if ( ! $post_id ) {741 $post_id = $post;742 }743 $parent_post = get_post( $post_id );744 745 $terms = get_the_terms( $parent_post, 'wp_theme' );746 747 if ( is_wp_error( $terms ) ) {748 return $terms;749 }750 751 if ( ! $terms ) {752 return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) );753 }754 755 $theme = $terms[0]->name;756 748 $template_file = _get_block_template_file( $post->post_type, $post->post_name ); 757 749 $has_theme_file = get_stylesheet() === $theme && null !== $template_file; 758 750 759 $origin = get_post_meta( $parent_post->ID, 'origin', true );760 $is_wp_suggestion = get_post_meta( $parent_post->ID, 'is_wp_suggestion', true );761 762 751 $template = new WP_Block_Template(); 763 752 $template->wp_id = $post->ID; 764 $template->id = $theme . '//' . $p arent_post->post_name;753 $template->id = $theme . '//' . $post->post_name; 765 754 $template->theme = $theme; 766 755 $template->content = $post->post_content; 767 756 $template->slug = $post->post_name; 768 757 $template->source = 'custom'; 769 $template->origin = ! empty( $ origin ) ? $origin: null;758 $template->origin = ! empty( $meta['origin'] ) ? $meta['origin'] : null; 770 759 $template->type = $post->post_type; 771 760 $template->description = $post->post_excerpt; … … 773 762 $template->status = $post->post_status; 774 763 $template->has_theme_file = $has_theme_file; 775 $template->is_custom = empty( $ is_wp_suggestion);764 $template->is_custom = empty( $meta['is_wp_suggestion'] ); 776 765 $template->author = $post->post_author; 777 766 $template->modified = $post->post_modified; 778 767 779 if ( 'wp_template' === $p arent_post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) {768 if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) { 780 769 $template->post_types = $template_file['postTypes']; 781 770 } 782 771 783 if ( 'wp_template' === $p arent_post->post_type && isset( $default_template_types[ $template->slug ] ) ) {772 if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) { 784 773 $template->is_custom = false; 785 774 } 775 776 if ( 'wp_template_part' === $post->post_type && isset( $terms['wp_template_part_area'] ) ) { 777 $template->area = $terms['wp_template_part_area']; 778 } 779 780 return $template; 781 } 782 783 /** 784 * Builds a unified template object based a post Object. 785 * 786 * @since 5.9.0 787 * @since 6.3.0 Added `modified` property to template objects. 788 * @since 6.4.0 Added support for a revision post to be passed to this function. 789 * @access private 790 * 791 * @param WP_Post $post Template post. 792 * @return WP_Block_Template|WP_Error Template or error object. 793 */ 794 function _build_block_template_result_from_post( $post ) { 795 $post_id = wp_is_post_revision( $post ); 796 if ( ! $post_id ) { 797 $post_id = $post; 798 } 799 $parent_post = get_post( $post_id ); 800 $post->post_name = $parent_post->post_name; 801 $post->post_type = $parent_post->post_type; 802 803 $terms = get_the_terms( $parent_post, 'wp_theme' ); 804 805 if ( is_wp_error( $terms ) ) { 806 return $terms; 807 } 808 809 if ( ! $terms ) { 810 return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) ); 811 } 812 813 $terms = array( 814 'wp_theme' => $terms[0]->name, 815 ); 786 816 787 817 if ( 'wp_template_part' === $parent_post->post_type ) { 788 818 $type_terms = get_the_terms( $parent_post, 'wp_template_part_area' ); 789 819 if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) { 790 $template->area = $type_terms[0]->name; 791 } 820 $terms['wp_template_part_area'] = $type_terms[0]->name; 821 } 822 } 823 824 $meta = array( 825 'origin' => get_post_meta( $parent_post->ID, 'origin', true ), 826 'is_wp_suggestion' => get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ), 827 ); 828 829 $template = _build_block_template_object_from_post_object( $post, $terms, $meta ); 830 831 if ( is_wp_error( $template ) ) { 832 return $template; 792 833 } 793 834 … … 1443 1484 * @access private 1444 1485 * 1445 * @param stdClass $post An object representing a template or template part 1446 * prepared for inserting or updating the database. 1447 * @param WP_REST_Request $request Request object. 1448 * @return stdClass The updated object representing a template or template part. 1449 */ 1450 function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) { 1451 $filter_name = current_filter(); 1452 if ( ! str_starts_with( $filter_name, 'rest_pre_insert_' ) ) { 1453 return $post; 1454 } 1455 $post_type = str_replace( 'rest_pre_insert_', '', $filter_name ); 1486 * @param stdClass $changes An object representing a template or template part 1487 * prepared for inserting or updating the database. 1488 * @param WP_REST_Request $deprecated Deprecated. Not used. 1489 * @return stdClass|WP_Error The updated object representing a template or template part. 1490 */ 1491 function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated = null ) { 1492 if ( null !== $deprecated ) { 1493 _deprecated_argument( __FUNCTION__, '6.5.1' ); 1494 } 1456 1495 1457 1496 $hooked_blocks = get_hooked_blocks(); 1458 1497 if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) { 1459 return $post; 1460 } 1461 1462 // At this point, the post has already been created. 1463 // We need to build the corresponding `WP_Block_Template` object as context argument for the visitor. 1464 // To that end, we need to suppress hooked blocks from getting inserted into the template. 1465 add_filter( 'hooked_block_types', '__return_empty_array', 99999, 0 ); 1466 $template = $request['id'] ? get_block_template( $request['id'], $post_type ) : null; 1467 remove_filter( 'hooked_block_types', '__return_empty_array', 99999 ); 1498 return $changes; 1499 } 1500 1501 $meta = isset( $changes->meta_input ) ? $changes->meta_input : array(); 1502 $terms = isset( $changes->tax_input ) ? $changes->tax_input : array(); 1503 1504 if ( empty( $changes->ID ) ) { 1505 // There's no post object for this template in the database for this template yet. 1506 $post = $changes; 1507 } else { 1508 // Find the existing post object. 1509 $post = get_post( $changes->ID ); 1510 1511 // If the post is a revision, use the parent post's post_name and post_type. 1512 $post_id = wp_is_post_revision( $post ); 1513 if ( $post_id ) { 1514 $parent_post = get_post( $post_id ); 1515 $post->post_name = $parent_post->post_name; 1516 $post->post_type = $parent_post->post_type; 1517 } 1518 1519 // Apply the changes to the existing post object. 1520 $post = (object) array_merge( (array) $post, (array) $changes ); 1521 1522 $type_terms = get_the_terms( $changes->ID, 'wp_theme' ); 1523 $terms['wp_theme'] = ! is_wp_error( $type_terms ) && ! empty( $type_terms ) ? $type_terms[0]->name : null; 1524 } 1525 1526 // Required for the WP_Block_Template. Update the post object with the current time. 1527 $post->post_modified = current_time( 'mysql' ); 1528 1529 // If the post_author is empty, set it to the current user. 1530 if ( empty( $post->post_author ) ) { 1531 $post->post_author = get_current_user_id(); 1532 } 1533 1534 if ( 'wp_template_part' === $post->post_type && ! isset( $terms['wp_template_part_area'] ) ) { 1535 $area_terms = get_the_terms( $changes->ID, 'wp_template_part_area' ); 1536 $terms['wp_template_part_area'] = ! is_wp_error( $area_terms ) && ! empty( $area_terms ) ? $area_terms[0]->name : null; 1537 } 1538 1539 $template = _build_block_template_object_from_post_object( new WP_Post( $post ), $terms, $meta ); 1540 1541 if ( is_wp_error( $template ) ) { 1542 return $template; 1543 } 1468 1544 1469 1545 $before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); 1470 1546 $after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' ); 1471 1547 1472 $blocks = parse_blocks( $post->post_content ); 1473 $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); 1474 1475 $post->post_content = $content; 1476 return $post; 1477 } 1548 $blocks = parse_blocks( $changes->post_content ); 1549 $changes->post_content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); 1550 1551 return $changes; 1552 }
Note: See TracChangeset
for help on using the changeset viewer.