-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclass.php
132 lines (123 loc) · 3.4 KB
/
class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* Class WP_JS_Widget_Text.
*
* @package JS_Widgets
*/
/**
* Class WP_JS_Widget_Text
*
* @package JS_Widgets
*/
class WP_JS_Widget_Text extends WP_Adapter_JS_Widget {
/**
* Adapted widget.
*
* @var WP_Widget_Text
*/
public $adapted_widget;
/**
* Get instance schema properties.
*
* @return array Schema.
*/
public function get_item_schema() {
$schema = array_merge(
parent::get_item_schema(),
array(
'text' => array(
'description' => __( 'The content for the widget.', 'js-widgets' ),
'type' => array( 'string', 'object' ),
'context' => array( 'view', 'edit', 'embed' ),
'properties' => array(
'raw' => array(
'description' => __( 'Content for the widget, as it exists in the database.', 'js-widgets' ),
'type' => 'string',
'context' => array( 'edit' ),
'required' => true,
'default' => '',
),
'rendered' => array(
'description' => __( 'HTML content for the widget, transformed for display.', 'js-widgets' ),
'type' => 'string',
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
),
),
'filter' => array(
'description' => __( 'Whether paragraphs will be added for double line breaks (wpautop).', 'js-widgets' ),
'type' => 'boolean',
'default' => false,
'context' => array( 'edit' ),
),
)
);
$schema['title']['properties']['raw']['default'] = '';
return $schema;
}
/**
* Render a widget instance for a REST API response.
*
* Map the instance data to the REST resource fields and add rendered fields.
*
* @inheritdoc
*
* @param array $instance Raw database instance.
* @param WP_REST_Request $request REST request.
* @return array Widget item.
*/
public function prepare_item_for_response( $instance, $request ) {
$instance = array_merge( $this->get_default_instance(), $instance );
/** This filter is documented in src/wp-includes/widgets/class-wp-widget-text.php */
$content_rendered = apply_filters( 'widget_text', $instance['text'], $instance, $this->adapted_widget );
if ( ! empty( $instance['filter'] ) ) {
$content_rendered = wpautop( $content_rendered );
}
$item = array_merge(
parent::prepare_item_for_response( $instance, $request ),
array(
'text' => array(
'raw' => $instance['text'],
'rendered' => $content_rendered,
),
'filter' => ! empty( $instance['filter'] ),
)
);
return $item;
}
/**
* Render JS template contents minus the `<script type="text/template">` wrapper.
*/
public function render_form_template() {
$this->render_title_form_field_template();
$this->render_form_field_template( array(
'name' => 'text',
'label' => __( 'Content:', 'default' ),
'type' => 'textarea',
'rows' => 16,
'cols' => 20,
) );
$this->render_form_field_template( array(
'name' => 'filter',
'label' => __( 'Automatically add paragraphs', 'default' ),
'type' => 'checkbox',
) );
}
/**
* Get configuration data for the form.
*
* @return array
*/
public function get_form_args() {
$args = parent::get_form_args();
$args['can_unfiltered_html'] = current_user_can( 'unfiltered_html' );
$args['l10n'] = array_merge(
$args['l10n'],
array(
'text_unfiltered_html_invalid' => __( 'Protected HTML such as script tags will be stripped from the content.', 'js-widgets' ),
)
);
return $args;
}
}