53 * @since 4.7.0 Added the Twenty Seventeen theme. |
56 * @since 4.7.0 Added the Twenty Seventeen theme. |
54 * @since 5.0.0 Added the Twenty Nineteen theme. |
57 * @since 5.0.0 Added the Twenty Nineteen theme. |
55 * @since 5.3.0 Added the Twenty Twenty theme. |
58 * @since 5.3.0 Added the Twenty Twenty theme. |
56 * @since 5.6.0 Added the Twenty Twenty-One theme. |
59 * @since 5.6.0 Added the Twenty Twenty-One theme. |
57 * @since 5.9.0 Added the Twenty Twenty-Two theme. |
60 * @since 5.9.0 Added the Twenty Twenty-Two theme. |
|
61 * @since 6.1.0 Added the Twenty Twenty-Three theme. |
|
62 * @since 6.4.0 Added the Twenty Twenty-Four theme. |
58 * @var string[] |
63 * @var string[] |
59 */ |
64 */ |
60 private static $default_themes = array( |
65 private static $default_themes = array( |
61 'classic' => 'WordPress Classic', |
66 'classic' => 'WordPress Classic', |
62 'default' => 'WordPress Default', |
67 'default' => 'WordPress Default', |
63 'twentyten' => 'Twenty Ten', |
68 'twentyten' => 'Twenty Ten', |
64 'twentyeleven' => 'Twenty Eleven', |
69 'twentyeleven' => 'Twenty Eleven', |
65 'twentytwelve' => 'Twenty Twelve', |
70 'twentytwelve' => 'Twenty Twelve', |
66 'twentythirteen' => 'Twenty Thirteen', |
71 'twentythirteen' => 'Twenty Thirteen', |
67 'twentyfourteen' => 'Twenty Fourteen', |
72 'twentyfourteen' => 'Twenty Fourteen', |
68 'twentyfifteen' => 'Twenty Fifteen', |
73 'twentyfifteen' => 'Twenty Fifteen', |
69 'twentysixteen' => 'Twenty Sixteen', |
74 'twentysixteen' => 'Twenty Sixteen', |
70 'twentyseventeen' => 'Twenty Seventeen', |
75 'twentyseventeen' => 'Twenty Seventeen', |
71 'twentynineteen' => 'Twenty Nineteen', |
76 'twentynineteen' => 'Twenty Nineteen', |
72 'twentytwenty' => 'Twenty Twenty', |
77 'twentytwenty' => 'Twenty Twenty', |
73 'twentytwentyone' => 'Twenty Twenty-One', |
78 'twentytwentyone' => 'Twenty Twenty-One', |
74 'twentytwentytwo' => 'Twenty Twenty-Two', |
79 'twentytwentytwo' => 'Twenty Twenty-Two', |
|
80 'twentytwentythree' => 'Twenty Twenty-Three', |
|
81 'twentytwentyfour' => 'Twenty Twenty-Four', |
75 ); |
82 ); |
76 |
83 |
77 /** |
84 /** |
78 * Renamed theme tags. |
85 * Renamed theme tags. |
79 * |
86 * |
269 ) |
306 ) |
270 ); |
307 ); |
271 } else { |
308 } else { |
272 $this->errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) ); |
309 $this->errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) ); |
273 } |
310 } |
274 $this->template = $this->stylesheet; |
311 $this->template = $this->stylesheet; |
|
312 $this->block_theme = false; |
|
313 $this->block_template_folders = $this->default_template_folders; |
275 $this->cache_add( |
314 $this->cache_add( |
276 'theme', |
315 'theme', |
277 array( |
316 array( |
278 'headers' => $this->headers, |
317 'block_template_folders' => $this->block_template_folders, |
279 'errors' => $this->errors, |
318 'block_theme' => $this->block_theme, |
280 'stylesheet' => $this->stylesheet, |
319 'headers' => $this->headers, |
281 'template' => $this->template, |
320 'errors' => $this->errors, |
|
321 'stylesheet' => $this->stylesheet, |
|
322 'template' => $this->template, |
282 ) |
323 ) |
283 ); |
324 ); |
284 if ( ! file_exists( $this->theme_root ) ) { // Don't cache this one. |
325 if ( ! file_exists( $this->theme_root ) ) { // Don't cache this one. |
285 $this->errors->add( 'theme_root_missing', __( '<strong>Error</strong>: The themes directory is either empty or does not exist. Please check your installation.' ) ); |
326 $this->errors->add( 'theme_root_missing', __( '<strong>Error:</strong> The themes directory is either empty or does not exist. Please check your installation.' ) ); |
286 } |
327 } |
287 return; |
328 return; |
288 } elseif ( ! is_readable( $this->theme_root . '/' . $theme_file ) ) { |
329 } elseif ( ! is_readable( $this->theme_root . '/' . $theme_file ) ) { |
289 $this->headers['Name'] = $this->stylesheet; |
330 $this->headers['Name'] = $this->stylesheet; |
290 $this->errors = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) ); |
331 $this->errors = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) ); |
291 $this->template = $this->stylesheet; |
332 $this->template = $this->stylesheet; |
|
333 $this->block_theme = false; |
|
334 $this->block_template_folders = $this->default_template_folders; |
292 $this->cache_add( |
335 $this->cache_add( |
293 'theme', |
336 'theme', |
294 array( |
337 array( |
295 'headers' => $this->headers, |
338 'block_template_folders' => $this->block_template_folders, |
296 'errors' => $this->errors, |
339 'block_theme' => $this->block_theme, |
297 'stylesheet' => $this->stylesheet, |
340 'headers' => $this->headers, |
298 'template' => $this->template, |
341 'errors' => $this->errors, |
|
342 'stylesheet' => $this->stylesheet, |
|
343 'template' => $this->template, |
299 ) |
344 ) |
300 ); |
345 ); |
301 return; |
346 return; |
302 } else { |
347 } else { |
303 $this->headers = get_file_data( $this->theme_root . '/' . $theme_file, self::$file_headers, 'theme' ); |
348 $this->headers = get_file_data( $this->theme_root . '/' . $theme_file, self::$file_headers, 'theme' ); |
304 // Default themes always trump their pretenders. |
349 /* |
305 // Properly identify default themes that are inside a directory within wp-content/themes. |
350 * Default themes always trump their pretenders. |
|
351 * Properly identify default themes that are inside a directory within wp-content/themes. |
|
352 */ |
306 $default_theme_slug = array_search( $this->headers['Name'], self::$default_themes, true ); |
353 $default_theme_slug = array_search( $this->headers['Name'], self::$default_themes, true ); |
307 if ( $default_theme_slug ) { |
354 if ( $default_theme_slug ) { |
308 if ( basename( $this->stylesheet ) != $default_theme_slug ) { |
355 if ( basename( $this->stylesheet ) !== $default_theme_slug ) { |
309 $this->headers['Name'] .= '/' . $this->stylesheet; |
356 $this->headers['Name'] .= '/' . $this->stylesheet; |
310 } |
357 } |
311 } |
358 } |
312 } |
359 } |
313 |
360 |
357 ); |
402 ); |
358 $this->errors = new WP_Error( 'theme_no_index', $error_message ); |
403 $this->errors = new WP_Error( 'theme_no_index', $error_message ); |
359 $this->cache_add( |
404 $this->cache_add( |
360 'theme', |
405 'theme', |
361 array( |
406 array( |
362 'headers' => $this->headers, |
407 'block_template_folders' => $this->get_block_template_folders(), |
363 'errors' => $this->errors, |
408 'block_theme' => $this->block_theme, |
364 'stylesheet' => $this->stylesheet, |
409 'headers' => $this->headers, |
365 'template' => $this->template, |
410 'errors' => $this->errors, |
|
411 'stylesheet' => $this->stylesheet, |
|
412 'template' => $this->template, |
366 ) |
413 ) |
367 ); |
414 ); |
368 return; |
415 return; |
369 } |
416 } |
370 } |
417 } |
371 |
418 |
372 // If we got our data from cache, we can assume that 'template' is pointing to the right place. |
419 // If we got our data from cache, we can assume that 'template' is pointing to the right place. |
373 if ( ! is_array( $cache ) && $this->template != $this->stylesheet && ! file_exists( $this->theme_root . '/' . $this->template . '/index.php' ) ) { |
420 if ( ! is_array( $cache ) |
374 // If we're in a directory of themes inside /themes, look for the parent nearby. |
421 && $this->template !== $this->stylesheet |
375 // wp-content/themes/directory-of-themes/* |
422 && ! file_exists( $this->theme_root . '/' . $this->template . '/index.php' ) |
|
423 ) { |
|
424 /* |
|
425 * If we're in a directory of themes inside /themes, look for the parent nearby. |
|
426 * wp-content/themes/directory-of-themes/* |
|
427 */ |
376 $parent_dir = dirname( $this->stylesheet ); |
428 $parent_dir = dirname( $this->stylesheet ); |
377 $directories = search_theme_directories(); |
429 $directories = search_theme_directories(); |
378 |
430 |
379 if ( '.' !== $parent_dir && file_exists( $this->theme_root . '/' . $parent_dir . '/' . $this->template . '/index.php' ) ) { |
431 if ( '.' !== $parent_dir |
|
432 && file_exists( $this->theme_root . '/' . $parent_dir . '/' . $this->template . '/index.php' ) |
|
433 ) { |
380 $this->template = $parent_dir . '/' . $this->template; |
434 $this->template = $parent_dir . '/' . $this->template; |
381 } elseif ( $directories && isset( $directories[ $this->template ] ) ) { |
435 } elseif ( $directories && isset( $directories[ $this->template ] ) ) { |
382 // Look for the template in the search_theme_directories() results, in case it is in another theme root. |
436 /* |
383 // We don't look into directories of themes, just the theme root. |
437 * Look for the template in the search_theme_directories() results, in case it is in another theme root. |
|
438 * We don't look into directories of themes, just the theme root. |
|
439 */ |
384 $theme_root_template = $directories[ $this->template ]['theme_root']; |
440 $theme_root_template = $directories[ $this->template ]['theme_root']; |
385 } else { |
441 } else { |
386 // Parent theme is missing. |
442 // Parent theme is missing. |
387 $this->errors = new WP_Error( |
443 $this->errors = new WP_Error( |
388 'theme_no_parent', |
444 'theme_no_parent', |
393 ) |
449 ) |
394 ); |
450 ); |
395 $this->cache_add( |
451 $this->cache_add( |
396 'theme', |
452 'theme', |
397 array( |
453 array( |
398 'headers' => $this->headers, |
454 'block_template_folders' => $this->get_block_template_folders(), |
399 'errors' => $this->errors, |
455 'block_theme' => $this->is_block_theme(), |
400 'stylesheet' => $this->stylesheet, |
456 'headers' => $this->headers, |
401 'template' => $this->template, |
457 'errors' => $this->errors, |
|
458 'stylesheet' => $this->stylesheet, |
|
459 'template' => $this->template, |
402 ) |
460 ) |
403 ); |
461 ); |
404 $this->parent = new WP_Theme( $this->template, $this->theme_root, $this ); |
462 $this->parent = new WP_Theme( $this->template, $this->theme_root, $this ); |
405 return; |
463 return; |
406 } |
464 } |
407 } |
465 } |
408 |
466 |
409 // Set the parent, if we're a child theme. |
467 // Set the parent, if we're a child theme. |
410 if ( $this->template != $this->stylesheet ) { |
468 if ( $this->template !== $this->stylesheet ) { |
411 // If we are a parent, then there is a problem. Only two generations allowed! Cancel things out. |
469 // If we are a parent, then there is a problem. Only two generations allowed! Cancel things out. |
412 if ( $_child instanceof WP_Theme && $_child->template == $this->stylesheet ) { |
470 if ( $_child instanceof WP_Theme && $_child->template === $this->stylesheet ) { |
413 $_child->parent = null; |
471 $_child->parent = null; |
414 $_child->errors = new WP_Error( |
472 $_child->errors = new WP_Error( |
415 'theme_parent_invalid', |
473 'theme_parent_invalid', |
416 sprintf( |
474 sprintf( |
417 /* translators: %s: Theme directory name. */ |
475 /* translators: %s: Theme directory name. */ |
439 ) |
499 ) |
440 ); |
500 ); |
441 $this->cache_add( |
501 $this->cache_add( |
442 'theme', |
502 'theme', |
443 array( |
503 array( |
444 'headers' => $this->headers, |
504 'block_template_folders' => $this->get_block_template_folders(), |
445 'errors' => $this->errors, |
505 'block_theme' => $this->is_block_theme(), |
446 'stylesheet' => $this->stylesheet, |
506 'headers' => $this->headers, |
447 'template' => $this->template, |
507 'errors' => $this->errors, |
|
508 'stylesheet' => $this->stylesheet, |
|
509 'template' => $this->template, |
448 ) |
510 ) |
449 ); |
511 ); |
450 } |
512 } |
451 return; |
513 return; |
452 } |
514 } |
453 // Set the parent. Pass the current instance so we can do the crazy checks above and assess errors. |
515 // Set the parent. Pass the current instance so we can do the checks above and assess errors. |
454 $this->parent = new WP_Theme( $this->template, isset( $theme_root_template ) ? $theme_root_template : $this->theme_root, $this ); |
516 $this->parent = new WP_Theme( $this->template, isset( $theme_root_template ) ? $theme_root_template : $this->theme_root, $this ); |
455 } |
517 } |
456 |
518 |
457 if ( wp_paused_themes()->get( $this->stylesheet ) && ( ! is_wp_error( $this->errors ) || ! isset( $this->errors->errors['theme_paused'] ) ) ) { |
519 if ( wp_paused_themes()->get( $this->stylesheet ) && ( ! is_wp_error( $this->errors ) || ! isset( $this->errors->errors['theme_paused'] ) ) ) { |
458 $this->errors = new WP_Error( 'theme_paused', __( 'This theme failed to load properly and was paused within the admin backend.' ) ); |
520 $this->errors = new WP_Error( 'theme_paused', __( 'This theme failed to load properly and was paused within the admin backend.' ) ); |
459 } |
521 } |
460 |
522 |
461 // We're good. If we didn't retrieve from cache, set it. |
523 // We're good. If we didn't retrieve from cache, set it. |
462 if ( ! is_array( $cache ) ) { |
524 if ( ! is_array( $cache ) ) { |
463 $cache = array( |
525 $cache = array( |
464 'headers' => $this->headers, |
526 'block_theme' => $this->is_block_theme(), |
465 'errors' => $this->errors, |
527 'block_template_folders' => $this->get_block_template_folders(), |
466 'stylesheet' => $this->stylesheet, |
528 'headers' => $this->headers, |
467 'template' => $this->template, |
529 'errors' => $this->errors, |
|
530 'stylesheet' => $this->stylesheet, |
|
531 'template' => $this->template, |
468 ); |
532 ); |
469 // If the parent theme is in another root, we'll want to cache this. Avoids an entire branch of filesystem calls above. |
533 // If the parent theme is in another root, we'll want to cache this. Avoids an entire branch of filesystem calls above. |
470 if ( isset( $theme_root_template ) ) { |
534 if ( isset( $theme_root_template ) ) { |
471 $cache['theme_root_template'] = $theme_root_template; |
535 $cache['theme_root_template'] = $theme_root_template; |
472 } |
536 } |
749 */ |
833 */ |
750 public function cache_delete() { |
834 public function cache_delete() { |
751 foreach ( array( 'theme', 'screenshot', 'headers', 'post_templates' ) as $key ) { |
835 foreach ( array( 'theme', 'screenshot', 'headers', 'post_templates' ) as $key ) { |
752 wp_cache_delete( $key . '-' . $this->cache_hash, 'themes' ); |
836 wp_cache_delete( $key . '-' . $this->cache_hash, 'themes' ); |
753 } |
837 } |
754 $this->template = null; |
838 $this->template = null; |
755 $this->textdomain_loaded = null; |
839 $this->textdomain_loaded = null; |
756 $this->theme_root_uri = null; |
840 $this->theme_root_uri = null; |
757 $this->parent = null; |
841 $this->parent = null; |
758 $this->errors = null; |
842 $this->errors = null; |
759 $this->headers_sanitized = null; |
843 $this->headers_sanitized = null; |
760 $this->name_translated = null; |
844 $this->name_translated = null; |
761 $this->headers = array(); |
845 $this->block_theme = null; |
|
846 $this->block_template_folders = null; |
|
847 $this->headers = array(); |
762 $this->__construct( $this->stylesheet, $this->theme_root ); |
848 $this->__construct( $this->stylesheet, $this->theme_root ); |
|
849 $this->delete_pattern_cache(); |
763 } |
850 } |
764 |
851 |
765 /** |
852 /** |
766 * Gets a raw, unformatted theme header. |
853 * Gets a raw, unformatted theme header. |
767 * |
854 * |
1672 /** This filter is documented in wp-includes/class-wp-theme.php */ |
1771 /** This filter is documented in wp-includes/class-wp-theme.php */ |
1673 return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id ); |
1772 return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id ); |
1674 } |
1773 } |
1675 |
1774 |
1676 /** |
1775 /** |
|
1776 * Returns the folder names of the block template directories. |
|
1777 * |
|
1778 * @since 6.4.0 |
|
1779 * |
|
1780 * @return string[] { |
|
1781 * Folder names used by block themes. |
|
1782 * |
|
1783 * @type string $wp_template Theme-relative directory name for block templates. |
|
1784 * @type string $wp_template_part Theme-relative directory name for block template parts. |
|
1785 * } |
|
1786 */ |
|
1787 public function get_block_template_folders() { |
|
1788 // Return set/cached value if available. |
|
1789 if ( isset( $this->block_template_folders ) ) { |
|
1790 return $this->block_template_folders; |
|
1791 } |
|
1792 |
|
1793 $this->block_template_folders = $this->default_template_folders; |
|
1794 |
|
1795 $stylesheet_directory = $this->get_stylesheet_directory(); |
|
1796 // If the theme uses deprecated block template folders. |
|
1797 if ( file_exists( $stylesheet_directory . '/block-templates' ) || file_exists( $stylesheet_directory . '/block-template-parts' ) ) { |
|
1798 $this->block_template_folders = array( |
|
1799 'wp_template' => 'block-templates', |
|
1800 'wp_template_part' => 'block-template-parts', |
|
1801 ); |
|
1802 } |
|
1803 return $this->block_template_folders; |
|
1804 } |
|
1805 |
|
1806 /** |
|
1807 * Gets block pattern data for a specified theme. |
|
1808 * Each pattern is defined as a PHP file and defines |
|
1809 * its metadata using plugin-style headers. The minimum required definition is: |
|
1810 * |
|
1811 * /** |
|
1812 * * Title: My Pattern |
|
1813 * * Slug: my-theme/my-pattern |
|
1814 * * |
|
1815 * |
|
1816 * The output of the PHP source corresponds to the content of the pattern, e.g.: |
|
1817 * |
|
1818 * <main><p><?php echo "Hello"; ?></p></main> |
|
1819 * |
|
1820 * If applicable, this will collect from both parent and child theme. |
|
1821 * |
|
1822 * Other settable fields include: |
|
1823 * |
|
1824 * - Description |
|
1825 * - Viewport Width |
|
1826 * - Inserter (yes/no) |
|
1827 * - Categories (comma-separated values) |
|
1828 * - Keywords (comma-separated values) |
|
1829 * - Block Types (comma-separated values) |
|
1830 * - Post Types (comma-separated values) |
|
1831 * - Template Types (comma-separated values) |
|
1832 * |
|
1833 * @since 6.4.0 |
|
1834 * |
|
1835 * @return array Block pattern data. |
|
1836 */ |
|
1837 public function get_block_patterns() { |
|
1838 $can_use_cached = ! wp_is_development_mode( 'theme' ); |
|
1839 |
|
1840 $pattern_data = $this->get_pattern_cache(); |
|
1841 if ( is_array( $pattern_data ) ) { |
|
1842 if ( $can_use_cached ) { |
|
1843 return $pattern_data; |
|
1844 } |
|
1845 // If in development mode, clear pattern cache. |
|
1846 $this->delete_pattern_cache(); |
|
1847 } |
|
1848 |
|
1849 $dirpath = $this->get_stylesheet_directory() . '/patterns/'; |
|
1850 $pattern_data = array(); |
|
1851 |
|
1852 if ( ! file_exists( $dirpath ) ) { |
|
1853 if ( $can_use_cached ) { |
|
1854 $this->set_pattern_cache( $pattern_data ); |
|
1855 } |
|
1856 return $pattern_data; |
|
1857 } |
|
1858 $files = glob( $dirpath . '*.php' ); |
|
1859 if ( ! $files ) { |
|
1860 if ( $can_use_cached ) { |
|
1861 $this->set_pattern_cache( $pattern_data ); |
|
1862 } |
|
1863 return $pattern_data; |
|
1864 } |
|
1865 |
|
1866 $default_headers = array( |
|
1867 'title' => 'Title', |
|
1868 'slug' => 'Slug', |
|
1869 'description' => 'Description', |
|
1870 'viewportWidth' => 'Viewport Width', |
|
1871 'inserter' => 'Inserter', |
|
1872 'categories' => 'Categories', |
|
1873 'keywords' => 'Keywords', |
|
1874 'blockTypes' => 'Block Types', |
|
1875 'postTypes' => 'Post Types', |
|
1876 'templateTypes' => 'Template Types', |
|
1877 ); |
|
1878 |
|
1879 $properties_to_parse = array( |
|
1880 'categories', |
|
1881 'keywords', |
|
1882 'blockTypes', |
|
1883 'postTypes', |
|
1884 'templateTypes', |
|
1885 ); |
|
1886 |
|
1887 foreach ( $files as $file ) { |
|
1888 $pattern = get_file_data( $file, $default_headers ); |
|
1889 |
|
1890 if ( empty( $pattern['slug'] ) ) { |
|
1891 _doing_it_wrong( |
|
1892 __FUNCTION__, |
|
1893 sprintf( |
|
1894 /* translators: 1: file name. */ |
|
1895 __( 'Could not register file "%s" as a block pattern ("Slug" field missing)' ), |
|
1896 $file |
|
1897 ), |
|
1898 '6.0.0' |
|
1899 ); |
|
1900 continue; |
|
1901 } |
|
1902 |
|
1903 if ( ! preg_match( '/^[A-z0-9\/_-]+$/', $pattern['slug'] ) ) { |
|
1904 _doing_it_wrong( |
|
1905 __FUNCTION__, |
|
1906 sprintf( |
|
1907 /* translators: 1: file name; 2: slug value found. */ |
|
1908 __( 'Could not register file "%1$s" as a block pattern (invalid slug "%2$s")' ), |
|
1909 $file, |
|
1910 $pattern['slug'] |
|
1911 ), |
|
1912 '6.0.0' |
|
1913 ); |
|
1914 } |
|
1915 |
|
1916 // Title is a required property. |
|
1917 if ( ! $pattern['title'] ) { |
|
1918 _doing_it_wrong( |
|
1919 __FUNCTION__, |
|
1920 sprintf( |
|
1921 /* translators: 1: file name. */ |
|
1922 __( 'Could not register file "%s" as a block pattern ("Title" field missing)' ), |
|
1923 $file |
|
1924 ), |
|
1925 '6.0.0' |
|
1926 ); |
|
1927 continue; |
|
1928 } |
|
1929 |
|
1930 // For properties of type array, parse data as comma-separated. |
|
1931 foreach ( $properties_to_parse as $property ) { |
|
1932 if ( ! empty( $pattern[ $property ] ) ) { |
|
1933 $pattern[ $property ] = array_filter( wp_parse_list( (string) $pattern[ $property ] ) ); |
|
1934 } else { |
|
1935 unset( $pattern[ $property ] ); |
|
1936 } |
|
1937 } |
|
1938 |
|
1939 // Parse properties of type int. |
|
1940 $property = 'viewportWidth'; |
|
1941 if ( ! empty( $pattern[ $property ] ) ) { |
|
1942 $pattern[ $property ] = (int) $pattern[ $property ]; |
|
1943 } else { |
|
1944 unset( $pattern[ $property ] ); |
|
1945 } |
|
1946 |
|
1947 // Parse properties of type bool. |
|
1948 $property = 'inserter'; |
|
1949 if ( ! empty( $pattern[ $property ] ) ) { |
|
1950 $pattern[ $property ] = in_array( |
|
1951 strtolower( $pattern[ $property ] ), |
|
1952 array( 'yes', 'true' ), |
|
1953 true |
|
1954 ); |
|
1955 } else { |
|
1956 unset( $pattern[ $property ] ); |
|
1957 } |
|
1958 |
|
1959 $key = str_replace( $dirpath, '', $file ); |
|
1960 |
|
1961 $pattern_data[ $key ] = $pattern; |
|
1962 } |
|
1963 |
|
1964 if ( $can_use_cached ) { |
|
1965 $this->set_pattern_cache( $pattern_data ); |
|
1966 } |
|
1967 |
|
1968 return $pattern_data; |
|
1969 } |
|
1970 |
|
1971 /** |
|
1972 * Gets block pattern cache. |
|
1973 * |
|
1974 * @since 6.4.0 |
|
1975 * @since 6.6.0 Uses transients to cache regardless of site environment. |
|
1976 * |
|
1977 * @return array|false Returns an array of patterns if cache is found, otherwise false. |
|
1978 */ |
|
1979 private function get_pattern_cache() { |
|
1980 if ( ! $this->exists() ) { |
|
1981 return false; |
|
1982 } |
|
1983 |
|
1984 $pattern_data = get_site_transient( 'wp_theme_files_patterns-' . $this->cache_hash ); |
|
1985 |
|
1986 if ( is_array( $pattern_data ) && $pattern_data['version'] === $this->get( 'Version' ) ) { |
|
1987 return $pattern_data['patterns']; |
|
1988 } |
|
1989 return false; |
|
1990 } |
|
1991 |
|
1992 /** |
|
1993 * Sets block pattern cache. |
|
1994 * |
|
1995 * @since 6.4.0 |
|
1996 * @since 6.6.0 Uses transients to cache regardless of site environment. |
|
1997 * |
|
1998 * @param array $patterns Block patterns data to set in cache. |
|
1999 */ |
|
2000 private function set_pattern_cache( array $patterns ) { |
|
2001 $pattern_data = array( |
|
2002 'version' => $this->get( 'Version' ), |
|
2003 'patterns' => $patterns, |
|
2004 ); |
|
2005 |
|
2006 /** |
|
2007 * Filters the cache expiration time for theme files. |
|
2008 * |
|
2009 * @since 6.6.0 |
|
2010 * |
|
2011 * @param int $cache_expiration Cache expiration time in seconds. |
|
2012 * @param string $cache_type Type of cache being set. |
|
2013 */ |
|
2014 $cache_expiration = (int) apply_filters( 'wp_theme_files_cache_ttl', self::$cache_expiration, 'theme_block_patterns' ); |
|
2015 |
|
2016 // We don't want to cache patterns infinitely. |
|
2017 if ( $cache_expiration <= 0 ) { |
|
2018 _doing_it_wrong( |
|
2019 __METHOD__, |
|
2020 sprintf( |
|
2021 /* translators: %1$s: The filter name.*/ |
|
2022 __( 'The %1$s filter must return an integer value greater than 0.' ), |
|
2023 '<code>wp_theme_files_cache_ttl</code>' |
|
2024 ), |
|
2025 '6.6.0' |
|
2026 ); |
|
2027 |
|
2028 $cache_expiration = self::$cache_expiration; |
|
2029 } |
|
2030 |
|
2031 set_site_transient( 'wp_theme_files_patterns-' . $this->cache_hash, $pattern_data, $cache_expiration ); |
|
2032 } |
|
2033 |
|
2034 /** |
|
2035 * Clears block pattern cache. |
|
2036 * |
|
2037 * @since 6.4.0 |
|
2038 * @since 6.6.0 Uses transients to cache regardless of site environment. |
|
2039 */ |
|
2040 public function delete_pattern_cache() { |
|
2041 delete_site_transient( 'wp_theme_files_patterns-' . $this->cache_hash ); |
|
2042 } |
|
2043 |
|
2044 /** |
1677 * Enables a theme for all sites on the current network. |
2045 * Enables a theme for all sites on the current network. |
1678 * |
2046 * |
1679 * @since 4.6.0 |
2047 * @since 4.6.0 |
1680 * |
2048 * |
1681 * @param string|string[] $stylesheets Stylesheet name or array of stylesheet names. |
2049 * @param string|string[] $stylesheets Stylesheet name or array of stylesheet names. |