Make WordPress Core


Ignore:
Timestamp:
12/02/2016 12:24:19 AM (7 years ago)
Author:
westonruter
Message:

Customize: Reuse existing non-auto-draft posts and existing auto-draft posts in the customized state with matching slugs when applying starter content.

  • Updates wp_unique_post_slug() to ignore auto-draft posts. Prevents publishing multiple posts that have the same slugs from starter content.
  • Fixes fatal error when attempting to save an header_image setting from a non-admin context.
  • Fixes substituting attachment symbols in options and theme mods.
  • Fixes applying starter content for header images and background images.

See #38114.
Fixes #38928.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/customize/manager.php

    r39409 r39411  
    315315        wp_set_current_user( self::$admin_user_id );
    316316        register_nav_menu( 'top', 'Top' );
     317        add_theme_support( 'custom-logo' );
     318        add_theme_support( 'custom-header' );
     319        add_theme_support( 'custom-background' );
     320
     321        $canola_file = DIR_TESTDATA . '/images/canola.jpg';
     322        $existing_canola_attachment_id = self::factory()->attachment->create_object( $canola_file, 0, array(
     323            'post_mime_type' => 'image/jpeg',
     324            'post_type' => 'attachment',
     325            'post_name' => 'canola',
     326        ) );
     327        $existing_published_home_page_id = $this->factory()->post->create( array(
     328            'post_name' => 'home',
     329            'post_type' => 'page',
     330            'post_status' => 'publish'
     331        ) );
     332        $existing_auto_draft_about_page_id = $this->factory()->post->create( array(
     333            'post_name' => 'about',
     334            'post_type' => 'page',
     335            'post_status' => 'auto-draft'
     336        ) );
    317337
    318338        global $wp_customize;
     
    352372                    'post_type' => 'post',
    353373                    'post_title' => 'Custom',
    354                     'thumbnail' => '{{featured-image-logo}}',
     374                    'thumbnail' => '{{waffles}}',
    355375                ),
    356376            ),
    357377            'attachments' => array(
    358                 'featured-image-logo' => array(
    359                     'post_title' => 'Featured Image',
    360                     'post_content' => 'Attachment Description',
    361                     'post_excerpt' => 'Attachment Caption',
     378                'waffles' => array(
     379                    'post_title' => 'Waffles',
     380                    'post_content' => 'Waffles Attachment Description',
     381                    'post_excerpt' => 'Waffles Attachment Caption',
    362382                    'file' => DIR_TESTDATA . '/images/waffles.jpg',
     383                ),
     384                'canola' => array(
     385                    'post_title' => 'Canola',
     386                    'post_content' => 'Canola Attachment Description',
     387                    'post_excerpt' => 'Canola Attachment Caption',
     388                    'file' => DIR_TESTDATA . '/images/canola.jpg',
    363389                ),
    364390            ),
     
    369395                'page_on_front'  => '{{home}}',
    370396                'page_for_posts' => '{{blog}}',
     397            ),
     398            'theme_mods' => array(
     399                'custom_logo' => '{{canola}}',
     400                'header_image' => '{{waffles}}',
     401                'background_image' => '{{waffles}}',
    371402            ),
    372403        );
     
    379410            'blogname',
    380411            'blogdescription',
     412            'custom_logo',
     413            'header_image_data',
     414            'background_image',
    381415            'widget_text[2]',
    382416            'widget_meta[3]',
     
    407441
    408442        $posts_by_name = array();
    409         $this->assertCount( 5, $changeset_values['nav_menus_created_posts'] );
     443        $this->assertCount( 6, $changeset_values['nav_menus_created_posts'] );
     444        $this->assertContains( $existing_published_home_page_id, $changeset_values['nav_menus_created_posts'], 'Expected reuse of non-auto-draft posts.' );
     445        $this->assertContains( $existing_canola_attachment_id, $changeset_values['nav_menus_created_posts'], 'Expected reuse of non-auto-draft attachment.' );
     446        $this->assertNotContains( $existing_auto_draft_about_page_id, $changeset_values['nav_menus_created_posts'], 'Expected non-reuse of auto-draft posts.' );
    410447        foreach ( $changeset_values['nav_menus_created_posts'] as $post_id ) {
    411448            $post = get_post( $post_id );
    412             $this->assertEquals( 'auto-draft', $post->post_status );
     449            if ( $post->ID === $existing_published_home_page_id ) {
     450                $this->assertEquals( 'publish', $post->post_status );
     451            } elseif ( $post->ID === $existing_canola_attachment_id ) {
     452                $this->assertEquals( 'inherit', $post->post_status );
     453            } else {
     454                $this->assertEquals( 'auto-draft', $post->post_status );
     455            }
    413456            $posts_by_name[ $post->post_name ] = $post->ID;
    414457        }
    415         $this->assertEquals( array( 'featured-image', 'home', 'about', 'blog', 'custom' ), array_keys( $posts_by_name ) );
     458        $this->assertEquals( array( 'waffles', 'canola', 'home', 'about', 'blog', 'custom' ), array_keys( $posts_by_name ) );
    416459        $this->assertEquals( 'Custom', get_post( $posts_by_name['custom'] )->post_title );
    417460        $this->assertEquals( 'sample-page-template.php', get_page_template_slug( $posts_by_name['about'] ) );
    418461        $this->assertEquals( '', get_page_template_slug( $posts_by_name['blog'] ) );
    419         $this->assertEquals( $posts_by_name['featured-image'], get_post_thumbnail_id( $posts_by_name['custom'] ) );
     462        $this->assertEquals( $posts_by_name['waffles'], get_post_thumbnail_id( $posts_by_name['custom'] ) );
    420463        $this->assertEquals( '', get_post_thumbnail_id( $posts_by_name['blog'] ) );
    421         $attachment_metadata = wp_get_attachment_metadata( $posts_by_name['featured-image'] );
    422         $this->assertEquals( 'Featured Image', get_post( $posts_by_name['featured-image'] )->post_title );
     464        $attachment_metadata = wp_get_attachment_metadata( $posts_by_name['waffles'] );
     465        $this->assertEquals( 'Waffles', get_post( $posts_by_name['waffles'] )->post_title );
    423466        $this->assertArrayHasKey( 'file', $attachment_metadata );
    424467        $this->assertContains( 'waffles', $attachment_metadata['file'] );
     
    473516
    474517        // Publish.
     518        $this->assertEmpty( get_custom_logo() );
     519        $this->assertEmpty( get_header_image() );
     520        $this->assertEmpty( get_background_image() );
     521        $this->assertEmpty( get_theme_mod( 'custom_logo' ) );
     522        $this->assertEmpty( get_theme_mod( 'header_image' ) );
     523        $this->assertEmpty( get_theme_mod( 'background_image' ) );
    475524        $this->assertEquals( 'auto-draft', get_post( $posts_by_name['about'] )->post_status );
    476         $this->assertEquals( 'auto-draft', get_post( $posts_by_name['featured-image'] )->post_status );
     525        $this->assertEquals( 'auto-draft', get_post( $posts_by_name['waffles'] )->post_status );
    477526        $this->assertNotEquals( $changeset_data['blogname']['value'], get_option( 'blogname' ) );
    478527        $r = $wp_customize->save_changeset_post( array( 'status' => 'publish' ) );
    479528        $this->assertInternalType( 'array', $r );
    480529        $this->assertEquals( 'publish', get_post( $posts_by_name['about'] )->post_status );
    481         $this->assertEquals( 'inherit', get_post( $posts_by_name['featured-image'] )->post_status );
     530        $this->assertEquals( 'inherit', get_post( $posts_by_name['waffles'] )->post_status );
    482531        $this->assertEquals( $changeset_data['blogname']['value'], get_option( 'blogname' ) );
     532        $this->assertNotEmpty( get_theme_mod( 'custom_logo' ) );
     533        $this->assertNotEmpty( get_theme_mod( 'header_image' ) );
     534        $this->assertNotEmpty( get_theme_mod( 'background_image' ) );
     535        $this->assertNotEmpty( get_custom_logo() );
     536        $this->assertNotEmpty( get_header_image() );
     537        $this->assertNotEmpty( get_background_image() );
     538        $this->assertContains( 'canola', get_custom_logo() );
     539        $this->assertContains( 'waffles', get_header_image() );
     540        $this->assertContains( 'waffles', get_background_image() );
    483541    }
    484542
Note: See TracChangeset for help on using the changeset viewer.