block-supports
2 years ago
compat
2 years ago
experimental
2 years ago
README.md
2 years ago
block-editor-settings.php
2 years ago
blocks.php
2 years ago
class-wp-duotone-gutenberg.php
2 years ago
class-wp-theme-json-data-gutenberg.php
2 years ago
class-wp-theme-json-gutenberg.php
2 years ago
class-wp-theme-json-resolver-gutenberg.php
2 years ago
client-assets.php
2 years ago
demo.php
2 years ago
experiments-page.php
2 years ago
global-styles-and-settings.php
2 years ago
init.php
2 years ago
load.php
2 years ago
script-loader.php
2 years ago
theme-i18n.json
2 years ago
theme.json
2 years ago
upgrade.php
2 years ago
blocks.php
372 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Block functions specific for the Gutenberg editor plugin. |
| 4 | * |
| 5 | * @package gutenberg |
| 6 | */ |
| 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 directory may not exist if working from a fresh clone. |
| 14 | $blocks_dirs = array( |
| 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 | 'details', |
| 26 | 'group', |
| 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 | 'heading.php' => 'core/heading', |
| 73 | 'latest-comments.php' => 'core/latest-comments', |
| 74 | 'latest-posts.php' => 'core/latest-posts', |
| 75 | 'loginout.php' => 'core/loginout', |
| 76 | 'navigation.php' => 'core/navigation', |
| 77 | 'navigation-link.php' => 'core/navigation-link', |
| 78 | 'navigation-submenu.php' => 'core/navigation-submenu', |
| 79 | 'page-list.php' => 'core/page-list', |
| 80 | 'pattern.php' => 'core/pattern', |
| 81 | 'post-author.php' => 'core/post-author', |
| 82 | 'post-author-name.php' => 'core/post-author-name', |
| 83 | 'post-author-biography.php' => 'core/post-author-biography', |
| 84 | 'post-comment.php' => 'core/post-comment', |
| 85 | 'post-comments-count.php' => 'core/post-comments-count', |
| 86 | 'post-comments-form.php' => 'core/post-comments-form', |
| 87 | 'post-comments-link.php' => 'core/post-comments-link', |
| 88 | 'post-content.php' => 'core/post-content', |
| 89 | 'post-date.php' => 'core/post-date', |
| 90 | 'post-excerpt.php' => 'core/post-excerpt', |
| 91 | 'post-featured-image.php' => 'core/post-featured-image', |
| 92 | 'post-navigation-link.php' => 'core/post-navigation-link', |
| 93 | 'post-terms.php' => 'core/post-terms', |
| 94 | 'post-time-to-read.php' => 'core/post-time-to-read', |
| 95 | 'post-title.php' => 'core/post-title', |
| 96 | 'query.php' => 'core/query', |
| 97 | 'post-template.php' => 'core/post-template', |
| 98 | 'query-no-results.php' => 'core/query-no-results', |
| 99 | 'query-pagination.php' => 'core/query-pagination', |
| 100 | 'query-pagination-next.php' => 'core/query-pagination-next', |
| 101 | 'query-pagination-numbers.php' => 'core/query-pagination-numbers', |
| 102 | 'query-pagination-previous.php' => 'core/query-pagination-previous', |
| 103 | 'query-title.php' => 'core/query-title', |
| 104 | 'read-more.php' => 'core/read-more', |
| 105 | 'rss.php' => 'core/rss', |
| 106 | 'search.php' => 'core/search', |
| 107 | 'shortcode.php' => 'core/shortcode', |
| 108 | 'social-link.php' => 'core/social-link', |
| 109 | 'site-logo.php' => 'core/site-logo', |
| 110 | 'site-tagline.php' => 'core/site-tagline', |
| 111 | 'site-title.php' => 'core/site-title', |
| 112 | 'tag-cloud.php' => 'core/tag-cloud', |
| 113 | 'template-part.php' => 'core/template-part', |
| 114 | 'term-description.php' => 'core/term-description', |
| 115 | ), |
| 116 | ), |
| 117 | __DIR__ . '/../build/edit-widgets/blocks/' => array( |
| 118 | 'block_folders' => array( |
| 119 | 'widget-area', |
| 120 | ), |
| 121 | 'block_names' => array(), |
| 122 | ), |
| 123 | __DIR__ . '/../build/widgets/blocks/' => array( |
| 124 | 'block_folders' => array( |
| 125 | 'legacy-widget', |
| 126 | 'widget-group', |
| 127 | ), |
| 128 | 'block_names' => array( |
| 129 | 'legacy-widget.php' => 'core/legacy-widget', |
| 130 | 'widget-group.php' => 'core/widget-group', |
| 131 | ), |
| 132 | ), |
| 133 | ); |
| 134 | foreach ( $blocks_dirs as $blocks_dir => $details ) { |
| 135 | $block_folders = $details['block_folders']; |
| 136 | $block_names = $details['block_names']; |
| 137 | |
| 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'] ) { |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | gutenberg_deregister_core_block_and_assets( $metadata['name'] ); |
| 150 | gutenberg_register_core_block_assets( $folder_name ); |
| 151 | register_block_type_from_metadata( $block_json_file ); |
| 152 | } |
| 153 | |
| 154 | foreach ( $block_names as $file => $sub_block_names ) { |
| 155 | if ( ! file_exists( $blocks_dir . $file ) ) { |
| 156 | continue; |
| 157 | } |
| 158 | |
| 159 | $sub_block_names_normalized = is_string( $sub_block_names ) ? array( $sub_block_names ) : $sub_block_names; |
| 160 | foreach ( $sub_block_names_normalized as $block_name ) { |
| 161 | gutenberg_deregister_core_block_and_assets( $block_name ); |
| 162 | gutenberg_register_core_block_assets( $block_name ); |
| 163 | } |
| 164 | |
| 165 | require_once $blocks_dir . $file; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | add_action( 'init', 'gutenberg_reregister_core_block_types' ); |
| 171 | |
| 172 | /** |
| 173 | * Deregisters the existing core block type and its assets. |
| 174 | * |
| 175 | * @param string $block_name The name of the block. |
| 176 | * |
| 177 | * @return void |
| 178 | */ |
| 179 | function gutenberg_deregister_core_block_and_assets( $block_name ) { |
| 180 | $registry = WP_Block_Type_Registry::get_instance(); |
| 181 | if ( $registry->is_registered( $block_name ) ) { |
| 182 | $block_type = $registry->get_registered( $block_name ); |
| 183 | if ( ! empty( $block_type->view_script_handles ) ) { |
| 184 | foreach ( $block_type->view_script_handles as $view_script_handle ) { |
| 185 | if ( str_starts_with( $view_script_handle, 'wp-block-' ) ) { |
| 186 | wp_deregister_script( $view_script_handle ); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | $registry->unregister( $block_name ); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Registers block styles for a core block. |
| 196 | * |
| 197 | * @param string $block_name The block-name. |
| 198 | * |
| 199 | * @return void |
| 200 | */ |
| 201 | function gutenberg_register_core_block_assets( $block_name ) { |
| 202 | if ( ! wp_should_load_separate_core_block_assets() ) { |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | $block_name = str_replace( 'core/', '', $block_name ); |
| 207 | |
| 208 | // When in production, use the plugin's version as the default asset version; |
| 209 | // else (for development or test) default to use the current time. |
| 210 | $default_version = defined( 'GUTENBERG_VERSION' ) && ! SCRIPT_DEBUG ? GUTENBERG_VERSION : time(); |
| 211 | |
| 212 | $style_path = "build/block-library/blocks/$block_name/"; |
| 213 | $stylesheet_url = gutenberg_url( $style_path . 'style.css' ); |
| 214 | $stylesheet_path = gutenberg_dir_path() . $style_path . ( is_rtl() ? 'style-rtl.css' : 'style.css' ); |
| 215 | |
| 216 | if ( file_exists( $stylesheet_path ) ) { |
| 217 | wp_deregister_style( "wp-block-{$block_name}" ); |
| 218 | wp_register_style( |
| 219 | "wp-block-{$block_name}", |
| 220 | $stylesheet_url, |
| 221 | array(), |
| 222 | $default_version |
| 223 | ); |
| 224 | wp_style_add_data( "wp-block-{$block_name}", 'rtl', 'replace' ); |
| 225 | |
| 226 | // Add a reference to the stylesheet's path to allow calculations for inlining styles in `wp_head`. |
| 227 | wp_style_add_data( "wp-block-{$block_name}", 'path', $stylesheet_path ); |
| 228 | } else { |
| 229 | wp_register_style( "wp-block-{$block_name}", false, array() ); |
| 230 | } |
| 231 | |
| 232 | // If the current theme supports wp-block-styles, dequeue the full stylesheet |
| 233 | // and instead attach each block's theme-styles to their block styles stylesheet. |
| 234 | if ( current_theme_supports( 'wp-block-styles' ) ) { |
| 235 | |
| 236 | // Dequeue the full stylesheet. |
| 237 | // Make sure this only runs once, it doesn't need to run for every block. |
| 238 | static $stylesheet_removed; |
| 239 | if ( ! $stylesheet_removed ) { |
| 240 | add_action( |
| 241 | 'wp_enqueue_scripts', |
| 242 | function() { |
| 243 | wp_dequeue_style( 'wp-block-library-theme' ); |
| 244 | } |
| 245 | ); |
| 246 | $stylesheet_removed = true; |
| 247 | } |
| 248 | |
| 249 | // Get the path to the block's stylesheet. |
| 250 | $theme_style_path = is_rtl() |
| 251 | ? "build/block-library/blocks/$block_name/theme-rtl.css" |
| 252 | : "build/block-library/blocks/$block_name/theme.css"; |
| 253 | |
| 254 | // If the file exists, enqueue it. |
| 255 | if ( file_exists( gutenberg_dir_path() . $theme_style_path ) ) { |
| 256 | |
| 257 | if ( file_exists( $stylesheet_path ) ) { |
| 258 | // If there is a main stylesheet for this block, append the theme styles to main styles. |
| 259 | wp_add_inline_style( |
| 260 | "wp-block-{$block_name}", |
| 261 | file_get_contents( gutenberg_dir_path() . $theme_style_path ) |
| 262 | ); |
| 263 | } else { |
| 264 | // If there is no main stylesheet for this block, register theme style. |
| 265 | wp_register_style( |
| 266 | "wp-block-{$block_name}", |
| 267 | gutenberg_url( $theme_style_path ), |
| 268 | array(), |
| 269 | $default_version |
| 270 | ); |
| 271 | wp_style_add_data( "wp-block-{$block_name}", 'path', gutenberg_dir_path() . $theme_style_path ); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | $editor_style_path = "build/block-library/blocks/$block_name/style-editor.css"; |
| 277 | if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) { |
| 278 | wp_deregister_style( "wp-block-{$block_name}-editor" ); |
| 279 | wp_register_style( |
| 280 | "wp-block-{$block_name}-editor", |
| 281 | gutenberg_url( $editor_style_path ), |
| 282 | array(), |
| 283 | $default_version |
| 284 | ); |
| 285 | wp_style_add_data( "wp-block-{$block_name}-editor", 'rtl', 'replace' ); |
| 286 | } else { |
| 287 | wp_register_style( "wp-block-{$block_name}-editor", false ); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Complements the implementation of block type `core/social-icon`, whether it |
| 293 | * be provided by core or the plugin, with derived block types for each |
| 294 | * "service" (WordPress, Twitter, etc.) supported by Social Links. |
| 295 | * |
| 296 | * This ensures backwards compatibility for any users running the Gutenberg |
| 297 | * plugin who have used Social Links prior to their conversion to block |
| 298 | * variations. |
| 299 | * |
| 300 | * This shim is INTENTIONALLY left out of core, as Social Links have never |
| 301 | * landed there. |
| 302 | * |
| 303 | * @see https://github.com/WordPress/gutenberg/pull/19887 |
| 304 | */ |
| 305 | function gutenberg_register_legacy_social_link_blocks() { |
| 306 | $services = array( |
| 307 | 'amazon', |
| 308 | 'bandcamp', |
| 309 | 'behance', |
| 310 | 'chain', |
| 311 | 'codepen', |
| 312 | 'deviantart', |
| 313 | 'dribbble', |
| 314 | 'dropbox', |
| 315 | 'etsy', |
| 316 | 'facebook', |
| 317 | 'feed', |
| 318 | 'fivehundredpx', |
| 319 | 'flickr', |
| 320 | 'foursquare', |
| 321 | 'goodreads', |
| 322 | 'google', |
| 323 | 'github', |
| 324 | 'instagram', |
| 325 | 'lastfm', |
| 326 | 'linkedin', |
| 327 | 'mail', |
| 328 | 'mastodon', |
| 329 | 'meetup', |
| 330 | 'medium', |
| 331 | 'pinterest', |
| 332 | 'pocket', |
| 333 | 'reddit', |
| 334 | 'skype', |
| 335 | 'snapchat', |
| 336 | 'soundcloud', |
| 337 | 'spotify', |
| 338 | 'tumblr', |
| 339 | 'twitch', |
| 340 | 'twitter', |
| 341 | 'vimeo', |
| 342 | 'vk', |
| 343 | 'wordpress', |
| 344 | 'yelp', |
| 345 | 'youtube', |
| 346 | ); |
| 347 | |
| 348 | foreach ( $services as $service ) { |
| 349 | register_block_type( |
| 350 | 'core/social-link-' . $service, |
| 351 | array( |
| 352 | 'category' => 'widgets', |
| 353 | 'attributes' => array( |
| 354 | 'url' => array( |
| 355 | 'type' => 'string', |
| 356 | ), |
| 357 | 'service' => array( |
| 358 | 'type' => 'string', |
| 359 | 'default' => $service, |
| 360 | ), |
| 361 | 'label' => array( |
| 362 | 'type' => 'string', |
| 363 | ), |
| 364 | ), |
| 365 | 'render_callback' => 'gutenberg_render_block_core_social_link', |
| 366 | ) |
| 367 | ); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' ); |
| 372 |