-
Notifications
You must be signed in to change notification settings - Fork 0
/
load-webcomponents_admin.php
335 lines (292 loc) · 9.82 KB
/
load-webcomponents_admin.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/**
* Initialize settings at admin_init hook
*/
function load_webcomponents_settings_init() {
// Register settings
register_setting(
'load_webcomponents_settings',
'load_webcomponents_settings',
array(
'sanitize_callback' => 'load_webcomponents_settings_sanitize_cb'
)
);
// Register section "Common Settings"
add_settings_section(
'load_webcomponents_settings_section_common',
__('Settings', 'load_webcomponents'),
'load_webcomponents_settings_section_common_cb',
'load_webcomponents_settings_page'
);
// Register field "Enable web components loading"
add_settings_field(
'load_webcomponents_setting_enable_loading',
__( 'Enable loading', 'load_webcomponents' ),
'load_webcomponents_setting_enable_loading_callback',
'load_webcomponents_settings_page',
'load_webcomponents_settings_section_common',
[
'label_for' => 'load_webcomponents_enable_loading',
]
);
// Register field "Allow src attribute"
add_settings_field(
'load_webcomponents_setting_allow_src',
__( 'Allow src attribute', 'load_webcomponents' ),
'load_webcomponents_setting_allow_src_callback',
'load_webcomponents_settings_page',
'load_webcomponents_settings_section_common',
[
'label_for' => 'load_webcomponents_allow_src',
]
);
// Register field "Web components list"
add_settings_field(
'load_webcomponents_setting_list',
__( 'List of available web components', 'load_webcomponents' ),
'load_webcomponents_setting_list_callback',
'load_webcomponents_settings_page',
'load_webcomponents_settings_section_common',
[
'label_for' => 'load_webcomponents_list',
]
);
}
add_action( 'admin_init', 'load_webcomponents_settings_init' );
// Callback function for section "Common Settings"
function load_webcomponents_settings_section_common_cb( $args ) {}
// Callback function for section "Web Components List"
function load_webcomponents_settings_section_list_cb( $args ) {}
/**
* Call back function for sanitization of settings data
*/
function load_webcomponents_settings_sanitize_cb($data) {
// Sanitize "enable loading"
if (isset($data['load_webcomponents_enable_loading']) && 1 != $data['load_webcomponents_enable_loading']) {
add_settings_error(
'requiredEnableLoadingCheckbox',
'empty',
__('Incorrent option.', 'load_webcomponents'),
'error'
);
unset( $data['load_webcomponents_enable_loading'] );
}
// Sanitize "allow src attribute"
if (isset($data['load_webcomponents_allow_src']) && 1 != $data['load_webcomponents_allow_src']) {
add_settings_error(
'requiredAllowSrcCheckbox',
'empty',
__('Incorrent option.', 'load_webcomponents'),
'error'
);
unset( $data['load_webcomponents_allow_src'] );
}
// Sanitize "allow src attribute"
if (! isset( $data['load_webcomponents_list'] ) || ! is_array( $data['load_webcomponents_list'] ) ) {
$data['load_webcomponents_list'] = array();
}
else {
$num_items = count( $data['load_webcomponents_list'] );
$items_removed_count = 0;
$error_set = array();
foreach ( $data['load_webcomponents_list'] as $index => $item ) {
// Sanitize Name
$data['load_webcomponents_list'][ $index ]['name'] = esc_html( $item['name'] );
// Sanitize URL
$data['load_webcomponents_list'][ $index ]['url'] = esc_url_raw( $item['url'] );
// Check if name AND url is set
if ( ! isset( $item['name'] ) || empty( $item['name'] )
|| ! isset( $item['url'] ) || empty( $item['url'] )
) {
unset( $data['load_webcomponents_list'][ $index ] );
// Old items
if ( $index+1 != $num_items ) {
$items_removed_count ++;
add_settings_error(
'itemRemovedFromList',
'empty',
__('Item removed from list.', 'load_webcomponents'),
'updated'
);
}
// New item
else {
if ( ! empty( $item['name'] ) || ! empty($item['url'] ) ) {
add_settings_error(
'requiredNameAndUrl',
'empty',
__('Please enter name <em>and</em> URL for the new web component.', 'load_webcomponents'),
'error'
);
}
}
}
// Check if name is unique
$unique = true;
foreach ( $data['load_webcomponents_list'] as $index2 => $item2 ) {
if ( isset( $item['name'] ) && isset( $item2['name'] ) && ! empty ( $item['name'] )
&& $index != $index2 && $item['name'] == $item2['name'] ) {
$error_id = 'name_' . esc_html( $item['name'] ) . '_not_unique';
if ( ! array_key_exists($error_id, $error_set) ) {
add_settings_error(
$error_id,
'empty',
sprintf( __('Name "%s" is not unique!', 'load_webcomponents'), esc_html( $item['name'] ) ),
'error'
);
$error_set[ $error_id ] = 1;
}
}
}
}
}
return $data;
}
/**
* Callback function for field "Enable loading"
*/
function load_webcomponents_setting_enable_loading_callback( $args ) {
// Load settings
$options = get_option( 'load_webcomponents_settings' );
// Field index
$id = esc_attr( $args['label_for'] );
?>
<input type="checkbox" id="<?php echo $id; ?>" name="load_webcomponents_settings[<?php echo $id; ?>]" value="1" <?php
checked( isset($options[$id]) );
?> />
<p class="description">
<?php _e('This is the main switch to (de)activate all web component loaded by this plugin.', 'load_webcomponents' ); ?>
</p>
<?php
}
/**
* Callback function for field "Allow src attribute"
*/
function load_webcomponents_setting_allow_src_callback( $args ) {
// Load settings
$options = get_option( 'load_webcomponents_settings' );
// Field index
$id = esc_attr( $args['label_for'] );
?>
<input type="checkbox" id="<?php echo $id; ?>" name="load_webcomponents_settings[<?php echo $id; ?>]" value="1" <?php
checked( isset($options[$id]) );
?> />
<p class="description">
<?php _e('Allow to add web components via the <code>src="<url>"</code> attribute.', 'load_webcomponents' ); ?>
</p>
<p class="description">
<?php esc_html_e('Standard and recommended method is to identify a component by its identifier and define the URL in the list below.', 'load_webcomponents' ); ?>
</p>
<?php
}
/**
* Callback function for field "Web components list"
*/
function load_webcomponents_setting_list_callback( $args ) {
// Load settings
$options = get_option( 'load_webcomponents_settings' );
// Field index
$id = esc_attr( $args['label_for'] );
// List array
$list = ( isset( $options[ $id ] ) && is_array( $options[ $id ] ) ) ? $options[ $id ] : array();
?>
<table>
<tr>
<th style="text-align:center;"><?php esc_html_e('#', 'load_webcomponents' ); ?></th>
<th style="text-align:center;"><?php esc_html_e('Identifier', 'load_webcomponents' ); ?></th>
<th style="text-align:center;"><?php esc_html_e('URL', 'load_webcomponents' ); ?></th>
</tr>
<?php
$count = 0;
foreach ( $list as $item ) {
load_webcomponents_settings_list_item( array(
'count' => $count,
'count_name' => $count + 1,
'name_value' => $item['name'],
'url_value' => $item['url'],
));
$count++;
}
load_webcomponents_settings_list_item( array(
'count' => $count,
'count_name' => __('New', 'load_webcomponents') . ':',
'name_value' => '',
'url_value' => '',
));
?>
</table>
<p class="description">
<?php esc_html_e('To delete an item from the list, just delete the identifier and/or URL of the item', 'load_webcomponents' ); ?>
</p>
<?php
}
function load_webcomponents_settings_list_item( $args ) {
if (! isset( $args['count'] ) || ! isset( $args['name_value'] ) || ! isset( $args['url_value'] ) ) {
return;
}
$count = (int) $args['count'];
?>
<tr>
<td style="text-align:center;">
<?php esc_html_e( $args['count_name'] ); ?>
</td>
<td>
<input type="text" name="load_webcomponents_settings[load_webcomponents_list][<?php echo $count; ?>][name]" value="<?php esc_html_e( $args['name_value']); ?>" />
</td>
<td>
<input type="text" name="load_webcomponents_settings[load_webcomponents_list][<?php echo $count; ?>][url]" value="<?php esc_html_e( $args['url_value']); ?>" size="60" />
</td>
</tr>
<?php
}
/**
* Register load_webcomponents_settings_page to the admin_menu action hook
*/
function load_webcomponents_settings_page() {
// Add settings page
add_options_page(
'Load Web Components',
'Load Web Components',
'manage_options',
'load_webcomponents',
'load_webcomponents_settings_page_html'
);
}
add_action( 'admin_menu', 'load_webcomponents_settings_page' );
/**
* Output settings page
*/
function load_webcomponents_settings_page_html() {
// Check user capabilities
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<h2><?php _e('Usage', 'load_webcomponents' ); ?></h2>
<p><?php _e('Load a web component or JavaScript library to a post or page just by adding a shortcode to the content. There are two types of shortcodes available:', 'load_webcomponents' ); ?></p>
<ol>
<li><code>[load-webcompontent src="<url>"]</code> − <?php
printf( __('where %s is the URL of the web component or JavaScript library.', 'load_webcomponents' ), '<code><url></code>');
?></li>
<li><code>[load-webcompontent name="<identifier>"]</code> − <?php
printf( __('where %s is the name of the web component and the URL is taken from the list below.', 'load_webcomponents' ), '<code><identifier></code>');
?></li>
</ol>
<p><?php _e('The type with <code>name="<identifier></code> is recommended, because if the URL of a component changes, it has be changed only on this settings page, and not on each post or page using the component.', 'load_webcomponents' ); ?></p>
<br />
<br />
<form action="options.php" method="post">
<?php
// Output security fields
settings_fields( 'load_webcomponents_settings' );
// Output setting sections and their fields
do_settings_sections( 'load_webcomponents_settings_page' );
// Output save settings button
submit_button();
?>
</form>
</div>
<?php
}