PluginProbe
Gutenberg / 23.3.0
Gutenberg v23.3.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / experimental / dashboard-widgets / class-wp-widget-type.php

class-wp-widget-type.php in Gutenberg 23.3.0, at lib/experimental/dashboard-widgets/class-wp-widget-type.php

110 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Widget Types API: WP_Widget_Type class.
4 *
5 * @package gutenberg
6 */
7
8 if ( ! class_exists( 'WP_Widget_Type' ) ) {
9
10 /**
11 * Internal class representing a widget type.
12 *
13 * Holds the metadata for a widget discovered by the build pipeline. Stored
14 * inside `WP_Widget_Type_Registry` once registered, and consumed by surface
15 * code that needs to enumerate or look up widget types.
16 *
17 * The shape is intentionally minimal: identity (`name`) plus the
18 * script-module handles the build pipeline produced for the widget.
19 * Placement and surface concerns (which page or sidebar uses the widget)
20 * live with the consumer, not on the type definition.
21 */
22 #[AllowDynamicProperties]
23 class WP_Widget_Type {
24
25 /**
26 * Allowed values for the `presentation` field. Treated as the
27 * single source of truth across the registry, REST schema, and
28 * any consumer that needs to validate or enumerate the set.
29 */
30 const PRESENTATION_VALUES = array( 'framed', 'content-bleed', 'full-bleed' );
31
32 /**
33 * Widget type key. Namespaced identifier, e.g. `core/hello-world`.
34 *
35 * @var string
36 */
37 public $name;
38
39 /**
40 * Script-module handle for the widget render module.
41 *
42 * Null when the widget folder did not ship a render entry point at
43 * build time.
44 *
45 * @var string|null
46 */
47 public $render_module = null;
48
49 /**
50 * Script-module handle for the widget metadata module.
51 *
52 * Null when the widget folder did not ship a widget entry point at
53 * build time.
54 *
55 * @var string|null
56 */
57 public $widget_module = null;
58
59 /**
60 * Authoring intent about how the widget wants to render. Static
61 * and declarative; not a user-editable attribute.
62 *
63 * One of {@see self::PRESENTATION_VALUES} (first entry is the
64 * default). Null when the widget did not declare the field.
65 *
66 * @var string|null
67 */
68 public $presentation = null;
69
70 /**
71 * Constructor.
72 *
73 * @param string $name Widget type name including namespace.
74 * @param array $args Optional. Widget type arguments. Each key is
75 * copied onto the corresponding object property.
76 * Default empty array.
77 */
78 public function __construct( $name, $args = array() ) {
79 $this->name = $name;
80 $this->set_props( $args );
81 }
82
83 /**
84 * Returns whether this widget type ships a renderable script module.
85 *
86 * @return bool
87 */
88 public function is_renderable() {
89 return ! empty( $this->render_module );
90 }
91
92 /**
93 * Hydrates the widget type properties from the args array.
94 *
95 * @param array $args Widget type arguments.
96 */
97 public function set_props( $args ) {
98 if ( ! is_array( $args ) ) {
99 return;
100 }
101
102 unset( $args['name'] );
103
104 foreach ( $args as $property_name => $property_value ) {
105 $this->$property_name = $property_value;
106 }
107 }
108 }
109 }
110