Edit File by line
/home/zeestwma/richards.../wp-inclu...
File: block-patterns.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Register the block patterns and block patterns categories
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @since 5.5.0
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
add_theme_support( 'core-block-patterns' );
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Registers the core block patterns and categories.
[11] Fix | Delete
*
[12] Fix | Delete
* @since 5.5.0
[13] Fix | Delete
* @since 6.3.0 Added source to core block patterns.
[14] Fix | Delete
* @access private
[15] Fix | Delete
*/
[16] Fix | Delete
function _register_core_block_patterns_and_categories() {
[17] Fix | Delete
$should_register_core_patterns = get_theme_support( 'core-block-patterns' );
[18] Fix | Delete
[19] Fix | Delete
if ( $should_register_core_patterns ) {
[20] Fix | Delete
$core_block_patterns = array(
[21] Fix | Delete
'query-standard-posts',
[22] Fix | Delete
'query-medium-posts',
[23] Fix | Delete
'query-small-posts',
[24] Fix | Delete
'query-grid-posts',
[25] Fix | Delete
'query-large-title-posts',
[26] Fix | Delete
'query-offset-posts',
[27] Fix | Delete
'social-links-shared-background-color',
[28] Fix | Delete
);
[29] Fix | Delete
[30] Fix | Delete
foreach ( $core_block_patterns as $core_block_pattern ) {
[31] Fix | Delete
$pattern = require __DIR__ . '/block-patterns/' . $core_block_pattern . '.php';
[32] Fix | Delete
$pattern['source'] = 'core';
[33] Fix | Delete
register_block_pattern( 'core/' . $core_block_pattern, $pattern );
[34] Fix | Delete
}
[35] Fix | Delete
}
[36] Fix | Delete
[37] Fix | Delete
register_block_pattern_category( 'banner', array( 'label' => _x( 'Banners', 'Block pattern category' ) ) );
[38] Fix | Delete
register_block_pattern_category(
[39] Fix | Delete
'buttons',
[40] Fix | Delete
array(
[41] Fix | Delete
'label' => _x( 'Buttons', 'Block pattern category' ),
[42] Fix | Delete
'description' => __( 'Patterns that contain buttons and call to actions.' ),
[43] Fix | Delete
)
[44] Fix | Delete
);
[45] Fix | Delete
register_block_pattern_category(
[46] Fix | Delete
'columns',
[47] Fix | Delete
array(
[48] Fix | Delete
'label' => _x( 'Columns', 'Block pattern category' ),
[49] Fix | Delete
'description' => __( 'Multi-column patterns with more complex layouts.' ),
[50] Fix | Delete
)
[51] Fix | Delete
);
[52] Fix | Delete
register_block_pattern_category(
[53] Fix | Delete
'text',
[54] Fix | Delete
array(
[55] Fix | Delete
'label' => _x( 'Text', 'Block pattern category' ),
[56] Fix | Delete
'description' => __( 'Patterns containing mostly text.' ),
[57] Fix | Delete
)
[58] Fix | Delete
);
[59] Fix | Delete
register_block_pattern_category(
[60] Fix | Delete
'query',
[61] Fix | Delete
array(
[62] Fix | Delete
'label' => _x( 'Posts', 'Block pattern category' ),
[63] Fix | Delete
'description' => __( 'Display your latest posts in lists, grids or other layouts.' ),
[64] Fix | Delete
)
[65] Fix | Delete
);
[66] Fix | Delete
register_block_pattern_category(
[67] Fix | Delete
'featured',
[68] Fix | Delete
array(
[69] Fix | Delete
'label' => _x( 'Featured', 'Block pattern category' ),
[70] Fix | Delete
'description' => __( 'A set of high quality curated patterns.' ),
[71] Fix | Delete
)
[72] Fix | Delete
);
[73] Fix | Delete
register_block_pattern_category(
[74] Fix | Delete
'call-to-action',
[75] Fix | Delete
array(
[76] Fix | Delete
'label' => _x( 'Call to Action', 'Block pattern category' ),
[77] Fix | Delete
'description' => __( 'Sections whose purpose is to trigger a specific action.' ),
[78] Fix | Delete
)
[79] Fix | Delete
);
[80] Fix | Delete
register_block_pattern_category(
[81] Fix | Delete
'team',
[82] Fix | Delete
array(
[83] Fix | Delete
'label' => _x( 'Team', 'Block pattern category' ),
[84] Fix | Delete
'description' => __( 'A variety of designs to display your team members.' ),
[85] Fix | Delete
)
[86] Fix | Delete
);
[87] Fix | Delete
register_block_pattern_category(
[88] Fix | Delete
'testimonials',
[89] Fix | Delete
array(
[90] Fix | Delete
'label' => _x( 'Testimonials', 'Block pattern category' ),
[91] Fix | Delete
'description' => __( 'Share reviews and feedback about your brand/business.' ),
[92] Fix | Delete
)
[93] Fix | Delete
);
[94] Fix | Delete
register_block_pattern_category(
[95] Fix | Delete
'services',
[96] Fix | Delete
array(
[97] Fix | Delete
'label' => _x( 'Services', 'Block pattern category' ),
[98] Fix | Delete
'description' => __( 'Briefly describe what your business does and how you can help.' ),
[99] Fix | Delete
)
[100] Fix | Delete
);
[101] Fix | Delete
register_block_pattern_category(
[102] Fix | Delete
'contact',
[103] Fix | Delete
array(
[104] Fix | Delete
'label' => _x( 'Contact', 'Block pattern category' ),
[105] Fix | Delete
'description' => __( 'Display your contact information.' ),
[106] Fix | Delete
)
[107] Fix | Delete
);
[108] Fix | Delete
register_block_pattern_category(
[109] Fix | Delete
'about',
[110] Fix | Delete
array(
[111] Fix | Delete
'label' => _x( 'About', 'Block pattern category' ),
[112] Fix | Delete
'description' => __( 'Introduce yourself.' ),
[113] Fix | Delete
)
[114] Fix | Delete
);
[115] Fix | Delete
register_block_pattern_category(
[116] Fix | Delete
'portfolio',
[117] Fix | Delete
array(
[118] Fix | Delete
'label' => _x( 'Portfolio', 'Block pattern category' ),
[119] Fix | Delete
'description' => __( 'Showcase your latest work.' ),
[120] Fix | Delete
)
[121] Fix | Delete
);
[122] Fix | Delete
register_block_pattern_category(
[123] Fix | Delete
'gallery',
[124] Fix | Delete
array(
[125] Fix | Delete
'label' => _x( 'Gallery', 'Block pattern category' ),
[126] Fix | Delete
'description' => __( 'Different layouts for displaying images.' ),
[127] Fix | Delete
)
[128] Fix | Delete
);
[129] Fix | Delete
register_block_pattern_category(
[130] Fix | Delete
'media',
[131] Fix | Delete
array(
[132] Fix | Delete
'label' => _x( 'Media', 'Block pattern category' ),
[133] Fix | Delete
'description' => __( 'Different layouts containing video or audio.' ),
[134] Fix | Delete
)
[135] Fix | Delete
);
[136] Fix | Delete
register_block_pattern_category(
[137] Fix | Delete
'videos',
[138] Fix | Delete
array(
[139] Fix | Delete
'label' => _x( 'Videos', 'Block pattern category' ),
[140] Fix | Delete
'description' => __( 'Different layouts containing videos.' ),
[141] Fix | Delete
)
[142] Fix | Delete
);
[143] Fix | Delete
register_block_pattern_category(
[144] Fix | Delete
'audio',
[145] Fix | Delete
array(
[146] Fix | Delete
'label' => _x( 'Audio', 'Block pattern category' ),
[147] Fix | Delete
'description' => __( 'Different layouts containing audio.' ),
[148] Fix | Delete
)
[149] Fix | Delete
);
[150] Fix | Delete
register_block_pattern_category(
[151] Fix | Delete
'posts',
[152] Fix | Delete
array(
[153] Fix | Delete
'label' => _x( 'Posts', 'Block pattern category' ),
[154] Fix | Delete
'description' => __( 'Display your latest posts in lists, grids or other layouts.' ),
[155] Fix | Delete
)
[156] Fix | Delete
);
[157] Fix | Delete
register_block_pattern_category(
[158] Fix | Delete
'footer',
[159] Fix | Delete
array(
[160] Fix | Delete
'label' => _x( 'Footers', 'Block pattern category' ),
[161] Fix | Delete
'description' => __( 'A variety of footer designs displaying information and site navigation.' ),
[162] Fix | Delete
)
[163] Fix | Delete
);
[164] Fix | Delete
register_block_pattern_category(
[165] Fix | Delete
'header',
[166] Fix | Delete
array(
[167] Fix | Delete
'label' => _x( 'Headers', 'Block pattern category' ),
[168] Fix | Delete
'description' => __( 'A variety of header designs displaying your site title and navigation.' ),
[169] Fix | Delete
)
[170] Fix | Delete
);
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
/**
[174] Fix | Delete
* Normalize the pattern properties to camelCase.
[175] Fix | Delete
*
[176] Fix | Delete
* The API's format is snake_case, `register_block_pattern()` expects camelCase.
[177] Fix | Delete
*
[178] Fix | Delete
* @since 6.2.0
[179] Fix | Delete
* @access private
[180] Fix | Delete
*
[181] Fix | Delete
* @param array $pattern Pattern as returned from the Pattern Directory API.
[182] Fix | Delete
* @return array Normalized pattern.
[183] Fix | Delete
*/
[184] Fix | Delete
function wp_normalize_remote_block_pattern( $pattern ) {
[185] Fix | Delete
if ( isset( $pattern['block_types'] ) ) {
[186] Fix | Delete
$pattern['blockTypes'] = $pattern['block_types'];
[187] Fix | Delete
unset( $pattern['block_types'] );
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
if ( isset( $pattern['viewport_width'] ) ) {
[191] Fix | Delete
$pattern['viewportWidth'] = $pattern['viewport_width'];
[192] Fix | Delete
unset( $pattern['viewport_width'] );
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
return (array) $pattern;
[196] Fix | Delete
}
[197] Fix | Delete
[198] Fix | Delete
/**
[199] Fix | Delete
* Register Core's official patterns from wordpress.org/patterns.
[200] Fix | Delete
*
[201] Fix | Delete
* @since 5.8.0
[202] Fix | Delete
* @since 5.9.0 The $current_screen argument was removed.
[203] Fix | Delete
* @since 6.2.0 Normalize the pattern from the API (snake_case) to the
[204] Fix | Delete
* format expected by `register_block_pattern` (camelCase).
[205] Fix | Delete
* @since 6.3.0 Add 'pattern-directory/core' to the pattern's 'source'.
[206] Fix | Delete
*
[207] Fix | Delete
* @param WP_Screen $deprecated Unused. Formerly the screen that the current request was triggered from.
[208] Fix | Delete
*/
[209] Fix | Delete
function _load_remote_block_patterns( $deprecated = null ) {
[210] Fix | Delete
if ( ! empty( $deprecated ) ) {
[211] Fix | Delete
_deprecated_argument( __FUNCTION__, '5.9.0' );
[212] Fix | Delete
$current_screen = $deprecated;
[213] Fix | Delete
if ( ! $current_screen->is_block_editor ) {
[214] Fix | Delete
return;
[215] Fix | Delete
}
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
$supports_core_patterns = get_theme_support( 'core-block-patterns' );
[219] Fix | Delete
[220] Fix | Delete
/**
[221] Fix | Delete
* Filter to disable remote block patterns.
[222] Fix | Delete
*
[223] Fix | Delete
* @since 5.8.0
[224] Fix | Delete
*
[225] Fix | Delete
* @param bool $should_load_remote
[226] Fix | Delete
*/
[227] Fix | Delete
$should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );
[228] Fix | Delete
[229] Fix | Delete
if ( $supports_core_patterns && $should_load_remote ) {
[230] Fix | Delete
$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
[231] Fix | Delete
$core_keyword_id = 11; // 11 is the ID for "core".
[232] Fix | Delete
$request->set_param( 'keyword', $core_keyword_id );
[233] Fix | Delete
$response = rest_do_request( $request );
[234] Fix | Delete
if ( $response->is_error() ) {
[235] Fix | Delete
return;
[236] Fix | Delete
}
[237] Fix | Delete
$patterns = $response->get_data();
[238] Fix | Delete
[239] Fix | Delete
foreach ( $patterns as $pattern ) {
[240] Fix | Delete
$pattern['source'] = 'pattern-directory/core';
[241] Fix | Delete
$normalized_pattern = wp_normalize_remote_block_pattern( $pattern );
[242] Fix | Delete
$pattern_name = 'core/' . sanitize_title( $normalized_pattern['title'] );
[243] Fix | Delete
register_block_pattern( $pattern_name, $normalized_pattern );
[244] Fix | Delete
}
[245] Fix | Delete
}
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
/**
[249] Fix | Delete
* Register `Featured` (category) patterns from wordpress.org/patterns.
[250] Fix | Delete
*
[251] Fix | Delete
* @since 5.9.0
[252] Fix | Delete
* @since 6.2.0 Normalized the pattern from the API (snake_case) to the
[253] Fix | Delete
* format expected by `register_block_pattern()` (camelCase).
[254] Fix | Delete
* @since 6.3.0 Add 'pattern-directory/featured' to the pattern's 'source'.
[255] Fix | Delete
*/
[256] Fix | Delete
function _load_remote_featured_patterns() {
[257] Fix | Delete
$supports_core_patterns = get_theme_support( 'core-block-patterns' );
[258] Fix | Delete
[259] Fix | Delete
/** This filter is documented in wp-includes/block-patterns.php */
[260] Fix | Delete
$should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );
[261] Fix | Delete
[262] Fix | Delete
if ( ! $should_load_remote || ! $supports_core_patterns ) {
[263] Fix | Delete
return;
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
[267] Fix | Delete
$featured_cat_id = 26; // This is the `Featured` category id from pattern directory.
[268] Fix | Delete
$request->set_param( 'category', $featured_cat_id );
[269] Fix | Delete
$response = rest_do_request( $request );
[270] Fix | Delete
if ( $response->is_error() ) {
[271] Fix | Delete
return;
[272] Fix | Delete
}
[273] Fix | Delete
$patterns = $response->get_data();
[274] Fix | Delete
$registry = WP_Block_Patterns_Registry::get_instance();
[275] Fix | Delete
foreach ( $patterns as $pattern ) {
[276] Fix | Delete
$pattern['source'] = 'pattern-directory/featured';
[277] Fix | Delete
$normalized_pattern = wp_normalize_remote_block_pattern( $pattern );
[278] Fix | Delete
$pattern_name = sanitize_title( $normalized_pattern['title'] );
[279] Fix | Delete
// Some patterns might be already registered as core patterns with the `core` prefix.
[280] Fix | Delete
$is_registered = $registry->is_registered( $pattern_name ) || $registry->is_registered( "core/$pattern_name" );
[281] Fix | Delete
if ( ! $is_registered ) {
[282] Fix | Delete
register_block_pattern( $pattern_name, $normalized_pattern );
[283] Fix | Delete
}
[284] Fix | Delete
}
[285] Fix | Delete
}
[286] Fix | Delete
[287] Fix | Delete
/**
[288] Fix | Delete
* Registers patterns from Pattern Directory provided by a theme's
[289] Fix | Delete
* `theme.json` file.
[290] Fix | Delete
*
[291] Fix | Delete
* @since 6.0.0
[292] Fix | Delete
* @since 6.2.0 Normalized the pattern from the API (snake_case) to the
[293] Fix | Delete
* format expected by `register_block_pattern()` (camelCase).
[294] Fix | Delete
* @since 6.3.0 Add 'pattern-directory/theme' to the pattern's 'source'.
[295] Fix | Delete
* @access private
[296] Fix | Delete
*/
[297] Fix | Delete
function _register_remote_theme_patterns() {
[298] Fix | Delete
/** This filter is documented in wp-includes/block-patterns.php */
[299] Fix | Delete
if ( ! apply_filters( 'should_load_remote_block_patterns', true ) ) {
[300] Fix | Delete
return;
[301] Fix | Delete
}
[302] Fix | Delete
[303] Fix | Delete
if ( ! wp_theme_has_theme_json() ) {
[304] Fix | Delete
return;
[305] Fix | Delete
}
[306] Fix | Delete
[307] Fix | Delete
$pattern_settings = wp_get_theme_directory_pattern_slugs();
[308] Fix | Delete
if ( empty( $pattern_settings ) ) {
[309] Fix | Delete
return;
[310] Fix | Delete
}
[311] Fix | Delete
[312] Fix | Delete
$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
[313] Fix | Delete
$request['slug'] = $pattern_settings;
[314] Fix | Delete
$response = rest_do_request( $request );
[315] Fix | Delete
if ( $response->is_error() ) {
[316] Fix | Delete
return;
[317] Fix | Delete
}
[318] Fix | Delete
$patterns = $response->get_data();
[319] Fix | Delete
$patterns_registry = WP_Block_Patterns_Registry::get_instance();
[320] Fix | Delete
foreach ( $patterns as $pattern ) {
[321] Fix | Delete
$pattern['source'] = 'pattern-directory/theme';
[322] Fix | Delete
$normalized_pattern = wp_normalize_remote_block_pattern( $pattern );
[323] Fix | Delete
$pattern_name = sanitize_title( $normalized_pattern['title'] );
[324] Fix | Delete
// Some patterns might be already registered as core patterns with the `core` prefix.
[325] Fix | Delete
$is_registered = $patterns_registry->is_registered( $pattern_name ) || $patterns_registry->is_registered( "core/$pattern_name" );
[326] Fix | Delete
if ( ! $is_registered ) {
[327] Fix | Delete
register_block_pattern( $pattern_name, $normalized_pattern );
[328] Fix | Delete
}
[329] Fix | Delete
}
[330] Fix | Delete
}
[331] Fix | Delete
[332] Fix | Delete
/**
[333] Fix | Delete
* Register any patterns that the active theme may provide under its
[334] Fix | Delete
* `./patterns/` directory.
[335] Fix | Delete
*
[336] Fix | Delete
* @since 6.0.0
[337] Fix | Delete
* @since 6.1.0 The `postTypes` property was added.
[338] Fix | Delete
* @since 6.2.0 The `templateTypes` property was added.
[339] Fix | Delete
* @since 6.4.0 Uses the `WP_Theme::get_block_patterns` method.
[340] Fix | Delete
* @access private
[341] Fix | Delete
*/
[342] Fix | Delete
function _register_theme_block_patterns() {
[343] Fix | Delete
[344] Fix | Delete
/*
[345] Fix | Delete
* During the bootstrap process, a check for active and valid themes is run.
[346] Fix | Delete
* If no themes are returned, the theme's functions.php file will not be loaded,
[347] Fix | Delete
* which can lead to errors if patterns expect some variables or constants to
[348] Fix | Delete
* already be set at this point, so bail early if that is the case.
[349] Fix | Delete
*/
[350] Fix | Delete
if ( empty( wp_get_active_and_valid_themes() ) ) {
[351] Fix | Delete
return;
[352] Fix | Delete
}
[353] Fix | Delete
[354] Fix | Delete
/*
[355] Fix | Delete
* Register patterns for the active theme. If the theme is a child theme,
[356] Fix | Delete
* let it override any patterns from the parent theme that shares the same slug.
[357] Fix | Delete
*/
[358] Fix | Delete
$themes = array();
[359] Fix | Delete
$theme = wp_get_theme();
[360] Fix | Delete
$themes[] = $theme;
[361] Fix | Delete
if ( $theme->parent() ) {
[362] Fix | Delete
$themes[] = $theme->parent();
[363] Fix | Delete
}
[364] Fix | Delete
$registry = WP_Block_Patterns_Registry::get_instance();
[365] Fix | Delete
[366] Fix | Delete
foreach ( $themes as $theme ) {
[367] Fix | Delete
$patterns = $theme->get_block_patterns();
[368] Fix | Delete
$dirpath = $theme->get_stylesheet_directory() . '/patterns/';
[369] Fix | Delete
$text_domain = $theme->get( 'TextDomain' );
[370] Fix | Delete
[371] Fix | Delete
foreach ( $patterns as $file => $pattern_data ) {
[372] Fix | Delete
if ( $registry->is_registered( $pattern_data['slug'] ) ) {
[373] Fix | Delete
continue;
[374] Fix | Delete
}
[375] Fix | Delete
[376] Fix | Delete
$file_path = $dirpath . $file;
[377] Fix | Delete
[378] Fix | Delete
if ( ! file_exists( $file_path ) ) {
[379] Fix | Delete
_doing_it_wrong(
[380] Fix | Delete
__FUNCTION__,
[381] Fix | Delete
sprintf(
[382] Fix | Delete
/* translators: %s: file name. */
[383] Fix | Delete
__( 'Could not register file "%s" as a block pattern as the file does not exist.' ),
[384] Fix | Delete
$file
[385] Fix | Delete
),
[386] Fix | Delete
'6.4.0'
[387] Fix | Delete
);
[388] Fix | Delete
$theme->delete_pattern_cache();
[389] Fix | Delete
continue;
[390] Fix | Delete
}
[391] Fix | Delete
[392] Fix | Delete
$pattern_data['filePath'] = $file_path;
[393] Fix | Delete
[394] Fix | Delete
// Translate the pattern metadata.
[395] Fix | Delete
// phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain,WordPress.WP.I18n.LowLevelTranslationFunction
[396] Fix | Delete
$pattern_data['title'] = translate_with_gettext_context( $pattern_data['title'], 'Pattern title', $text_domain );
[397] Fix | Delete
if ( ! empty( $pattern_data['description'] ) ) {
[398] Fix | Delete
// phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain,WordPress.WP.I18n.LowLevelTranslationFunction
[399] Fix | Delete
$pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', $text_domain );
[400] Fix | Delete
}
[401] Fix | Delete
[402] Fix | Delete
register_block_pattern( $pattern_data['slug'], $pattern_data );
[403] Fix | Delete
}
[404] Fix | Delete
}
[405] Fix | Delete
}
[406] Fix | Delete
add_action( 'init', '_register_theme_block_patterns' );
[407] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function