Make WordPress Core

Ticket #32687: 32687.2.diff

File 32687.2.diff, 11.7 KB (added by valendesigns, 11 years ago)

Move param sanitization to the correct method and clean up unit tests.

  • 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..c30b67f 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 { 
    124152                                );
    125153                        }
    126154                } elseif ( 'taxonomy' === $obj_type ) {
    127                         $terms = get_terms( $taxonomy_or_post_type, array(
     155                        $terms = get_terms( $obj_name, array(
    128156                                'child_of'     => 0,
    129157                                'exclude'      => '',
    130158                                'hide_empty'   => false,
    final class WP_Customize_Nav_Menus { 
    137165                                'pad_counts'   => false,
    138166                        ) );
    139167                        if ( is_wp_error( $terms ) ) {
    140                                 wp_send_json_error( array( 'message' => wp_strip_all_tags( $terms->get_error_message(), true ) ) );
     168                                return $terms;
    141169                        }
    142170
    143171                        foreach ( $terms as $term ) {
    final class WP_Customize_Nav_Menus { 
    152180                        }
    153181                }
    154182
    155                 wp_send_json_success( array( 'items' => $items ) );
     183                return $items;
    156184        }
    157185
    158186        /**
    final class WP_Customize_Nav_Menus { 
    176204                }
    177205
    178206                $s = sanitize_text_field( wp_unslash( $_POST['search'] ) );
    179                 $results = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) );
     207                $items = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) );
    180208
    181                 if ( empty( $results ) ) {
    182                         wp_send_json_error( array( 'message' => __( 'No results found.' ) ) );
     209                if ( empty( $items ) ) {
     210                        wp_send_json_error( array( 'message' => __( 'No menu items found.' ) ) );
    183211                } else {
    184                         wp_send_json_success( array( 'items' => $results ) );
     212                        wp_send_json_success( array( 'items' => $items ) );
    185213                }
    186214        }
    187215
    final class WP_Customize_Nav_Menus { 
    193221         * @since 4.3.0
    194222         *
    195223         * @param array $args Optional. Accepts 'pagenum' and 's' (search) arguments.
    196          * @return array Results.
     224         * @return array Menu items.
    197225         */
    198226        public function search_available_items_query( $args = array() ) {
    199                 $results = array();
     227                $items = array();
    200228
    201229                $post_type_objects = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
    202230                $query = array(
    final class WP_Customize_Nav_Menus { 
    226254                                        /* translators: %d: ID of a post */
    227255                                        $post_title = sprintf( __( '#%d (no title)' ), $post->ID );
    228256                                }
    229                                 $results[] = array(
     257                                $items[] = array(
    230258                                        'id'         => 'post-' . $post->ID,
    231259                                        'type'       => 'post_type',
    232260                                        'type_label' => $post_type_objects[ $post->post_type ]->labels->singular_name,
    final class WP_Customize_Nav_Menus { 
    248276                // Check if any taxonomies were found.
    249277                if ( ! empty( $terms ) ) {
    250278                        foreach ( $terms as $term ) {
    251                                 $results[] = array(
     279                                $items[] = array(
    252280                                        'id'         => 'term-' . $term->term_id,
    253281                                        'type'       => 'taxonomy',
    254282                                        'type_label' => get_taxonomy( $term->taxonomy )->labels->singular_name,
    final class WP_Customize_Nav_Menus { 
    259287                        }
    260288                }
    261289
    262                 return $results;
     290                return $items;
    263291        }
    264292
    265293        /**
  • 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..c85edc0 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'  => $post_id,
     125                );
     126
     127                // Offset the query and get the second page of menu items.
     128                $items = $menus->load_available_items_query( 'post_type', 'post', 1 );
     129                $this->assertContains( $expected, $items );
     130        }
    57131
    58                 $this->markTestIncomplete( 'This test has not been implemented.' );
     132        /**
     133         * Test the load_available_items_query method returns page item.
     134         *
     135         * @see WP_Customize_Nav_Menus::load_available_items_query()
     136         */
     137        function test_load_available_items_query_returns_page_item() {
     138                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     139
     140                // Create page.
     141                $page_id = $this->factory->post->create( array( 'post_title' => 'Page Title', 'post_type' => 'page' ) );
     142
     143                // Expected menu item array.
     144                $expected = array(
     145                        'id'         => "post-{$page_id}",
     146                        'title'      => 'Page Title',
     147                        'type'       => 'post_type',
     148                        'type_label' => 'Page',
     149                        'object'     => 'page',
     150                        'object_id'  => $page_id,
     151                );
    59152
     153                $items = $menus->load_available_items_query( 'post_type', 'page', 0 );
     154                $this->assertContains( $expected, $items );
    60155        }
    61156
    62157        /**
    63          * Test the search_available_items_ajax method.
     158         * Test the load_available_items_query method returns post item.
    64159         *
    65          * @see WP_Customize_Nav_Menus::search_available_items_ajax()
     160         * @see WP_Customize_Nav_Menus::load_available_items_query()
    66161         */
    67         function test_search_available_items_ajax() {
     162        function test_load_available_items_query_returns_post_item() {
     163                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     164
     165                // Create post.
     166                $post_id = $this->factory->post->create( array( 'post_title' => 'Post Title' ) );
    68167
    69                 $this->markTestIncomplete( 'This test has not been implemented.' );
     168                // Expected menu item array.
     169                $expected = array(
     170                        'id'         => "post-{$post_id}",
     171                        'title'      => 'Post Title',
     172                        'type'       => 'post_type',
     173                        'type_label' => 'Post',
     174                        'object'     => 'post',
     175                        'object_id'  => $post_id,
     176                );
    70177
     178                $items = $menus->load_available_items_query( 'post_type', 'post', 0 );
     179                $this->assertContains( $expected, $items );
     180        }
     181
     182        /**
     183         * Test the load_available_items_query method returns term item.
     184         *
     185         * @see WP_Customize_Nav_Menus::load_available_items_query()
     186         */
     187        function test_load_available_items_query_returns_term_item() {
     188                $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     189
     190                // Create term.
     191                $term_id = $this->factory->category->create( array( 'name' => 'Term Title' ) );
     192
     193                // Expected menu item array.
     194                $expected = array(
     195                        'id'         => "term-{$term_id}",
     196                        'title'      => 'Term Title',
     197                        'type'       => 'taxonomy',
     198                        'type_label' => 'Category',
     199                        'object'     => 'category',
     200                        'object_id'  => $term_id,
     201                );
     202
     203                $items = $menus->load_available_items_query( 'taxonomy', 'category', 0 );
     204                $this->assertContains( $expected, $items );
    71205        }
    72206
    73207        /**
    class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { 
    447581
    448582        }
    449583
    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 
    460584}