WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/10/2017 07:08:51 AM (2 years ago)
Author:
westonruter
Message:

Customize: Improve behavior and extensibility of theme loading and searching.

  • Introduce WP_Customize_Themes_Section::$filter_type, which has built-in functionality for local and remote filtering. When this set to local, all themes are assumed to be loaded from Ajax when the section is first loaded, and subsequent searching/filtering is applied to the loaded collection of themes within the section. This is how the core "Installed" section behaves - third-party sources with limited numbers of themes may consider leveraging this implementation. When this is set to remote, searching and filtering always triggers a new remote query via Ajax. The core "WordPress.org" section uses this approach, as it has over 5000 themes to search.
  • Refactor filterSearch() to accept a raw term string as input. This enables a feature filter to be used on a section where filter_type is local.
  • Refactor filter() on a theme control to check for an array of terms. Also sort the results by the number of matches. Rather than searching for an exact match, this will now search for each word in a search distinctly, allowing things like tags to rank in search results more accurately.
  • Split loadControls() into two functions for themes section JS: loadThemes() to initiate and manage an Ajax request and loadControls() to create theme controls based on the results of the Ajax call. If third-party sections need to change the way controls are loaded, such as by using a custom control subclass of WP_Customize_Theme_Control, this allows them to use the core logic for managing the Ajax call and only override the actual control-creation process.
  • Introduce customize_load_themes filter to facilitate loading themes from third-party sources (or modifying the results of the core sections).
  • Bring significant improvements to the installed themes search filter.

Props celloexpressions.
Amends [41648].
See #37661.
Fixes #42049.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r41802 r41807  
    44164416                'title'       => __( 'WordPress.org themes' ),
    44174417                'action'      => 'wporg',
     4418                'filter_type' => 'remote',
    44184419                'capability'  => 'install_themes',
    44194420                'panel'       => 'themes',
     
    49484949        $theme_action = sanitize_key( $_POST['theme_action'] );
    49494950        $themes = array();
     4951        $args = array();
     4952
     4953        // Define query filters based on user input.
     4954        if ( ! array_key_exists( 'search', $_POST ) ) {
     4955            $args['search'] = '';
     4956        } else {
     4957            $args['search'] = sanitize_text_field( wp_unslash( $_POST['search'] ) );
     4958        }
     4959
     4960        if ( ! array_key_exists( 'tags', $_POST ) ) {
     4961            $args['tag'] = '';
     4962        } else {
     4963            $args['tag'] = array_map( 'sanitize_text_field', wp_unslash( (array) $_POST['tags'] ) );
     4964        }
     4965
     4966        if ( ! array_key_exists( 'page', $_POST ) ) {
     4967            $args['page'] = 1;
     4968        } else {
     4969            $args['page'] = absint( $_POST['page'] );
     4970        }
    49504971
    49514972        require_once ABSPATH . 'wp-admin/includes/theme.php';
     4973
    49524974        if ( 'installed' === $theme_action ) {
     4975
     4976            // Load all installed themes from wp_prepare_themes_for_js().
    49534977            $themes = array( 'themes' => wp_prepare_themes_for_js() );
    49544978            foreach ( $themes['themes'] as &$theme ) {
     
    49564980                $theme['active'] = ( isset( $_POST['customized_theme'] ) && $_POST['customized_theme'] === $theme['id'] );
    49574981            }
     4982
    49584983        } elseif ( 'wporg' === $theme_action ) {
     4984
     4985            // Load WordPress.org themes from the .org API and normalize data to match installed theme objects.
    49594986            if ( ! current_user_can( 'install_themes' ) ) {
    49604987                wp_die( -1 );
     
    49624989
    49634990            // Arguments for all queries.
    4964             $args = array(
     4991            $wporg_args = array(
    49654992                'per_page' => 100,
    4966                 'page' => isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 1,
    49674993                'fields' => array(
    49684994                    'screenshot_url' => true,
     
    49805006            );
    49815007
    4982             // Define query filters based on user input.
    4983             if ( ! array_key_exists( 'search', $_POST ) ) {
    4984                 $args['search'] = '';
    4985             } else {
    4986                 $args['search'] = sanitize_text_field( wp_unslash( $_POST['search'] ) );
    4987             }
    4988 
    4989             if ( ! array_key_exists( 'tags', $_POST ) ) {
    4990                 $args['tag'] = '';
    4991             } else {
    4992                 $args['tag'] = array_map( 'sanitize_text_field', wp_unslash( (array) $_POST['tags'] ) );
    4993             }
     5008            $args = array_merge( $wporg_args, $args );
    49945009
    49955010            if ( '' === $args['search'] && '' === $args['tag'] ) {
     
    50625077            } // End foreach().
    50635078        } // End if().
     5079
     5080        /**
     5081         * Filters the theme data loaded in the customizer.
     5082         *
     5083         * This allows theme data to be loading from an external source,
     5084         * or modification of data loaded from `wp_prepare_themes_for_js()`
     5085         * or WordPress.org via `themes_api()`.
     5086         *
     5087         * @since 4.9.0
     5088         *
     5089         * @see wp_prepare_themes_for_js()
     5090         * @see themes_api()
     5091         * @see WP_Customize_Manager::__construct()
     5092         *
     5093         * @param array                $themes  Nested array of theme data.
     5094         * @param array                $args    List of arguments, such as page, search term, and tags to query for.
     5095         * @param WP_Customize_Manager $manager Instance of Customize manager.
     5096         */
     5097        $themes = apply_filters( 'customize_load_themes', $themes, $args, $this );
     5098
    50645099        wp_send_json_success( $themes );
    50655100    }
Note: See TracChangeset for help on using the changeset viewer.