Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 63 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Widget_Type_Registry | |
0.00% |
0 / 63 |
|
0.00% |
0 / 6 |
272 | |
0.00% |
0 / 1 |
| register | |
0.00% |
0 / 40 |
|
0.00% |
0 / 1 |
56 | |||
| unregister | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| get_registered | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| get_all_registered | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| is_registered | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_instance | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Widget Types API: Widget_Type_Registry class. |
| 4 | * |
| 5 | * PA-namespaced copy of the dashboard widget-type registry (the core/Gutenberg |
| 6 | * version is gated behind an experimental flag). Stays fully isolated from any |
| 7 | * core registry so the two never share state. |
| 8 | * |
| 9 | * @package automattic/jetpack-premium-analytics |
| 10 | */ |
| 11 | |
| 12 | namespace Automattic\Jetpack\PremiumAnalytics; |
| 13 | |
| 14 | /** |
| 15 | * Stores Widget_Type instances keyed by their namespaced name. |
| 16 | * |
| 17 | * Singleton hydrated once per request from the build manifest at `init` (see |
| 18 | * widget-types.php); consumers query the registry instead of re-parsing the |
| 19 | * manifest. |
| 20 | */ |
| 21 | #[\AllowDynamicProperties] |
| 22 | final class Widget_Type_Registry { |
| 23 | |
| 24 | /** |
| 25 | * Registered widget types, as `$name => $instance` pairs. |
| 26 | * |
| 27 | * @var Widget_Type[] |
| 28 | */ |
| 29 | private $registered_widget_types = array(); |
| 30 | |
| 31 | /** |
| 32 | * Container for the main instance of the class. |
| 33 | * |
| 34 | * @var Widget_Type_Registry|null |
| 35 | */ |
| 36 | private static $instance = null; |
| 37 | |
| 38 | /** |
| 39 | * Registers a widget type. |
| 40 | * |
| 41 | * @param string|Widget_Type $name Widget type name including namespace, or |
| 42 | * a complete Widget_Type instance. When an |
| 43 | * instance is provided the `$args` parameter |
| 44 | * is ignored. |
| 45 | * @param array $args Optional. Array of widget type arguments. |
| 46 | * Accepts any public property of |
| 47 | * Widget_Type. Default empty array. |
| 48 | * @return Widget_Type|false The registered widget type on success, or false |
| 49 | * on failure. |
| 50 | */ |
| 51 | public function register( $name, $args = array() ) { |
| 52 | $widget_type = null; |
| 53 | if ( $name instanceof Widget_Type ) { |
| 54 | $widget_type = $name; |
| 55 | $name = $widget_type->name; |
| 56 | } |
| 57 | |
| 58 | if ( ! is_string( $name ) ) { |
| 59 | _doing_it_wrong( |
| 60 | __METHOD__, |
| 61 | esc_html__( 'Widget type names must be strings.', 'jetpack-premium-analytics' ), |
| 62 | '0.1.0' |
| 63 | ); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | if ( preg_match( '/[A-Z]+/', $name ) ) { |
| 68 | _doing_it_wrong( |
| 69 | __METHOD__, |
| 70 | esc_html__( 'Widget type names must not contain uppercase characters.', 'jetpack-premium-analytics' ), |
| 71 | '0.1.0' |
| 72 | ); |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | $name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/'; |
| 77 | if ( ! preg_match( $name_matcher, $name ) ) { |
| 78 | _doing_it_wrong( |
| 79 | __METHOD__, |
| 80 | esc_html__( 'Widget type names must contain a namespace prefix. Example: my-plugin/my-custom-widget-type', 'jetpack-premium-analytics' ), |
| 81 | '0.1.0' |
| 82 | ); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | if ( $this->is_registered( $name ) ) { |
| 87 | _doing_it_wrong( |
| 88 | __METHOD__, |
| 89 | sprintf( |
| 90 | /* translators: %s: Widget type name. */ |
| 91 | esc_html__( 'Widget type "%s" is already registered.', 'jetpack-premium-analytics' ), |
| 92 | esc_html( $name ) |
| 93 | ), |
| 94 | '0.1.0' |
| 95 | ); |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | if ( ! $widget_type ) { |
| 100 | $widget_type = new Widget_Type( $name, $args ); |
| 101 | } |
| 102 | |
| 103 | $this->registered_widget_types[ $name ] = $widget_type; |
| 104 | |
| 105 | return $widget_type; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Unregisters a widget type. |
| 110 | * |
| 111 | * @param string|Widget_Type $name Widget type name including namespace, or |
| 112 | * a complete Widget_Type instance. |
| 113 | * @return Widget_Type|false The unregistered widget type on success, or |
| 114 | * false on failure. |
| 115 | */ |
| 116 | public function unregister( $name ) { |
| 117 | if ( $name instanceof Widget_Type ) { |
| 118 | $name = $name->name; |
| 119 | } |
| 120 | |
| 121 | if ( ! $this->is_registered( $name ) ) { |
| 122 | _doing_it_wrong( |
| 123 | __METHOD__, |
| 124 | sprintf( |
| 125 | /* translators: %s: Widget type name. */ |
| 126 | esc_html__( 'Widget type "%s" is not registered.', 'jetpack-premium-analytics' ), |
| 127 | esc_html( $name ) |
| 128 | ), |
| 129 | '0.1.0' |
| 130 | ); |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | $unregistered_widget_type = $this->registered_widget_types[ $name ]; |
| 135 | unset( $this->registered_widget_types[ $name ] ); |
| 136 | |
| 137 | return $unregistered_widget_type; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Retrieves a registered widget type. |
| 142 | * |
| 143 | * @param string $name Widget type name including namespace. |
| 144 | * @return Widget_Type|null The registered widget type, or null if it is not |
| 145 | * registered. |
| 146 | */ |
| 147 | public function get_registered( $name ) { |
| 148 | if ( ! $this->is_registered( $name ) ) { |
| 149 | return null; |
| 150 | } |
| 151 | |
| 152 | return $this->registered_widget_types[ $name ]; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Retrieves all registered widget types. |
| 157 | * |
| 158 | * @return Widget_Type[] Associative array of `$name => $widget_type` pairs. |
| 159 | */ |
| 160 | public function get_all_registered() { |
| 161 | return $this->registered_widget_types; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Checks if a widget type is registered. |
| 166 | * |
| 167 | * @param string $name Widget type name including namespace. |
| 168 | * @return bool True if the widget type is registered, false otherwise. |
| 169 | */ |
| 170 | public function is_registered( $name ) { |
| 171 | return isset( $this->registered_widget_types[ $name ] ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Utility method to retrieve the main instance of the class. |
| 176 | * |
| 177 | * The instance will be created if it does not exist yet. |
| 178 | * |
| 179 | * @return Widget_Type_Registry The main instance. |
| 180 | */ |
| 181 | public static function get_instance() { |
| 182 | if ( null === self::$instance ) { |
| 183 | self::$instance = new self(); |
| 184 | } |
| 185 | |
| 186 | return self::$instance; |
| 187 | } |
| 188 | } |