Make WordPress Core


Ignore:
Timestamp:
07/18/2015 11:19:33 PM (10 years ago)
Author:
westonruter
Message:

Customizer: Finish unit tests for nav menus.

Removes object_type restriction to allow for future extensibility. Refactors some methods to improve testability. Includes new tests for Ajax requests.

Fixes #32687.
Props valendesigns, welcher, westonruter.

File:
1 edited

Legend:

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

    r33256 r33322  
    8181
    8282        $obj_type = sanitize_key( $_POST['obj_type'] );
    83         if ( ! in_array( $obj_type, array( 'post_type', 'taxonomy' ) ) ) {
    84             wp_send_json_error( 'nav_menus_invalid_obj_type' );
    85         }
    86 
    87         $taxonomy_or_post_type = sanitize_key( $_POST['type'] );
    88         $page = isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 0;
     83        $obj_name = sanitize_key( $_POST['type'] );
     84        $page = empty( $_POST['page'] ) ? 0 : absint( $_POST['page'] );
     85        $items = $this->load_available_items_query( $obj_type, $obj_name, $page );
     86
     87        if ( is_wp_error( $items ) ) {
     88            wp_send_json_error( $items->get_error_code() );
     89        } else {
     90            wp_send_json_success( array( 'items' => $items ) );
     91        }
     92    }
     93
     94    /**
     95     * Performs the post_type and taxonomy queries for loading available menu items.
     96     *
     97     * @since 4.3.0
     98     * @access public
     99     *
     100     * @param string $obj_type Optional. Accepts any custom object type and has built-in support for
     101     *                         'post_type' and 'taxonomy'. Default is 'post_type'.
     102     * @param string $obj_name Optional. Accepts any registered taxonomy or post type name. Default is 'page'.
     103     * @param int    $page     Optional. The page number used to generate the query offset. Default is '0'.
     104     * @return WP_Error|array Returns either a WP_Error object or an array of menu items.
     105     */
     106    public function load_available_items_query( $obj_type = 'post_type', $obj_name = 'page', $page = 0 ) {
    89107        $items = array();
    90108
    91109        if ( 'post_type' === $obj_type ) {
    92             if ( ! get_post_type_object( $taxonomy_or_post_type ) ) {
    93                 wp_send_json_error( 'nav_menus_invalid_post_type' );
     110            if ( ! get_post_type_object( $obj_name ) ) {
     111                return new WP_Error( 'nav_menus_invalid_post_type' );
    94112            }
    95113
    96             if ( 0 === $page && 'page' === $taxonomy_or_post_type ) {
     114            if ( 0 === $page && 'page' === $obj_name ) {
    97115                // Add "Home" link. Treat as a page, but switch to custom on add.
    98116                $items[] = array(
     
    111129                'orderby'     => 'date',
    112130                'order'       => 'DESC',
    113                 'post_type'   => $taxonomy_or_post_type,
     131                'post_type'   => $obj_name,
    114132            ) );
    115133            foreach ( $posts as $post ) {
     
    130148            }
    131149        } elseif ( 'taxonomy' === $obj_type ) {
    132             $terms = get_terms( $taxonomy_or_post_type, array(
     150            $terms = get_terms( $obj_name, array(
    133151                'child_of'     => 0,
    134152                'exclude'      => '',
     
    143161            ) );
    144162            if ( is_wp_error( $terms ) ) {
    145                 wp_send_json_error( $terms->get_error_code() );
     163                return $terms;
    146164            }
    147165
     
    159177        }
    160178
    161         wp_send_json_success( array( 'items' => $items ) );
     179        return $items;
    162180    }
    163181
     
    185203
    186204        $s = sanitize_text_field( wp_unslash( $_POST['search'] ) );
    187         $results = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) );
    188 
    189         if ( empty( $results ) ) {
    190             wp_send_json_error( array( 'message' => __( 'No results found.' ) ) );
     205        $items = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) );
     206
     207        if ( empty( $items ) ) {
     208            wp_send_json_error( array( 'message' => __( 'No menu items found.' ) ) );
    191209        } else {
    192             wp_send_json_success( array( 'items' => $results ) );
     210            wp_send_json_success( array( 'items' => $items ) );
    193211        }
    194212    }
     
    203221     *
    204222     * @param array $args Optional. Accepts 'pagenum' and 's' (search) arguments.
    205      * @return array Results.
     223     * @return array Menu items.
    206224     */
    207225    public function search_available_items_query( $args = array() ) {
    208         $results = array();
     226        $items = array();
    209227
    210228        $post_type_objects = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
     
    236254                    $post_title = sprintf( __( '#%d (no title)' ), $post->ID );
    237255                }
    238                 $results[] = array(
     256                $items[] = array(
    239257                    'id'         => 'post-' . $post->ID,
    240258                    'title'      => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),
     
    259277        if ( ! empty( $terms ) ) {
    260278            foreach ( $terms as $term ) {
    261                 $results[] = array(
     279                $items[] = array(
    262280                    'id'         => 'term-' . $term->term_id,
    263281                    'title'      => html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) ),
     
    271289        }
    272290
    273         return $results;
     291        return $items;
    274292    }
    275293
Note: See TracChangeset for help on using the changeset viewer.