forked from ampproject/amp-wp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-test-widgets-to-sidebar.php
467 lines (446 loc) · 12.3 KB
/
add-test-widgets-to-sidebar.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<?php
/**
* Adds an instance of every default WordPress widget to the first registered sidebar.
*
* @codeCoverageIgnore
* @package AMP
*/
/**
* Creates widgets, and adds them to a sidebar.
*
* @return string $sidebar The sidebar to which the widgets were added.
*/
function amp_populate_sidebar() {
global $wp_registered_sidebars;
$widgets = amp_get_widgets();
$sidebar = amp_get_first_sidebar();
$created_widgets = array();
foreach ( $widgets as $widget ) {
if ( ! amp_widget_already_in_sidebar( $widget, $sidebar ) ) {
amp_create_widget( $widget );
$created_widgets[] = amp_get_widget_id( $widget['widget'] );
}
}
amp_add_widgets_to_sidebar( $created_widgets, $sidebar );
return isset( $wp_registered_sidebars[ $sidebar ]['name'] ) ? $wp_registered_sidebars[ $sidebar ]['name'] : $sidebar;
}
/**
* Gets the widget data, in order to create them.
*
* @return array $widgets Data for the widgets.
*/
function amp_get_widgets() {
return array(
array(
'widget' => 'media_audio',
'settings' => array_merge(
amp_media_widget( 'audio' ),
array(
'title' => 'Test Audio Widget: No Loop or Preload',
)
),
),
array(
'widget' => 'media_audio',
'settings' => array_merge(
amp_media_widget( 'audio' ),
array(
'title' => 'Test Audio Widget: With Loop, Preload',
'loop' => true,
'preload' => 'auto',
)
),
),
array(
'widget' => 'archives',
'settings' => array(
'title' => 'Test Archives Widget: No Dropdown or Count',
),
),
array(
'widget' => 'archives',
'settings' => array(
'title' => 'Test Archives Widget: With Dropdown, Count',
'count' => 1,
'dropdown' => 1,
),
),
array(
'widget' => 'calendar',
),
array(
'widget' => 'categories',
'settings' => array(
'title' => 'Test Categories Widget: No Count, Dropdown, or Hierarchical',
),
),
array(
'widget' => 'categories',
'settings' => array(
'title' => 'Test Categories Widget: With Count',
'count' => 1,
),
),
array(
'widget' => 'categories',
'settings' => array(
'title' => 'Test Categories Widget: With Count, Dropdown, And Hierarchical',
'count' => 1,
'dropdown' => 1,
'hierarchical' => 1,
),
),
array(
'widget' => 'custom_html',
'settings' => array(
'title' => 'Test Custom HTML Widget',
'content' => '<h2>Example Custom HTML widget</h2><article>Here is some custom HTML with a <a href="http://example.com/m">link</a></article>',
),
),
array(
'widget' => 'media_gallery',
'settings' => array_merge(
amp_gallery_widget(),
array(
'link_type' => 'post',
'orderby_random' => false,
'size' => 'thumbnail',
)
),
),
array(
'widget' => 'media_image',
'settings' => array_merge(
amp_image_widget(),
array(
'alt' => 'Example alt value',
'caption' => 'Example caption',
'image_classes' => 'foo bar',
'image_title' => 'example image title',
'link_classes' => 'baz bar',
'link_rel' => 'nofollow',
'link_target_blank' => false,
'link_type' => 'custom',
'link_url' => 'http://example.com/amp-test',
'size' => 'full',
)
),
),
array(
'widget' => 'meta',
),
array(
'widget' => 'nav_menu',
'settings' => array(
'nav_menu' => amp_menu(),
),
),
array(
'widget' => 'pages',
),
array(
'widget' => 'recent-comments',
),
array(
'widget' => 'recent-posts',
'settings' => array(
'show_date' => true,
),
),
array(
'widget' => 'rss',
'settings' => array(
'title' => 'Test RSS Widget: No Content, Author, or Date',
'url' => 'https://amphtml.wordpress.com/feed/',
'show_author' => 0,
'show_date' => 0,
'show_summary' => 0,
),
),
array(
'widget' => 'rss',
'settings' => array(
'title' => 'Test RSS Widget: With Content, Author, Date',
'url' => 'https://amphtml.wordpress.com/feed/',
'show_author' => 1,
'show_date' => 1,
'show_summary' => 1,
),
),
array(
'widget' => 'search',
),
array(
'widget' => 'tag_cloud',
'settings' => array(
'title' => 'Test Tag Widget, No Count',
),
),
array(
'widget' => 'tag_cloud',
'settings' => array(
'title' => 'Test Tag Widget, With Count',
'count' => 1,
),
),
array(
'widget' => 'text',
'settings' => array(
'filter' => true,
'text' => '<strong>Example Headline</strong><ul><li>This is to test possible text</li><li>This should display as expected</li></ul>',
'visual' => true,
),
),
array(
'widget' => 'media_video',
'settings' => array_merge(
amp_media_widget( 'video' ),
array(
'title' => 'Test Video Widget: No Loop or Preload',
'loop' => false,
'preload' => 'none',
)
),
),
array(
'widget' => 'media_video',
'settings' => array_merge(
amp_media_widget( 'video' ),
array(
'title' => 'Test Video Widget: With Loop, Preload',
'loop' => true,
'preload' => 'metadata',
)
),
),
);
}
/**
* Get the settings for a media widget.
*
* Can apply to 'audio' or 'video' widgets.
* Queries for a single post of the media type.
* If none exists, amp_media() will throw an error.
*
* @param string $type The type of media. like 'audio' or 'video'.
* @return array $audio_widget The settings for the media widget.
*/
function amp_media_widget( $type ) {
$all_media = amp_media( $type, 1 );
$media = reset( $all_media );
return array(
'attachment_id' => $media->ID,
'url' => $media->guid,
);
}
/**
* Get the settings for the gallery widget.
*
* @return array $audio_widget The settings for the gallery widget.
*/
function amp_gallery_widget() {
$images = amp_media( 'image', 3 );
return array(
'ids' => wp_list_pluck( $images, 'ID' ),
);
}
/**
* Gets the settings for the image widget.
*
* @return array $image_widget The settings for the image widget.
*/
function amp_image_widget() {
$all_images = amp_media( 'image', 1 );
$image = reset( $all_images );
$metadata = wp_get_attachment_metadata( $image->ID );
$default_dimension = 100;
return array(
'attachment_id' => $image->ID,
'height' => isset( $metadata['height'] ) ? $metadata['height'] : $default_dimension,
'width' => isset( $metadata['width'] ) ? $metadata['width'] : $default_dimension,
'url' => wp_get_attachment_url( $image->ID ),
);
}
/**
* Get media items of a certain type.
*
* @throws Exception If not enough posts exist.
* @param integer $type The post_mime_type of the media item.
* @param integer $count The number of images for which to query.
* @return array|WP_CLI::error The media IDs, or an error on failure.
*/
function amp_media( $type, $count = 3 ) {
$query = new \WP_Query(
array(
'post_type' => 'attachment',
'post_mime_type' => $type,
'post_status' => 'inherit',
'posts_per_page' => $count,
)
);
if ( $query->post_count < $count ) {
throw new Exception(
sprintf(
'Please ensure at least %1$s "%2$s" attachments are accessible and run this again. There are currently only %3$s.',
$count,
$type,
$query->found_posts
)
);
}
return $query->get_posts();
}
/**
* Gets a menu ID, if it has more than 4 items.
*
* Iterates through all of the menus.
* And returns the first that has 4 items.
*
* @throws Exception When there is no menu with 4 items.
* @return integer|WP_CLI::error $menu_id The menu ID, or an error if no menu has at least 4 items.
*/
function amp_menu() {
$menus = wp_get_nav_menus();
$minimum_count = 4;
foreach ( $menus as $menu ) {
if ( $menu->count >= $minimum_count ) {
return $menu->term_id;
}
}
throw new Exception( 'Please add at least 4 items to a menu.' );
}
/**
* Whether a widget with the same values exists in the sidebar.
*
* Iterate through all of the widgets in the sidebar.
* Find all of the widgets with the same ID base, like 'text.'
* Then, find if one of those has the same settings as the new widget settings.
*
* @param array $widget The settings of the widget.
* @param string $sidebar The slug of the sidebar.
* @return boolean $in_sidebar Whether the widget is in the sidebar.
*/
function amp_widget_already_in_sidebar( $widget, $sidebar ) {
$sidebars = wp_get_sidebars_widgets();
if ( empty( $sidebars[ $sidebar ] ) ) {
return false;
}
$id_base = $widget['widget'];
$all_widget_data = get_option( 'widget_' . $id_base, array() );
foreach ( $sidebars[ $sidebar ] as $possible_widget ) {
if ( false !== strpos( $possible_widget, $id_base ) ) {
/*
* If there aren't any settings for the widget, any instance of it is enough.
* For example, a 'Pages' widget.
*/
if ( ! isset( $widget['settings'] ) ) {
return true;
}
preg_match( '/\d+/', $possible_widget, $matches );
$id = $matches[0];
if ( isset( $all_widget_data[ $id ] ) ) {
$widget_data = $all_widget_data[ $id ];
} else {
continue;
}
// Find if all of the settings in $widget['settings'] are present in the widget that's already in the sidebar.
if ( array() === array_diff_assoc( array_map( 'serialize', $widget['settings'] ), array_map( 'serialize', $widget_data ) ) ) {
return true;
}
}
}
return false;
}
/**
* Creates a widget, based on the passed settings.
*
* @param array $widget The data for the widget.
* @return void.
*/
function amp_create_widget( $widget ) {
if ( ! isset( $widget['widget'] ) ) {
return;
}
$id_base = $widget['widget'];
$option_key = 'widget_' . $id_base;
$widgets = get_option( $option_key, array() );
$settings = isset( $widget['settings'] ) ? $widget['settings'] : array();
if ( ! isset( $settings['title'] ) ) {
$title = str_replace( '_', ' ', $id_base );
$title = str_replace( '-', ' ', $title );
$settings['title'] = sprintf( 'Test %s Widget', ucwords( $title ) );
}
$number = 1;
unset( $widgets['_multiwidget'] );
if ( ! empty( $widgets ) ) {
$number = max( array_keys( $widgets ) );
$number = max( 1, $number );
}
$number++;
$widgets[ $number ] = $settings;
update_option( $option_key, $widgets );
}
/**
* Gets the first registered sidebar.
*
* @throws Exception When there is no registered sidebar.
* @return string|WP_CLI::error The first registered sidebar on success; error otherwise.
*/
function amp_get_first_sidebar() {
$sidebar = reset( $GLOBALS['wp_registered_sidebars'] );
if ( ! isset( $sidebar['id'] ) ) {
throw new Exception( 'Please make sure at least one sidebar is registered.' );
}
return $sidebar['id'];
}
/**
* Gets a widget ID, based on its name.
*
* Mainly copied from import_theme_starter_content().
* Increments the ID, based on the number of existing widgets.
*
* @see WP_Customize_Manager::import_theme_starter_content().
* @param string $id_base The ID base of the widget, like 'text'.
* @return string $widget_id The ID of the widget, like 'text-2'.
*/
function amp_get_widget_id( $id_base ) {
$settings = get_option( "widget_{$id_base}", array() );
if ( $settings instanceof ArrayObject || $settings instanceof ArrayIterator ) {
$settings = $settings->getArrayCopy();
}
// Get the widget's maximum widget number.
$widget_numbers = array_keys( $settings );
if ( count( $widget_numbers ) > 0 ) {
$widget_numbers[] = 1;
$widget_number = call_user_func_array( 'max', $widget_numbers );
} else {
$widget_number = 1;
}
return sprintf( '%s-%d', $id_base, $widget_number );
}
/**
* Adds the newly-created widgets to the sidebar.
*
* @param array $widgets The widget IDs to add to the sidebar.
* @param string $sidebar The sidebar to which to add the widget, like sidebar-1.
* @return void
*/
function amp_add_widgets_to_sidebar( $widgets, $sidebar ) {
$sidebars = wp_get_sidebars_widgets();
foreach ( $widgets as $widget ) {
$sidebars[ $sidebar ][] = $widget;
}
update_option( 'sidebars_widgets', $sidebars );
}
// Bootstrap the file.
if ( defined( 'WP_CLI' ) ) {
try {
$sidebar = amp_populate_sidebar();
WP_CLI::success( sprintf( 'Please visit a page with the sidebar: %s.', esc_html( $sidebar ) ) );
} catch ( Exception $e ) {
WP_CLI::error( $e->getMessage() );
}
} else {
echo 'Must be run in WP-CLI via: wp eval-file bin/add-test-widgets-to-sidebar.php.';
exit( 1 );
}