wp_prepare_themes_for_js( WP_Theme[] $themes = null )

Prepare themes for JavaScript.


Description Description


Parameters Parameters

$themes

(WP_Theme[]) (Optional) Array of theme objects to prepare. Defaults to all allowed themes.

Default value: null


Top ↑

Return Return

(array) An associative array of theme data, sorted by name.


Top ↑

Source Source

File: wp-admin/includes/theme.php

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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
function wp_prepare_themes_for_js( $themes = null ) {
    $current_theme = get_stylesheet();
 
    /**
     * Filters theme data before it is prepared for JavaScript.
     *
     * Passing a non-empty array will result in wp_prepare_themes_for_js() returning
     * early with that value instead.
     *
     * @since 4.2.0
     *
     * @param array           $prepared_themes An associative array of theme data. Default empty array.
     * @param WP_Theme[]|null $themes          An array of theme objects to prepare, if any.
     * @param string          $current_theme   The current theme slug.
     */
    $prepared_themes = (array) apply_filters( 'pre_prepare_themes_for_js', array(), $themes, $current_theme );
 
    if ( ! empty( $prepared_themes ) ) {
        return $prepared_themes;
    }
 
    // Make sure the current theme is listed first.
    $prepared_themes[ $current_theme ] = array();
 
    if ( null === $themes ) {
        $themes = wp_get_themes( array( 'allowed' => true ) );
        if ( ! isset( $themes[ $current_theme ] ) ) {
            $themes[ $current_theme ] = wp_get_theme();
        }
    }
 
    $updates = array();
    if ( current_user_can( 'update_themes' ) ) {
        $updates_transient = get_site_transient( 'update_themes' );
        if ( isset( $updates_transient->response ) ) {
            $updates = $updates_transient->response;
        }
    }
 
    WP_Theme::sort_by_name( $themes );
 
    $parents = array();
 
    foreach ( $themes as $theme ) {
        $slug         = $theme->get_stylesheet();
        $encoded_slug = urlencode( $slug );
 
        $parent = false;
        if ( $theme->parent() ) {
            $parent           = $theme->parent();
            $parents[ $slug ] = $parent->get_stylesheet();
            $parent           = $parent->display( 'Name' );
        }
 
        $customize_action = null;
        if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
            $customize_action = esc_url(
                add_query_arg(
                    array(
                        'return' => urlencode( esc_url_raw( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ),
                    ),
                    wp_customize_url( $slug )
                )
            );
        }
 
        $prepared_themes[ $slug ] = array(
            'id'           => $slug,
            'name'         => $theme->display( 'Name' ),
            'screenshot'   => array( $theme->get_screenshot() ), // @todo multiple
            'description'  => $theme->display( 'Description' ),
            'author'       => $theme->display( 'Author', false, true ),
            'authorAndUri' => $theme->display( 'Author' ),
            'version'      => $theme->display( 'Version' ),
            'tags'         => $theme->display( 'Tags' ),
            'parent'       => $parent,
            'active'       => $slug === $current_theme,
            'hasUpdate'    => isset( $updates[ $slug ] ),
            'hasPackage'   => isset( $updates[ $slug ] ) && ! empty( $updates[ $slug ]['package'] ),
            'update'       => get_theme_update_available( $theme ),
            'actions'      => array(
                'activate'  => current_user_can( 'switch_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=activate&stylesheet=' . $encoded_slug ), 'switch-theme_' . $slug ) : null,
                'customize' => $customize_action,
                'delete'    => current_user_can( 'delete_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=delete&stylesheet=' . $encoded_slug ), 'delete-theme_' . $slug ) : null,
            ),
        );
    }
 
    // Remove 'delete' action if theme has an active child
    if ( ! empty( $parents ) && array_key_exists( $current_theme, $parents ) ) {
        unset( $prepared_themes[ $parents[ $current_theme ] ]['actions']['delete'] );
    }
 
    /**
     * Filters the themes prepared for JavaScript, for themes.php.
     *
     * Could be useful for changing the order, which is by name by default.
     *
     * @since 3.8.0
     *
     * @param array $prepared_themes Array of themes.
     */
    $prepared_themes = apply_filters( 'wp_prepare_themes_for_js', $prepared_themes );
    $prepared_themes = array_values( $prepared_themes );
    return array_filter( $prepared_themes );
}

Top ↑

Changelog Changelog

Changelog
Version Description
3.8.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.