Make WordPress Core

Changeset 38624


Ignore:
Timestamp:
09/20/2016 12:46:54 AM (10 years ago)
Author:
westonruter
Message:

Customize: Let static_front_page section be contextually active based on whether there are any published pages.

If there are no pages when the customizer is opened, the static_front_page section will be hidden. As soon as a page is created in the customizer session, the static_front_page section will be revealed. Previously the section would not be registered if there were no pages. Page stubs created via nav menus will appear in the dropdown-pages controls for page_for_posts and page_on_front, and such page stubs will thus cause the static_front_page section to appear. Plugins that facilitate page creation in the customizer by filtering get_pages will also cause the section to appear.

See #34923.
Fixes #38013.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/js/customize-nav-menus.js

    r38618 r38624  
    105105                                api.Menus.insertedAutoDrafts.push( response.post_id );
    106106                                api( 'nav_menus_created_posts' ).set( _.clone( api.Menus.insertedAutoDrafts ) );
     107
     108                                if ( 'page' === params.post_type ) {
     109
     110                                        // Activate static front page controls as this could be the first page created.
     111                                        if ( api.section.has( 'static_front_page' ) ) {
     112                                                api.section( 'static_front_page' ).activate();
     113                                        }
     114
     115                                        // Add new page to dropdown-pages controls.
     116                                        api.control.each( function( control ) {
     117                                                var select;
     118                                                if ( 'dropdown-pages' === control.params.type ) {
     119                                                        select = control.container.find( 'select[name^="_customize-dropdown-pages-"]' );
     120                                                        select.append( new Option( params.post_title, response.post_id ) );
     121                                                }
     122                                        } );
     123                                }
    107124                        }
    108125                } );
  • trunk/src/wp-includes/class-wp-customize-control.php

    r38470 r38624  
    534534                                <?php endif; ?>
    535535
    536                                 <?php $dropdown = wp_dropdown_pages(
     536                                <?php
     537                                $dropdown_name = '_customize-dropdown-pages-' . $this->id;
     538                                $show_option_none = __( '&mdash; Select &mdash;' );
     539                                $option_none_value = '0';
     540                                $dropdown = wp_dropdown_pages(
    537541                                        array(
    538                                                 'name'              => '_customize-dropdown-pages-' . $this->id,
     542                                                'name'              => $dropdown_name,
    539543                                                'echo'              => 0,
    540                                                 'show_option_none'  => __( '&mdash; Select &mdash;' ),
    541                                                 'option_none_value' => '0',
     544                                                'show_option_none'  => $show_option_none,
     545                                                'option_none_value' => $option_none_value,
    542546                                                'selected'          => $this->value(),
    543547                                        )
    544548                                );
     549                                if ( empty( $dropdown ) ) {
     550                                        $dropdown = sprintf( '<select id="%1$s" name="%1$s">', esc_attr( $dropdown_name ) );
     551                                        $dropdown .= sprintf( '<option value="%1$s">%2$s</option>', esc_attr( $option_none_value ), esc_html( $show_option_none ) );
     552                                        $dropdown .= '</select>';
     553                                }
    545554
    546555                                // Hackily add in the data link parameter.
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r38513 r38624  
    22552255                }
    22562256
    2257                 /* Static Front Page */
    2258                 // #WP19627
    2259 
    2260                 // Replicate behavior from options-reading.php and hide front page options if there are no pages
    2261                 if ( get_pages() ) {
    2262                         $this->add_section( 'static_front_page', array(
    2263                                 'title'          => __( 'Static Front Page' ),
    2264                         //      'theme_supports' => 'static-front-page',
    2265                                 'priority'       => 120,
    2266                                 'description'    => __( 'Your theme supports a static front page.' ),
    2267                         ) );
    2268 
    2269                         $this->add_setting( 'show_on_front', array(
    2270                                 'default'        => get_option( 'show_on_front' ),
    2271                                 'capability'     => 'manage_options',
    2272                                 'type'           => 'option',
    2273                         //      'theme_supports' => 'static-front-page',
    2274                         ) );
    2275 
    2276                         $this->add_control( 'show_on_front', array(
    2277                                 'label'   => __( 'Front page displays' ),
    2278                                 'section' => 'static_front_page',
    2279                                 'type'    => 'radio',
    2280                                 'choices' => array(
    2281                                         'posts' => __( 'Your latest posts' ),
    2282                                         'page'  => __( 'A static page' ),
    2283                                 ),
    2284                         ) );
    2285 
    2286                         $this->add_setting( 'page_on_front', array(
    2287                                 'type'       => 'option',
    2288                                 'capability' => 'manage_options',
    2289                         //      'theme_supports' => 'static-front-page',
    2290                         ) );
    2291 
    2292                         $this->add_control( 'page_on_front', array(
    2293                                 'label'      => __( 'Front page' ),
    2294                                 'section'    => 'static_front_page',
    2295                                 'type'       => 'dropdown-pages',
    2296                         ) );
    2297 
    2298                         $this->add_setting( 'page_for_posts', array(
    2299                                 'type'           => 'option',
    2300                                 'capability'     => 'manage_options',
    2301                         //      'theme_supports' => 'static-front-page',
    2302                         ) );
    2303 
    2304                         $this->add_control( 'page_for_posts', array(
    2305                                 'label'      => __( 'Posts page' ),
    2306                                 'section'    => 'static_front_page',
    2307                                 'type'       => 'dropdown-pages',
    2308                         ) );
    2309                 }
     2257                /*
     2258                 * Static Front Page
     2259                 * See also https://core.trac.wordpress.org/ticket/19627 which introduces the the static-front-page theme_support.
     2260                 * The following replicates behavior from options-reading.php.
     2261                 */
     2262
     2263                $this->add_section( 'static_front_page', array(
     2264                        'title' => __( 'Static Front Page' ),
     2265                        'priority' => 120,
     2266                        'description' => __( 'Your theme supports a static front page.' ),
     2267                        'active_callback' => array( $this, 'has_published_pages' ),
     2268                ) );
     2269
     2270                $this->add_setting( 'show_on_front', array(
     2271                        'default' => get_option( 'show_on_front' ),
     2272                        'capability' => 'manage_options',
     2273                        'type' => 'option',
     2274                ) );
     2275
     2276                $this->add_control( 'show_on_front', array(
     2277                        'label' => __( 'Front page displays' ),
     2278                        'section' => 'static_front_page',
     2279                        'type' => 'radio',
     2280                        'choices' => array(
     2281                                'posts' => __( 'Your latest posts' ),
     2282                                'page'  => __( 'A static page' ),
     2283                        ),
     2284                ) );
     2285
     2286                $this->add_setting( 'page_on_front', array(
     2287                        'type'       => 'option',
     2288                        'capability' => 'manage_options',
     2289                ) );
     2290
     2291                $this->add_control( 'page_on_front', array(
     2292                        'label' => __( 'Front page' ),
     2293                        'section' => 'static_front_page',
     2294                        'type' => 'dropdown-pages',
     2295                ) );
     2296
     2297                $this->add_setting( 'page_for_posts', array(
     2298                        'type' => 'option',
     2299                        'capability' => 'manage_options',
     2300                ) );
     2301
     2302                $this->add_control( 'page_for_posts', array(
     2303                        'label' => __( 'Posts page' ),
     2304                        'section' => 'static_front_page',
     2305                        'type' => 'dropdown-pages',
     2306                ) );
     2307        }
     2308
     2309        /**
     2310         * Return whether there are published pages.
     2311         *
     2312         * Used as active callback for static front page section and controls.
     2313         *
     2314         * @access private
     2315         * @since 4.7.0
     2316         *
     2317         * @returns bool Whether there are published (or to be published) pages.
     2318         */
     2319        public function has_published_pages() {
     2320
     2321                $setting = $this->get_setting( 'nav_menus_created_posts' );
     2322                if ( $setting ) {
     2323                        foreach ( $setting->value() as $post_id ) {
     2324                                if ( 'page' === get_post_type( $post_id ) ) {
     2325                                        return true;
     2326                                }
     2327                        }
     2328                }
     2329
     2330                return 0 !== count( get_pages() );
    23102331        }
    23112332
  • trunk/tests/phpunit/tests/customize/manager.php

    r38513 r38624  
    400400
    401401        /**
     402         * Test WP_Customize_Manager::has_published_pages().
     403         *
     404         * @ticket 38013
     405         * @covers WP_Customize_Manager::has_published_pages()
     406         */
     407        function test_has_published_pages() {
     408                foreach ( get_pages() as $page ) {
     409                        wp_delete_post( $page->ID, true );
     410                }
     411                $this->assertFalse( $this->manager->has_published_pages() );
     412
     413                $this->factory()->post->create( array( 'post_type' => 'page', 'post_status' => 'private' ) );
     414                $this->assertFalse( $this->manager->has_published_pages() );
     415
     416                $this->factory()->post->create( array( 'post_type' => 'page', 'post_status' => 'publish' ) );
     417                $this->assertTrue( $this->manager->has_published_pages() );
     418        }
     419
     420        /**
     421         * Ensure that page stubs created via nav menus will cause has_published_pages to return true.
     422         *
     423         * @ticket 38013
     424         * @covers WP_Customize_Manager::has_published_pages()
     425         */
     426        function test_has_published_pages_when_nav_menus_created_posts() {
     427                foreach ( get_pages() as $page ) {
     428                        wp_delete_post( $page->ID, true );
     429                }
     430                $this->assertFalse( $this->manager->has_published_pages() );
     431
     432                wp_set_current_user( $this->factory()->user->create( array( 'role' => 'editor' ) ) );
     433                $this->manager->nav_menus->customize_register();
     434                $setting_id = 'nav_menus_created_posts';
     435                $setting = $this->manager->get_setting( $setting_id );
     436                $this->assertInstanceOf( 'WP_Customize_Filter_Setting', $setting );
     437                $auto_draft_page = $this->factory()->post->create( array( 'post_type' => 'page', 'post_status' => 'auto-draft' ) );
     438                $this->manager->set_post_value( $setting_id, array( $auto_draft_page ) );
     439                $setting->preview();
     440                $this->assertTrue( $this->manager->has_published_pages() );
     441        }
     442
     443        /**
    402444         * Test the WP_Customize_Manager::register_dynamic_settings() method.
    403445         *
Note: See TracChangeset for help on using the changeset viewer.