Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../public_h.../wp-inclu...
File: class-wp-block-patterns-registry.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Blocks API: WP_Block_Patterns_Registry class
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage Blocks
[5] Fix | Delete
* @since 5.5.0
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Class used for interacting with block patterns.
[10] Fix | Delete
*
[11] Fix | Delete
* @since 5.5.0
[12] Fix | Delete
*/
[13] Fix | Delete
#[AllowDynamicProperties]
[14] Fix | Delete
final class WP_Block_Patterns_Registry {
[15] Fix | Delete
/**
[16] Fix | Delete
* Registered block patterns array.
[17] Fix | Delete
*
[18] Fix | Delete
* @since 5.5.0
[19] Fix | Delete
* @var array[]
[20] Fix | Delete
*/
[21] Fix | Delete
private $registered_patterns = array();
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Patterns registered outside the `init` action.
[25] Fix | Delete
*
[26] Fix | Delete
* @since 6.0.0
[27] Fix | Delete
* @var array[]
[28] Fix | Delete
*/
[29] Fix | Delete
private $registered_patterns_outside_init = array();
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Container for the main instance of the class.
[33] Fix | Delete
*
[34] Fix | Delete
* @since 5.5.0
[35] Fix | Delete
* @var WP_Block_Patterns_Registry|null
[36] Fix | Delete
*/
[37] Fix | Delete
private static $instance = null;
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Registers a block pattern.
[41] Fix | Delete
*
[42] Fix | Delete
* @since 5.5.0
[43] Fix | Delete
* @since 5.8.0 Added support for the `blockTypes` property.
[44] Fix | Delete
* @since 6.1.0 Added support for the `postTypes` property.
[45] Fix | Delete
* @since 6.2.0 Added support for the `templateTypes` property.
[46] Fix | Delete
* @since 6.5.0 Added support for the `filePath` property.
[47] Fix | Delete
*
[48] Fix | Delete
* @param string $pattern_name Block pattern name including namespace.
[49] Fix | Delete
* @param array $pattern_properties {
[50] Fix | Delete
* List of properties for the block pattern.
[51] Fix | Delete
*
[52] Fix | Delete
* @type string $title Required. A human-readable title for the pattern.
[53] Fix | Delete
* @type string $content Optional. Block HTML markup for the pattern.
[54] Fix | Delete
* If not provided, the content will be retrieved from the `filePath` if set.
[55] Fix | Delete
* If both `content` and `filePath` are not set, the pattern will not be registered.
[56] Fix | Delete
* @type string $description Optional. Visually hidden text used to describe the pattern
[57] Fix | Delete
* in the inserter. A description is optional, but is strongly
[58] Fix | Delete
* encouraged when the title does not fully describe what the
[59] Fix | Delete
* pattern does. The description will help users discover the
[60] Fix | Delete
* pattern while searching.
[61] Fix | Delete
* @type int $viewportWidth Optional. The intended width of the pattern to allow for a scaled
[62] Fix | Delete
* preview within the pattern inserter.
[63] Fix | Delete
* @type bool $inserter Optional. Determines whether the pattern is visible in inserter.
[64] Fix | Delete
* To hide a pattern so that it can only be inserted programmatically,
[65] Fix | Delete
* set this to false. Default true.
[66] Fix | Delete
* @type string[] $categories Optional. A list of registered pattern categories used to group
[67] Fix | Delete
* block patterns. Block patterns can be shown on multiple categories.
[68] Fix | Delete
* A category must be registered separately in order to be used here.
[69] Fix | Delete
* @type string[] $keywords Optional. A list of aliases or keywords that help users discover
[70] Fix | Delete
* the pattern while searching.
[71] Fix | Delete
* @type string[] $blockTypes Optional. A list of block names including namespace that could use
[72] Fix | Delete
* the block pattern in certain contexts (placeholder, transforms).
[73] Fix | Delete
* The block pattern is available in the block editor inserter
[74] Fix | Delete
* regardless of this list of block names.
[75] Fix | Delete
* Certain blocks support further specificity besides the block name
[76] Fix | Delete
* (e.g. for `core/template-part` you can specify areas
[77] Fix | Delete
* like `core/template-part/header` or `core/template-part/footer`).
[78] Fix | Delete
* @type string[] $postTypes Optional. An array of post types that the pattern is restricted
[79] Fix | Delete
* to be used with. The pattern will only be available when editing one
[80] Fix | Delete
* of the post types passed on the array. For all the other post types
[81] Fix | Delete
* not part of the array the pattern is not available at all.
[82] Fix | Delete
* @type string[] $templateTypes Optional. An array of template types where the pattern fits.
[83] Fix | Delete
* @type string $filePath Optional. The full path to the file containing the block pattern content.
[84] Fix | Delete
* }
[85] Fix | Delete
* @return bool True if the pattern was registered with success and false otherwise.
[86] Fix | Delete
*/
[87] Fix | Delete
public function register( $pattern_name, $pattern_properties ) {
[88] Fix | Delete
if ( ! isset( $pattern_name ) || ! is_string( $pattern_name ) ) {
[89] Fix | Delete
_doing_it_wrong(
[90] Fix | Delete
__METHOD__,
[91] Fix | Delete
__( 'Pattern name must be a string.' ),
[92] Fix | Delete
'5.5.0'
[93] Fix | Delete
);
[94] Fix | Delete
return false;
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
if ( ! isset( $pattern_properties['title'] ) || ! is_string( $pattern_properties['title'] ) ) {
[98] Fix | Delete
_doing_it_wrong(
[99] Fix | Delete
__METHOD__,
[100] Fix | Delete
__( 'Pattern title must be a string.' ),
[101] Fix | Delete
'5.5.0'
[102] Fix | Delete
);
[103] Fix | Delete
return false;
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
if ( ! isset( $pattern_properties['filePath'] ) ) {
[107] Fix | Delete
if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) {
[108] Fix | Delete
_doing_it_wrong(
[109] Fix | Delete
__METHOD__,
[110] Fix | Delete
__( 'Pattern content must be a string.' ),
[111] Fix | Delete
'5.5.0'
[112] Fix | Delete
);
[113] Fix | Delete
return false;
[114] Fix | Delete
}
[115] Fix | Delete
}
[116] Fix | Delete
[117] Fix | Delete
$pattern = array_merge(
[118] Fix | Delete
$pattern_properties,
[119] Fix | Delete
array( 'name' => $pattern_name )
[120] Fix | Delete
);
[121] Fix | Delete
[122] Fix | Delete
$this->registered_patterns[ $pattern_name ] = $pattern;
[123] Fix | Delete
[124] Fix | Delete
// If the pattern is registered inside an action other than `init`, store it
[125] Fix | Delete
// also to a dedicated array. Used to detect deprecated registrations inside
[126] Fix | Delete
// `admin_init` or `current_screen`.
[127] Fix | Delete
if ( current_action() && 'init' !== current_action() ) {
[128] Fix | Delete
$this->registered_patterns_outside_init[ $pattern_name ] = $pattern;
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
return true;
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
/**
[135] Fix | Delete
* Unregisters a block pattern.
[136] Fix | Delete
*
[137] Fix | Delete
* @since 5.5.0
[138] Fix | Delete
*
[139] Fix | Delete
* @param string $pattern_name Block pattern name including namespace.
[140] Fix | Delete
* @return bool True if the pattern was unregistered with success and false otherwise.
[141] Fix | Delete
*/
[142] Fix | Delete
public function unregister( $pattern_name ) {
[143] Fix | Delete
if ( ! $this->is_registered( $pattern_name ) ) {
[144] Fix | Delete
_doing_it_wrong(
[145] Fix | Delete
__METHOD__,
[146] Fix | Delete
/* translators: %s: Pattern name. */
[147] Fix | Delete
sprintf( __( 'Pattern "%s" not found.' ), $pattern_name ),
[148] Fix | Delete
'5.5.0'
[149] Fix | Delete
);
[150] Fix | Delete
return false;
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
unset( $this->registered_patterns[ $pattern_name ] );
[154] Fix | Delete
unset( $this->registered_patterns_outside_init[ $pattern_name ] );
[155] Fix | Delete
[156] Fix | Delete
return true;
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
/**
[160] Fix | Delete
* Prepares the content of a block pattern. If hooked blocks are registered, they get injected into the pattern,
[161] Fix | Delete
* when they met the defined criteria.
[162] Fix | Delete
*
[163] Fix | Delete
* @since 6.4.0
[164] Fix | Delete
*
[165] Fix | Delete
* @param array $pattern Registered pattern properties.
[166] Fix | Delete
* @param array $hooked_blocks The list of hooked blocks.
[167] Fix | Delete
* @return string The content of the block pattern.
[168] Fix | Delete
*/
[169] Fix | Delete
private function prepare_content( $pattern, $hooked_blocks ) {
[170] Fix | Delete
$content = $pattern['content'];
[171] Fix | Delete
[172] Fix | Delete
$before_block_visitor = '_inject_theme_attribute_in_template_part_block';
[173] Fix | Delete
$after_block_visitor = null;
[174] Fix | Delete
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
[175] Fix | Delete
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $pattern, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
[176] Fix | Delete
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $pattern, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
[177] Fix | Delete
}
[178] Fix | Delete
$blocks = parse_blocks( $content );
[179] Fix | Delete
$content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
[180] Fix | Delete
[181] Fix | Delete
return $content;
[182] Fix | Delete
}
[183] Fix | Delete
[184] Fix | Delete
/**
[185] Fix | Delete
* Retrieves the content of a registered block pattern.
[186] Fix | Delete
*
[187] Fix | Delete
* @since 6.5.0
[188] Fix | Delete
*
[189] Fix | Delete
* @param string $pattern_name Block pattern name including namespace.
[190] Fix | Delete
* @param bool $outside_init_only Optional. Return only patterns registered outside the `init` action. Default false.
[191] Fix | Delete
* @return string The content of the block pattern.
[192] Fix | Delete
*/
[193] Fix | Delete
private function get_content( $pattern_name, $outside_init_only = false ) {
[194] Fix | Delete
if ( $outside_init_only ) {
[195] Fix | Delete
$patterns = &$this->registered_patterns_outside_init;
[196] Fix | Delete
} else {
[197] Fix | Delete
$patterns = &$this->registered_patterns;
[198] Fix | Delete
}
[199] Fix | Delete
if ( ! isset( $patterns[ $pattern_name ]['content'] ) && isset( $patterns[ $pattern_name ]['filePath'] ) ) {
[200] Fix | Delete
ob_start();
[201] Fix | Delete
include $patterns[ $pattern_name ]['filePath'];
[202] Fix | Delete
$patterns[ $pattern_name ]['content'] = ob_get_clean();
[203] Fix | Delete
unset( $patterns[ $pattern_name ]['filePath'] );
[204] Fix | Delete
}
[205] Fix | Delete
return $patterns[ $pattern_name ]['content'];
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
/**
[209] Fix | Delete
* Retrieves an array containing the properties of a registered block pattern.
[210] Fix | Delete
*
[211] Fix | Delete
* @since 5.5.0
[212] Fix | Delete
*
[213] Fix | Delete
* @param string $pattern_name Block pattern name including namespace.
[214] Fix | Delete
* @return array Registered pattern properties.
[215] Fix | Delete
*/
[216] Fix | Delete
public function get_registered( $pattern_name ) {
[217] Fix | Delete
if ( ! $this->is_registered( $pattern_name ) ) {
[218] Fix | Delete
return null;
[219] Fix | Delete
}
[220] Fix | Delete
[221] Fix | Delete
$pattern = $this->registered_patterns[ $pattern_name ];
[222] Fix | Delete
$pattern['content'] = $this->get_content( $pattern_name );
[223] Fix | Delete
$pattern['content'] = $this->prepare_content( $pattern, get_hooked_blocks() );
[224] Fix | Delete
[225] Fix | Delete
return $pattern;
[226] Fix | Delete
}
[227] Fix | Delete
[228] Fix | Delete
/**
[229] Fix | Delete
* Retrieves all registered block patterns.
[230] Fix | Delete
*
[231] Fix | Delete
* @since 5.5.0
[232] Fix | Delete
*
[233] Fix | Delete
* @param bool $outside_init_only Return only patterns registered outside the `init` action.
[234] Fix | Delete
* @return array[] Array of arrays containing the registered block patterns properties,
[235] Fix | Delete
* and per style.
[236] Fix | Delete
*/
[237] Fix | Delete
public function get_all_registered( $outside_init_only = false ) {
[238] Fix | Delete
$patterns = $outside_init_only
[239] Fix | Delete
? $this->registered_patterns_outside_init
[240] Fix | Delete
: $this->registered_patterns;
[241] Fix | Delete
$hooked_blocks = get_hooked_blocks();
[242] Fix | Delete
[243] Fix | Delete
foreach ( $patterns as $index => $pattern ) {
[244] Fix | Delete
$pattern['content'] = $this->get_content( $pattern['name'], $outside_init_only );
[245] Fix | Delete
$patterns[ $index ]['content'] = $this->prepare_content( $pattern, $hooked_blocks );
[246] Fix | Delete
}
[247] Fix | Delete
[248] Fix | Delete
return array_values( $patterns );
[249] Fix | Delete
}
[250] Fix | Delete
[251] Fix | Delete
/**
[252] Fix | Delete
* Checks if a block pattern is registered.
[253] Fix | Delete
*
[254] Fix | Delete
* @since 5.5.0
[255] Fix | Delete
*
[256] Fix | Delete
* @param string $pattern_name Block pattern name including namespace.
[257] Fix | Delete
* @return bool True if the pattern is registered, false otherwise.
[258] Fix | Delete
*/
[259] Fix | Delete
public function is_registered( $pattern_name ) {
[260] Fix | Delete
return isset( $this->registered_patterns[ $pattern_name ] );
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
public function __wakeup() {
[264] Fix | Delete
if ( ! $this->registered_patterns ) {
[265] Fix | Delete
return;
[266] Fix | Delete
}
[267] Fix | Delete
if ( ! is_array( $this->registered_patterns ) ) {
[268] Fix | Delete
throw new UnexpectedValueException();
[269] Fix | Delete
}
[270] Fix | Delete
foreach ( $this->registered_patterns as $value ) {
[271] Fix | Delete
if ( ! is_array( $value ) ) {
[272] Fix | Delete
throw new UnexpectedValueException();
[273] Fix | Delete
}
[274] Fix | Delete
}
[275] Fix | Delete
$this->registered_patterns_outside_init = array();
[276] Fix | Delete
}
[277] Fix | Delete
[278] Fix | Delete
/**
[279] Fix | Delete
* Utility method to retrieve the main instance of the class.
[280] Fix | Delete
*
[281] Fix | Delete
* The instance will be created if it does not exist yet.
[282] Fix | Delete
*
[283] Fix | Delete
* @since 5.5.0
[284] Fix | Delete
*
[285] Fix | Delete
* @return WP_Block_Patterns_Registry The main instance.
[286] Fix | Delete
*/
[287] Fix | Delete
public static function get_instance() {
[288] Fix | Delete
if ( null === self::$instance ) {
[289] Fix | Delete
self::$instance = new self();
[290] Fix | Delete
}
[291] Fix | Delete
[292] Fix | Delete
return self::$instance;
[293] Fix | Delete
}
[294] Fix | Delete
}
[295] Fix | Delete
[296] Fix | Delete
/**
[297] Fix | Delete
* Registers a new block pattern.
[298] Fix | Delete
*
[299] Fix | Delete
* @since 5.5.0
[300] Fix | Delete
*
[301] Fix | Delete
* @param string $pattern_name Block pattern name including namespace.
[302] Fix | Delete
* @param array $pattern_properties List of properties for the block pattern.
[303] Fix | Delete
* See WP_Block_Patterns_Registry::register() for accepted arguments.
[304] Fix | Delete
* @return bool True if the pattern was registered with success and false otherwise.
[305] Fix | Delete
*/
[306] Fix | Delete
function register_block_pattern( $pattern_name, $pattern_properties ) {
[307] Fix | Delete
return WP_Block_Patterns_Registry::get_instance()->register( $pattern_name, $pattern_properties );
[308] Fix | Delete
}
[309] Fix | Delete
[310] Fix | Delete
/**
[311] Fix | Delete
* Unregisters a block pattern.
[312] Fix | Delete
*
[313] Fix | Delete
* @since 5.5.0
[314] Fix | Delete
*
[315] Fix | Delete
* @param string $pattern_name Block pattern name including namespace.
[316] Fix | Delete
* @return bool True if the pattern was unregistered with success and false otherwise.
[317] Fix | Delete
*/
[318] Fix | Delete
function unregister_block_pattern( $pattern_name ) {
[319] Fix | Delete
return WP_Block_Patterns_Registry::get_instance()->unregister( $pattern_name );
[320] Fix | Delete
}
[321] Fix | Delete
[322] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function