Make WordPress Core

Ticket #14439: 14439.patch

File 14439.patch, 3.9 KB (added by tyxla, 9 years ago)

Refreshing and improving the patch, adding unit tests

  • src/wp-includes/nav-menu.php

     
    780780 * @since 3.0.0
    781781 *
    782782 * @param int $object_id The ID of the original object.
    783  * @param string $object_type The type of object, such as "taxonomy" or "post_type."
    784  * @param string $taxonomy If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
    785  * @return array The array of menu item IDs; empty array if none;
     783 * @param string $object_type The type of object, such as "taxonomy" or "post_type".
     784 * @param string $taxonomy If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to.
     785 * @param int $menu_id The ID of the menu to search into. "0" will search in all menus.
     786 * @return array The array of menu item IDs; empty array if none.
    786787 */
    787 function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
     788function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '', $menu_id = 0 ) {
    788789        $object_id = (int) $object_id;
    789790        $menu_item_ids = array();
    790791
     792        $query_args = array(
     793                'meta_key' => '_menu_item_object_id',
     794                'meta_value' => $object_id,
     795                'post_status' => 'any',
     796                'post_type' => 'nav_menu_item',
     797                'posts_per_page' => -1,
     798        );
     799
     800        if ( $menu_id ) {
     801                $term = get_term( $menu_id, 'nav_menu' );
     802                if ( !$term || is_wp_error($term) ) {
     803                        return new WP_Error( 'invalid_menu_id', __( 'Invalid menu ID.' ) );
     804                }
     805
     806                $query_args['taxonomy'] = 'nav_menu';
     807                $query_args['term'] = $term->slug;
     808        }
     809
    791810        $query = new WP_Query;
    792         $menu_items = $query->query(
    793                 array(
    794                         'meta_key' => '_menu_item_object_id',
    795                         'meta_value' => $object_id,
    796                         'post_status' => 'any',
    797                         'post_type' => 'nav_menu_item',
    798                         'posts_per_page' => -1,
    799                 )
    800         );
     811        $menu_items = $query->query($query_args);
     812
    801813        foreach( (array) $menu_items as $menu_item ) {
    802814                if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
    803815                        $menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
  • tests/phpunit/tests/post/nav-menu.php

     
    9090        }
    9191
    9292        /**
     93         * @ticket 14439
     94         */
     95        function test_wp_get_associated_nav_menu_items_menu_id() {
     96
     97                // Create the second menu
     98                $menu_id_2 = wp_create_nav_menu( rand_str() );
     99
     100                // Create our post object
     101                $post_id = $this->factory->post->create();
     102
     103                // Insert the menu item into the first menu
     104                $post_insert_1 = wp_update_nav_menu_item( $this->menu_id, 0, array(
     105                        'menu-item-type' => 'post_type',
     106                        'menu-item-object' => 'post',
     107                        'menu-item-object-id' => $post_id,
     108                        'menu-item-status' => 'publish'
     109                ) );
     110
     111                // Insert the menu item into the second menu
     112                $post_insert_2 = wp_update_nav_menu_item( $menu_id_2, 0, array(
     113                        'menu-item-type' => 'post_type',
     114                        'menu-item-object' => 'post',
     115                        'menu-item-object-id' => $post_id,
     116                        'menu-item-status' => 'publish'
     117                ) );
     118
     119                $post_items = wp_get_associated_nav_menu_items( $post_id, 'post_type', '', $menu_id_2);
     120                $this->assertEqualSets( array( $post_insert_2 ), $post_items );
     121        }
     122
     123        /**
     124         * @ticket 14439
     125         */
     126        function test_wp_get_associated_nav_menu_items_wrong_menu_id() {
     127
     128                $post_id = $this->factory->post->create();
     129
     130                $post_insert = wp_update_nav_menu_item( $this->menu_id, 0, array(
     131                        'menu-item-type' => 'post_type',
     132                        'menu-item-object' => 'post',
     133                        'menu-item-object-id' => $post_id,
     134                        'menu-item-status' => 'publish'
     135                ) );
     136
     137                $post_items = wp_get_associated_nav_menu_items( $post_id, 'post_type', '', 12345678);
     138                $this->assertInstanceOf( 'WP_Error', $post_items );
     139        }
     140
     141        /**
    93142         * @ticket 27113
    94143         */
    95144        function test_orphan_nav_menu_item() {