WordPress.org

Make WordPress Core

Changeset 49203


Ignore:
Timestamp:
10/19/2020 03:38:11 PM (5 weeks ago)
Author:
SergeyBiryukov
Message:

Widgets: Introduce before_sidebar and after_sidebar arguments for register_sidebar().

Props deepaklalwani, flixos90, christophherr, dgwyer, markoheijnen, morganestes, audrasjb.
Fixes #19709.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/widgets.php

    r48839 r49203  
    220220 *
    221221 * @since 2.2.0
     222 * @since 5.6.0 Added the `before_sidebar` and `after_sidebar` arguments.
    222223 *
    223224 * @global array $wp_registered_sidebars Registered sidebars.
     
    226227 *     Optional. Array or string of arguments for the sidebar being registered.
    227228 *
    228  *     @type string $name          The name or title of the sidebar displayed in the Widgets
    229  *                                 interface. Default 'Sidebar $instance'.
    230  *     @type string $id            The unique identifier by which the sidebar will be called.
    231  *                                 Default 'sidebar-$instance'.
    232  *     @type string $description   Description of the sidebar, displayed in the Widgets interface.
    233  *                                 Default empty string.
    234  *     @type string $class         Extra CSS class to assign to the sidebar in the Widgets interface.
    235  *                                 Default empty.
    236  *     @type string $before_widget HTML content to prepend to each widget's HTML output when
    237  *                                 assigned to this sidebar. Default is an opening list item element.
    238  *     @type string $after_widget  HTML content to append to each widget's HTML output when
    239  *                                 assigned to this sidebar. Default is a closing list item element.
    240  *     @type string $before_title  HTML content to prepend to the sidebar title when displayed.
    241  *                                 Default is an opening h2 element.
    242  *     @type string $after_title   HTML content to append to the sidebar title when displayed.
    243  *                                 Default is a closing h2 element.
     229 *     @type string $name           The name or title of the sidebar displayed in the Widgets
     230 *                                  interface. Default 'Sidebar $instance'.
     231 *     @type string $id             The unique identifier by which the sidebar will be called.
     232 *                                  Default 'sidebar-$instance'.
     233 *     @type string $description    Description of the sidebar, displayed in the Widgets interface.
     234 *                                  Default empty string.
     235 *     @type string $class          Extra CSS class to assign to the sidebar in the Widgets interface.
     236 *                                  Default empty.
     237 *     @type string $before_widget  HTML content to prepend to each widget's HTML output when
     238 *                                  assigned to this sidebar. Default is an opening list item element.
     239 *     @type string $after_widget   HTML content to append to each widget's HTML output when
     240 *                                  assigned to this sidebar. Default is a closing list item element.
     241 *     @type string $before_title   HTML content to prepend to the sidebar title when displayed.
     242 *                                  Default is an opening h2 element.
     243 *     @type string $after_title    HTML content to append to the sidebar title when displayed.
     244 *                                  Default is a closing h2 element.
     245 *     @type string $before_sidebar HTML content to prepend to the sidebar when displayed.
     246 *                                  Outputs after the {@see 'dynamic_sidebar_before'} action.
     247 *                                  Default empty string.
     248 *     @type string $after_sidebar  HTML content to append to the sidebar when displayed.
     249 *                                  Outputs before the {@see 'dynamic_sidebar_after'} action.
     250 *                                  Default empty string.
    244251 * }
    245252 * @return string Sidebar ID added to $wp_registered_sidebars global.
     
    254261    $defaults = array(
    255262        /* translators: %d: Sidebar number. */
    256         'name'          => sprintf( __( 'Sidebar %d' ), $i ),
    257         'id'            => "sidebar-$i",
    258         'description'   => '',
    259         'class'         => '',
    260         'before_widget' => '<li id="%1$s" class="widget %2$s">',
    261         'after_widget'  => "</li>\n",
    262         'before_title'  => '<h2 class="widgettitle">',
    263         'after_title'   => "</h2>\n",
     263        'name'           => sprintf( __( 'Sidebar %d' ), $i ),
     264        'id'             => "sidebar-$i",
     265        'description'    => '',
     266        'class'          => '',
     267        'before_widget'  => '<li id="%1$s" class="widget %2$s">',
     268        'after_widget'   => "</li>\n",
     269        'before_title'   => '<h2 class="widgettitle">',
     270        'after_title'    => "</h2>\n",
     271        'before_sidebar' => '',
     272        'after_sidebar'  => '',
    264273    );
    265274
     
    692701    }
    693702
     703    $sidebar = $wp_registered_sidebars[ $index ];
     704
     705    $sidebar['before_sidebar'] = sprintf( $sidebar['before_sidebar'], $sidebar['id'], $sidebar['class'] );
     706
    694707    /**
    695708     * Fires before widgets are rendered in a dynamic sidebar.
     
    705718     */
    706719    do_action( 'dynamic_sidebar_before', $index, true );
    707     $sidebar = $wp_registered_sidebars[ $index ];
     720
     721    if ( ! empty( $sidebar['before_sidebar'] ) ) {
     722        echo $sidebar['before_sidebar'];
     723    }
    708724
    709725    $did_one = false;
     
    808824    }
    809825
     826    if ( ! empty( $sidebar['after_sidebar'] ) ) {
     827        echo $sidebar['after_sidebar'];
     828    }
     829
    810830    /**
    811831     * Fires after widgets are rendered in a dynamic sidebar.
  • trunk/tests/phpunit/tests/widgets.php

    r48939 r49203  
    245245        unregister_sidebar( $sidebar_id );
    246246        $this->assertArrayNotHasKey( $sidebar_id, $wp_registered_sidebars );
     247    }
     248
     249    /**
     250     * @group sidebar
     251     * @ticket 19709
     252     */
     253    public function test_register_sidebar_with_after_and_before_sidebar() {
     254        global $wp_registered_sidebars;
     255
     256        $sidebar_id = 'test-sidebar';
     257        register_sidebar(
     258            array(
     259                'id'             => $sidebar_id,
     260                'before_sidebar' => '<div id="%1$s" class="before-sidebar %2$s">',
     261                'after_sidebar'  => '</div> <!-- .before-sidebar -->',
     262                'class'          => 'test-sidebar',
     263            )
     264        );
     265
     266        $this->assertArrayHasKey( $sidebar_id, $wp_registered_sidebars );
     267        $this->assertContains( '<div id="%1$s" class="before-sidebar %2$s">', $wp_registered_sidebars[ $sidebar_id ]['before_sidebar'] );
     268        $this->assertContains( '</div> <!-- .before-sidebar -->', $wp_registered_sidebars[ $sidebar_id ]['after_sidebar'] );
     269
     270    }
     271
     272    /**
     273     * @group sidebar
     274     * @ticket 19709
     275     */
     276    public function test_register_sidebar_without_after_and_before_sidebar() {
     277        global $wp_registered_sidebars;
     278
     279        $sidebar_id = 'test-sidebar-2';
     280        register_sidebar(
     281            array(
     282                'id' => $sidebar_id,
     283            )
     284        );
     285
     286        $this->assertArrayHasKey( $sidebar_id, $wp_registered_sidebars );
     287        $this->assertEmpty( $wp_registered_sidebars[ $sidebar_id ]['before_sidebar'] );
     288        $this->assertEmpty( $wp_registered_sidebars[ $sidebar_id ]['after_sidebar'] );
     289
    247290    }
    248291
Note: See TracChangeset for help on using the changeset viewer.