Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 43 additions & 25 deletions src/wp-includes/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,22 +225,28 @@ function register_sidebars( $number = 1, $args = array() ) {
* @param array|string $args {
* Optional. Array or string of arguments for the sidebar being registered.
*
* @type string $name The name or title of the sidebar displayed in the Widgets
* interface. Default 'Sidebar $instance'.
* @type string $id The unique identifier by which the sidebar will be called.
* Default 'sidebar-$instance'.
* @type string $description Description of the sidebar, displayed in the Widgets interface.
* Default empty string.
* @type string $class Extra CSS class to assign to the sidebar in the Widgets interface.
* Default empty.
* @type string $before_widget HTML content to prepend to each widget's HTML output when
* assigned to this sidebar. Default is an opening list item element.
* @type string $after_widget HTML content to append to each widget's HTML output when
* assigned to this sidebar. Default is a closing list item element.
* @type string $before_title HTML content to prepend to the sidebar title when displayed.
* Default is an opening h2 element.
* @type string $after_title HTML content to append to the sidebar title when displayed.
* Default is a closing h2 element.
* @type string $name The name or title of the sidebar displayed in the Widgets
* interface. Default 'Sidebar $instance'.
* @type string $id The unique identifier by which the sidebar will be called.
* Default 'sidebar-$instance'.
* @type string $description Description of the sidebar, displayed in the Widgets interface.
* Default empty string.
* @type string $class Extra CSS class to assign to the sidebar in the Widgets interface.
* Default empty.
* @type string $before_widget HTML content to prepend to each widget's HTML output when
* assigned to this sidebar. Default is an opening list item element.
* @type string $after_widget HTML content to append to each widget's HTML output when
* assigned to this sidebar. Default is a closing list item element.
* @type string $before_title HTML content to prepend to the sidebar title when displayed.
* Default is an opening h2 element.
* @type string $after_title HTML content to append to the sidebar title when displayed.
* Default is a closing h2 element.
* @type string $before_sidebar HTML content to prepend to the sidebar.
* Outputs after the dynamic_sidebar_before action.
* Default is an empty string.
* @type string $after_sidebar HTML content to append to the sidebar when displayed.
* Outputs before the dynamic_sidebar_after action.
* Default is an empty string.
* }
* @return string Sidebar ID added to $wp_registered_sidebars global.
*/
Expand All @@ -253,14 +259,16 @@ function register_sidebar( $args = array() ) {

$defaults = array(
/* translators: %d: Sidebar number. */
'name' => sprintf( __( 'Sidebar %d' ), $i ),
'id' => "sidebar-$i",
'description' => '',
'class' => '',
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => "</li>\n",
'before_title' => '<h2 class="widgettitle">',
'after_title' => "</h2>\n",
'name' => sprintf( __( 'Sidebar %d' ), $i ),
'id' => "sidebar-$i",
'description' => '',
'class' => '',
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => "</li>\n",
'before_title' => '<h2 class="widgettitle">',
'after_title' => "</h2>\n",
'before_sidebar' => '',
'after_sidebar' => '',
);

/**
Expand Down Expand Up @@ -691,6 +699,9 @@ function dynamic_sidebar( $index = 1 ) {
return apply_filters( 'dynamic_sidebar_has_widgets', false, $index );
}

$sidebar = $wp_registered_sidebars[ $index ];
$sidebar['before_sidebar'] = sprintf( $sidebar['before_sidebar'], $sidebar['id'], $sidebar['class'] );

/**
* Fires before widgets are rendered in a dynamic sidebar.
*
Expand All @@ -704,7 +715,10 @@ function dynamic_sidebar( $index = 1 ) {
* Default true.
*/
do_action( 'dynamic_sidebar_before', $index, true );
$sidebar = $wp_registered_sidebars[ $index ];

if ( ! empty( $sidebar['before_sidebar'] ) ) {
echo $sidebar['before_sidebar'];
}

$did_one = false;
foreach ( (array) $sidebars_widgets[ $index ] as $id ) {
Expand Down Expand Up @@ -807,6 +821,10 @@ function dynamic_sidebar( $index = 1 ) {
}
}

if ( ! empty( $sidebar['after_sidebar'] ) ) {
echo $sidebar['after_sidebar'];
}

/**
* Fires after widgets are rendered in a dynamic sidebar.
*
Expand Down
41 changes: 41 additions & 0 deletions tests/phpunit/tests/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,47 @@ function test_unregister_sidebar_with_numeric_id() {
$this->assertArrayNotHasKey( $sidebar_id, $wp_registered_sidebars );
}

/**
* @group sidebar
*/
public function test_register_sidebar_with_after_and_before_sidebar() {
global $wp_registered_sidebars;

$sidebar_id = 'test-sidebar';
register_sidebar(
array(
'id' => $sidebar_id,
'before_sidebar' => '<div id="%1$s" class="before-sidebar %2$s">',
'after_sidebar' => '</div> <!-- .before-sidebar -->',
'class' => 'test-sidebar',
)
);

$this->assertArrayHasKey( $sidebar_id, $wp_registered_sidebars );
$this->assertContains( '<div id="%1$s" class="before-sidebar %2$s">', $wp_registered_sidebars[ $sidebar_id ]['before_sidebar'] );
$this->assertContains( '</div> <!-- .before-sidebar -->', $wp_registered_sidebars[ $sidebar_id ]['after_sidebar'] );

}

/**
* @group sidebar
*/
public function test_register_sidebar_without_after_and_before_sidebar() {
global $wp_registered_sidebars;

$sidebar_id = 'test-sidebar-2';
register_sidebar(
array(
'id' => $sidebar_id,
)
);

$this->assertArrayHasKey( $sidebar_id, $wp_registered_sidebars );
$this->assertEmpty( $wp_registered_sidebars[ $sidebar_id ]['before_sidebar'] );
$this->assertEmpty( $wp_registered_sidebars[ $sidebar_id ]['after_sidebar'] );

}

/**
* Utility hook callback used to store a sidebar ID mid-function.
*/
Expand Down