WP_Customize_Manager8b7c:f320:99b9:690f:4595:cd17:293a:c069handle_load_themes_request()  Loads themes into the theme browsing/installation UI.   Source  File: wp-includes/class-wp-customize-manager.php. View all references  public function handle_load_themes_request() {
	check_ajax_referer( 'switch_themes', 'nonce' );

	if ( ! current_user_can( 'switch_themes' ) ) {
		wp_die( -1 );
	}

	if ( empty( $_POST['theme_action'] ) ) {
		wp_send_json_error( 'missing_theme_action' );
	}
	$theme_action = sanitize_key( $_POST['theme_action'] );
	$themes       = array();
	$args         = array();

	// Define query filters based on user input.
	if ( ! array_key_exists( 'search', $_POST ) ) {
		$args['search'] = '';
	} else {
		$args['search'] = sanitize_text_field( wp_unslash( $_POST['search'] ) );
	}

	if ( ! array_key_exists( 'tags', $_POST ) ) {
		$args['tag'] = '';
	} else {
		$args['tag'] = array_map( 'sanitize_text_field', wp_unslash( (array) $_POST['tags'] ) );
	}

	if ( ! array_key_exists( 'page', $_POST ) ) {
		$args['page'] = 1;
	} else {
		$args['page'] = absint( $_POST['page'] );
	}

	require_once ABSPATH . 'wp-admin/includes/theme.php';

	if ( 'installed' === $theme_action ) {

		// Load all installed themes from wp_prepare_themes_for_js().
		$themes = array( 'themes' => array() );
		foreach ( wp_prepare_themes_for_js() as $theme ) {
			$theme['type']      = 'installed';
			$theme['active']    = ( isset( $_POST['customized_theme'] ) && $_POST['customized_theme'] === $theme['id'] );
			$themes['themes'][] = $theme;
		}
	} elseif ( 'wporg' === $theme_action ) {

		// Load WordPress.org themes from the .org API and normalize data to match installed theme objects.
		if ( ! current_user_can( 'install_themes' ) ) {
			wp_die( -1 );
		}

		// Arguments for all queries.
		$wporg_args = array(
			'per_page' => 100,
			'fields'   => array(
				'reviews_url' => true, // Explicitly request the reviews URL to be linked from the customizer.
			),
		);

		$args = array_merge( $wporg_args, $args );

		if ( '' === $args['search'] && '' === $args['tag'] ) {
			$args['browse'] = 'new'; // Sort by latest themes by default.
		}

		// Load themes from the .org API.
		$themes = themes_api( 'query_themes', $args );
		if ( is_wp_error( $themes ) ) {
			wp_send_json_error();
		}

		// This list matches the allowed tags in wp-admin/includes/theme-install.php.
		$themes_allowedtags                     = array_fill_keys(
			array( 'a', 'abbr', 'acronym', 'code', 'pre', 'em', 'strong', 'div', 'p', 'ul', 'ol', 'li', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'img' ),
			array()
		);
		$themes_allowedtags['a']                = array_fill_keys( array( 'href', 'title', 'target' ), true );
		$themes_allowedtags['acronym']['title'] = true;
		$themes_allowedtags['abbr']['title']    = true;
		$themes_allowedtags['img']              = array_fill_keys( array( 'src', 'class', 'alt' ), true );

		// Prepare a list of installed themes to check against before the loop.
		$installed_themes = array();
		$wp_themes        = wp_get_themes();
		foreach ( $wp_themes as $theme ) {
			$installed_themes[] = $theme->get_stylesheet();
		}
		$update_php = network_admin_url( 'update.php?action=install-theme' );

		// Set up properties for themes available on WordPress.org.
		foreach ( $themes->themes as &$theme ) {
			$theme->install_url = add_query_arg(
				array(
					'theme'    => $theme->slug,
					'_wpnonce' => wp_create_nonce( 'install-theme_' . $theme->slug ),
				),
				$update_php
			);

			$theme->name        = wp_kses( $theme->name, $themes_allowedtags );
			$theme->version     = wp_kses( $theme->version, $themes_allowedtags );
			$theme->description = wp_kses( $theme->description, $themes_allowedtags );
			$theme->stars       = wp_star_rating(
				array(
					'rating' => $theme->rating,
					'type'   => 'percent',
					'number' => $theme->num_ratings,
					'echo'   => false,
				)
			);
			$theme->num_ratings = number_format_i18n( $theme->num_ratings );
			$theme->preview_url = set_url_scheme( $theme->preview_url );

			// Handle themes that are already installed as installed themes.
			if ( in_array( $theme->slug, $installed_themes, true ) ) {
				$theme->type = 'installed';
			} else {
				$theme->type = $theme_action;
			}

			// Set active based on customized theme.
			$theme->active = ( isset( $_POST['customized_theme'] ) && $_POST['customized_theme'] === $theme->slug );

			// Map available theme properties to installed theme properties.
			$theme->id            = $theme->slug;
			$theme->screenshot    = array( $theme->screenshot_url );
			$theme->authorAndUri  = wp_kses( $theme->author['display_name'], $themes_allowedtags );
			$theme->compatibleWP  = is_wp_version_compatible( $theme->requires ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName
			$theme->compatiblePHP = is_php_version_compatible( $theme->requires_php ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName

			if ( isset( $theme->parent ) ) {
				$theme->parent = $theme->parent['slug'];
			} else {
				$theme->parent = false;
			}
			unset( $theme->slug );
			unset( $theme->screenshot_url );
			unset( $theme->author );
		} // End foreach().
	} // End if().

	/**
	 * Filters the theme data loaded in the customizer.
	 *
	 * This allows theme data to be loading from an external source,
	 * or modification of data loaded from `wp_prepare_themes_for_js()`
	 * or WordPress.org via `themes_api()`.
	 *
	 * @since 4.9.0
	 *
	 * @see wp_prepare_themes_for_js()
	 * @see themes_api()
	 * @see WP_Customize_Manager8b7c:f320:99b9:690f:4595:cd17:293a:c069__construct()
	 *
	 * @param array|stdClass       $themes  Nested array or object of theme data.
	 * @param array                $args    List of arguments, such as page, search term, and tags to query for.
	 * @param WP_Customize_Manager $manager Instance of Customize manager.
	 */
	$themes = apply_filters( 'customize_load_themes', $themes, $args, $this );

	wp_send_json_success( $themes );
}
   Hooks     apply_filters( 'customize_load_themes', array|stdClass $themes, array $args, WP_Customize_Manager $manager )   
Filters the theme data loaded in the customizer.      Related  Uses    Uses Description      is_wp_version_compatible() wp-includes/functions.php   Checks compatibility with the current WordPress version.     wp_prepare_themes_for_js() wp-admin/includes/theme.php   Prepares themes for JavaScript.     themes_api() wp-admin/includes/theme.php   Retrieves theme installer pages from the WordPress.org Themes API.     wp_star_rating() wp-admin/includes/template.php   Outputs a HTML element with a star rating for a given rating.     wp_get_themes() wp-includes/theme.php   Returns an array of WP_Theme objects based on the arguments.     sanitize_text_field() wp-includes/formatting.php   Sanitizes a string from user input or from the database.     network_admin_url() wp-includes/link-template.php   Retrieves the URL to the admin area for the network.     set_url_scheme() wp-includes/link-template.php   Sets the scheme for a URL.     wp_create_nonce() wp-includes/pluggable.php   Creates a cryptographic token tied to a specific action, user, user session, and window of time.     is_php_version_compatible() wp-includes/functions.php   Checks compatibility with the current PHP version.     wp_kses() wp-includes/kses.php   Filters text content and strips out disallowed HTML.     wp_die() wp-includes/functions.php   Kills WordPress execution and displays HTML page with an error message.     apply_filters() wp-includes/plugin.php   Calls the callback functions that have been added to a filter hook.     number_format_i18n() wp-includes/functions.php   Converts float number to format based on the locale.     add_query_arg() wp-includes/functions.php   Retrieves a modified URL query string.     check_ajax_referer() wp-includes/pluggable.php   Verifies the Ajax request to prevent processing requests external of the blog.     wp_send_json_success() wp-includes/functions.php   Sends a JSON response back to an Ajax request, indicating success.     absint() wp-includes/functions.php   Converts a value to non-negative integer.     wp_send_json_error() wp-includes/functions.php   Sends a JSON response back to an Ajax request, indicating failure.     sanitize_key() wp-includes/formatting.php   Sanitizes a string key.     wp_unslash() wp-includes/formatting.php   Removes slashes from a string or recursively removes slashes from strings within an array.     current_user_can() wp-includes/capabilities.php   Returns whether the current user has the specified capability.     is_wp_error() wp-includes/load.php   Checks whether the given variable is a WordPress Error.   
    Changelog    Version Description     4.9.0 Introduced.    

    
{'path': 'public/docs/wordpress/classes/wp_customize_manager/handle_load_themes_request.html', 'source': 'DevDocs.IO'}