Changeset 56649 for trunk/src/wp-includes/blocks.php
- Timestamp:
- 09/21/2023 04:16:05 PM (20 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks.php
r56644 r56649 752 752 $hooked_blocks = array(); 753 753 foreach ( $block_types as $block_type ) { 754 if ( ! property_exists( $block_type, 'block_hooks' ) || ! is_array( $block_type->block_hooks ) ) { 755 continue; 756 } 754 757 foreach ( $block_type->block_hooks as $anchor_block_type => $relative_position ) { 755 758 if ( $anchor_block_type === $name ) { … … 759 762 } 760 763 return $hooked_blocks; 764 } 765 766 /** 767 * Returns a function that injects the theme attribute into, and hooked blocks before, a given block. 768 * 769 * The returned function can be used as `$pre_callback` argument to `traverse_and_serialize_block(s)`, 770 * where it will inject the `theme` attribute into all Template Part blocks, and prepend the markup for 771 * any blocks hooked `before` the given block and as its parent's `first_child`, respectively. 772 * 773 * @since 6.4.0 774 * @access private 775 * 776 * @param WP_Block_Template|array $context A block template, template part, or pattern that the blocks belong to. 777 * @return callable A function that returns the serialized markup for the given block, 778 * including the markup for any hooked blocks before it. 779 */ 780 function make_before_block_visitor( $context ) { 781 /** 782 * Injects hooked blocks before the given block, injects the `theme` attribute into Template Part blocks, and returns the serialized markup. 783 * 784 * If the current block is a Template Part block, inject the `theme` attribute. 785 * Furthermore, prepend the markup for any blocks hooked `before` the given block and as its parent's 786 * `first_child`, respectively, to the serialized markup for the given block. 787 * 788 * @param array $block The block to inject the theme attribute into, and hooked blocks before. 789 * @param array $parent The parent block of the given block. 790 * @param array $prev The previous sibling block of the given block. 791 * @return string The serialized markup for the given block, with the markup for any hooked blocks prepended to it. 792 */ 793 return function( &$block, $parent = null, $prev = null ) use ( $context ) { 794 _inject_theme_attribute_in_template_part_block( $block ); 795 796 $markup = ''; 797 798 if ( $parent && ! $prev ) { 799 // Candidate for first-child insertion. 800 $hooked_blocks_for_parent = get_hooked_blocks( $parent['blockName'] ); 801 foreach ( $hooked_blocks_for_parent as $hooked_block_type => $relative_position ) { 802 if ( 'first_child' === $relative_position ) { 803 $hooked_block_markup = get_comment_delimited_block_content( $hooked_block_type, array(), '' ); 804 /** This filter is documented in wp-includes/blocks.php */ 805 $markup .= apply_filters( 'inject_hooked_block_markup', $hooked_block_markup, $hooked_block_type, $relative_position, $parent, $context ); 806 } 807 } 808 } 809 810 $hooked_blocks = get_hooked_blocks( $block['blockName'] ); 811 foreach ( $hooked_blocks as $hooked_block_type => $relative_position ) { 812 if ( 'before' === $relative_position ) { 813 $hooked_block_markup = get_comment_delimited_block_content( $hooked_block_type, array(), '' ); 814 /** 815 * Filters the serialized markup of a hooked block. 816 * 817 * @since 6.4.0 818 * 819 * @param string $hooked_block_markup The serialized markup of the hooked block. 820 * @param string $hooked_block_type The type of the hooked block. 821 * @param string $relative_position The relative position of the hooked block. 822 * Can be one of 'before', 'after', 'first_child', or 'last_child'. 823 * @param array $block The anchor block. 824 * @param WP_Block_Template|array $context The block template, template part, or pattern that the anchor block belongs to. 825 */ 826 $markup .= apply_filters( 'inject_hooked_block_markup', $hooked_block_markup, $hooked_block_type, $relative_position, $block, $context ); 827 } 828 } 829 830 return $markup; 831 }; 832 } 833 834 /** 835 * Returns a function that injects the hooked blocks after a given block. 836 * 837 * The returned function can be used as `$post_callback` argument to `traverse_and_serialize_block(s)`, 838 * where it will append the markup for any blocks hooked `after` the given block and as its parent's 839 * `last_child`, respectively. 840 * 841 * @since 6.4.0 842 * @access private 843 * 844 * @param WP_Block_Template|array $context A block template, template part, or pattern that the blocks belong to. 845 * @return callable A function that returns the serialized markup for the given block, 846 * including the markup for any hooked blocks after it. 847 */ 848 function make_after_block_visitor( $context ) { 849 /** 850 * Injects hooked blocks after the given block, and returns the serialized markup. 851 * 852 * Append the markup for any blocks hooked `after` the given block and as its parent's 853 * `last_child`, respectively, to the serialized markup for the given block. 854 * 855 * @param array $block The block to inject the hooked blocks after. 856 * @param array $parent The parent block of the given block. 857 * @param array $next The next sibling block of the given block. 858 * @return string The serialized markup for the given block, with the markup for any hooked blocks appended to it. 859 */ 860 return function( &$block, $parent = null, $next = null ) use ( $context ) { 861 $markup = ''; 862 863 $hooked_blocks = get_hooked_blocks( $block['blockName'] ); 864 foreach ( $hooked_blocks as $hooked_block_type => $relative_position ) { 865 if ( 'after' === $relative_position ) { 866 $hooked_block_markup = get_comment_delimited_block_content( $hooked_block_type, array(), '' ); 867 /** This filter is documented in wp-includes/blocks.php */ 868 $markup .= apply_filters( 'inject_hooked_block_markup', $hooked_block_markup, $hooked_block_type, $relative_position, $block, $context ); 869 } 870 } 871 872 if ( $parent && ! $next ) { 873 // Candidate for last-child insertion. 874 $hooked_blocks_for_parent = get_hooked_blocks( $parent['blockName'] ); 875 foreach ( $hooked_blocks_for_parent as $hooked_block_type => $relative_position ) { 876 if ( 'last_child' === $relative_position ) { 877 $hooked_block_markup = get_comment_delimited_block_content( $hooked_block_type, array(), '' ); 878 /** This filter is documented in wp-includes/blocks.php */ 879 $markup .= apply_filters( 'inject_hooked_block_markup', $hooked_block_markup, $hooked_block_type, $relative_position, $parent, $context ); 880 } 881 } 882 } 883 884 return $markup; 885 }; 761 886 } 762 887
Note: See TracChangeset
for help on using the changeset viewer.