-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfunctions.php
More file actions
664 lines (574 loc) · 20.5 KB
/
functions.php
File metadata and controls
664 lines (574 loc) · 20.5 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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
<?php
namespace WordPressdotorg\Theme\Theme_Directory_2024;
const THEME_POST_TYPE = 'repopackage';
require_once( __DIR__ . '/inc/block-bindings.php' );
require_once( __DIR__ . '/inc/block-config.php' );
require_once( __DIR__ . '/inc/embed.php' );
require_once( __DIR__ . '/inc/i18n.php' );
require_once( __DIR__ . '/inc/seo-social-meta.php' );
// Block files
require_once( __DIR__ . '/src/business-model-notice/index.php' );
require_once( __DIR__ . '/src/child-theme-notice/index.php' );
require_once( __DIR__ . '/src/meta-list/index.php' );
require_once( __DIR__ . '/src/theme-available-translations/index.php' );
require_once( __DIR__ . '/src/theme-downloads/index.php' );
require_once( __DIR__ . '/src/theme-patterns/index.php' );
require_once( __DIR__ . '/src/theme-previewer/index.php' );
require_once( __DIR__ . '/src/theme-previewer-settings/index.php' );
require_once( __DIR__ . '/src/theme-settings/index.php' );
require_once( __DIR__ . '/src/theme-status-notice/index.php' );
require_once( __DIR__ . '/src/theme-style-variations/index.php' );
require_once( __DIR__ . '/src/theme-style-variations-items/index.php' );
require_once( __DIR__ . '/src/theme-upload-form/index.php' );
/**
* Actions and filters.
*/
add_action( 'init', __NAMESPACE__ . '\fix_term_imports' );
add_action( 'init', __NAMESPACE__ . '\set_up_rewrites' );
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
add_filter( 'post_thumbnail_html', __NAMESPACE__ . '\post_thumbnail_html', 10, 5 );
add_action( 'body_class', __NAMESPACE__ . '\add_extra_body_class' );
add_filter( 'frontpage_template_hierarchy', __NAMESPACE__ . '\use_archive_template_paged' );
add_filter( 'search_template_hierarchy', __NAMESPACE__ . '\use_archive_template_paged' );
add_action( 'single_template_hierarchy', __NAMESPACE__ . '\load_theme_preview' );
add_filter( 'query_loop_block_query_vars', __NAMESPACE__ . '\modify_query_loop_block_query_vars', 10, 2 );
add_filter( 'post_type_link', __NAMESPACE__ . '\update_theme_shop_permalink', 10, 2 );
add_action( 'template_redirect', __NAMESPACE__ . '\redirect_term_archives' );
add_filter( 'redirect_canonical', __NAMESPACE__ . '\strip_tag_query_string' );
// Update the styles and patterns cache when themes are published/updated.
add_action( 'publish_repopackage', __NAMESPACE__ . '\update_cached_style_variations', 100, 1 );
add_action( 'publish_repopackage', __NAMESPACE__ . '\update_cached_theme_patterns', 100, 1 );
add_action( 'wporg_themes_update_version_live', __NAMESPACE__ . '\update_cached_style_variations', 100, 1 );
add_action( 'wporg_themes_update_version_live', __NAMESPACE__ . '\update_cached_theme_patterns', 100, 1 );
// Remove filters added by plugin.
remove_filter( 'post_thumbnail_html', 'wporg_themes_post_thumbnail_html', 10, 5 );
// Hide admin bar on preview pages.
add_filter(
'show_admin_bar',
function( $should_show ) {
global $wp_query;
if ( isset( $wp_query->query_vars['view'] ) ) {
return false;
}
return $should_show;
},
2000
);
add_action(
'init',
function() {
// Don't swap author link with w.org profile link.
remove_all_filters( 'author_link' );
// Remove the "By…" from the author name block.
remove_filter( 'render_block_core/post-author-name', 'WordPressdotorg\Theme\Parent_2021\Gutenberg_Tweaks\render_author_prefix', 10, 2 );
// Re-enable pagination meta tags.
remove_filter( 'wporg_rel_next_pages', '__return_zero' );
}
);
/**
* Temporary fix for permission problem during local install.
*/
function fix_term_imports() {
if ( defined( 'WP_CLI' ) ) {
remove_filter( 'pre_insert_term', 'wporg_themes_pre_insert_term' );
}
}
/**
* Set up the rewrite updates:
* - Add the preview endpoint to theme pages.
* - Enable the tag_slug__and query parameter.
*/
function set_up_rewrites() {
global $wp;
$wp->add_query_var( 'tag_slug__and' );
add_rewrite_endpoint( 'preview', EP_PERMALINK, 'view' );
}
/**
* Enqueue scripts and styles.
*/
function enqueue_assets() {
$asset_file = get_theme_file_path( 'build/style/index.asset.php' );
if ( ! file_exists( $asset_file ) ) {
return;
}
// The parent style is registered as `wporg-parent-2021-style`, and will be loaded unless
// explicitly unregistered. We can load any child-theme overrides by declaring the parent
// stylesheet as a dependency.
$metadata = require $asset_file;
wp_enqueue_style(
'wporg-theme-directory-2024',
get_theme_file_uri( 'build/style/style-index.css' ),
array( 'wporg-parent-2021-style', 'wporg-global-fonts' ),
$metadata['version']
);
}
/**
* Use theme screenshot for post thumbnails, add attributes to image tag.
*
* @param string $html
* @param int $post_id
* @param int $post_thumbnail_id
* @param string $size
* @param string[] $attr
*
* @return string
*/
function post_thumbnail_html( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
$current_post = get_post( $post_id );
if ( THEME_POST_TYPE == $current_post->post_type ) {
$theme = new \WPORG_Themes_Repo_Package( $current_post );
$src = add_query_arg(
array(
'w' => $size,
'strip' => 'all',
),
$theme->screenshot_url()
);
$html = '<img src="' . esc_url( $src ) . '"';
if ( is_array( $attr ) ) {
$attr['alt'] = '';
foreach ( $attr as $name => $value ) {
$html .= " $name=" . '"' . $value . '"';
}
}
$html .= ' />';
} else if ( 'theme_shop' == $current_post->post_type ) {
$src = get_post_meta( $current_post->ID, 'image_url', true );
if ( ! $src ) {
$url = get_post_meta( $current_post->ID, 'url', true );
if ( $url ) {
$src = add_query_arg(
array(
'w' => 700,
'vpw' => 1200,
'vph' => 900,
),
'https://s0.wp.com/mshots/v1/' . urlencode( $url )
);
}
}
if ( ! $src ) {
return '';
}
$html = '<img src="' . esc_url( $src ) . '"';
if ( is_array( $attr ) ) {
$attr['alt'] = '';
foreach ( $attr as $name => $value ) {
$html .= " $name=" . '"' . $value . '"';
}
}
$html .= ' />';
}
return $html;
}
/**
* Add some custom classes to `body`.
*
* @param string[] $classes An array of body class names.
*/
function add_extra_body_class( $classes ) {
global $wp_query;
if ( isset( $wp_query->query_vars['view'] ) ) {
$classes[] = 'wporg-theme-preview';
}
if ( ! empty( $wp_query->query_vars['browse'] ) ) {
$classes[] = 'browse-' . $wp_query->query_vars['browse'];
}
return $classes;
}
/**
* Switch to the archive.html template on paged requests & other browse tabs.
* Show a logged out view if viewing favorites anonymously.
*
* @param string[] $templates A list of template candidates, in descending order of priority.
*/
function use_archive_template_paged( $templates ) {
global $wp_query;
$current_browse = $wp_query->query['browse'] ?? false;
if ( is_paged() || $current_browse || is_search() ) {
array_unshift( $templates, 'archive.html' );
}
if ( 'favorites' === $current_browse && ! get_current_user_id() ) {
array_unshift( $templates, 'archive-favorites-anon.html' );
}
return $templates;
}
/**
* If this is the `view` query, use preview template.
*
* @param string[] $templates A list of template candidates, in descending order of priority.
*/
function load_theme_preview( $templates ) {
global $wp_query;
if ( isset( $wp_query->query_vars['view'] ) ) {
return [ 'preview.html' ];
}
return $templates;
}
/**
* Override the Query Loop block on the Commercial page.
*
* @param array $query Array containing parameters for `WP_Query` as parsed by the block context.
* @param WP_Block $block Block instance.
*
* @return array
*/
function modify_query_loop_block_query_vars( $query, $block ) {
if ( is_page( 'commercial' ) && 'theme_shop' === $block->context['query']['postType'] ) {
$query = array(
'post_type' => 'theme_shop',
'posts_per_page' => -1,
'orderby' => 'rand(' . gmdate( 'YmdH' ) . ')',
);
}
return $query;
}
/**
* Update the permalink for theme shops, should redirect to the shop URL.
*
* @param string $post_link The post's permalink.
* @param WP_Post $post The post in question.
*/
function update_theme_shop_permalink( $post_link, $post ) {
if ( 'theme_shop' === $post->post_type ) {
$url = get_post_meta( $post->ID, 'url', true );
if ( $url ) {
$post_link = $url;
}
}
return $post_link;
}
/**
* Redirect category and tag archives to their canonical URLs.
*
* This prevents double URLs for queries. For example:
* - /?browse=commercial -> /browse/commercial/
* - /?tag_slug__and[]=block-styles&tag_slug__and[]=four-columns -> /tags/block-styles+four-columns/
* - /?browse=commercial&tag_slug__and[]=block-styles -> /browse/commercial/?tag_slug__and[]=block-styles
* - /?s=blue -> /search/blue
*/
function redirect_term_archives() {
global $wp_query, $wp;
$url = false;
$query_vars = [];
// Return early if we're already on an author or browse page.
if ( str_starts_with( $wp->request, 'author/' ) || str_starts_with( $wp->request, 'browse/' ) ) {
return;
}
$browse = sanitize_text_field( $wp_query->query['browse'] ?? '' );
$terms = array_map( 'sanitize_text_field', get_query_tags() );
if ( ! empty( $browse ) ) {
$url = home_url( '/browse/' . $browse . '/' );
$query_vars = [ 's', 'tag', 'tag_slug__and' ];
} else if ( count( $terms ) && ! str_starts_with( $wp->request, 'tags/' ) ) {
if ( count( $terms ) === 1 ) {
$url = get_term_link( $terms[0], 'post_tag' );
if ( is_wp_error( $url ) ) {
$url = home_url( '/' );
}
} else {
$path = 'tags/' . implode( '+', $terms ) . '/';
$url = home_url( $path );
}
$query_vars = [ 's', 'browse' ];
} else if ( ! empty( $wp_query->query['s'] ) && empty( $wp->request ) ) {
$search = sanitize_text_field( $wp_query->query['s'] );
$url = home_url( 'search/' . $search . '/' );
}
if ( $url ) {
// Pass through the other query parameters.
foreach ( $query_vars as $query_var ) {
if ( isset( $wp_query->query[ $query_var ] ) ) {
$url = add_query_arg( $query_var, $wp_query->query[ $query_var ], $url );
}
}
// Redirect to the new permalink-style URL.
wp_safe_redirect( $url );
exit;
}
}
/**
* Strip out the tag_slug__and query string before the redirect happens.
*
* This prevents URLs like `/tags/three-columns/?tag_slug__and[0]=three-columns`.
*
* @param string $redirect_url The redirect URL.
*
* @return string The updated URL.
*/
function strip_tag_query_string( $redirect_url ) {
if ( str_contains( $redirect_url, '/tags/' ) ) {
$redirect_url = remove_query_arg( 'tag_slug__and', $redirect_url );
}
return $redirect_url;
}
/**
* Generate the support URL for this theme.
*
* @param string $path Path to the forum (theme name, maybe with review path).
*
* @return string
*/
function get_support_url( $path ) {
return 'https://wordpress.org/support/theme/' . $path;
}
/**
* Get the theme object, either from a given post or the current post object.
*
* @param int|WP_Post|null $_post Optional. Post ID or post object.
*
* @return object|false Theme object, false if not a theme.
*/
function get_theme_information( $_post = false ) {
global $post;
if ( ! $_post ) {
$_post = $post;
}
$theme_post = get_post( $_post );
// Not a post, or not a theme post type.
if ( ! ( $theme_post instanceof \WP_Post ) || THEME_POST_TYPE !== $theme_post->post_type ) {
return false;
}
$theme = wporg_themes_theme_information( $theme_post->post_name );
if ( isset( $theme->error ) ) {
return false;
}
return $theme;
}
/**
* Get the selected tags from the current query.
*
* @return array
*/
function get_query_tags() {
global $wp_query;
$tags = isset( $wp_query->query['tag'] ) ? $wp_query->query['tag'] : array();
if ( is_array( $tags ) ) {
// Workaround to make sure the default tag query behavior still works.
$tags = [];
} else if ( is_string( $tags ) ) {
$tags = explode( '+', $tags );
}
$tags_and = isset( $wp_query->query['tag_slug__and'] ) ? (array) $wp_query->query['tag_slug__and'] : array();
// Filter out arrays in the array.
$tags_and = array_filter( $tags_and, 'is_scalar' );
return array_merge( $tags, $tags_and );
}
/**
* Given a set of tag slugs, fetch the labels from the feature list.
*
* Using the featured list ensures the label will be translated (if available).
* If a tag is not found, it will be silently filtered out.
*
* @param string[] $tags List of tags.
*
* @return array
*/
function get_tag_labels( $tags ) {
$features = wporg_themes_get_feature_list( 'active' );
$labels = array_map(
function( $tag ) use ( $features ) {
foreach ( $features as $list ) {
if ( isset( $list[ $tag ] ) ) {
return $list[ $tag ];
}
}
return false;
},
$tags
);
return array_filter( $labels );
}
/**
* Get all possible features. This list mirrors core, but needs the local
* textdomain as the terms have been updated to be sentence-case.
*
* @param string $include Optional. Type of list: 'active', 'deprecated' or 'all'. Default 'active'.
* @param string $subset Optional. Returns only the selected subset of features.
*
* @return array List of features.
*/
function wporg_themes_get_feature_list( $include = 'active', $subset = '' ) {
$features = array();
if ( 'active' === $include || 'all' === $include ) {
$features = array(
__( 'Layout', 'wporg-themes' ) => array(
'grid-layout' => __( 'Grid layout', 'wporg-themes' ),
'one-column' => __( 'One column', 'wporg-themes' ),
'two-columns' => __( 'Two columns', 'wporg-themes' ),
'three-columns' => __( 'Three columns', 'wporg-themes' ),
'four-columns' => __( 'Four columns', 'wporg-themes' ),
'left-sidebar' => __( 'Left sidebar', 'wporg-themes' ),
'right-sidebar' => __( 'Right sidebar', 'wporg-themes' ),
'wide-blocks' => __( 'Wide blocks', 'wporg-themes' ),
),
__( 'Features', 'wporg-themes' ) => array(
'accessibility-ready' => __( 'Accessibility ready', 'wporg-themes' ),
'block-patterns' => __( 'Block editor patterns', 'wporg-themes' ),
'block-styles' => __( 'Block editor styles', 'wporg-themes' ),
'full-site-editing' => __( 'Block themes', 'wporg-themes' ),
'buddypress' => __( 'BuddyPress', 'wporg-themes' ),
'custom-background' => __( 'Custom background', 'wporg-themes' ),
'custom-colors' => __( 'Custom colors', 'wporg-themes' ),
'custom-header' => __( 'Custom header', 'wporg-themes' ),
'custom-logo' => __( 'Custom logo', 'wporg-themes' ),
'custom-menu' => __( 'Custom menu', 'wporg-themes' ),
'editor-style' => __( 'Editor style', 'wporg-themes' ),
'featured-image-header' => __( 'Featured image header', 'wporg-themes' ),
'featured-images' => __( 'Featured images', 'wporg-themes' ),
'flexible-header' => __( 'Flexible header', 'wporg-themes' ),
'footer-widgets' => __( 'Footer widgets', 'wporg-themes' ),
'front-page-post-form' => __( 'Front page posting', 'wporg-themes' ),
'full-width-template' => __( 'Full width template', 'wporg-themes' ),
'microformats' => __( 'Microformats', 'wporg-themes' ),
'post-formats' => __( 'Post formats', 'wporg-themes' ),
'rtl-language-support' => __( 'RTL language support', 'wporg-themes' ),
'sticky-post' => __( 'Sticky post', 'wporg-themes' ),
'style-variations' => __( 'Style variations', 'wporg-themes' ),
'template-editing' => __( 'Template editing', 'wporg-themes' ),
'theme-options' => __( 'Theme options', 'wporg-themes' ),
'threaded-comments' => __( 'Threaded comments', 'wporg-themes' ),
'translation-ready' => __( 'Translation ready', 'wporg-themes' ),
),
__( 'Subject', 'wporg-themes' ) => array(
'blog' => __( 'Blog', 'wporg-themes' ),
'e-commerce' => __( 'E-commerce', 'wporg-themes' ),
'education' => __( 'Education', 'wporg-themes' ),
'entertainment' => __( 'Entertainment', 'wporg-themes' ),
'food-and-drink' => __( 'Food & drink', 'wporg-themes' ),
'holiday' => __( 'Holiday', 'wporg-themes' ),
'news' => __( 'News', 'wporg-themes' ),
'photography' => __( 'Photography', 'wporg-themes' ),
'portfolio' => __( 'Portfolio', 'wporg-themes' ),
),
);
}
if ( 'deprecated' === $include || 'all' === $include ) {
$features[ __( 'Colors', 'wporg-themes' ) ] = array(
'black' => __( 'Black', 'wporg-themes' ),
'blue' => __( 'Blue', 'wporg-themes' ),
'brown' => __( 'Brown', 'wporg-themes' ),
'gray' => __( 'Gray', 'wporg-themes' ),
'green' => __( 'Green', 'wporg-themes' ),
'orange' => __( 'Orange', 'wporg-themes' ),
'pink' => __( 'Pink', 'wporg-themes' ),
'purple' => __( 'Purple', 'wporg-themes' ),
'red' => __( 'Red', 'wporg-themes' ),
'silver' => __( 'Silver', 'wporg-themes' ),
'tan' => __( 'Tan', 'wporg-themes' ),
'white' => __( 'White', 'wporg-themes' ),
'yellow' => __( 'Yellow', 'wporg-themes' ),
'dark' => __( 'Dark', 'wporg-themes' ),
'light' => __( 'Light', 'wporg-themes' ),
);
if ( 'deprecated' === $include ) {
// Initialize arrays.
$features[ __( 'Layout', 'wporg-themes' ) ] = array();
$features[ __( 'Features', 'wporg-themes' ) ] = array();
$features[ __( 'Subject', 'wporg-themes' ) ] = array();
}
$features[ __( 'Layout', 'wporg-themes' ) ] = array_merge(
$features[ __( 'Layout', 'wporg-themes' ) ],
array(
'fixed-layout' => __( 'Fixed Layout', 'wporg-themes' ),
'fluid-layout' => __( 'Fluid Layout', 'wporg-themes' ),
'responsive-layout' => __( 'Responsive Layout', 'wporg-themes' ),
)
);
$features[ __( 'Features', 'wporg-themes' ) ] = array_merge(
$features[ __( 'Features', 'wporg-themes' ) ],
array(
'blavatar' => __( 'Blavatar', 'wporg-themes' ),
)
);
$features[ __( 'Subject', 'wporg-themes' ) ] = array_merge(
$features[ __( 'Subject', 'wporg-themes' ) ],
array(
'photoblogging' => __( 'Photoblogging', 'wporg-themes' ),
'seasonal' => __( 'Seasonal', 'wporg-themes' ),
)
);
}
if ( 'layouts' === $subset ) {
return $features[ __( 'Layout', 'wporg-themes' ) ];
} else if ( 'features' === $subset ) {
return $features[ __( 'Features', 'wporg-themes' ) ];
} else if ( 'subjects' === $subset ) {
return $features[ __( 'Subject', 'wporg-themes' ) ];
}
return $features;
}
/**
* Get the list of patterns from wp-themes.com API.
*/
function get_theme_patterns( $post ) {
$patterns = get_post_meta( $post->ID, 'theme_patterns', true );
if ( ! is_array( $patterns ) ) {
$patterns = update_cached_theme_patterns( $post->ID );
}
return $patterns;
}
/**
* Get the list of style variations from wp-themes.com API.
*/
function get_theme_style_variations( $post ) {
$styles = get_post_meta( $post->ID, 'style_variations', true );
if ( ! is_array( $styles ) ) {
$styles = update_cached_style_variations( $post->ID );
}
return $styles;
}
/**
* Update the cached style variations for a theme.
*
* @param int $post_id
* @return array The style variations.
*/
function update_cached_style_variations( $post_id ) {
$post = get_post( $post_id );
// If the postmeta doesn't yet exist, set it to empty for other requests.
if ( ! is_array( $post->style_variations ) ) {
update_post_meta( $post_id, 'style_variations', [] );
}
// Fetch fresh data.
$url = 'https://wp-themes.com/' . $post->post_name . '/';
$url = add_query_arg( 'rest_route', '/wporg-styles/v2/variations', $url );
$response = wp_remote_get( $url );
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return;
}
$styles = json_decode( wp_remote_retrieve_body( $response ) );
// If the response is not an error but invalid, transform into something that will not error.
if ( empty( $styles ) ) {
$styles = [];
}
update_post_meta( $post_id, 'style_variations', $styles );
return $styles;
}
/**
* Update the cached theme patterns for a theme.
*
* @param int $post_id
* @return array The theme patterns.
*/
function update_cached_theme_patterns( $post_id ) {
$post = get_post( $post_id );
// If the postmeta doesn't yet exist, set it to empty for other requests.
if ( ! is_array( $post->theme_patterns ) ) {
update_post_meta( $post_id, 'theme_patterns', [] );
}
// Fetch fresh data.
$url = 'https://wp-themes.com/' . $post->post_name . '/';
$url = add_query_arg( 'rest_route', '/wporg-patterns/v2/patterns', $url );
$response = wp_remote_get( $url );
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return;
}
$patterns = json_decode( wp_remote_retrieve_body( $response ) );
// If the response is not an error but invalid, transform into something that will not error.
if ( empty( $patterns ) ) {
$patterns = [];
}
update_post_meta( $post_id, 'theme_patterns', $patterns );
return $patterns;
}