| 1 |
<?php |
| 2 |
/** |
| 3 |
* Block patterns registration. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Register Gutenberg bundled patterns. |
| 10 |
*/ |
| 11 |
function gutenberg_register_gutenberg_patterns() { |
| 12 |
// Register categories used for block patterns. |
| 13 |
if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( 'query' ) ) { |
| 14 |
register_block_pattern_category( 'query', array( 'label' => __( 'Query', 'gutenberg' ) ) ); |
| 15 |
} |
| 16 |
|
| 17 |
$patterns = array( |
| 18 |
'query-standard-posts' => array( |
| 19 |
'title' => _x( 'Standard', 'Block pattern title', 'gutenberg' ), |
| 20 |
'blockTypes' => array( 'core/query' ), |
| 21 |
'categories' => array( 'query' ), |
| 22 |
'content' => '<!-- wp:query {"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} --> |
| 23 |
<div class="wp-block-query"> |
| 24 |
<!-- wp:post-template --> |
| 25 |
<!-- wp:post-title {"isLink":true} /--> |
| 26 |
<!-- wp:post-featured-image {"isLink":true,"align":"wide"} /--> |
| 27 |
<!-- wp:post-excerpt /--> |
| 28 |
<!-- wp:separator --> |
| 29 |
<hr class="wp-block-separator"/> |
| 30 |
<!-- /wp:separator --> |
| 31 |
<!-- wp:post-date /--> |
| 32 |
<!-- /wp:post-template --> |
| 33 |
</div> |
| 34 |
<!-- /wp:query -->', |
| 35 |
), |
| 36 |
'query-medium-posts' => array( |
| 37 |
'title' => _x( 'Image at left', 'Block pattern title', 'gutenberg' ), |
| 38 |
'blockTypes' => array( 'core/query' ), |
| 39 |
'categories' => array( 'query' ), |
| 40 |
'content' => '<!-- wp:query {"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} --> |
| 41 |
<div class="wp-block-query"> |
| 42 |
<!-- wp:post-template --> |
| 43 |
<!-- wp:columns {"align":"wide"} --> |
| 44 |
<div class="wp-block-columns alignwide"><!-- wp:column {"width":"66.66%"} --> |
| 45 |
<div class="wp-block-column" style="flex-basis:66.66%"><!-- wp:post-featured-image {"isLink":true} /--></div> |
| 46 |
<!-- /wp:column --> |
| 47 |
<!-- wp:column {"width":"33.33%"} --> |
| 48 |
<div class="wp-block-column" style="flex-basis:33.33%"><!-- wp:post-title {"isLink":true} /--> |
| 49 |
<!-- wp:post-excerpt /--></div> |
| 50 |
<!-- /wp:column --></div> |
| 51 |
<!-- /wp:columns --> |
| 52 |
<!-- /wp:post-template --> |
| 53 |
</div> |
| 54 |
<!-- /wp:query -->', |
| 55 |
), |
| 56 |
'query-small-posts' => array( |
| 57 |
'title' => _x( 'Small image and title', 'Block pattern title', 'gutenberg' ), |
| 58 |
'blockTypes' => array( 'core/query' ), |
| 59 |
'categories' => array( 'query' ), |
| 60 |
'content' => '<!-- wp:query {"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} --> |
| 61 |
<div class="wp-block-query"> |
| 62 |
<!-- wp:post-template --> |
| 63 |
<!-- wp:columns {"verticalAlignment":"center"} --> |
| 64 |
<div class="wp-block-columns are-vertically-aligned-center"><!-- wp:column {"verticalAlignment":"center","width":"25%"} --> |
| 65 |
<div class="wp-block-column is-vertically-aligned-center" style="flex-basis:25%"><!-- wp:post-featured-image {"isLink":true} /--></div> |
| 66 |
<!-- /wp:column --> |
| 67 |
<!-- wp:column {"verticalAlignment":"center","width":"75%"} --> |
| 68 |
<div class="wp-block-column is-vertically-aligned-center" style="flex-basis:75%"><!-- wp:post-title {"isLink":true} /--></div> |
| 69 |
<!-- /wp:column --></div> |
| 70 |
<!-- /wp:columns --> |
| 71 |
<!-- /wp:post-template --> |
| 72 |
</div> |
| 73 |
<!-- /wp:query -->', |
| 74 |
), |
| 75 |
'query-grid-posts' => array( |
| 76 |
'title' => _x( 'Grid', 'Block pattern title', 'gutenberg' ), |
| 77 |
'blockTypes' => array( 'core/query' ), |
| 78 |
'categories' => array( 'query' ), |
| 79 |
'content' => '<!-- wp:query {"query":{"perPage":6,"pages":0,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"exclude","inherit":false},"displayLayout":{"type":"flex","columns":3}} --> |
| 80 |
<div class="wp-block-query"> |
| 81 |
<!-- wp:post-template --> |
| 82 |
<!-- wp:group {"style":{"spacing":{"padding":{"top":"30px","right":"30px","bottom":"30px","left":"30px"}}},"layout":{"inherit":false}} --> |
| 83 |
<div class="wp-block-group" style="padding-top:30px;padding-right:30px;padding-bottom:30px;padding-left:30px"><!-- wp:post-title {"isLink":true} /--> |
| 84 |
<!-- wp:post-excerpt /--> |
| 85 |
<!-- wp:post-date /--></div> |
| 86 |
<!-- /wp:group --> |
| 87 |
<!-- /wp:post-template --> |
| 88 |
</div> |
| 89 |
<!-- /wp:query -->', |
| 90 |
), |
| 91 |
'query-large-title-posts' => array( |
| 92 |
'title' => _x( 'Large title', 'Block pattern title', 'gutenberg' ), |
| 93 |
'blockTypes' => array( 'core/query' ), |
| 94 |
'categories' => array( 'query' ), |
| 95 |
'content' => '<!-- wp:group {"align":"full","style":{"spacing":{"padding":{"top":"100px","right":"100px","bottom":"100px","left":"100px"}},"color":{"text":"#ffffff","background":"#000000"}}} --> |
| 96 |
<div class="wp-block-group alignfull has-text-color has-background" style="background-color:#000000;color:#ffffff;padding-top:100px;padding-right:100px;padding-bottom:100px;padding-left:100px"><!-- wp:query {"query":{"perPage":3,"pages":0,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} --> |
| 97 |
<div class="wp-block-query"><!-- wp:post-template --> |
| 98 |
<!-- wp:separator {"customColor":"#ffffff","align":"wide","className":"is-style-wide"} --> |
| 99 |
<hr class="wp-block-separator alignwide has-text-color has-background is-style-wide" style="background-color:#ffffff;color:#ffffff"/> |
| 100 |
<!-- /wp:separator --> |
| 101 |
|
| 102 |
<!-- wp:columns {"verticalAlignment":"center","align":"wide"} --> |
| 103 |
<div class="wp-block-columns alignwide are-vertically-aligned-center"><!-- wp:column {"verticalAlignment":"center","width":"20%"} --> |
| 104 |
<div class="wp-block-column is-vertically-aligned-center" style="flex-basis:20%"><!-- wp:post-date {"style":{"color":{"text":"#ffffff"}},"fontSize":"extra-small"} /--></div> |
| 105 |
<!-- /wp:column --> |
| 106 |
|
| 107 |
<!-- wp:column {"verticalAlignment":"center","width":"80%"} --> |
| 108 |
<div class="wp-block-column is-vertically-aligned-center" style="flex-basis:80%"><!-- wp:post-title {"isLink":true,"style":{"typography":{"fontSize":"72px","lineHeight":"1.1"},"color":{"text":"#ffffff","link":"#ffffff"}}} /--></div> |
| 109 |
<!-- /wp:column --></div> |
| 110 |
<!-- /wp:columns --> |
| 111 |
<!-- /wp:post-template --></div> |
| 112 |
<!-- /wp:query --></div> |
| 113 |
<!-- /wp:group -->', |
| 114 |
), |
| 115 |
'query-offset-posts' => array( |
| 116 |
'title' => _x( 'Offset', 'Block pattern title', 'gutenberg' ), |
| 117 |
'blockTypes' => array( 'core/query' ), |
| 118 |
'categories' => array( 'query' ), |
| 119 |
'content' => '<!-- wp:group {"style":{"spacing":{"padding":{"top":"30px","right":"30px","bottom":"30px","left":"30px"}}},"layout":{"inherit":false}} --> |
| 120 |
<div class="wp-block-group" style="padding-top:30px;padding-right:30px;padding-bottom:30px;padding-left:30px"><!-- wp:columns --> |
| 121 |
<div class="wp-block-columns"><!-- wp:column {"width":"50%"} --> |
| 122 |
<div class="wp-block-column" style="flex-basis:50%"><!-- wp:query {"query":{"perPage":2,"pages":0,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"exclude","inherit":false},"displayLayout":{"type":"list"}} --> |
| 123 |
<div class="wp-block-query"><!-- wp:post-template --> |
| 124 |
<!-- wp:post-featured-image /--> |
| 125 |
<!-- wp:post-title /--> |
| 126 |
<!-- wp:post-date /--> |
| 127 |
<!-- wp:spacer {"height":200} --> |
| 128 |
<div style="height:200px" aria-hidden="true" class="wp-block-spacer"></div> |
| 129 |
<!-- /wp:spacer --> |
| 130 |
<!-- /wp:post-template --></div> |
| 131 |
<!-- /wp:query --></div> |
| 132 |
<!-- /wp:column --> |
| 133 |
<!-- wp:column {"width":"50%"} --> |
| 134 |
<div class="wp-block-column" style="flex-basis:50%"><!-- wp:query {"query":{"perPage":2,"pages":0,"offset":2,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"exclude","inherit":false},"displayLayout":{"type":"list"}} --> |
| 135 |
<div class="wp-block-query"><!-- wp:post-template --> |
| 136 |
<!-- wp:spacer {"height":200} --> |
| 137 |
<div style="height:200px" aria-hidden="true" class="wp-block-spacer"></div> |
| 138 |
<!-- /wp:spacer --> |
| 139 |
<!-- wp:post-featured-image /--> |
| 140 |
<!-- wp:post-title /--> |
| 141 |
<!-- wp:post-date /--> |
| 142 |
<!-- /wp:post-template --></div> |
| 143 |
<!-- /wp:query --></div> |
| 144 |
<!-- /wp:column --></div> |
| 145 |
<!-- /wp:columns --></div> |
| 146 |
<!-- /wp:group -->', |
| 147 |
), |
| 148 |
// Initial block pattern to be used with block transformations with patterns. |
| 149 |
'social-links-shared-background-color' => array( |
| 150 |
'title' => _x( 'Social links with a shared background color', 'Block pattern title', 'gutenberg' ), |
| 151 |
'categories' => array( 'buttons' ), |
| 152 |
'blockTypes' => array( 'core/social-links' ), |
| 153 |
'viewportWidth' => 500, |
| 154 |
'content' => '<!-- wp:social-links {"customIconColor":"#ffffff","iconColorValue":"#ffffff","customIconBackgroundColor":"#3962e3","iconBackgroundColorValue":"#3962e3","className":"has-icon-color"} --> |
| 155 |
<ul class="wp-block-social-links has-icon-color has-icon-background-color"><!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /--> |
| 156 |
<!-- wp:social-link {"url":"#","service":"chain"} /--> |
| 157 |
<!-- wp:social-link {"url":"#","service":"mail"} /--></ul> |
| 158 |
<!-- /wp:social-links -->', |
| 159 |
), |
| 160 |
); |
| 161 |
|
| 162 |
foreach ( $patterns as $name => $pattern ) { |
| 163 |
$pattern_name = 'core/' . $name; |
| 164 |
if ( ! WP_Block_Patterns_Registry::get_instance()->is_registered( $pattern_name ) ) { |
| 165 |
register_block_pattern( $pattern_name, $pattern ); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Deactivate the legacy patterns bundled with WordPress. |
| 172 |
*/ |
| 173 |
function gutenberg_remove_core_patterns() { |
| 174 |
$core_block_patterns = array( |
| 175 |
'text-two-columns', |
| 176 |
'two-buttons', |
| 177 |
'two-images', |
| 178 |
'text-two-columns-with-images', |
| 179 |
'text-three-columns-buttons', |
| 180 |
'large-header', |
| 181 |
'large-header-button', |
| 182 |
'three-buttons', |
| 183 |
'heading-paragraph', |
| 184 |
'quote', |
| 185 |
'query-standard-posts', |
| 186 |
'query-medium-posts', |
| 187 |
'query-small-posts', |
| 188 |
'query-grid-posts', |
| 189 |
'query-large-title-posts', |
| 190 |
'query-offset-posts', |
| 191 |
'social-links-shared-background-color', |
| 192 |
); |
| 193 |
|
| 194 |
foreach ( $core_block_patterns as $core_block_pattern ) { |
| 195 |
$name = 'core/' . $core_block_pattern; |
| 196 |
if ( WP_Block_Patterns_Registry::get_instance()->is_registered( $name ) ) { |
| 197 |
unregister_block_pattern( $name ); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Register Core's official patterns from wordpress.org/patterns. |
| 204 |
* |
| 205 |
* @since 5.8.0 |
| 206 |
*/ |
| 207 |
function gutenberg_load_remote_core_patterns() { |
| 208 |
// This is the core function that provides the same feature. |
| 209 |
if ( function_exists( '_load_remote_block_patterns' ) ) { |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Filter to disable remote block patterns. |
| 215 |
* |
| 216 |
* @since 5.8.0 |
| 217 |
* |
| 218 |
* @param bool $should_load_remote |
| 219 |
*/ |
| 220 |
$should_load_remote = apply_filters( 'should_load_remote_block_patterns', true ); |
| 221 |
|
| 222 |
if ( $should_load_remote ) { |
| 223 |
$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' ); |
| 224 |
$core_keyword_id = 11; // 11 is the ID for "core". |
| 225 |
$request->set_param( 'keyword', $core_keyword_id ); |
| 226 |
$response = rest_do_request( $request ); |
| 227 |
if ( $response->is_error() ) { |
| 228 |
return; |
| 229 |
} |
| 230 |
$patterns = $response->get_data(); |
| 231 |
|
| 232 |
foreach ( $patterns as $settings ) { |
| 233 |
$pattern_name = 'core/' . sanitize_title( $settings['title'] ); |
| 234 |
register_block_pattern( $pattern_name, (array) $settings ); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Register `Featured` (category) patterns from wordpress.org/patterns. |
| 241 |
* |
| 242 |
* @since 5.9.0 |
| 243 |
*/ |
| 244 |
function gutenberg_load_remote_featured_patterns() { |
| 245 |
/** |
| 246 |
* Filter to disable remote block patterns. |
| 247 |
* |
| 248 |
* @since 5.8.0 |
| 249 |
* |
| 250 |
* @param bool $should_load_remote |
| 251 |
*/ |
| 252 |
$should_load_remote = apply_filters( 'should_load_remote_block_patterns', true ); |
| 253 |
|
| 254 |
if ( ! $should_load_remote ) { |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( 'featured' ) ) { |
| 259 |
register_block_pattern_category( 'featured', array( 'label' => __( 'Featured', 'gutenberg' ) ) ); |
| 260 |
} |
| 261 |
$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' ); |
| 262 |
$request['category'] = 26; // This is the `Featured` category id from pattern directory. |
| 263 |
$response = rest_do_request( $request ); |
| 264 |
if ( $response->is_error() ) { |
| 265 |
return; |
| 266 |
} |
| 267 |
$patterns = $response->get_data(); |
| 268 |
foreach ( $patterns as $pattern ) { |
| 269 |
$pattern_name = sanitize_title( $pattern['title'] ); |
| 270 |
$registry = WP_Block_Patterns_Registry::get_instance(); |
| 271 |
// Some patterns might be already registerd as `core patterns with the `core` prefix. |
| 272 |
$is_registered = $registry->is_registered( $pattern_name ) || $registry->is_registered( "core/$pattern_name" ); |
| 273 |
if ( ! $is_registered ) { |
| 274 |
register_block_pattern( $pattern_name, (array) $pattern ); |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
add_action( |
| 281 |
'init', |
| 282 |
function() { |
| 283 |
if ( ! get_theme_support( 'core-block-patterns' ) || ! function_exists( 'unregister_block_pattern' ) ) { |
| 284 |
return; |
| 285 |
} |
| 286 |
gutenberg_remove_core_patterns(); |
| 287 |
gutenberg_register_gutenberg_patterns(); |
| 288 |
} |
| 289 |
); |
| 290 |
|
| 291 |
add_action( |
| 292 |
'current_screen', |
| 293 |
function( $current_screen ) { |
| 294 |
if ( ! get_theme_support( 'core-block-patterns' ) ) { |
| 295 |
return; |
| 296 |
} |
| 297 |
|
| 298 |
$is_site_editor = ( function_exists( 'gutenberg_is_edit_site_page' ) && gutenberg_is_edit_site_page( $current_screen->id ) ); |
| 299 |
if ( $current_screen->is_block_editor || $is_site_editor ) { |
| 300 |
gutenberg_load_remote_core_patterns(); |
| 301 |
gutenberg_load_remote_featured_patterns(); |
| 302 |
} |
| 303 |
} |
| 304 |
); |
| 305 |
|