WordPress.org

Make WordPress Core

Changeset 39365


Ignore:
Timestamp:
11/27/16 03:04:35 (12 months ago)
Author:
westonruter
Message:

Customize: Fix logic for previewing the URL for nav_menu_item settings for terms and post type archives.

Fixes typo in args passed to get_term_link() which caused a fatal error due to this call returning a WP_Error which was set to url. Also fixes never-satisfiable condition for obtaining post type archive URL. Also ensures that WP_Error never leaks through as url by setting it to an empty string. Adds missing unit tests.

Amends [38991].
See #38114.
Fixes #38945.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/customize/class-wp-customize-nav-menu-item-setting.php

    r39345 r39365  
    605605 
    606606        // Ensure nav menu item URL is set according to linked object. 
    607         if ( ! empty( $post->object_id ) ) { 
    608             if ( 'post_type' === $post->type ) { 
    609                 $post->url = get_permalink( $post->object_id ); 
    610             } elseif ( 'post_type_archive' === $post->type && ! empty( $post->object ) ) { 
    611                 $post->url = get_post_type_archive_link( $post->object ); 
    612             } elseif ( 'taxonomy' == $post->type && ! empty( $post->object ) ) { 
    613                 $post->url = get_term_link( (int) $post->object, $post->object ); 
    614             } 
     607        if ( 'post_type' === $post->type && ! empty( $post->object_id ) ) { 
     608            $post->url = get_permalink( $post->object_id ); 
     609        } elseif ( 'taxonomy' === $post->type && ! empty( $post->object ) && ! empty( $post->object_id ) ) { 
     610            $post->url = get_term_link( (int) $post->object_id, $post->object ); 
     611        } elseif ( 'post_type_archive' === $post->type && ! empty( $post->object ) ) { 
     612            $post->url = get_post_type_archive_link( $post->object ); 
     613        } 
     614        if ( is_wp_error( $post->url ) ) { 
     615            $post->url = ''; 
    615616        } 
    616617 
  • trunk/tests/phpunit/tests/customize/nav-menu-item-setting.php

    r38858 r39365  
    835835 
    836836    /** 
     837     * Test WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item() to set url for posts, terms, and post type archives. 
     838     * 
     839     * @ticket 38945 
     840     * @covers WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item() 
     841     */ 
     842    function test_value_as_wp_post_nav_menu_item_term_urls() { 
     843        $term_id = self::factory()->term->create( array( 'taxonomy' => 'category' ) ); 
     844        register_post_type( 'press_release', array( 
     845            'has_archive' => true, 
     846        ) ); 
     847        $post_id = self::factory()->post->create( array( 'post_type' => 'press_release' ) ); 
     848 
     849        // Term. 
     850        $setting = new WP_Customize_Nav_Menu_Item_Setting( 
     851            $this->wp_customize, 
     852            'nav_menu_item[-1]' 
     853        ); 
     854        $this->wp_customize->set_post_value( $setting->id, array( 
     855            'type' => 'taxonomy', 
     856            'object' => 'category', 
     857            'object_id' => $term_id, 
     858            'title' => 'Category', 
     859            'url' => '', 
     860        ) ); 
     861        $setting->preview(); 
     862        $nav_menu_item = $setting->value_as_wp_post_nav_menu_item(); 
     863        $this->assertEquals( get_term_link( $term_id ), $nav_menu_item->url ); 
     864 
     865        // Post. 
     866        $setting = new WP_Customize_Nav_Menu_Item_Setting( 
     867            $this->wp_customize, 
     868            'nav_menu_item[-2]' 
     869        ); 
     870        $this->wp_customize->set_post_value( $setting->id, array( 
     871            'type' => 'post_type', 
     872            'object' => 'press_release', 
     873            'object_id' => $post_id, 
     874            'title' => 'PR', 
     875            'url' => '', 
     876        ) ); 
     877        $setting->preview(); 
     878        $nav_menu_item = $setting->value_as_wp_post_nav_menu_item(); 
     879        $this->assertEquals( get_permalink( $post_id ), $nav_menu_item->url ); 
     880 
     881        // Post type archive. 
     882        $setting = new WP_Customize_Nav_Menu_Item_Setting( 
     883            $this->wp_customize, 
     884            'nav_menu_item[-3]' 
     885        ); 
     886        $this->wp_customize->set_post_value( $setting->id, array( 
     887            'type' => 'post_type_archive', 
     888            'object' => 'press_release', 
     889            'title' => 'PR', 
     890            'url' => '', 
     891        ) ); 
     892        $setting->preview(); 
     893        $nav_menu_item = $setting->value_as_wp_post_nav_menu_item(); 
     894        $this->assertEquals( get_post_type_archive_link( 'press_release' ), $nav_menu_item->url ); 
     895    } 
     896 
     897    /** 
    837898     * Test WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item() where title is empty. 
    838899     * 
Note: See TracChangeset for help on using the changeset viewer.