-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathclass-starter-content-generated.php
More file actions
602 lines (524 loc) · 17.4 KB
/
Copy pathclass-starter-content-generated.php
File metadata and controls
602 lines (524 loc) · 17.4 KB
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
<?php
/**
* Randomly generated starter content.
*
* @package Newspack
*/
namespace Newspack;
use WP_Error;
defined( 'ABSPATH' ) || exit;
/**
* Randomly generated Lorem Ipsum starter content.
*/
class Starter_Content_Generated extends Starter_Content_Provider {
/**
* Starter categories.
*
* @var array A set of starter categories.
*/
protected static $starter_categories = [ 'Featured', 'Sports', 'Entertainment', 'Opinion', 'News', 'Events', 'Longform', 'Arts', 'Politics', 'Science', 'Tech', 'Health' ];
/**
* Prefix for starter categories.
*
* @var string
*/
private static $starter_category_prefix = '_newspack_';
/**
* Initialize starter content provider.
*
* @param array $args Extra params. Currently unused in this provider.
* @return bool True.
*/
public static function initialize( $args = [] ) {
self::create_categories();
return true;
}
/**
* Create the Xth starter post.
*
* @param int $post_index Index of the post to create.
* @return int Post ID of existing post at that index or newly created post at that index.
*/
public static function create_post( $post_index ) {
$existing_post_id = self::get_starter_post( $post_index );
if ( $existing_post_id ) {
wp_trash_post( $existing_post_id );
}
if ( ! function_exists( 'wp_insert_post' ) ) {
require_once ABSPATH . 'wp-admin/includes/post.php';
}
$page_templates = [ '', 'single-feature.php' ];
$paragraphs = explode( PHP_EOL, self::get_lipsum( 'paras', 5 ) );
$title = self::generate_title( $post_index );
$post_data = [
'post_title' => $title,
'post_name' => sanitize_title_with_dashes( $title, '', 'save' ),
'post_status' => 'publish',
'post_content' => html_entity_decode(
implode(
'',
array_map(
function( $paragraph ) {
return strlen( trim( $paragraph ) ) ?
self::create_block( 'paragraph', null, '<p>' . $paragraph . '</p>' ) :
'';
},
$paragraphs
)
)
),
'post_excerpt' => ucfirst( self::get_lipsum( 'words', wp_rand( 20, 30 ) ) ) . '.',
'meta_input' => [
'newspack_post_subtitle' => self::generate_title() . '.',
],
];
if ( Starter_Content::is_e2e() ) {
$post_data['post_date'] = '2020-03-03 10:00:00';
}
$post_id = wp_insert_post( $post_data );
$page_template_id = Starter_Content::is_e2e() ? ( $post_index % 2 ) : wp_rand( 0, 1 );
$page_template = $page_templates[ $page_template_id ];
$update = [
'ID' => $post_id,
'page_template' => $page_template,
];
wp_update_post( $update );
$newspack_featured_image_positions = [ null, 'behind', 'beside' ];
$newspack_featured_image_index = Starter_Content::is_e2e() ? ( $post_index % 3 ) : wp_rand( 0, 2 );
$newspack_featured_image_position = $newspack_featured_image_positions[ $newspack_featured_image_index ];
if ( $newspack_featured_image_position ) {
add_post_meta( $post_id, 'newspack_featured_image_position', $newspack_featured_image_position );
}
$attachment_id = self::add_featured_image( $post_id, $post_index );
$categories = get_categories(
[
'orderby' => 'count',
'order' => 'ASC',
'hide_empty' => false,
// Exclude 'Uncategorized' which has ID of 1.
'exclude' => '1', // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
'number' => 1,
]
);
$category_ids = self::get_starter_categories_ids();
$category_id = Starter_Content::is_e2e() ? $category_ids[ $post_index ] : $categories[0];
wp_set_post_categories( $post_id, $category_id );
// Set Yoast primary category.
update_post_meta( $post_id, '_yoast_wpseo_primary_category', $category_id->term_id );
wp_publish_post( $post_id );
self::mark_starter_post( $post_id, $post_index );
return $post_id;
}
/**
* Create starter categories.
*
* @return array Array of category IDs.
*/
public static function create_categories() {
if ( ! function_exists( 'wp_create_category' ) ) {
require_once ABSPATH . 'wp-admin/includes/taxonomy.php';
}
self::remove_starter_categories();
$category_ids = array_map(
function( $category ) {
$created_category = wp_insert_term( $category, 'category', [ 'slug' => self::$starter_category_prefix . $category ] );
return $created_category['term_id'];
},
self::$starter_categories
);
return $category_ids;
}
/**
* Create the starter homepage.
*
* @return int Post ID of existing starter homepage or newly created starter homepage.
*/
public static function create_homepage() {
$existing_homepage_id = self::get_starter_homepage();
if ( $existing_homepage_id ) {
wp_delete_post( $existing_homepage_id, true );
}
if ( ! function_exists( 'wp_insert_post' ) ) {
require_once ABSPATH . 'wp-admin/includes/post.php';
}
$categories = [];
foreach ( self::get_starter_categories_ids() as $category_id ) {
$categories[] = (object) [
'term_id' => $category_id,
'name' => get_term( $category_id )->name,
];
}
ob_start();
?>
<!-- wp:columns {"className":"is-style-borders"} --><div class="wp-block-columns is-style-borders">
<!-- wp:column {"width":66.66} --><div class="wp-block-column" style="flex-basis:66.66%">
<!-- wp:newspack-blocks/homepage-articles {"postsToShow":1,"categories":[<?php echo esc_attr( $categories[1]->term_id ); ?>],"sectionHeader":"<?php echo esc_html( $categories[1]->name ); ?>"} /-->
</div><!-- /wp:column -->
<!-- wp:column {"width":33.33} --><div class="wp-block-column" style="flex-basis:33.33%">
<!-- wp:newspack-blocks/homepage-articles {"showExcerpt":false,"imageShape":"square","showAvatar":false,"postsToShow":3,"mediaPosition":"left","categories":[<?php echo esc_attr( $categories[2]->term_id ); ?>],"typeScale":2,"imageScale":1,"sectionHeader":"<?php echo esc_html( $categories[2]->name ); ?>"} /-->
<!-- wp:separator {"className":"is-style-wide"} --><hr class="wp-block-separator is-style-wide"/><!-- /wp:separator -->
<!-- wp:newspack-blocks/homepage-articles {"showExcerpt":false,"imageShape":"square","postsToShow":3, "mediaPosition":"left","categories":[<?php echo esc_attr( $categories[3]->term_id ); ?>],"typeScale":2,"imageScale":1,"sectionHeader":"<?php echo esc_html( $categories[3]->name ); ?>"} /-->
</div><!-- /wp:column -->
</div><!-- /wp:columns -->
<!-- wp:separator {"className":"is-style-wide"} --><hr class="wp-block-separator is-style-wide"/><!-- /wp:separator -->
<!-- wp:columns {"className":"is-style-borders"} --><div class="wp-block-columns is-style-borders">
<!-- wp:column --><div class="wp-block-column">
<!-- wp:newspack-blocks/homepage-articles {"className":"is-style-borders","showExcerpt":false,"showAuthor":false,"postsToShow":1,"categories":[<?php echo esc_attr( $categories[4]->term_id ); ?>],"typeScale":3,"imageScale":1,"sectionHeader":"<?php echo esc_html( $categories[4]->name ); ?>"} /-->
<!-- wp:newspack-blocks/homepage-articles {"className":"is-style-borders","showExcerpt":false,"showAuthor":false,"showAvatar":false,"postsToShow":2,"mediaPosition":"left","categories":[<?php echo esc_attr( $categories[5]->term_id ); ?>],"typeScale":2,"imageScale":1} /-->
</div><!-- /wp:column -->
<!-- wp:column --><div class="wp-block-column">
<!-- wp:newspack-blocks/homepage-articles {"className":"is-style-borders","showExcerpt":false,"showAuthor":false,"postsToShow":1,"categories":[<?php echo esc_attr( $categories[6]->term_id ); ?>],"typeScale":3,"imageScale":1,"sectionHeader":"<?php echo esc_html( $categories[6]->name ); ?>"} /-->
<!-- wp:newspack-blocks/homepage-articles {"className":"is-style-borders","showExcerpt":false,"showAuthor":false,"showAvatar":false,"postsToShow":2,"mediaPosition":"left","categories":[<?php echo esc_attr( $categories[7]->term_id ); ?>],"typeScale":2,"imageScale":1} /-->
</div><!-- /wp:column -->
<!-- wp:column --><div class="wp-block-column">
<!-- wp:newspack-blocks/homepage-articles {"className":"is-style-borders","showExcerpt":false,"showImage":false,"showAuthor":false,"showAvatar":false,"postsToShow":1,"mediaPosition":"left","categories":[<?php echo esc_attr( $categories[8]->term_id ); ?>],"imageScale":1,"sectionHeader":"<?php echo esc_html( $categories[8]->name ); ?>"} /-->
<!-- wp:newspack-blocks/homepage-articles {"className":"is-style-borders","showExcerpt":false,"showImage":false,"showAuthor":false,"showAvatar":false,"postsToShow":4,"mediaPosition":"left","categories":[<?php echo esc_attr( $categories[9]->term_id ); ?>],"typeScale":3,"imageScale":1} /-->
</div><!-- /wp:column -->
</div><!-- /wp:columns -->
<?php
$content = ob_get_clean();
$post_data = [
'post_title' => 'Homepage',
'post_type' => 'page',
'post_content' => $content,
];
$page_id = wp_insert_post( $post_data );
wp_publish_post( $page_id );
update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $page_id );
set_theme_mod( 'hide_front_page_title', true );
self::mark_starter_homepage( $page_id );
return $page_id;
}
/**
* Generate a post title
*
* @param int $post_index The index of the created post.
* @return string The title.
*/
public static function generate_title( $post_index = 0 ) {
if ( Starter_Content::is_e2e() ) {
return $post_index . ' ' . file_get_contents( NEWSPACK_ABSPATH . 'includes/raw_assets/markup/title.txt' ); // phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown -- Reads a bundled plugin asset from the local filesystem.
}
$title = self::get_lipsum( 'words', wp_rand( 7, 14 ) );
$title = ucfirst( strtolower( str_replace( '.', '', $title ) ) ); // Remove periods, convert to sentence case.
return $title;
}
/**
* Lorem ipsum word list used by {@see self::get_lipsum()}.
*
* @var string[]
*/
private const LIPSUM_WORDS = [
'lorem',
'ipsum',
'dolor',
'sit',
'amet',
'consectetur',
'adipiscing',
'elit',
'sed',
'do',
'eiusmod',
'tempor',
'incididunt',
'ut',
'labore',
'et',
'dolore',
'magna',
'aliqua',
'enim',
'ad',
'minim',
'veniam',
'quis',
'nostrud',
'exercitation',
'ullamco',
'laboris',
'nisi',
'aliquip',
'ex',
'ea',
'commodo',
'consequat',
'duis',
'aute',
'irure',
'in',
'reprehenderit',
'voluptate',
'velit',
'esse',
'cillum',
'eu',
'fugiat',
'nulla',
'pariatur',
'excepteur',
'sint',
'occaecat',
'cupidatat',
'non',
'proident',
'sunt',
'culpa',
'qui',
'officia',
'deserunt',
'mollit',
'anim',
'id',
'est',
'laborum',
'at',
'vero',
'eos',
'accusamus',
'accusantium',
'doloremque',
'laudantium',
'totam',
'rem',
'aperiam',
'eaque',
'ipsa',
'quae',
'ab',
'illo',
'inventore',
'veritatis',
'quasi',
'architecto',
'beatae',
'vitae',
'dicta',
'explicabo',
'nemo',
'ipsam',
'voluptas',
'aspernatur',
'odit',
'aut',
'sequi',
'nesciunt',
'neque',
'porro',
'quisquam',
'dolorem',
'adipisci',
'numquam',
'eius',
'modi',
'tempora',
'incidunt',
'magnam',
'quaerat',
'etiam',
'minima',
'nostrum',
'exercitationem',
'corporis',
'suscipit',
'laboriosam',
'aliquid',
'commodi',
'consequatur',
'quo',
'vel',
'illum',
'eum',
'iure',
];
/**
* Generate dummy text.
*
* @param string $type The type of Lorem Ipsum to retrieve: paras|words.
* @param int $amount The number of items to retrieve.
* @return string Lorem Ipsum.
*/
public static function get_lipsum( $type, $amount ) {
if ( Starter_Content::is_e2e() ) {
return file_get_contents( NEWSPACK_ABSPATH . 'includes/raw_assets/markup/body.txt' ); // phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown -- Reads a bundled plugin asset from the local filesystem.
}
if ( 'paras' === $type ) {
return implode( PHP_EOL, self::lipsum_paragraphs( $amount ) );
}
return implode( ' ', self::lipsum_words( $amount ) );
}
/**
* Pick $count random words from the lorem ipsum pool.
*
* @param int $count Number of words to return.
* @return string[]
*/
private static function lipsum_words( $count ) {
$max = count( self::LIPSUM_WORDS ) - 1;
$output = [];
for ( $i = 0; $i < $count; $i++ ) {
$output[] = self::LIPSUM_WORDS[ wp_rand( 0, $max ) ];
}
return $output;
}
/**
* Build $count lorem ipsum paragraphs.
*
* @param int $count Number of paragraphs to return.
* @return string[]
*/
private static function lipsum_paragraphs( $count ) {
$paragraphs = [];
for ( $i = 0; $i < $count; $i++ ) {
$sentences = [];
$num = wp_rand( 4, 7 );
for ( $j = 0; $j < $num; $j++ ) {
$words = self::lipsum_words( wp_rand( 6, 14 ) );
$words[0] = ucfirst( $words[0] );
$sentences[] = implode( ' ', $words ) . '.';
}
$paragraphs[] = implode( ' ', $sentences );
}
return $paragraphs;
}
/**
* Create a Gutenberg Block
*
* @param string $block_type The block type.
* @param array $attributes The attributes of the block.
* @param string $content The content of the block.
*
* @return string Block markup.
*/
private static function create_block( $block_type = null, $attributes = null, $content = '' ) {
return sprintf(
'<!-- wp:%s %s -->%s<!-- /wp:%s -->',
$block_type,
$attributes ? wp_json_encode( $attributes ) : '',
$content,
$block_type
);
}
/**
* Add a featured image to a post.
*
* @param int $post_id The post ID.
* @param int $post_index The index of the post within the set of starter content posts.
*
* @return string Block markup.
*/
private static function add_featured_image( $post_id, $post_index ) {
if ( ! function_exists( 'wp_crop_image' ) ) {
require_once ABSPATH . 'wp-admin/includes/image.php';
}
$filename = Starter_Content::is_e2e() ? self::copy_bundled_image() : self::download_random_image();
if ( ! $filename ) {
return null;
}
$filetype = wp_check_filetype( basename( $filename ), null );
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit',
);
$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
}
/**
* Copy the bundled starter content image into the uploads directory.
*
* E2E provisioning creates dozens of posts in a single run and they all share this
* image, so it ships with the plugin: nothing on that path depends on a third-party
* image host being reachable.
*
* @return string|null Path to the copy in the uploads directory, or null on failure.
*/
private static function copy_bundled_image() {
if ( ! function_exists( 'wp_upload_bits' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$file = wp_upload_bits(
'automated_upload.jpg',
null,
file_get_contents( NEWSPACK_ABSPATH . 'includes/raw_assets/images/starter-content-featured-image.jpg' ) // phpcs:ignore WordPressVIPMinimum.Performance.FetchingRemoteData.FileGetContentsUnknown -- Reads a bundled plugin asset from the local filesystem.
);
if ( ! $file || ! empty( $file['error'] ) || empty( $file['file'] ) ) {
return null;
}
return $file['file'];
}
/**
* Download a random image and place it in the uploads directory.
*
* @return string|null Path to the downloaded file in the uploads directory, or null on failure.
*/
private static function download_random_image() {
if ( ! function_exists( 'download_url' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}
$temp_file = download_url( 'https://picsum.photos/1200/800' );
if ( is_wp_error( $temp_file ) ) {
return null;
}
// wp_handle_sideload() takes $file by reference and writes back into it, so the
// array has to be a variable. Passing the literal is a fatal on PHP 8, which took
// down every non-E2E starter-content post before it could get a featured image.
$file = [
'name' => 'automated_upload.jpg',
'type' => mime_content_type( $temp_file ),
'tmp_name' => $temp_file,
'error' => 0,
'size' => filesize( $temp_file ),
];
$file_attributes = wp_handle_sideload(
$file,
[
'test_form' => false,
'test_size' => true,
'test_upload' => true,
]
);
if ( is_wp_error( $file_attributes ) || ! empty( $file_attributes['error'] ) ) {
return null;
}
return $file_attributes['file'];
}
/**
* Remove all starter content.
*/
public static function remove_starter_content() {
self::remove_starter_categories();
parent::remove_starter_content();
}
/**
* Get starter categories IDs.
*/
private static function get_starter_categories_ids() {
global $wpdb;
$category_ids = array_column(
$wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->prepare(
"SELECT term_id FROM $wpdb->terms WHERE slug LIKE %s",
self::$starter_category_prefix . '%'
)
),
'term_id'
);
return $category_ids;
}
/**
* Removes starter categories.
*/
public static function remove_starter_categories() {
foreach ( self::get_starter_categories_ids() as $category_id ) {
wp_delete_category( $category_id );
}
}
}