Make WordPress Core

Ticket #32687: 32687.3.diff

File 32687.3.diff, 12.0 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 7d32cbb..a70986e 100644
    final class WP_Customize_Nav_Menus { 
    7272                if ( ! current_user_can( 'edit_theme_options' ) ) {
    7373                        wp_send_json_error( array( 'message' => __( 'Error: invalid user capabilities.' ) ) );
    7474                }
     75
    7576                if ( empty( $_POST['obj_type'] ) || empty( $_POST['type'] ) ) {
    7677                        wp_send_json_error( array( 'message' => __( 'Missing obj_type or type param.' ) ) );
    7778                }
    7879
    79                 $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 ) ) );
     80                if ( empty( $_POST['page'] ) ) {
     81                        $_POST['page'] = 0;
     82                }
     83
     84                $items = $this->load_available_items_query( $_POST['obj_type'], $_POST['type'], $_POST['page'] );
     85
     86                if ( is_wp_error( $items ) ) {
     87                        wp_send_json_error( array( 'message' => wp_strip_all_tags( $items->get_error_message(), true ) ) );
    8288                }
    83                 $taxonomy_or_post_type = sanitize_key( $_POST['type'] );
    84                 $page = isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 0;
     89
     90                wp_send_json_success( array( 'items' => $items ) );
     91        }
     92
     93        /**
     94         * Performs the post_type and taxonomy queries for loading available menu items.
     95         *
     96         * @since 4.3.0
     97         *
     98         * @param string $obj_type Optional. Accepts 'post_type' or 'taxonomy'. Default is 'post_type'.
     99         * @param string $obj_name Optional. Accepts any registered taxonomy or post type name. Default is 'page'.
     100         * @param int $page Optional. The page number used to generate the query offset. Default is '0'.
     101         * @return WP_Error|array Returns either a WP_Error object or an array of menu items.
     102         */
     103        public function load_available_items_query( $obj_type = 'post_type', $obj_name = 'page', $page = 0 ) {
    85104                $items = array();
    86105
     106                // Sanitize params
     107                $obj_type = sanitize_key( $obj_type );
     108                $obj_name = sanitize_key( $obj_name );
     109                $page     = absint( $page );
     110
     111                if ( ! in_array( $obj_type, array( 'post_type', 'taxonomy' ) ) ) {
     112                        return new WP_Error( 'invalid_obj_type', __( 'Invalid obj_type param: ' . $obj_type ) );
     113                }
     114
    87115                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.' ) ) );
     116                        if ( ! get_post_type_object( $obj_name ) ) {
     117                                return new WP_Error( 'unknown_post_type', __( 'Unknown post type.' ) );
    90118                        }
    91119
    92                         if ( 0 === $page && 'page' === $taxonomy_or_post_type ) {
     120                        if ( 0 === $page && 'page' === $obj_name ) {
    93121                                // Add "Home" link. Treat as a page, but switch to custom on add.
    94122                                $items[] = array(
    95123                                        'id'         => 'home',
    final class WP_Customize_Nav_Menus { 
    106134                                'offset'      => 10 * $page,
    107135                                'orderby'     => 'date',
    108136                                'order'       => 'DESC',
    109                                 'post_type'   => $taxonomy_or_post_type,
     137                                'post_type'   => $obj_name,
    110138                        ) );
    111139                        foreach ( $posts as $post ) {
    112140                                $post_title = $post->post_title;
    final class WP_Customize_Nav_Menus { 
    125153                                );
    126154                        }
    127155                } elseif ( 'taxonomy' === $obj_type ) {
    128                         $terms = get_terms( $taxonomy_or_post_type, array(
     156                        $terms = get_terms( $obj_name, array(
    129157                                'child_of'     => 0,
    130158                                'exclude'      => '',
    131159                                'hide_empty'   => false,
    final class WP_Customize_Nav_Menus { 
    138166                                'pad_counts'   => false,
    139167                        ) );
    140168                        if ( is_wp_error( $terms ) ) {
    141                                 wp_send_json_error( array( 'message' => wp_strip_all_tags( $terms->get_error_message(), true ) ) );
     169                                return $terms;
    142170                        }
    143171
    144172                        foreach ( $terms as $term ) {
    final class WP_Customize_Nav_Menus { 
    154182                        }
    155183                }
    156184
    157                 wp_send_json_success( array( 'items' => $items ) );
     185                return $items;
    158186        }
    159187
    160188        /**
    final class WP_Customize_Nav_Menus { 
    178206                }
    179207
    180208                $s = sanitize_text_field( wp_unslash( $_POST['search'] ) );
    181                 $results = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) );
     209                $items = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) );
    182210
    183                 if ( empty( $results ) ) {
    184                         wp_send_json_error( array( 'message' => __( 'No results found.' ) ) );
     211                if ( empty( $items ) ) {
     212                        wp_send_json_error( array( 'message' => __( 'No menu items found.' ) ) );
    185213                } else {
    186                         wp_send_json_success( array( 'items' => $results ) );
     214                        wp_send_json_success( array( 'items' => $items ) );
    187215                }
    188216        }
    189217
    final class WP_Customize_Nav_Menus { 
    195223         * @since 4.3.0
    196224         *
    197225         * @param array $args Optional. Accepts 'pagenum' and 's' (search) arguments.
    198          * @return array Results.
     226         * @return array Menu items.
    199227         */
    200228        public function search_available_items_query( $args = array() ) {
    201                 $results = array();
     229                $items = array();
    202230
    203231                $post_type_objects = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
    204232                $query = array(
    final class WP_Customize_Nav_Menus { 
    228256                                        /* translators: %d: ID of a post */
    229257                                        $post_title = sprintf( __( '#%d (no title)' ), $post->ID );
    230258                                }
    231                                 $results[] = array(
     259                                $items[] = array(
    232260                                        'id'         => 'post-' . $post->ID,
    233261                                        'title'      => html_entity_decode( $post_title, ENT_QUOTES, get_bloginfo( 'charset' ) ),
    234262                                        'type'       => 'post_type',
    final class WP_Customize_Nav_Menus { 
    251279                // Check if any taxonomies were found.
    252280                if ( ! empty( $terms ) ) {
    253281                        foreach ( $terms as $term ) {
    254                                 $results[] = array(
     282                                $items[] = array(
    255283                                        'id'         => 'term-' . $term->term_id,
    256284                                        'title'      => html_entity_decode( $term->name, ENT_QUOTES, get_bloginfo( 'charset' ) ),
    257285                                        'type'       => 'taxonomy',
    final class WP_Customize_Nav_Menus { 
    263291                        }
    264292                }
    265293
    266                 return $results;
     294                return $items;
    267295        }
    268296
    269297        /**
  • tests/phpunit/tests/customize/nav-menus.php

    diff --git tests/phpunit/tests/customize/nav-menus.php tests/phpunit/tests/customize/nav-menus.php
    index 7c0ee3d..0b4c5c4 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 );
     62
     63                // Invalid post type $obj_name.
     64                $items = $menus->load_available_items_query( 'post_type', 'invalid' );
     65                $this->assertInstanceOf( 'WP_Error', $items );
     66
     67                // Invalid taxonomy $obj_name.
     68                $items = $menus->load_available_items_query( 'taxonomy', 'invalid' );
     69                $this->assertInstanceOf( 'WP_Error', $items );
     70        }
     71
     72        /**
     73         * Test the load_available_items_query method maybe returns the home page item.
     74         *
     75         * @see WP_Customize_Nav_Menus::load_available_items_query()
     76         */
     77        function test_load_available_items_query_maybe_returns_home() {
     78                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     79
     80                // Expected menu item array.
     81                $expected = array(
     82                        'id'         => 'home',
     83                        'title'      => _x( 'Home', 'nav menu home label' ),
     84                        'type'       => 'custom',
     85                        'type_label' => __( 'Custom Link' ),
     86                        'object'     => '',
     87                        'url'        => home_url(),
     88                );
     89
     90                // Create pages.
     91                $this->factory->post->create_many( 15, array( 'post_type' => 'page' ) );
     92
     93                // Home is included in menu items when page is zero.
     94                $items = $menus->load_available_items_query( 'post_type', 'page', 0 );
     95                $this->assertContains( $expected, $items );
     96
     97                // Home is not included in menu items when page is larger than zero.
     98                $items = $menus->load_available_items_query( 'post_type', 'page', 1 );
     99                $this->assertNotEmpty( $items );
     100                $this->assertNotContains( $expected, $items );
     101        }
     102
     103        /**
     104         * Test the load_available_items_query method returns post item.
     105         *
     106         * @see WP_Customize_Nav_Menus::load_available_items_query()
     107         */
     108        function test_load_available_items_query_returns_post_item_with_page_number() {
     109                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     110
     111                // Create page.
     112                $post_id = $this->factory->post->create( array( 'post_title' => 'Post Title' ) );
     113
     114                // Create pages.
     115                $this->factory->post->create_many( 10 );
     116
     117                // Expected menu item array.
     118                $expected = array(
     119                        'id'         => "post-{$post_id}",
     120                        'title'      => 'Post Title',
     121                        'type'       => 'post_type',
     122                        'type_label' => 'Post',
     123                        'object'     => 'post',
     124                        'object_id'  => intval( $post_id ),
     125                        'url'        => get_permalink( intval( $post_id ) ),
     126                );
     127
     128                // Offset the query and get the second page of menu items.
     129                $items = $menus->load_available_items_query( 'post_type', 'post', 1 );
     130                $this->assertContains( $expected, $items );
     131        }
    57132
    58                 $this->markTestIncomplete( 'This test has not been implemented.' );
     133        /**
     134         * Test the load_available_items_query method returns page item.
     135         *
     136         * @see WP_Customize_Nav_Menus::load_available_items_query()
     137         */
     138        function test_load_available_items_query_returns_page_item() {
     139                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     140
     141                // Create page.
     142                $page_id = $this->factory->post->create( array( 'post_title' => 'Page Title', 'post_type' => 'page' ) );
     143
     144                // Expected menu item array.
     145                $expected = array(
     146                        'id'         => "post-{$page_id}",
     147                        'title'      => 'Page Title',
     148                        'type'       => 'post_type',
     149                        'type_label' => 'Page',
     150                        'object'     => 'page',
     151                        'object_id'  => intval( $page_id ),
     152                        'url'        => get_permalink( intval( $page_id ) ),
     153                );
    59154
     155                $items = $menus->load_available_items_query( 'post_type', 'page', 0 );
     156                $this->assertContains( $expected, $items );
    60157        }
    61158
    62159        /**
    63          * Test the search_available_items_ajax method.
     160         * Test the load_available_items_query method returns post item.
    64161         *
    65          * @see WP_Customize_Nav_Menus::search_available_items_ajax()
     162         * @see WP_Customize_Nav_Menus::load_available_items_query()
    66163         */
    67         function test_search_available_items_ajax() {
     164        function test_load_available_items_query_returns_post_item() {
     165                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     166
     167                // Create post.
     168                $post_id = $this->factory->post->create( array( 'post_title' => 'Post Title' ) );
    68169
    69                 $this->markTestIncomplete( 'This test has not been implemented.' );
     170                // Expected menu item array.
     171                $expected = array(
     172                        'id'         => "post-{$post_id}",
     173                        'title'      => 'Post Title',
     174                        'type'       => 'post_type',
     175                        'type_label' => 'Post',
     176                        'object'     => 'post',
     177                        'object_id'  => intval( $post_id ),
     178                        'url'        => get_permalink( intval( $post_id ) ),
     179                );
    70180
     181                $items = $menus->load_available_items_query( 'post_type', 'post', 0 );
     182                $this->assertContains( $expected, $items );
     183        }
     184
     185        /**
     186         * Test the load_available_items_query method returns term item.
     187         *
     188         * @see WP_Customize_Nav_Menus::load_available_items_query()
     189         */
     190        function test_load_available_items_query_returns_term_item() {
     191                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     192
     193                // Create term.
     194                $term_id = $this->factory->category->create( array( 'name' => 'Term Title' ) );
     195
     196                // Expected menu item array.
     197                $expected = array(
     198                        'id'         => "term-{$term_id}",
     199                        'title'      => 'Term Title',
     200                        'type'       => 'taxonomy',
     201                        'type_label' => 'Category',
     202                        'object'     => 'category',
     203                        'object_id'  => intval( $term_id ),
     204                        'url'        => get_term_link( intval( $term_id ), 'category' ),
     205                );
     206
     207                $items = $menus->load_available_items_query( 'taxonomy', 'category', 0 );
     208                $this->assertContains( $expected, $items );
    71209        }
    72210
    73211        /**
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    449587
    450588        }
    451589
    452         /**
    453          * Test the render_menu method.
    454          *
    455          * @see WP_Customize_Nav_Menus::render_menu()
    456          */
    457         function test_render_menu() {
    458 
    459                 $this->markTestIncomplete( 'This test has not been implemented.' );
    460         }
    461 
    462590}