wp_prepare_themes_for_js

Prepare themes for JavaScript.

Description

(array) wp_prepare_themes_for_js( (null) $themes = null ); 

Returns (array)

An associative array of theme data, sorted by name.

Parameters (1)

0. $themes — Optional. (null) => null
Array of WP_Theme objects to prepare. Defaults to all allowed themes.

Usage

  1. if ( !function_exists( 'wp_prepare_themes_for_js' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/theme.php'; 
  3.  
  4. // Optional. Array of WP_Theme objects to prepare. 
  5. // Defaults to all allowed themes. 
  6. $themes = null; 
  7.  
  8. // NOTICE! Understand what this does before running. 
  9. $result = wp_prepare_themes_for_js($themes); 
  10.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/theme.php  
  1. function wp_prepare_themes_for_js( $themes = null ) { 
  2. $current_theme = get_stylesheet(); 
  3.  
  4. /** 
  5. * Filters theme data before it is prepared for JavaScript. 
  6. * Passing a non-empty array will result in wp_prepare_themes_for_js() returning 
  7. * early with that value instead. 
  8. * @since 4.2.0 
  9. * @param array $prepared_themes An associative array of theme data. Default empty array. 
  10. * @param null|array $themes An array of WP_Theme objects to prepare, if any. 
  11. * @param string $current_theme The current theme slug. 
  12. */ 
  13. $prepared_themes = (array) apply_filters( 'pre_prepare_themes_for_js', array(), $themes, $current_theme ); 
  14.  
  15. if ( ! empty( $prepared_themes ) ) { 
  16. return $prepared_themes; 
  17.  
  18. // Make sure the current theme is listed first. 
  19. $prepared_themes[ $current_theme ] = array(); 
  20.  
  21. if ( null === $themes ) { 
  22. $themes = wp_get_themes( array( 'allowed' => true ) ); 
  23. if ( ! isset( $themes[ $current_theme ] ) ) { 
  24. $themes[ $current_theme ] = wp_get_theme(); 
  25.  
  26. $updates = array(); 
  27. if ( current_user_can( 'update_themes' ) ) { 
  28. $updates_transient = get_site_transient( 'update_themes' ); 
  29. if ( isset( $updates_transient->response ) ) { 
  30. $updates = $updates_transient->response; 
  31.  
  32. WP_Theme::sort_by_name( $themes ); 
  33.  
  34. $parents = array(); 
  35.  
  36. foreach ( $themes as $theme ) { 
  37. $slug = $theme->get_stylesheet(); 
  38. $encoded_slug = urlencode( $slug ); 
  39.  
  40. $parent = false; 
  41. if ( $theme->parent() ) { 
  42. $parent = $theme->parent()->display( 'Name' ); 
  43. $parents[ $slug ] = $theme->parent()->get_stylesheet(); 
  44.  
  45. $customize_action = null; 
  46. if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) { 
  47. $customize_action = esc_url( add_query_arg
  48. array( 
  49. 'return' => urlencode( esc_url_raw( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ),  
  50. ),  
  51. wp_customize_url( $slug ) 
  52. ) ); 
  53.  
  54. $prepared_themes[ $slug ] = array( 
  55. 'id' => $slug,  
  56. 'name' => $theme->display( 'Name' ),  
  57. 'screenshot' => array( $theme->get_screenshot() ), // @todo multiple 
  58. 'description' => $theme->display( 'Description' ),  
  59. 'author' => $theme->display( 'Author', false, true ),  
  60. 'authorAndUri' => $theme->display( 'Author' ),  
  61. 'version' => $theme->display( 'Version' ),  
  62. 'tags' => $theme->display( 'Tags' ),  
  63. 'parent' => $parent,  
  64. 'active' => $slug === $current_theme,  
  65. 'hasUpdate' => isset( $updates[ $slug ] ),  
  66. 'hasPackage' => isset( $updates[ $slug ] ) && ! empty( $updates[ $slug ][ 'package' ] ),  
  67. 'update' => get_theme_update_available( $theme ),  
  68. 'actions' => array( 
  69. 'activate' => current_user_can( 'switch_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=activate&stylesheet=' . $encoded_slug ), 'switch-theme_' . $slug ) : null,  
  70. 'customize' => $customize_action,  
  71. 'delete' => current_user_can( 'delete_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=delete&stylesheet=' . $encoded_slug ), 'delete-theme_' . $slug ) : null,  
  72. ),  
  73. ); 
  74.  
  75. // Remove 'delete' action if theme has an active child 
  76. if ( ! empty( $parents ) && array_key_exists( $current_theme, $parents ) ) { 
  77. unset( $prepared_themes[ $parents[ $current_theme ] ]['actions']['delete'] ); 
  78.  
  79. /** 
  80. * Filters the themes prepared for JavaScript, for themes.php. 
  81. * Could be useful for changing the order, which is by name by default. 
  82. * @since 3.8.0 
  83. * @param array $prepared_themes Array of themes. 
  84. */ 
  85. $prepared_themes = apply_filters( 'wp_prepare_themes_for_js', $prepared_themes ); 
  86. $prepared_themes = array_values( $prepared_themes ); 
  87. return array_filter( $prepared_themes );