| @@ -9,94 +9,172 @@ | ||
| 9 | 9 | * Substitutes the implementation of a core-registered block type, if exists, |
| 10 | 10 | * with the built result from the plugin. |
| 11 | 11 | */ |
| 12 | 12 | function gutenberg_reregister_core_block_types() { |
| 13 | + // Blocks directory may not exist if working from a fresh clone. | |
| 13 | 14 | $blocks_dirs = array( |
| 14 | - __DIR__ . '/../build/scripts/block-library/', | |
| 15 | - __DIR__ . '/../build/scripts/edit-widgets/blocks/', | |
| 16 | - __DIR__ . '/../build/scripts/widgets/blocks/', | |
| 15 | + __DIR__ . '/../build/block-library/blocks/' => array( | |
| 16 | + 'block_folders' => array( | |
| 17 | + 'audio', | |
| 18 | + 'button', | |
| 19 | + 'buttons', | |
| 20 | + 'freeform', | |
| 21 | + 'code', | |
| 22 | + 'column', | |
| 23 | + 'columns', | |
| 24 | + 'comments', | |
| 25 | + 'group', | |
| 26 | + 'heading', | |
| 27 | + 'html', | |
| 28 | + 'list', | |
| 29 | + 'list-item', | |
| 30 | + 'media-text', | |
| 31 | + 'missing', | |
| 32 | + 'more', | |
| 33 | + 'nextpage', | |
| 34 | + 'paragraph', | |
| 35 | + 'preformatted', | |
| 36 | + 'pullquote', | |
| 37 | + 'quote', | |
| 38 | + 'separator', | |
| 39 | + 'social-links', | |
| 40 | + 'spacer', | |
| 41 | + 'table', | |
| 42 | + 'table-of-contents', | |
| 43 | + 'text-columns', | |
| 44 | + 'verse', | |
| 45 | + 'video', | |
| 46 | + 'embed', | |
| 47 | + ), | |
| 48 | + 'block_names' => array( | |
| 49 | + 'archives.php' => 'core/archives', | |
| 50 | + 'avatar.php' => 'core/avatar', | |
| 51 | + 'block.php' => 'core/block', | |
| 52 | + 'calendar.php' => 'core/calendar', | |
| 53 | + 'categories.php' => 'core/categories', | |
| 54 | + 'cover.php' => 'core/cover', | |
| 55 | + 'comment-author-avatar.php' => 'core/comment-author-avatar', | |
| 56 | + 'comment-author-name.php' => 'core/comment-author-name', | |
| 57 | + 'comment-content.php' => 'core/comment-content', | |
| 58 | + 'comment-date.php' => 'core/comment-date', | |
| 59 | + 'comment-edit-link.php' => 'core/comment-edit-link', | |
| 60 | + 'comment-reply-link.php' => 'core/comment-reply-link', | |
| 61 | + 'comment-template.php' => 'core/comment-template', | |
| 62 | + 'comments-pagination.php' => 'core/comments-pagination', | |
| 63 | + 'comments-pagination-next.php' => 'core/comments-pagination-next', | |
| 64 | + 'comments-pagination-numbers.php' => 'core/comments-pagination-numbers', | |
| 65 | + 'comments-pagination-previous.php' => 'core/comments-pagination-previous', | |
| 66 | + 'comments-title.php' => 'core/comments-title', | |
| 67 | + 'comments.php' => 'core/comments', | |
| 68 | + 'file.php' => 'core/file', | |
| 69 | + 'home-link.php' => 'core/home-link', | |
| 70 | + 'image.php' => 'core/image', | |
| 71 | + 'gallery.php' => 'core/gallery', | |
| 72 | + 'latest-comments.php' => 'core/latest-comments', | |
| 73 | + 'latest-posts.php' => 'core/latest-posts', | |
| 74 | + 'loginout.php' => 'core/loginout', | |
| 75 | + 'navigation.php' => 'core/navigation', | |
| 76 | + 'navigation-link.php' => 'core/navigation-link', | |
| 77 | + 'navigation-submenu.php' => 'core/navigation-submenu', | |
| 78 | + 'page-list.php' => 'core/page-list', | |
| 79 | + 'pattern.php' => 'core/pattern', | |
| 80 | + 'post-author.php' => 'core/post-author', | |
| 81 | + 'post-author-name.php' => 'core/post-author-name', | |
| 82 | + 'post-author-biography.php' => 'core/post-author-biography', | |
| 83 | + 'post-comment.php' => 'core/post-comment', | |
| 84 | + 'post-comments-count.php' => 'core/post-comments-count', | |
| 85 | + 'post-comments-form.php' => 'core/post-comments-form', | |
| 86 | + 'post-comments-link.php' => 'core/post-comments-link', | |
| 87 | + 'post-content.php' => 'core/post-content', | |
| 88 | + 'post-date.php' => 'core/post-date', | |
| 89 | + 'post-excerpt.php' => 'core/post-excerpt', | |
| 90 | + 'post-featured-image.php' => 'core/post-featured-image', | |
| 91 | + 'post-navigation-link.php' => 'core/post-navigation-link', | |
| 92 | + 'post-terms.php' => 'core/post-terms', | |
| 93 | + 'post-title.php' => 'core/post-title', | |
| 94 | + 'query.php' => 'core/query', | |
| 95 | + 'post-template.php' => 'core/post-template', | |
| 96 | + 'query-no-results.php' => 'core/query-no-results', | |
| 97 | + 'query-pagination.php' => 'core/query-pagination', | |
| 98 | + 'query-pagination-next.php' => 'core/query-pagination-next', | |
| 99 | + 'query-pagination-numbers.php' => 'core/query-pagination-numbers', | |
| 100 | + 'query-pagination-previous.php' => 'core/query-pagination-previous', | |
| 101 | + 'query-title.php' => 'core/query-title', | |
| 102 | + 'read-more.php' => 'core/read-more', | |
| 103 | + 'rss.php' => 'core/rss', | |
| 104 | + 'search.php' => 'core/search', | |
| 105 | + 'shortcode.php' => 'core/shortcode', | |
| 106 | + 'social-link.php' => 'core/social-link', | |
| 107 | + 'site-logo.php' => 'core/site-logo', | |
| 108 | + 'site-tagline.php' => 'core/site-tagline', | |
| 109 | + 'site-title.php' => 'core/site-title', | |
| 110 | + 'tag-cloud.php' => 'core/tag-cloud', | |
| 111 | + 'template-part.php' => 'core/template-part', | |
| 112 | + 'term-description.php' => 'core/term-description', | |
| 113 | + ), | |
| 114 | + ), | |
| 115 | + __DIR__ . '/../build/edit-widgets/blocks/' => array( | |
| 116 | + 'block_folders' => array( | |
| 117 | + 'widget-area', | |
| 118 | + ), | |
| 119 | + 'block_names' => array(), | |
| 120 | + ), | |
| 121 | + __DIR__ . '/../build/widgets/blocks/' => array( | |
| 122 | + 'block_folders' => array( | |
| 123 | + 'legacy-widget', | |
| 124 | + 'widget-group', | |
| 125 | + ), | |
| 126 | + 'block_names' => array( | |
| 127 | + 'legacy-widget.php' => 'core/legacy-widget', | |
| 128 | + 'widget-group.php' => 'core/widget-group', | |
| 129 | + ), | |
| 130 | + ), | |
| 17 | 131 | ); |
| 132 | + foreach ( $blocks_dirs as $blocks_dir => $details ) { | |
| 133 | + $block_folders = $details['block_folders']; | |
| 134 | + $block_names = $details['block_names']; | |
| 18 | 135 | |
| 19 | - foreach ( $blocks_dirs as $blocks_dir ) { | |
| 20 | - $manifest_path = $blocks_dir . 'blocks-manifest.php'; | |
| 21 | - $blocks = require $manifest_path; | |
| 136 | + $registry = WP_Block_Type_Registry::get_instance(); | |
| 22 | 137 | |
| 23 | - foreach ( $blocks as $block_name_folder => $metadata ) { | |
| 24 | - if ( ! is_array( $metadata ) || ! isset( $metadata['name'] ) ) { | |
| 138 | + foreach ( $block_folders as $folder_name ) { | |
| 139 | + $block_json_file = $blocks_dir . $folder_name . '/block.json'; | |
| 140 | + | |
| 141 | + // Ideally, all paths to block metadata files should be listed in | |
| 142 | + // WordPress core. In this place we should rather use filter | |
| 143 | + // to replace paths with overrides defined by the plugin. | |
| 144 | + $metadata = json_decode( file_get_contents( $block_json_file ), true ); | |
| 145 | + if ( ! is_array( $metadata ) || ! $metadata['name'] ) { | |
| 25 | 146 | continue; |
| 26 | 147 | } |
| 27 | 148 | |
| 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 ); | |
| 149 | + if ( $registry->is_registered( $metadata['name'] ) ) { | |
| 150 | + $registry->unregister( $metadata['name'] ); | |
| 36 | 151 | } |
| 37 | - } | |
| 38 | - } | |
| 39 | -} | |
| 40 | 152 | |
| 41 | -add_action( 'init', 'gutenberg_reregister_core_block_types' ); | |
| 42 | - | |
| 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' ); | |
| 153 | + gutenberg_register_core_block_assets( $folder_name ); | |
| 154 | + register_block_type_from_metadata( $block_json_file ); | |
| 71 | 155 | } |
| 72 | - } | |
| 73 | -} | |
| 74 | 156 | |
| 75 | -add_action( 'init', 'gutenberg_defer_block_view_scripts', 100 ); | |
| 157 | + foreach ( $block_names as $file => $sub_block_names ) { | |
| 158 | + if ( ! file_exists( $blocks_dir . $file ) ) { | |
| 159 | + continue; | |
| 160 | + } | |
| 76 | 161 | |
| 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 ); | |
| 162 | + $sub_block_names_normalized = is_string( $sub_block_names ) ? array( $sub_block_names ) : $sub_block_names; | |
| 163 | + foreach ( $sub_block_names_normalized as $block_name ) { | |
| 164 | + if ( $registry->is_registered( $block_name ) ) { | |
| 165 | + $registry->unregister( $block_name ); | |
| 92 | 166 | } |
| 167 | + gutenberg_register_core_block_assets( $block_name ); | |
| 93 | 168 | } |
| 169 | + | |
| 170 | + require_once $blocks_dir . $file; | |
| 94 | 171 | } |
| 95 | - $registry->unregister( $block_name ); | |
| 96 | 172 | } |
| 97 | 173 | } |
| 98 | 174 | |
| 175 | +add_action( 'init', 'gutenberg_reregister_core_block_types' ); | |
| 176 | + | |
| 99 | 177 | /** |
| 100 | 178 | * Registers block styles for a core block. |
| 101 | 179 | * |
| 102 | 180 | * @param string $block_name The block-name. |
| @@ -103,17 +181,8 @@ | ||
| 103 | 181 | * |
| 104 | 182 | * @return void |
| 105 | 183 | */ |
| 106 | 184 | 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 | - } | |
| 115 | - | |
| 116 | 185 | if ( ! wp_should_load_separate_core_block_assets() ) { |
| 117 | 186 | return; |
| 118 | 187 | } |
| 119 | 188 | |
| @@ -120,17 +189,15 @@ | ||
| 120 | 189 | $block_name = str_replace( 'core/', '', $block_name ); |
| 121 | 190 | |
| 122 | 191 | // When in production, use the plugin's version as the default asset version; |
| 123 | 192 | // 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'; | |
| 193 | + $default_version = defined( 'GUTENBERG_VERSION' ) && ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? GUTENBERG_VERSION : time(); | |
| 126 | 194 | |
| 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' ); | |
| 195 | + $style_path = "build/block-library/blocks/$block_name/"; | |
| 196 | + $stylesheet_url = gutenberg_url( $style_path . 'style.css' ); | |
| 197 | + $stylesheet_path = gutenberg_dir_path() . $style_path . ( is_rtl() ? 'style-rtl.css' : 'style.css' ); | |
| 130 | 198 | |
| 131 | 199 | if ( file_exists( $stylesheet_path ) ) { |
| 132 | - | |
| 133 | 200 | wp_deregister_style( "wp-block-{$block_name}" ); |
| 134 | 201 | wp_register_style( |
| 135 | 202 | "wp-block-{$block_name}", |
| 136 | 203 | $stylesheet_url, |
| @@ -137,9 +204,9 @@ | ||
| 137 | 204 | array(), |
| 138 | 205 | $default_version |
| 139 | 206 | ); |
| 140 | 207 | wp_style_add_data( "wp-block-{$block_name}", 'rtl', 'replace' ); |
| 141 | - wp_style_add_data( "wp-block-{$block_name}", 'suffix', $suffix ); | |
| 208 | + | |
| 142 | 209 | // Add a reference to the stylesheet's path to allow calculations for inlining styles in `wp_head`. |
| 143 | 210 | wp_style_add_data( "wp-block-{$block_name}", 'path', $stylesheet_path ); |
| 144 | 211 | } else { |
| 145 | 212 | wp_register_style( "wp-block-{$block_name}", false, array() ); |
| @@ -144,44 +211,62 @@ | ||
| 144 | 211 | } else { |
| 145 | 212 | wp_register_style( "wp-block-{$block_name}", false, array() ); |
| 146 | 213 | } |
| 147 | 214 | |
| 148 | - /* | |
| 149 | - * If the current theme supports wp-block-styles, dequeue the core styles | |
| 150 | - * and enqueue the plugin ones instead. | |
| 151 | - */ | |
| 215 | + // If the current theme supports wp-block-styles, dequeue the full stylesheet | |
| 216 | + // and instead attach each block's theme-styles to their block styles stylesheet. | |
| 152 | 217 | if ( current_theme_supports( 'wp-block-styles' ) ) { |
| 153 | 218 | |
| 219 | + // Dequeue the full stylesheet. | |
| 220 | + // Make sure this only runs once, it doesn't need to run for every block. | |
| 221 | + static $stylesheet_removed; | |
| 222 | + if ( ! $stylesheet_removed ) { | |
| 223 | + add_action( | |
| 224 | + 'wp_enqueue_scripts', | |
| 225 | + function() { | |
| 226 | + wp_dequeue_style( 'wp-block-library-theme' ); | |
| 227 | + } | |
| 228 | + ); | |
| 229 | + $stylesheet_removed = true; | |
| 230 | + } | |
| 231 | + | |
| 154 | 232 | // Get the path to the block's stylesheet. |
| 155 | 233 | $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"; | |
| 234 | + ? "build/block-library/blocks/$block_name/theme-rtl.css" | |
| 235 | + : "build/block-library/blocks/$block_name/theme.css"; | |
| 158 | 236 | |
| 159 | 237 | // If the file exists, enqueue it. |
| 160 | 238 | 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 ); | |
| 239 | + | |
| 240 | + if ( file_exists( $stylesheet_path ) ) { | |
| 241 | + // If there is a main stylesheet for this block, append the theme styles to main styles. | |
| 242 | + wp_add_inline_style( | |
| 243 | + "wp-block-{$block_name}", | |
| 244 | + file_get_contents( gutenberg_dir_path() . $theme_style_path ) | |
| 245 | + ); | |
| 246 | + } else { | |
| 247 | + // If there is no main stylesheet for this block, register theme style. | |
| 248 | + wp_register_style( | |
| 249 | + "wp-block-{$block_name}", | |
| 250 | + gutenberg_url( $theme_style_path ), | |
| 251 | + array(), | |
| 252 | + $default_version | |
| 253 | + ); | |
| 254 | + wp_style_add_data( "wp-block-{$block_name}", 'path', gutenberg_dir_path() . $theme_style_path ); | |
| 255 | + } | |
| 170 | 256 | } |
| 171 | 257 | } |
| 172 | 258 | |
| 173 | - $editor_style_path = "build/styles/block-library/$block_name/style-editor{$suffix}.css"; | |
| 259 | + $editor_style_path = "build/block-library/blocks/$block_name/style-editor.css"; | |
| 174 | 260 | if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) { |
| 175 | 261 | wp_deregister_style( "wp-block-{$block_name}-editor" ); |
| 176 | 262 | wp_register_style( |
| 177 | 263 | "wp-block-{$block_name}-editor", |
| 178 | - $gutenberg_url_root . $editor_style_path, | |
| 264 | + gutenberg_url( $editor_style_path ), | |
| 179 | 265 | array(), |
| 180 | 266 | $default_version |
| 181 | 267 | ); |
| 182 | 268 | wp_style_add_data( "wp-block-{$block_name}-editor", 'rtl', 'replace' ); |
| 183 | - wp_style_add_data( "wp-block-{$block_name}-editor", 'suffix', $suffix ); | |
| 184 | 269 | } else { |
| 185 | 270 | wp_register_style( "wp-block-{$block_name}-editor", false ); |
| 186 | 271 | } |
| 187 | 272 | } |
| @@ -197,9 +282,9 @@ | ||
| 197 | 282 | * |
| 198 | 283 | * This shim is INTENTIONALLY left out of core, as Social Links have never |
| 199 | 284 | * landed there. |
| 200 | 285 | * |
| 201 | - * @link https://github.com/WordPress/gutenberg/pull/19887 | |
| 286 | + * @see https://github.com/WordPress/gutenberg/pull/19887 | |
| 202 | 287 | */ |
| 203 | 288 | function gutenberg_register_legacy_social_link_blocks() { |
| 204 | 289 | $services = array( |
| 205 | 290 | 'amazon', |
| @@ -266,123 +351,4 @@ | ||
| 266 | 351 | } |
| 267 | 352 | } |
| 268 | 353 | |
| 269 | 354 | add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' ); |
| 270 | - | |
| 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 | - } | |
| 286 | - | |
| 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; | |
| 293 | - } | |
| 294 | - | |
| 295 | - return $value; | |
| 296 | -} | |
| 297 | - | |
| 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 ''; | |
| 316 | - } | |
| 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 | -} | |
| 328 | - | |
| 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 | -} | |
| 337 | - | |
| 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' ); | |
| 345 | -} | |
| 346 | - | |
| 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 | -} | |
| 361 | - | |
| 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; | |
| 378 | - } | |
| 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; | |
| 384 | -} | |
| 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 ); | |