| 1 |
<?php |
| 2 |
/** |
| 3 |
* Blocks API: WP_Block_Patterns_Registry class |
| 4 |
* |
| 5 |
* @package Gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class used for interacting with patterns. |
| 10 |
* |
| 11 |
* This class can be removed when plugin support requires WordPress 5.5.0+. |
| 12 |
* |
| 13 |
* @see https://core.trac.wordpress.org/ticket/50445 |
| 14 |
* @see https://core.trac.wordpress.org/changeset/48156 |
| 15 |
*/ |
| 16 |
final class WP_Block_Patterns_Registry { |
| 17 |
/** |
| 18 |
* Registered patterns array. |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
private $registered_patterns = array(); |
| 23 |
|
| 24 |
/** |
| 25 |
* Container for the main instance of the class. |
| 26 |
* |
| 27 |
* @var WP_Block_Patterns_Registry|null |
| 28 |
*/ |
| 29 |
private static $instance = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* Registers a pattern. |
| 33 |
* |
| 34 |
* @param string $pattern_name Pattern name including namespace. |
| 35 |
* @param array $pattern_properties Array containing the properties of the pattern: Title, content, description, viewportWidth, categories, keywords. |
| 36 |
* @return boolean True if the pattern was registered with success and false otherwise. |
| 37 |
*/ |
| 38 |
public function register( $pattern_name, $pattern_properties ) { |
| 39 |
if ( ! isset( $pattern_name ) || ! is_string( $pattern_name ) ) { |
| 40 |
$message = __( 'Pattern name must be a string.', 'gutenberg' ); |
| 41 |
_doing_it_wrong( __METHOD__, $message, '7.8.0' ); |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
if ( ! isset( $pattern_properties['title'] ) || ! is_string( $pattern_properties['title'] ) ) { |
| 46 |
$message = __( 'Pattern title must be a string.', 'gutenberg' ); |
| 47 |
_doing_it_wrong( __METHOD__, $message, '8.5.0' ); |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) { |
| 52 |
$message = __( 'Pattern content must be a string.', 'gutenberg' ); |
| 53 |
_doing_it_wrong( __METHOD__, $message, '8.5.0' ); |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
$this->registered_patterns[ $pattern_name ] = array_merge( |
| 58 |
$pattern_properties, |
| 59 |
array( 'name' => $pattern_name ) |
| 60 |
); |
| 61 |
|
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Unregisters a pattern. |
| 67 |
* |
| 68 |
* @param string $pattern_name Pattern name including namespace. |
| 69 |
* @return boolean True if the pattern was unregistered with success and false otherwise. |
| 70 |
*/ |
| 71 |
public function unregister( $pattern_name ) { |
| 72 |
if ( ! $this->is_registered( $pattern_name ) ) { |
| 73 |
/* translators: 1: Pattern name. */ |
| 74 |
$message = sprintf( __( 'Pattern "%1$s" not found.', 'gutenberg' ), $pattern_name ); |
| 75 |
_doing_it_wrong( __METHOD__, $message, '7.8.0' ); |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
unset( $this->registered_patterns[ $pattern_name ] ); |
| 80 |
|
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Retrieves an array containing the properties of a registered pattern. |
| 86 |
* |
| 87 |
* @param string $pattern_name Pattern name including namespace. |
| 88 |
* @return array Registered pattern properties. |
| 89 |
*/ |
| 90 |
public function get_registered( $pattern_name ) { |
| 91 |
if ( ! $this->is_registered( $pattern_name ) ) { |
| 92 |
return null; |
| 93 |
} |
| 94 |
|
| 95 |
return $this->registered_patterns[ $pattern_name ]; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Retrieves all registered patterns. |
| 100 |
* |
| 101 |
* @return array Array of arrays containing the registered patterns properties, |
| 102 |
* and per style. |
| 103 |
*/ |
| 104 |
public function get_all_registered() { |
| 105 |
return array_values( $this->registered_patterns ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Checks if a pattern is registered. |
| 110 |
* |
| 111 |
* @param string $pattern_name Pattern name including namespace. |
| 112 |
* @return bool True if the pattern is registered, false otherwise. |
| 113 |
*/ |
| 114 |
public function is_registered( $pattern_name ) { |
| 115 |
return isset( $this->registered_patterns[ $pattern_name ] ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Utility method to retrieve the main instance of the class. |
| 120 |
* |
| 121 |
* The instance will be created if it does not exist yet. |
| 122 |
* |
| 123 |
* @since 5.3.0 |
| 124 |
* |
| 125 |
* @return WP_Block_Patterns_Registry The main instance. |
| 126 |
*/ |
| 127 |
public static function get_instance() { |
| 128 |
if ( null === self::$instance ) { |
| 129 |
self::$instance = new self(); |
| 130 |
} |
| 131 |
|
| 132 |
return self::$instance; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Registers a new pattern. |
| 138 |
* |
| 139 |
* @param string $pattern_name Pattern name including namespace. |
| 140 |
* @param array $pattern_properties Array containing the properties of the pattern. |
| 141 |
* |
| 142 |
* @return boolean True if the pattern was registered with success and false otherwise. |
| 143 |
*/ |
| 144 |
function register_block_pattern( $pattern_name, $pattern_properties ) { |
| 145 |
return WP_Block_Patterns_Registry::get_instance()->register( $pattern_name, $pattern_properties ); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Unregisters a pattern. |
| 150 |
* |
| 151 |
* @param string $pattern_name Pattern name including namespace. |
| 152 |
* |
| 153 |
* @return boolean True if the pattern was unregistered with success and false otherwise. |
| 154 |
*/ |
| 155 |
function unregister_block_pattern( $pattern_name ) { |
| 156 |
return WP_Block_Patterns_Registry::get_instance()->unregister( $pattern_name ); |
| 157 |
} |
| 158 |
|