| @@ -5,162 +5,269 @@ | ||
| 5 | 5 | * @package gutenberg |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | - * Retrieves registered social link blocks | |
| 10 | - * | |
| 11 | - * @return array Array of strings containing the registered social link block names. | |
| 12 | - */ | |
| 13 | -function gutenberg_get_registered_social_link_blocks() { | |
| 14 | - $social_link_prefix = 'core/social-link'; | |
| 15 | - $social_link_prefix_length = strlen( $social_link_prefix ); | |
| 16 | - | |
| 17 | - $registry = WP_Block_Type_Registry::get_instance(); | |
| 18 | - $block_types = $registry->get_all_registered(); | |
| 19 | - | |
| 20 | - $registered_social_link_blocks = array(); | |
| 21 | - foreach ( $block_types as $block_type ) { | |
| 22 | - // Block type name starts with $social_link_prefix. | |
| 23 | - if ( strncmp( $block_type->name, $social_link_prefix, $social_link_prefix_length ) === 0 ) { | |
| 24 | - $registered_social_link_blocks[] = $block_type->name; | |
| 25 | - } | |
| 26 | - } | |
| 27 | - return $registered_social_link_blocks; | |
| 28 | -} | |
| 29 | - | |
| 30 | -/** | |
| 31 | 9 | * Substitutes the implementation of a core-registered block type, if exists, |
| 32 | 10 | * with the built result from the plugin. |
| 33 | 11 | */ |
| 34 | 12 | function gutenberg_reregister_core_block_types() { |
| 35 | 13 | // Blocks directory may not exist if working from a fresh clone. |
| 36 | - $blocks_dir = dirname( __FILE__ ) . '/../build/block-library/blocks/'; | |
| 37 | - if ( ! file_exists( $blocks_dir ) ) { | |
| 38 | - return; | |
| 39 | - } | |
| 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 | + 'file', | |
| 25 | + 'gallery', | |
| 26 | + 'group', | |
| 27 | + 'heading', | |
| 28 | + 'html', | |
| 29 | + 'image', | |
| 30 | + 'list', | |
| 31 | + 'media-text', | |
| 32 | + 'missing', | |
| 33 | + 'more', | |
| 34 | + 'navigation-link', | |
| 35 | + 'nextpage', | |
| 36 | + 'paragraph', | |
| 37 | + 'preformatted', | |
| 38 | + 'pullquote', | |
| 39 | + 'quote', | |
| 40 | + 'separator', | |
| 41 | + 'social-links', | |
| 42 | + 'spacer', | |
| 43 | + 'subhead', | |
| 44 | + 'table', | |
| 45 | + 'text-columns', | |
| 46 | + 'verse', | |
| 47 | + 'video', | |
| 48 | + 'embed', | |
| 49 | + ), | |
| 50 | + 'block_names' => array_merge( | |
| 51 | + array( | |
| 52 | + 'archives.php' => 'core/archives', | |
| 53 | + 'block.php' => 'core/block', | |
| 54 | + 'calendar.php' => 'core/calendar', | |
| 55 | + 'categories.php' => 'core/categories', | |
| 56 | + 'cover.php' => 'core/cover', | |
| 57 | + 'latest-comments.php' => 'core/latest-comments', | |
| 58 | + 'latest-posts.php' => 'core/latest-posts', | |
| 59 | + 'navigation.php' => 'core/navigation', | |
| 60 | + 'navigation-link.php' => 'core/navigation-link', | |
| 61 | + 'rss.php' => 'core/rss', | |
| 62 | + 'search.php' => 'core/search', | |
| 63 | + 'shortcode.php' => 'core/shortcode', | |
| 64 | + 'social-link.php' => 'core/social-link', | |
| 65 | + 'tag-cloud.php' => 'core/tag-cloud', | |
| 66 | + 'post-author.php' => 'core/post-author', | |
| 67 | + 'post-comment.php' => 'core/post-comment', | |
| 68 | + 'post-comment-author.php' => 'core/post-comment-author', | |
| 69 | + 'post-comment-content.php' => 'core/post-comment-content', | |
| 70 | + 'post-comment-date.php' => 'core/post-comment-date', | |
| 71 | + 'post-comments.php' => 'core/post-comments', | |
| 72 | + 'post-comments-count.php' => 'core/post-comments-count', | |
| 73 | + 'post-comments-form.php' => 'core/post-comments-form', | |
| 74 | + 'post-content.php' => 'core/post-content', | |
| 75 | + 'post-date.php' => 'core/post-date', | |
| 76 | + 'post-excerpt.php' => 'core/post-excerpt', | |
| 77 | + 'post-featured-image.php' => 'core/post-featured-image', | |
| 78 | + 'post-hierarchical-terms.php' => 'core/post-hierarchical-terms', | |
| 79 | + 'post-tags.php' => 'core/post-tags', | |
| 80 | + 'post-title.php' => 'core/post-title', | |
| 81 | + 'query.php' => 'core/query', | |
| 82 | + 'query-loop.php' => 'core/query-loop', | |
| 83 | + 'query-pagination.php' => 'core/query-pagination', | |
| 84 | + 'site-logo.php' => 'core/site-logo', | |
| 85 | + 'site-tagline.php' => 'core/site-tagline', | |
| 86 | + 'site-title.php' => 'core/site-title', | |
| 87 | + 'template-part.php' => 'core/template-part', | |
| 88 | + ) | |
| 89 | + ), | |
| 90 | + ), | |
| 91 | + __DIR__ . '/../build/edit-widgets/blocks/' => array( | |
| 92 | + 'block_folders' => array( | |
| 93 | + 'legacy-widget', | |
| 94 | + 'widget-area', | |
| 95 | + ), | |
| 96 | + 'block_names' => array( | |
| 97 | + 'legacy-widget.php' => 'core/legacy-widget', | |
| 98 | + 'widget-area.php' => 'core/widget-area', | |
| 99 | + ), | |
| 100 | + ), | |
| 101 | + ); | |
| 102 | + foreach ( $blocks_dirs as $blocks_dir => $details ) { | |
| 103 | + $block_folders = $details['block_folders']; | |
| 104 | + $block_names = $details['block_names']; | |
| 40 | 105 | |
| 41 | - $block_names = array( | |
| 42 | - 'archives.php' => 'core/archives', | |
| 43 | - 'block.php' => 'core/block', | |
| 44 | - 'calendar.php' => 'core/calendar', | |
| 45 | - 'categories.php' => 'core/categories', | |
| 46 | - 'latest-comments.php' => 'core/latest-comments', | |
| 47 | - 'latest-posts.php' => 'core/latest-posts', | |
| 48 | - 'legacy-widget.php' => 'core/legacy-widget', | |
| 49 | - 'navigation.php' => 'core/navigation', | |
| 50 | - 'rss.php' => 'core/rss', | |
| 51 | - 'shortcode.php' => 'core/shortcode', | |
| 52 | - 'search.php' => 'core/search', | |
| 53 | - 'social-link.php' => gutenberg_get_registered_social_link_blocks(), | |
| 54 | - 'tag-cloud.php' => 'core/tag-cloud', | |
| 55 | - 'site-title.php' => 'core/site-title', | |
| 56 | - 'template-part.php' => 'core/template-part', | |
| 57 | - 'post-title.php' => 'core/post-title', | |
| 58 | - 'post-content.php' => 'core/post-content', | |
| 59 | - 'post-author.php' => 'core/post-author', | |
| 60 | - 'post-date.php' => 'core/post-date', | |
| 61 | - 'post-excerpt.php' => 'core/post-excerpt', | |
| 62 | - ); | |
| 106 | + $registry = WP_Block_Type_Registry::get_instance(); | |
| 63 | 107 | |
| 64 | - $registry = WP_Block_Type_Registry::get_instance(); | |
| 108 | + foreach ( $block_folders as $folder_name ) { | |
| 109 | + $block_json_file = $blocks_dir . $folder_name . '/block.json'; | |
| 65 | 110 | |
| 66 | - foreach ( $block_names as $file => $block_names ) { | |
| 67 | - if ( ! file_exists( $blocks_dir . $file ) ) { | |
| 68 | - return; | |
| 111 | + // Ideally, all paths to block metadata files should be listed in | |
| 112 | + // WordPress core. In this place we should rather use filter | |
| 113 | + // to replace paths with overrides defined by the plugin. | |
| 114 | + $metadata = json_decode( file_get_contents( $block_json_file ), true ); | |
| 115 | + if ( ! is_array( $metadata ) || ! $metadata['name'] ) { | |
| 116 | + return false; | |
| 117 | + } | |
| 118 | + | |
| 119 | + if ( $registry->is_registered( $metadata['name'] ) ) { | |
| 120 | + $registry->unregister( $metadata['name'] ); | |
| 121 | + } | |
| 122 | + | |
| 123 | + gutenberg_register_core_block_styles( $folder_name ); | |
| 124 | + register_block_type_from_metadata( $block_json_file ); | |
| 69 | 125 | } |
| 70 | 126 | |
| 71 | - if ( is_string( $block_names ) ) { | |
| 72 | - if ( $registry->is_registered( $block_names ) ) { | |
| 73 | - $registry->unregister( $block_names ); | |
| 127 | + foreach ( $block_names as $file => $block_names ) { | |
| 128 | + if ( ! file_exists( $blocks_dir . $file ) ) { | |
| 129 | + return; | |
| 74 | 130 | } |
| 75 | - } elseif ( is_array( $block_names ) ) { | |
| 76 | - foreach ( $block_names as $block_name ) { | |
| 77 | - if ( $registry->is_registered( $block_name ) ) { | |
| 78 | - $registry->unregister( $block_name ); | |
| 131 | + | |
| 132 | + if ( is_string( $block_names ) ) { | |
| 133 | + if ( $registry->is_registered( $block_names ) ) { | |
| 134 | + $registry->unregister( $block_names ); | |
| 79 | 135 | } |
| 136 | + gutenberg_register_core_block_styles( $block_names ); | |
| 137 | + } elseif ( is_array( $block_names ) ) { | |
| 138 | + foreach ( $block_names as $block_name ) { | |
| 139 | + if ( $registry->is_registered( $block_name ) ) { | |
| 140 | + $registry->unregister( $block_name ); | |
| 141 | + } | |
| 142 | + gutenberg_register_core_block_styles( $block_name ); | |
| 143 | + } | |
| 80 | 144 | } |
| 145 | + | |
| 146 | + require $blocks_dir . $file; | |
| 81 | 147 | } |
| 82 | - | |
| 83 | - require $blocks_dir . $file; | |
| 84 | 148 | } |
| 85 | 149 | } |
| 150 | + | |
| 86 | 151 | add_action( 'init', 'gutenberg_reregister_core_block_types' ); |
| 87 | 152 | |
| 88 | -if ( ! function_exists( 'register_block_style' ) ) { | |
| 89 | - /** | |
| 90 | - * Registers a new block style. | |
| 91 | - * | |
| 92 | - * @param string $block_name Block type name including namespace. | |
| 93 | - * @param array $style_properties Array containing the properties of the style name, label, style (name of the stylesheet to be enqueued), inline_style (string containing the CSS to be added). | |
| 94 | - * | |
| 95 | - * @return boolean True if the block style was registered with success and false otherwise. | |
| 96 | - */ | |
| 97 | - function register_block_style( $block_name, $style_properties ) { | |
| 98 | - return WP_Block_Styles_Registry::get_instance()->register( $block_name, $style_properties ); | |
| 153 | +/** | |
| 154 | + * Registers block styles for a core block. | |
| 155 | + * | |
| 156 | + * @param string $block_name The block-name. | |
| 157 | + * | |
| 158 | + * @return void | |
| 159 | + */ | |
| 160 | +function gutenberg_register_core_block_styles( $block_name ) { | |
| 161 | + if ( ! gutenberg_should_load_separate_block_styles() ) { | |
| 162 | + return; | |
| 99 | 163 | } |
| 100 | -} | |
| 101 | 164 | |
| 102 | -if ( ! function_exists( 'unregister_block_style' ) ) { | |
| 103 | - /** | |
| 104 | - * Unregisters a block style. | |
| 105 | - * | |
| 106 | - * @param string $block_name Block type name including namespace. | |
| 107 | - * @param array $block_style_name Block style name. | |
| 108 | - * | |
| 109 | - * @return boolean True if the block style was unregistered with success and false otherwise. | |
| 110 | - */ | |
| 111 | - function unregister_block_style( $block_name, $block_style_name ) { | |
| 112 | - return WP_Block_Styles_Registry::get_instance()->unregister( $block_name, $block_style_name ); | |
| 165 | + $block_name = str_replace( 'core/', '', $block_name ); | |
| 166 | + | |
| 167 | + $style_path = is_rtl() | |
| 168 | + ? "build/block-library/blocks/$block_name/style-rtl.css" | |
| 169 | + : "build/block-library/blocks/$block_name/style.css"; | |
| 170 | + $editor_style_path = is_rtl() | |
| 171 | + ? "build/block-library/blocks/$block_name/style-editor-rtl.css" | |
| 172 | + : "build/block-library/blocks/$block_name/style-editor.css"; | |
| 173 | + | |
| 174 | + if ( file_exists( gutenberg_dir_path() . $style_path ) ) { | |
| 175 | + wp_register_style( | |
| 176 | + 'wp-block-' . $block_name, | |
| 177 | + gutenberg_url( $style_path ), | |
| 178 | + array(), | |
| 179 | + filemtime( gutenberg_dir_path() . $style_path ) | |
| 180 | + ); | |
| 113 | 181 | } |
| 114 | -} | |
| 115 | 182 | |
| 116 | -if ( ! has_action( 'enqueue_block_assets', 'enqueue_block_styles_assets' ) ) { | |
| 117 | - /** | |
| 118 | - * Function responsible for enqueuing the styles required for block styles functionality on the editor and on the frontend. | |
| 119 | - */ | |
| 120 | - function gutenberg_enqueue_block_styles_assets() { | |
| 121 | - $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered(); | |
| 122 | - | |
| 123 | - foreach ( $block_styles as $styles ) { | |
| 124 | - foreach ( $styles as $style_properties ) { | |
| 125 | - if ( isset( $style_properties['style_handle'] ) ) { | |
| 126 | - wp_enqueue_style( $style_properties['style_handle'] ); | |
| 127 | - } | |
| 128 | - if ( isset( $style_properties['inline_style'] ) ) { | |
| 129 | - wp_add_inline_style( 'wp-block-library', $style_properties['inline_style'] ); | |
| 130 | - } | |
| 131 | - } | |
| 132 | - } | |
| 183 | + if ( file_exists( gutenberg_dir_path() . $editor_style_path ) ) { | |
| 184 | + wp_register_style( | |
| 185 | + 'wp-block-' . $block_name . '-editor', | |
| 186 | + gutenberg_url( $editor_style_path ), | |
| 187 | + array(), | |
| 188 | + filemtime( gutenberg_dir_path() . $editor_style_path ) | |
| 189 | + ); | |
| 133 | 190 | } |
| 134 | - add_action( 'enqueue_block_assets', 'gutenberg_enqueue_block_styles_assets', 30 ); | |
| 135 | 191 | } |
| 136 | -if ( ! has_action( 'enqueue_block_editor_assets', 'enqueue_editor_block_styles_assets' ) ) { | |
| 137 | - /** | |
| 138 | - * Function responsible for enqueuing the assets required for block styles functionality on the editor. | |
| 139 | - */ | |
| 140 | - function gutenberg_enqueue_editor_block_styles_assets() { | |
| 141 | - $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered(); | |
| 142 | 192 | |
| 143 | - $register_script_lines = array( '( function() {' ); | |
| 144 | - foreach ( $block_styles as $block_name => $styles ) { | |
| 145 | - foreach ( $styles as $style_properties ) { | |
| 146 | - $register_script_lines[] = sprintf( | |
| 147 | - ' wp.blocks.registerBlockStyle( \'%s\', %s );', | |
| 148 | - $block_name, | |
| 149 | - wp_json_encode( | |
| 150 | - array( | |
| 151 | - 'name' => $style_properties['name'], | |
| 152 | - 'label' => $style_properties['label'], | |
| 153 | - ) | |
| 154 | - ) | |
| 155 | - ); | |
| 156 | - } | |
| 157 | - } | |
| 158 | - $register_script_lines[] = '} )();'; | |
| 159 | - $inline_script = implode( "\n", $register_script_lines ); | |
| 193 | +/** | |
| 194 | + * Complements the implementation of block type `core/social-icon`, whether it | |
| 195 | + * be provided by core or the plugin, with derived block types for each | |
| 196 | + * "service" (WordPress, Twitter, etc.) supported by Social Links. | |
| 197 | + * | |
| 198 | + * This ensures backwards compatibility for any users running the Gutenberg | |
| 199 | + * plugin who have used Social Links prior to their conversion to block | |
| 200 | + * variations. | |
| 201 | + * | |
| 202 | + * This shim is INTENTIONALLY left out of core, as Social Links haven't yet | |
| 203 | + * landed there. | |
| 204 | + * | |
| 205 | + * @see https://github.com/WordPress/gutenberg/pull/19887 | |
| 206 | + */ | |
| 207 | +function gutenberg_register_legacy_social_link_blocks() { | |
| 208 | + $services = array( | |
| 209 | + 'amazon', | |
| 210 | + 'bandcamp', | |
| 211 | + 'behance', | |
| 212 | + 'chain', | |
| 213 | + 'codepen', | |
| 214 | + 'deviantart', | |
| 215 | + 'dribbble', | |
| 216 | + 'dropbox', | |
| 217 | + 'etsy', | |
| 218 | + 'facebook', | |
| 219 | + 'feed', | |
| 220 | + 'fivehundredpx', | |
| 221 | + 'flickr', | |
| 222 | + 'foursquare', | |
| 223 | + 'goodreads', | |
| 224 | + 'google', | |
| 225 | + 'github', | |
| 226 | + 'instagram', | |
| 227 | + 'lastfm', | |
| 228 | + 'linkedin', | |
| 229 | + 'mail', | |
| 230 | + 'mastodon', | |
| 231 | + 'meetup', | |
| 232 | + 'medium', | |
| 233 | + 'pinterest', | |
| 234 | + 'pocket', | |
| 235 | + 'reddit', | |
| 236 | + 'skype', | |
| 237 | + 'snapchat', | |
| 238 | + 'soundcloud', | |
| 239 | + 'spotify', | |
| 240 | + 'tumblr', | |
| 241 | + 'twitch', | |
| 242 | + 'twitter', | |
| 243 | + 'vimeo', | |
| 244 | + 'vk', | |
| 245 | + 'wordpress', | |
| 246 | + 'yelp', | |
| 247 | + 'youtube', | |
| 248 | + ); | |
| 160 | 249 | |
| 161 | - wp_register_script( 'wp-block-styles', false, array( 'wp-blocks' ), true, true ); | |
| 162 | - wp_add_inline_script( 'wp-block-styles', $inline_script ); | |
| 163 | - wp_enqueue_script( 'wp-block-styles' ); | |
| 250 | + foreach ( $services as $service ) { | |
| 251 | + register_block_type( | |
| 252 | + 'core/social-link-' . $service, | |
| 253 | + array( | |
| 254 | + 'category' => 'widgets', | |
| 255 | + 'attributes' => array( | |
| 256 | + 'url' => array( | |
| 257 | + 'type' => 'string', | |
| 258 | + ), | |
| 259 | + 'service' => array( | |
| 260 | + 'type' => 'string', | |
| 261 | + 'default' => $service, | |
| 262 | + ), | |
| 263 | + 'label' => array( | |
| 264 | + 'type' => 'string', | |
| 265 | + ), | |
| 266 | + ), | |
| 267 | + 'render_callback' => 'gutenberg_render_block_core_social_link', | |
| 268 | + ) | |
| 269 | + ); | |
| 164 | 270 | } |
| 165 | - add_action( 'enqueue_block_editor_assets', 'gutenberg_enqueue_editor_block_styles_assets' ); | |
| 166 | 271 | } |
| 272 | + | |
| 273 | +add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' ); | |