Make WordPress Core

Ticket #32687: 32687.diff

File 32687.diff, 9.8 KB (added by valendesigns, 11 years ago)
  • src/wp-includes/class-wp-customize-nav-menus.php

    diff --git src/wp-includes/class-wp-customize-nav-menus.php src/wp-includes/class-wp-customize-nav-menus.php
    index 3d9e60f..2dfd708 100644
    final class WP_Customize_Nav_Menus { 
    7777                }
    7878
    7979                $obj_type = sanitize_key( $_POST['obj_type'] );
    80                 if ( ! in_array( $obj_type, array( 'post_type', 'taxonomy' ) ) ) {
    81                         wp_send_json_error( array( 'message' => __( 'Invalid obj_type param: ' . $obj_type ) ) );
    82                 }
    83                 $taxonomy_or_post_type = sanitize_key( $_POST['type'] );
     80                $obj_name = sanitize_key( $_POST['type'] );
    8481                $page = isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 0;
     82                $items = $this->load_available_items_query( $obj_type, $obj_name, $page );
     83
     84                if ( is_wp_error( $items ) ) {
     85                        wp_send_json_error( array( 'message' => wp_strip_all_tags( $items->get_error_message(), true ) ) );
     86                }
     87
     88                wp_send_json_success( array( 'items' => $items ) );
     89        }
     90
     91        /**
     92         * Performs post & term queries for loading available menu items.
     93         *
     94         * @since 4.3.0
     95         *
     96         * @param string $obj_type Optional. Accepts 'post_type' or 'taxonomy'. Default is 'post_type'.
     97         * @param string $obj_name Optional. Accepts any registered taxonomy or post type name. Default is 'page'.
     98         * @param int $page Optional. The page number used to generate the query offset. Default is '0'.
     99         * @return WP_Error|array Returns either a WP_Error object or an array of menu items.
     100         */
     101        public function load_available_items_query( $obj_type = 'post_type', $obj_name = 'page', $page = 0 ) {
    85102                $items = array();
    86103
     104                if ( ! in_array( $obj_type, array( 'post_type', 'taxonomy' ) ) ) {
     105                        return new WP_Error( 'invalid_obj_type', __( 'Invalid obj_type param: ' . $obj_type ) );
     106                }
     107
    87108                if ( 'post_type' === $obj_type ) {
    88                         if ( ! get_post_type_object( $taxonomy_or_post_type ) ) {
    89                                 wp_send_json_error( array( 'message' => __( 'Unknown post type.' ) ) );
     109                        if ( ! get_post_type_object( $obj_name ) ) {
     110                                return new WP_Error( 'unknown_post_type', __( 'Unknown post type.' ) );
    90111                        }
    91112
    92                         if ( 0 === $page && 'page' === $taxonomy_or_post_type ) {
     113                        if ( 0 === $page && 'page' === $obj_name ) {
    93114                                // Add "Home" link. Treat as a page, but switch to custom on add.
    94115                                $items[] = array(
    95116                                        'id'         => 'home',
    final class WP_Customize_Nav_Menus { 
    106127                                'offset'      => 10 * $page,
    107128                                'orderby'     => 'date',
    108129                                'order'       => 'DESC',
    109                                 'post_type'   => $taxonomy_or_post_type,
     130                                'post_type'   => $obj_name,
    110131                        ) );
    111132                        foreach ( $posts as $post ) {
    112133                                $post_title = $post->post_title;
    final class WP_Customize_Nav_Menus { 
    124145                                );
    125146                        }
    126147                } elseif ( 'taxonomy' === $obj_type ) {
    127                         $terms = get_terms( $taxonomy_or_post_type, array(
     148                        $terms = get_terms( $obj_name, array(
    128149                                'child_of'     => 0,
    129150                                'exclude'      => '',
    130151                                'hide_empty'   => false,
    final class WP_Customize_Nav_Menus { 
    137158                                'pad_counts'   => false,
    138159                        ) );
    139160                        if ( is_wp_error( $terms ) ) {
    140                                 wp_send_json_error( array( 'message' => wp_strip_all_tags( $terms->get_error_message(), true ) ) );
     161                                return $terms;
    141162                        }
    142163
    143164                        foreach ( $terms as $term ) {
    final class WP_Customize_Nav_Menus { 
    152173                        }
    153174                }
    154175
    155                 wp_send_json_success( array( 'items' => $items ) );
     176                return $items;
    156177        }
    157178
    158179        /**
    final class WP_Customize_Nav_Menus { 
    176197                }
    177198
    178199                $s = sanitize_text_field( wp_unslash( $_POST['search'] ) );
    179                 $results = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) );
     200                $items = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) );
    180201
    181                 if ( empty( $results ) ) {
    182                         wp_send_json_error( array( 'message' => __( 'No results found.' ) ) );
     202                if ( empty( $items ) ) {
     203                        wp_send_json_error( array( 'message' => __( 'No menu items found.' ) ) );
    183204                } else {
    184                         wp_send_json_success( array( 'items' => $results ) );
     205                        wp_send_json_success( array( 'items' => $items ) );
    185206                }
    186207        }
    187208
    final class WP_Customize_Nav_Menus { 
    193214         * @since 4.3.0
    194215         *
    195216         * @param array $args Optional. Accepts 'pagenum' and 's' (search) arguments.
    196          * @return array Results.
     217         * @return array Menu items.
    197218         */
    198219        public function search_available_items_query( $args = array() ) {
    199                 $results = array();
     220                $items = array();
    200221
    201222                $post_type_objects = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
    202223                $query = array(
    final class WP_Customize_Nav_Menus { 
    226247                                        /* translators: %d: ID of a post */
    227248                                        $post_title = sprintf( __( '#%d (no title)' ), $post->ID );
    228249                                }
    229                                 $results[] = array(
     250                                $items[] = array(
    230251                                        'id'         => 'post-' . $post->ID,
    231252                                        'type'       => 'post_type',
    232253                                        'type_label' => $post_type_objects[ $post->post_type ]->labels->singular_name,
    final class WP_Customize_Nav_Menus { 
    248269                // Check if any taxonomies were found.
    249270                if ( ! empty( $terms ) ) {
    250271                        foreach ( $terms as $term ) {
    251                                 $results[] = array(
     272                                $items[] = array(
    252273                                        'id'         => 'term-' . $term->term_id,
    253274                                        'type'       => 'taxonomy',
    254275                                        'type_label' => get_taxonomy( $term->taxonomy )->labels->singular_name,
    final class WP_Customize_Nav_Menus { 
    259280                        }
    260281                }
    261282
    262                 return $results;
     283                return $items;
    263284        }
    264285
    265286        /**
  • tests/phpunit/tests/customize/nav-menus.php

    diff --git tests/phpunit/tests/customize/nav-menus.php tests/phpunit/tests/customize/nav-menus.php
    index 0cb6fe6..37bd729 100644
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    4949        }
    5050
    5151        /**
    52          * Test the test_load_available_items_ajax method.
     52         * Test that the load_available_items_query method returns a WP_Error object.
    5353         *
    54          * @see WP_Customize_Nav_Menus::load_available_items_ajax()
     54         * @see WP_Customize_Nav_Menus::load_available_items_query()
    5555         */
    56         function test_load_available_items_ajax() {
     56        function test_load_available_items_query_returns_wp_error() {
     57                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     58
     59                // Invalid $obj_type.
     60                $items = $menus->load_available_items_query( 'invalid' );
     61                $this->assertInstanceOf( 'WP_Error', $items );
    5762
    58                 $this->markTestIncomplete( 'This test has not been implemented.' );
     63                // Invalid post type $obj_name.
     64                $items = $menus->load_available_items_query( 'post_type', 'invalid' );
     65                $this->assertInstanceOf( 'WP_Error', $items );
    5966
     67                // Invalid taxonomy $obj_name.
     68                $items = $menus->load_available_items_query( 'taxonomy', 'invalid' );
     69                $this->assertInstanceOf( 'WP_Error', $items );
    6070        }
    6171
    6272        /**
    63          * Test the search_available_items_ajax method.
     73         * Test the load_available_items_query method returns the home page item.
    6474         *
    65          * @see WP_Customize_Nav_Menus::search_available_items_ajax()
     75         * @see WP_Customize_Nav_Menus::load_available_items_query()
    6676         */
    67         function test_search_available_items_ajax() {
     77        function test_load_available_items_query_returns_home() {
     78                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
    6879
    69                 $this->markTestIncomplete( 'This test has not been implemented.' );
     80                // Home is included in menu items.
     81                $items = $menus->load_available_items_query();
     82                $expected = array(
     83                        'id'         => 'home',
     84                        'title'      => _x( 'Home', 'nav menu home label' ),
     85                        'type'       => 'custom',
     86                        'type_label' => __( 'Custom Link' ),
     87                        'object'     => '',
     88                        'url'        => home_url(),
     89                );
     90                $this->assertContains( $expected, $items );
     91        }
    7092
     93        /**
     94         * Test the load_available_items_query method returns post item.
     95         *
     96         * @see WP_Customize_Nav_Menus::load_available_items_query()
     97         */
     98        function test_load_available_items_query_returns_page_item() {
     99                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     100
     101                // Create page
     102                $page_id = $this->factory->post->create( array( 'post_title' => 'Page Title', 'post_type' => 'page' ) );
     103                $items = $menus->load_available_items_query( 'post_type', 'page', 0 );
     104                $expected = array(
     105                        'id'         => "post-{$page_id}",
     106                        'title'      => 'Page Title',
     107                        'type'       => 'post_type',
     108                        'type_label' => 'Page',
     109                        'object'     => 'page',
     110                        'object_id'  => $page_id,
     111                );
     112                $this->assertContains( $expected, $items );
     113        }
     114
     115        /**
     116         * Test the load_available_items_query method returns post item.
     117         *
     118         * @see WP_Customize_Nav_Menus::load_available_items_query()
     119         */
     120        function test_load_available_items_query_returns_post_item() {
     121                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     122
     123                // Create post
     124                $post_id = $this->factory->post->create( array( 'post_title' => 'Post Title' ) );
     125                $items = $menus->load_available_items_query( 'post_type', 'post', 0 );
     126                $expected = array(
     127                        'id'         => "post-{$post_id}",
     128                        'title'      => 'Post Title',
     129                        'type'       => 'post_type',
     130                        'type_label' => 'Post',
     131                        'object'     => 'post',
     132                        'object_id'  => $post_id,
     133                );
     134                $this->assertContains( $expected, $items );
     135        }
     136
     137        /**
     138         * Test the load_available_items_query method returns term item.
     139         *
     140         * @see WP_Customize_Nav_Menus::load_available_items_query()
     141         */
     142        function test_load_available_items_query_returns_term_item() {
     143                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     144
     145                // Create term
     146                $term_id = $this->factory->category->create( array( 'name' => 'Term Title' ) );
     147                $items = $menus->load_available_items_query( 'taxonomy', 'category', 0 );
     148                $expected = array(
     149                        'id'         => "term-{$term_id}",
     150                        'title'      => 'Term Title',
     151                        'type'       => 'taxonomy',
     152                        'type_label' => 'Category',
     153                        'object'     => 'category',
     154                        'object_id'  => $term_id,
     155                );
     156                $this->assertContains( $expected, $items );
    71157        }
    72158
    73159        /**
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    447533
    448534        }
    449535
    450         /**
    451          * Test the render_menu method.
    452          *
    453          * @see WP_Customize_Nav_Menus::render_menu()
    454          */
    455         function test_render_menu() {
    456 
    457                 $this->markTestIncomplete( 'This test has not been implemented.' );
    458         }
    459 
    460536}