| 1 |
<?php |
| 2 |
/** |
| 3 |
* Block and style registration functions. |
| 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 |
'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']; |
| 105 |
|
| 106 |
$registry = WP_Block_Type_Registry::get_instance(); |
| 107 |
|
| 108 |
foreach ( $block_folders as $folder_name ) { |
| 109 |
$block_json_file = $blocks_dir . $folder_name . '/block.json'; |
| 110 |
|
| 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 ); |
| 125 |
} |
| 126 |
|
| 127 |
foreach ( $block_names as $file => $block_names ) { |
| 128 |
if ( ! file_exists( $blocks_dir . $file ) ) { |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
if ( is_string( $block_names ) ) { |
| 133 |
if ( $registry->is_registered( $block_names ) ) { |
| 134 |
$registry->unregister( $block_names ); |
| 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 |
} |
| 144 |
} |
| 145 |
|
| 146 |
require $blocks_dir . $file; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
add_action( 'init', 'gutenberg_reregister_core_block_types' ); |
| 152 |
|
| 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; |
| 163 |
} |
| 164 |
|
| 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 |
); |
| 181 |
} |
| 182 |
|
| 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 |
); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 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 |
); |
| 249 |
|
| 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 |
); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
add_action( 'init', 'gutenberg_register_legacy_social_link_blocks' ); |
| 274 |
|