| @@ -1,388 +1,271 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Block functions specific for the Gutenberg editor plugin. | |
| 3 | + * Functions related to editor blocks for the Gutenberg editor plugin. | |
| 4 | 4 | * |
| 5 | 5 | * @package gutenberg |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | -/** | |
| 9 | - * Substitutes the implementation of a core-registered block type, if exists, | |
| 10 | - * with the built result from the plugin. | |
| 11 | - */ | |
| 12 | -function gutenberg_reregister_core_block_types() { | |
| 13 | - $blocks_dirs = array( | |
| 14 | - __DIR__ . '/../build/scripts/block-library/', | |
| 15 | - __DIR__ . '/../build/scripts/edit-widgets/blocks/', | |
| 16 | - __DIR__ . '/../build/scripts/widgets/blocks/', | |
| 17 | - ); | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + die( 'Silence is golden.' ); | |
| 10 | +} | |
| 18 | 11 | |
| 19 | - foreach ( $blocks_dirs as $blocks_dir ) { | |
| 20 | - $manifest_path = $blocks_dir . 'blocks-manifest.php'; | |
| 21 | - $blocks = require $manifest_path; | |
| 12 | +if ( ! function_exists( 'register_block_type' ) ) { | |
| 13 | + /** | |
| 14 | + * Registers a block type. | |
| 15 | + * | |
| 16 | + * @since 0.1.0 | |
| 17 | + * @since 0.6.0 Now also accepts a WP_Block_Type instance as first parameter. | |
| 18 | + * | |
| 19 | + * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a | |
| 20 | + * complete WP_Block_Type instance. In case a WP_Block_Type | |
| 21 | + * is provided, the $args parameter will be ignored. | |
| 22 | + * @param array $args { | |
| 23 | + * Optional. Array of block type arguments. Any arguments may be defined, however the | |
| 24 | + * ones described below are supported by default. Default empty array. | |
| 25 | + * | |
| 26 | + * @type callable $render_callback Callback used to render blocks of this block type. | |
| 27 | + * } | |
| 28 | + * @return WP_Block_Type|false The registered block type on success, or false on failure. | |
| 29 | + */ | |
| 30 | + function register_block_type( $name, $args = array() ) { | |
| 31 | + return WP_Block_Type_Registry::get_instance()->register( $name, $args ); | |
| 32 | + } | |
| 33 | +} | |
| 22 | 34 | |
| 23 | - foreach ( $blocks as $block_name_folder => $metadata ) { | |
| 24 | - if ( ! is_array( $metadata ) || ! isset( $metadata['name'] ) ) { | |
| 25 | - continue; | |
| 26 | - } | |
| 35 | +if ( ! function_exists( 'unregister_block_type' ) ) { | |
| 36 | + /** | |
| 37 | + * Unregisters a block type. | |
| 38 | + * | |
| 39 | + * @since 0.1.0 | |
| 40 | + * @since 0.6.0 Now also accepts a WP_Block_Type instance as first parameter. | |
| 41 | + * | |
| 42 | + * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a | |
| 43 | + * complete WP_Block_Type instance. | |
| 44 | + * @return WP_Block_Type|false The unregistered block type on success, or false on failure. | |
| 45 | + */ | |
| 46 | + function unregister_block_type( $name ) { | |
| 47 | + return WP_Block_Type_Registry::get_instance()->unregister( $name ); | |
| 48 | + } | |
| 49 | +} | |
| 27 | 50 | |
| 28 | - gutenberg_deregister_core_block_and_assets( $metadata['name'] ); | |
| 29 | - gutenberg_register_core_block_assets( $block_name_folder ); | |
| 30 | - | |
| 31 | - $php_file = $blocks_dir . $block_name_folder . '.php'; | |
| 32 | - if ( file_exists( $php_file ) ) { | |
| 33 | - require_once $php_file; | |
| 34 | - } else { | |
| 35 | - register_block_type_from_metadata( $blocks_dir . $block_name_folder ); | |
| 36 | - } | |
| 51 | +if ( ! function_exists( 'gutenberg_parse_blocks' ) ) { | |
| 52 | + /** | |
| 53 | + * Parses blocks out of a content string. | |
| 54 | + * | |
| 55 | + * @since 0.5.0 | |
| 56 | + * | |
| 57 | + * @param string $content Post content. | |
| 58 | + * @return array Array of parsed block objects. | |
| 59 | + */ | |
| 60 | + function gutenberg_parse_blocks( $content ) { | |
| 61 | + /** | |
| 62 | + * Filter to allow plugins to replace the server-side block parser | |
| 63 | + * | |
| 64 | + * @since 3.8.0 | |
| 65 | + * | |
| 66 | + * @param string $parser_class Name of block parser class | |
| 67 | + */ | |
| 68 | + $parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' ); | |
| 69 | + // Load default block parser for server-side parsing if the default parser class is being used. | |
| 70 | + if ( 'WP_Block_Parser' === $parser_class ) { | |
| 71 | + require_once dirname( __FILE__ ) . '/../packages/block-serialization-default-parser/parser.php'; | |
| 37 | 72 | } |
| 73 | + $parser = new $parser_class(); | |
| 74 | + return $parser->parse( $content ); | |
| 38 | 75 | } |
| 39 | 76 | } |
| 40 | 77 | |
| 41 | -add_action( 'init', 'gutenberg_reregister_core_block_types' ); | |
| 78 | +if ( ! function_exists( 'get_dynamic_block_names' ) ) { | |
| 79 | + /** | |
| 80 | + * Returns an array of the names of all registered dynamic block types. | |
| 81 | + * | |
| 82 | + * @return array Array of dynamic block names. | |
| 83 | + */ | |
| 84 | + function get_dynamic_block_names() { | |
| 85 | + $dynamic_block_names = array(); | |
| 42 | 86 | |
| 43 | -/** | |
| 44 | - * Adds the defer loading strategy to all registered blocks. | |
| 45 | - * | |
| 46 | - * This function would not be part of core merge. Instead, the register_block_script_handle() function would be patched | |
| 47 | - * as follows. | |
| 48 | - * | |
| 49 | - * ``` | |
| 50 | - * --- a/wp-includes/blocks.php | |
| 51 | - * +++ b/wp-includes/blocks.php | |
| 52 | - * @ @ -153,7 +153,8 @ @ function register_block_script_handle( $metadata, $field_name, $index = 0 ) { | |
| 53 | - * $script_handle, | |
| 54 | - * $script_uri, | |
| 55 | - * $script_dependencies, | |
| 56 | - * - $script_asset['version'] ?? false | |
| 57 | - * + $script_asset['version'] ?? false, | |
| 58 | - * + array( 'strategy' => 'defer' ) | |
| 59 | - * ); | |
| 60 | - * if ( ! $result ) { | |
| 61 | - * return false; | |
| 62 | - * ``` | |
| 63 | - * | |
| 64 | - * @see register_block_script_handle() | |
| 65 | - */ | |
| 66 | -function gutenberg_defer_block_view_scripts() { | |
| 67 | - $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered(); | |
| 68 | - foreach ( $block_types as $block_type ) { | |
| 69 | - foreach ( $block_type->view_script_handles as $view_script_handle ) { | |
| 70 | - wp_script_add_data( $view_script_handle, 'strategy', 'defer' ); | |
| 87 | + $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered(); | |
| 88 | + foreach ( $block_types as $block_type ) { | |
| 89 | + if ( $block_type->is_dynamic() ) { | |
| 90 | + $dynamic_block_names[] = $block_type->name; | |
| 91 | + } | |
| 71 | 92 | } |
| 93 | + | |
| 94 | + return $dynamic_block_names; | |
| 72 | 95 | } |
| 73 | 96 | } |
| 74 | 97 | |
| 75 | -add_action( 'init', 'gutenberg_defer_block_view_scripts', 100 ); | |
| 98 | +if ( ! function_exists( 'get_dynamic_blocks_regex' ) ) { | |
| 99 | + /** | |
| 100 | + * Retrieve the dynamic blocks regular expression for searching. | |
| 101 | + * | |
| 102 | + * @since 3.6.0 | |
| 103 | + * | |
| 104 | + * @return string | |
| 105 | + */ | |
| 106 | + function get_dynamic_blocks_regex() { | |
| 107 | + $dynamic_block_names = get_dynamic_block_names(); | |
| 108 | + $dynamic_block_pattern = ( | |
| 109 | + '/<!--\s+wp:(' . | |
| 110 | + str_replace( | |
| 111 | + '/', | |
| 112 | + '\/', // Escape namespace, not handled by preg_quote. | |
| 113 | + str_replace( | |
| 114 | + 'core/', | |
| 115 | + '(?:core/)?', // Allow implicit core namespace, but don't capture. | |
| 116 | + implode( | |
| 117 | + '|', // Join block names into capture group alternation. | |
| 118 | + array_map( | |
| 119 | + 'preg_quote', // Escape block name for regular expression. | |
| 120 | + $dynamic_block_names | |
| 121 | + ) | |
| 122 | + ) | |
| 123 | + ) | |
| 124 | + ) . | |
| 125 | + ')(\s+(\{.*?\}))?\s+(\/)?-->/' | |
| 126 | + ); | |
| 76 | 127 | |
| 77 | -/** | |
| 78 | - * Deregisters the existing core block type and its assets. | |
| 79 | - * | |
| 80 | - * @param string $block_name The name of the block. | |
| 81 | - * | |
| 82 | - * @return void | |
| 83 | - */ | |
| 84 | -function gutenberg_deregister_core_block_and_assets( $block_name ) { | |
| 85 | - $registry = WP_Block_Type_Registry::get_instance(); | |
| 86 | - if ( $registry->is_registered( $block_name ) ) { | |
| 87 | - $block_type = $registry->get_registered( $block_name ); | |
| 88 | - if ( ! empty( $block_type->view_script_handles ) ) { | |
| 89 | - foreach ( $block_type->view_script_handles as $view_script_handle ) { | |
| 90 | - if ( str_starts_with( $view_script_handle, 'wp-block-' ) ) { | |
| 91 | - wp_deregister_script( $view_script_handle ); | |
| 92 | - } | |
| 93 | - } | |
| 94 | - } | |
| 95 | - $registry->unregister( $block_name ); | |
| 128 | + return $dynamic_block_pattern; | |
| 96 | 129 | } |
| 97 | 130 | } |
| 98 | 131 | |
| 99 | 132 | /** |
| 100 | - * Registers block styles for a core block. | |
| 133 | + * Renders a single block into a HTML string. | |
| 101 | 134 | * |
| 102 | - * @param string $block_name The block-name. | |
| 135 | + * @since 1.9.0 | |
| 136 | + * @since 4.4.0 renders full nested tree of blocks before reassembling into HTML string | |
| 137 | + * @global WP_Post $post The post to edit. | |
| 103 | 138 | * |
| 104 | - * @return void | |
| 139 | + * @param array $block A single parsed block object. | |
| 140 | + * @return string String of rendered HTML. | |
| 105 | 141 | */ |
| 106 | -function gutenberg_register_core_block_assets( $block_name ) { | |
| 107 | - static $gutenberg_url_root = null; | |
| 108 | - // Running `gutenberg_url` inside of a loop can be expensive in systems with | |
| 109 | - // many callbacks attached to the `plugins_url` hook. | |
| 110 | - // Since all of the paths have the same root, we can instead retrieve the | |
| 111 | - // corresponding URL root once, and manually concatenate the URL below. | |
| 112 | - if ( is_null( $gutenberg_url_root ) ) { | |
| 113 | - $gutenberg_url_root = gutenberg_url( '/' ); | |
| 114 | - } | |
| 142 | +function gutenberg_render_block( $block ) { | |
| 143 | + global $post; | |
| 115 | 144 | |
| 116 | - if ( ! wp_should_load_separate_core_block_assets() ) { | |
| 117 | - return; | |
| 145 | + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); | |
| 146 | + $is_dynamic = $block['blockName'] && null !== $block_type && $block_type->is_dynamic(); | |
| 147 | + $inner_content = ''; | |
| 148 | + $index = 0; | |
| 149 | + | |
| 150 | + foreach ( $block['innerContent'] as $chunk ) { | |
| 151 | + $inner_content .= is_string( $chunk ) ? $chunk : gutenberg_render_block( $block['innerBlocks'][ $index++ ] ); | |
| 118 | 152 | } |
| 119 | 153 | |
| 120 | - $block_name = str_replace( 'core/', '', $block_name ); | |
| 154 | + if ( $is_dynamic ) { | |
| 155 | + $attributes = is_array( $block['attrs'] ) ? (array) $block['attrs'] : array(); | |
| 156 | + $global_post = $post; | |
| 157 | + $output = $block_type->render( $attributes, $inner_content ); | |
| 158 | + $post = $global_post; | |
| 121 | 159 | |
| 122 | - // When in production, use the plugin's version as the default asset version; | |
| 123 | - // else (for development or test) default to use the current time. | |
| 124 | - $default_version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time(); | |
| 125 | - $suffix = SCRIPT_DEBUG ? '' : '.min'; | |
| 160 | + return $output; | |
| 161 | + } | |
| 126 | 162 | |
| 127 | - $style_path = "build/styles/block-library/$block_name/"; | |
| 128 | - $stylesheet_url = $gutenberg_url_root . $style_path . 'style' . $suffix . '.css'; | |
| 129 | - $stylesheet_path = gutenberg_dir_path() . $style_path . ( is_rtl() ? 'style-rtl' . $suffix . '.css' : 'style' . $suffix . '.css' ); | |
| 163 | + return $inner_content; | |
| 164 | +} | |
| 130 | 165 | |
| 131 | - if ( file_exists( $stylesheet_path ) ) { | |
| 132 | - | |
| 133 | - wp_deregister_style( "wp-block-{$block_name}" ); | |
| 134 | - wp_register_style( | |
| 135 | - "wp-block-{$block_name}", | |
| 136 | - $stylesheet_url, | |
| 137 | - array(), | |
| 138 | - $default_version | |
| 139 | - ); | |
| 140 | - wp_style_add_data( "wp-block-{$block_name}", 'rtl', 'replace' ); | |
| 141 | - wp_style_add_data( "wp-block-{$block_name}", 'suffix', $suffix ); | |
| 142 | - // Add a reference to the stylesheet's path to allow calculations for inlining styles in `wp_head`. | |
| 143 | - wp_style_add_data( "wp-block-{$block_name}", 'path', $stylesheet_path ); | |
| 144 | - } else { | |
| 145 | - wp_register_style( "wp-block-{$block_name}", false, array() ); | |
| 146 | - } | |
| 147 | - | |
| 148 | - /* | |
| 149 | - * If the current theme supports wp-block-styles, dequeue the core styles | |
| 150 | - * and enqueue the plugin ones instead. | |
| 166 | +if ( ! function_exists( 'do_blocks' ) ) { | |
| 167 | + /** | |
| 168 | + * Parses dynamic blocks out of `post_content` and re-renders them. | |
| 169 | + * | |
| 170 | + * @since 0.1.0 | |
| 171 | + * @since 4.4.0 performs full parse on input post content | |
| 172 | + * | |
| 173 | + * @param string $content Post content. | |
| 174 | + * @return string Updated post content. | |
| 151 | 175 | */ |
| 152 | - if ( current_theme_supports( 'wp-block-styles' ) ) { | |
| 176 | + function do_blocks( $content ) { | |
| 177 | + // If there are blocks in this content, we shouldn't run wpautop() on it later. | |
| 178 | + $priority = has_filter( 'the_content', 'wpautop' ); | |
| 179 | + if ( false !== $priority && doing_filter( 'the_content' ) && has_blocks( $content ) ) { | |
| 180 | + remove_filter( 'the_content', 'wpautop', $priority ); | |
| 181 | + add_filter( 'the_content', '_restore_wpautop_hook', $priority + 1 ); | |
| 182 | + } | |
| 153 | 183 | |
| 154 | - // Get the path to the block's stylesheet. | |
| 155 | - $theme_style_path = is_rtl() | |
| 156 | - ? "build/styles/block-library/$block_name/theme-rtl{$suffix}.css" | |
| 157 | - : "build/styles/block-library/$block_name/theme{$suffix}.css"; | |
| 184 | + $blocks = gutenberg_parse_blocks( $content ); | |
| 185 | + $output = ''; | |
| 158 | 186 | |
| 159 | - // If the file exists, enqueue it. | |
| 160 | - if ( file_exists( gutenberg_dir_path() . $theme_style_path ) ) { | |
| 161 | - wp_deregister_style( "wp-block-{$block_name}-theme" ); | |
| 162 | - wp_register_style( | |
| 163 | - "wp-block-{$block_name}-theme", | |
| 164 | - $gutenberg_url_root . $theme_style_path, | |
| 165 | - array(), | |
| 166 | - $default_version | |
| 167 | - ); | |
| 168 | - wp_style_add_data( "wp-block-{$block_name}-theme", 'path', gutenberg_dir_path() . $theme_style_path ); | |
| 169 | - wp_style_add_data( "wp-block-{$block_name}-theme", 'suffix', $suffix ); | |
| 187 | + foreach ( $blocks as $block ) { | |
| 188 | + $output .= gutenberg_render_block( $block ); | |
| 170 | 189 | } |
| 171 | - } | |
| 172 | 190 | |
| 173 | - $editor_style_path = "build/styles/block-library/$block_name/style-editor{$suffix}.css"; | |
| 174 | - if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) { | |
| 175 | - wp_deregister_style( "wp-block-{$block_name}-editor" ); | |
| 176 | - wp_register_style( | |
| 177 | - "wp-block-{$block_name}-editor", | |
| 178 | - $gutenberg_url_root . $editor_style_path, | |
| 179 | - array(), | |
| 180 | - $default_version | |
| 181 | - ); | |
| 182 | - wp_style_add_data( "wp-block-{$block_name}-editor", 'rtl', 'replace' ); | |
| 183 | - wp_style_add_data( "wp-block-{$block_name}-editor", 'suffix', $suffix ); | |
| 184 | - } else { | |
| 185 | - wp_register_style( "wp-block-{$block_name}-editor", false ); | |
| 191 | + return $output; | |
| 186 | 192 | } |
| 187 | -} | |
| 188 | 193 | |
| 189 | -/** | |
| 190 | - * Complements the implementation of block type `core/social-icon`, whether it | |
| 191 | - * be provided by core or the plugin, with derived block types for each | |
| 192 | - * "service" (WordPress, Twitter, etc.) supported by Social Links. | |
| 193 | - * | |
| 194 | - * This ensures backwards compatibility for any users running the Gutenberg | |
| 195 | - * plugin who have used Social Links prior to their conversion to block | |
| 196 | - * variations. | |
| 197 | - * | |
| 198 | - * This shim is INTENTIONALLY left out of core, as Social Links have never | |
| 199 | - * landed there. | |
| 200 | - * | |
| 201 | - * @link https://github.com/WordPress/gutenberg/pull/19887 | |
| 202 | - */ | |
| 203 | -function gutenberg_register_legacy_social_link_blocks() { | |
| 204 | - $services = array( | |
| 205 | - 'amazon', | |
| 206 | - 'bandcamp', | |
| 207 | - 'behance', | |
| 208 | - 'chain', | |
| 209 | - 'codepen', | |
| 210 | - 'deviantart', | |
| 211 | - 'dribbble', | |
| 212 | - 'dropbox', | |
| 213 | - 'etsy', | |
| 214 | - 'facebook', | |
| 215 | - 'feed', | |
| 216 | - 'fivehundredpx', | |
| 217 | - 'flickr', | |
| 218 | - 'foursquare', | |
| 219 | - 'goodreads', | |
| 220 | - 'google', | |
| 221 | - 'github', | |
| 222 | - 'instagram', | |
| 223 | - 'lastfm', | |
| 224 | - 'linkedin', | |
| 225 | - 'mail', | |
| 226 | - 'mastodon', | |
| 227 | - 'meetup', | |
| 228 | - 'medium', | |
| 229 | - 'pinterest', | |
| 230 | - 'pocket', | |
| 231 | - 'reddit', | |
| 232 | - 'skype', | |
| 233 | - 'snapchat', | |
| 234 | - 'soundcloud', | |
| 235 | - 'spotify', | |
| 236 | - 'tumblr', | |
| 237 | - 'twitch', | |
| 238 | - 'twitter', | |
| 239 | - 'vimeo', | |
| 240 | - 'vk', | |
| 241 | - 'wordpress', | |
| 242 | - 'yelp', | |
| 243 | - 'youtube', | |
| 244 | - ); | |
| 245 | - | |
| 246 | - foreach ( $services as $service ) { | |
| 247 | - register_block_type( | |
| 248 | - 'core/social-link-' . $service, | |
| 249 | - array( | |
| 250 | - 'category' => 'widgets', | |
| 251 | - 'attributes' => array( | |
| 252 | - 'url' => array( | |
| 253 | - 'type' => 'string', | |
| 254 | - ), | |
| 255 | - 'service' => array( | |
| 256 | - 'type' => 'string', | |
| 257 | - 'default' => $service, | |
| 258 | - ), | |
| 259 | - 'label' => array( | |
| 260 | - 'type' => 'string', | |
| 261 | - ), | |
| 262 | - ), | |
| 263 | - 'render_callback' => 'gutenberg_render_block_core_social_link', | |
| 264 | - ) | |
| 265 | - ); | |
| 266 | - } | |
| 194 | + add_filter( 'the_content', 'do_blocks', 7 ); // BEFORE do_shortcode() and oembed. | |
| 267 | 195 | } |
| 268 | 196 | |
| 269 | -add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' ); | |
| 197 | +if ( ! function_exists( '_restore_wpautop_hook' ) ) { | |
| 198 | + /** | |
| 199 | + * If do_blocks() needs to remove wpautop() from the `the_content` filter, | |
| 200 | + * this re-adds it afterwards, for subsequent `the_content` usage. | |
| 201 | + * | |
| 202 | + * @access private | |
| 203 | + * | |
| 204 | + * @since 4.6.0 | |
| 205 | + * | |
| 206 | + * @param string $content The post content running through this filter. | |
| 207 | + * @return string The unmodified content. | |
| 208 | + */ | |
| 209 | + function _restore_wpautop_hook( $content ) { | |
| 210 | + $current_priority = has_filter( 'the_content', '_restore_wpautop_hook' ); | |
| 270 | 211 | |
| 271 | -/** | |
| 272 | - * Migrate the legacy `sync_status` meta key (added 16.1) to the new `wp_pattern_sync_status` meta key (16.1.1). | |
| 273 | - * | |
| 274 | - * This filter is INTENTIONALLY left out of core as the meta key was fist introduced to core in 6.3 as `wp_pattern_sync_status`. | |
| 275 | - * see https://github.com/WordPress/gutenberg/pull/52232 | |
| 276 | - * | |
| 277 | - * @param mixed $value The value to return, either a single metadata value or an array of values depending on the value of $single. | |
| 278 | - * @param int $object_id ID of the object metadata is for. | |
| 279 | - * @param string $meta_key Metadata key. | |
| 280 | - * @param bool $single Whether to return only the first value of the specified $meta_key. | |
| 281 | - */ | |
| 282 | -function gutenberg_legacy_wp_block_post_meta( $value, $object_id, $meta_key, $single ) { | |
| 283 | - if ( 'wp_pattern_sync_status' !== $meta_key ) { | |
| 284 | - return $value; | |
| 285 | - } | |
| 212 | + add_filter( 'the_content', 'wpautop', $current_priority - 1 ); | |
| 213 | + remove_filter( 'the_content', '_restore_wpautop_hook', $current_priority ); | |
| 286 | 214 | |
| 287 | - $sync_status = get_post_meta( $object_id, 'sync_status', $single ); | |
| 288 | - | |
| 289 | - if ( $single && 'unsynced' === $sync_status ) { | |
| 290 | - return $sync_status; | |
| 291 | - } elseif ( isset( $sync_status[0] ) && 'unsynced' === $sync_status[0] ) { | |
| 292 | - return $sync_status; | |
| 215 | + return $content; | |
| 293 | 216 | } |
| 294 | - | |
| 295 | - return $value; | |
| 296 | 217 | } |
| 297 | 218 | |
| 298 | -add_filter( 'default_post_metadata', 'gutenberg_legacy_wp_block_post_meta', 10, 4 ); | |
| 299 | - | |
| 300 | - | |
| 301 | - | |
| 302 | -/** | |
| 303 | - * Strips all HTML from the content of footnotes, and sanitizes the ID. | |
| 304 | - * | |
| 305 | - * This function expects slashed data on the footnotes content. | |
| 306 | - * | |
| 307 | - * @access private | |
| 308 | - * | |
| 309 | - * @param string $footnotes JSON encoded string of an array containing the content and ID of each footnote. | |
| 310 | - * @return string Filtered content without any HTML on the footnote content and with the sanitized id. | |
| 311 | - */ | |
| 312 | -function _gutenberg_filter_post_meta_footnotes( $footnotes ) { | |
| 313 | - $footnotes_decoded = json_decode( $footnotes, true ); | |
| 314 | - if ( ! is_array( $footnotes_decoded ) ) { | |
| 315 | - return ''; | |
| 219 | +if ( ! function_exists( 'strip_dynamic_blocks' ) ) { | |
| 220 | + /** | |
| 221 | + * Remove all dynamic blocks from the given content. | |
| 222 | + * | |
| 223 | + * @since 3.6.0 | |
| 224 | + * | |
| 225 | + * @param string $content Content of the current post. | |
| 226 | + * @return string | |
| 227 | + */ | |
| 228 | + function strip_dynamic_blocks( $content ) { | |
| 229 | + return preg_replace( get_dynamic_blocks_regex(), '', $content ); | |
| 316 | 230 | } |
| 317 | - $footnotes_sanitized = array(); | |
| 318 | - foreach ( $footnotes_decoded as $footnote ) { | |
| 319 | - if ( ! empty( $footnote['content'] ) && ! empty( $footnote['id'] ) ) { | |
| 320 | - $footnotes_sanitized[] = array( | |
| 321 | - 'id' => sanitize_key( $footnote['id'] ), | |
| 322 | - 'content' => wp_unslash( wp_filter_post_kses( wp_slash( $footnote['content'] ) ) ), | |
| 323 | - ); | |
| 324 | - } | |
| 325 | - } | |
| 326 | - return wp_json_encode( $footnotes_sanitized ); | |
| 327 | 231 | } |
| 328 | 232 | |
| 329 | -/** | |
| 330 | - * Adds the filters to filter footnotes meta field. | |
| 331 | - * | |
| 332 | - * @access private | |
| 333 | - */ | |
| 334 | -function _gutenberg_footnotes_kses_init_filters() { | |
| 335 | - add_filter( 'sanitize_post_meta_footnotes', '_gutenberg_filter_post_meta_footnotes' ); | |
| 336 | -} | |
| 233 | +if ( ! function_exists( 'strip_dynamic_blocks_add_filter' ) ) { | |
| 234 | + /** | |
| 235 | + * Adds the content filter to strip dynamic blocks from excerpts. | |
| 236 | + * | |
| 237 | + * It's a bit hacky for now, but once this gets merged into core the function | |
| 238 | + * can just be called in `wp_trim_excerpt()`. | |
| 239 | + * | |
| 240 | + * @since 3.6.0 | |
| 241 | + * | |
| 242 | + * @param string $text Excerpt. | |
| 243 | + * @return string | |
| 244 | + */ | |
| 245 | + function strip_dynamic_blocks_add_filter( $text ) { | |
| 246 | + add_filter( 'the_content', 'strip_dynamic_blocks', 6 ); | |
| 337 | 247 | |
| 338 | -/** | |
| 339 | - * Removes the filters that filter footnotes meta field. | |
| 340 | - * | |
| 341 | - * @access private | |
| 342 | - */ | |
| 343 | -function _gutenberg_footnotes_remove_filters() { | |
| 344 | - remove_filter( 'sanitize_post_meta_footnotes', '_gutenberg_filter_post_meta_footnotes' ); | |
| 248 | + return $text; | |
| 249 | + } | |
| 250 | + add_filter( 'get_the_excerpt', 'strip_dynamic_blocks_add_filter', 9 ); // Before wp_trim_excerpt(). | |
| 345 | 251 | } |
| 346 | 252 | |
| 347 | -/** | |
| 348 | - * Registers the filter of footnotes meta field if the user does not have unfiltered_html capability. | |
| 349 | - * | |
| 350 | - * @access private | |
| 351 | - */ | |
| 352 | -function _gutenberg_footnotes_kses_init() { | |
| 353 | - if ( function_exists( '_wp_filter_post_meta_footnotes' ) ) { | |
| 354 | - return; | |
| 355 | - } | |
| 356 | - _gutenberg_footnotes_remove_filters(); | |
| 357 | - if ( ! current_user_can( 'unfiltered_html' ) ) { | |
| 358 | - _gutenberg_footnotes_kses_init_filters(); | |
| 359 | - } | |
| 360 | -} | |
| 253 | +if ( ! function_exists( 'strip_dynamic_blocks_remove_filter' ) ) { | |
| 254 | + /** | |
| 255 | + * Removes the content filter to strip dynamic blocks from excerpts. | |
| 256 | + * | |
| 257 | + * It's a bit hacky for now, but once this gets merged into core the function | |
| 258 | + * can just be called in `wp_trim_excerpt()`. | |
| 259 | + * | |
| 260 | + * @since 3.6.0 | |
| 261 | + * | |
| 262 | + * @param string $text Excerpt. | |
| 263 | + * @return string | |
| 264 | + */ | |
| 265 | + function strip_dynamic_blocks_remove_filter( $text ) { | |
| 266 | + remove_filter( 'the_content', 'strip_dynamic_blocks', 6 ); | |
| 361 | 267 | |
| 362 | -/** | |
| 363 | - * Initializes footnotes meta field filters when imported data should be filtered. | |
| 364 | - * | |
| 365 | - * This filter is the last being executed on force_filtered_html_on_import. | |
| 366 | - * If the input of the filter is true it means we are in an import situation and should | |
| 367 | - * enable kses, independently of the user capabilities. | |
| 368 | - * So in that case we call _gutenberg_footnotes_kses_init_filters; | |
| 369 | - * | |
| 370 | - * @access private | |
| 371 | - * | |
| 372 | - * @param string $arg Input argument of the filter. | |
| 373 | - * @return string Input argument of the filter. | |
| 374 | - */ | |
| 375 | -function _gutenberg_footnotes_force_filtered_html_on_import_filter( $arg ) { | |
| 376 | - if ( function_exists( '_wp_filter_post_meta_footnotes' ) ) { | |
| 377 | - return; | |
| 268 | + return $text; | |
| 378 | 269 | } |
| 379 | - // force_filtered_html_on_import is true we need to init the global styles kses filters. | |
| 380 | - if ( $arg ) { | |
| 381 | - _gutenberg_footnotes_kses_init_filters(); | |
| 382 | - } | |
| 383 | - return $arg; | |
| 270 | + add_filter( 'wp_trim_excerpt', 'strip_dynamic_blocks_remove_filter', 0 ); // Before all other. | |
| 384 | 271 | } |
| 385 | - | |
| 386 | -add_action( 'init', '_gutenberg_footnotes_kses_init' ); | |
| 387 | -add_action( 'set_current_user', '_gutenberg_footnotes_kses_init' ); | |
| 388 | -add_filter( 'force_filtered_html_on_import', '_gutenberg_footnotes_force_filtered_html_on_import_filter', 999 ); | |