Fix for #796
This commit is contained in:
parent
613f16e462
commit
0090d78e57
215
inc/widgets.php
215
inc/widgets.php
|
@ -6,43 +6,84 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Count number of widgets in a sidebar
|
* Add filter to the parameters passed to a widget's display callback.
|
||||||
* Used to add classes to widget areas so widgets can be displayed one, two, three or four per row
|
* The filter is evaluated on both the front and the back end!
|
||||||
|
*
|
||||||
|
* @link https://developer.wordpress.org/reference/hooks/dynamic_sidebar_params/
|
||||||
*/
|
*/
|
||||||
if ( ! function_exists( 'understrap_slbd_count_widgets' ) ) {
|
add_filter( 'dynamic_sidebar_params', 'understrap_widget_classes' );
|
||||||
function understrap_slbd_count_widgets( $sidebar_id ) {
|
|
||||||
// If loading from front page, consult $_wp_sidebars_widgets rather than options
|
|
||||||
// to see if wp_convert_widget_settings() has made manipulations in memory.
|
|
||||||
global $_wp_sidebars_widgets;
|
|
||||||
if ( empty( $_wp_sidebars_widgets ) ) :
|
|
||||||
$_wp_sidebars_widgets = get_option( 'sidebars_widgets', array() );
|
|
||||||
endif;
|
|
||||||
|
|
||||||
$sidebars_widgets_count = $_wp_sidebars_widgets;
|
if ( ! function_exists( 'understrap_widget_classes' ) ) {
|
||||||
|
/**
|
||||||
|
* Count number of visible widgets in a sidebar and add classes to widgets accordingly,
|
||||||
|
* so widgets can be displayed one, two, three or four per row.
|
||||||
|
*
|
||||||
|
* @global array $sidebars_widgets
|
||||||
|
*
|
||||||
|
* @param array $params {
|
||||||
|
* @type array $args {
|
||||||
|
* An array of widget display arguments.
|
||||||
|
*
|
||||||
|
* @type string $name Name of the sidebar the widget is assigned to.
|
||||||
|
* @type string $id ID of the sidebar the widget is assigned to.
|
||||||
|
* @type string $description The sidebar description.
|
||||||
|
* @type string $class CSS class applied to the sidebar container.
|
||||||
|
* @type string $before_widget HTML markup to prepend to each widget in the sidebar.
|
||||||
|
* @type string $after_widget HTML markup to append to each widget in the sidebar.
|
||||||
|
* @type string $before_title HTML markup to prepend to the widget title when displayed.
|
||||||
|
* @type string $after_title HTML markup to append to the widget title when displayed.
|
||||||
|
* @type string $widget_id ID of the widget.
|
||||||
|
* @type string $widget_name Name of the widget.
|
||||||
|
* }
|
||||||
|
* @type array $widget_args {
|
||||||
|
* An array of multi-widget arguments.
|
||||||
|
*
|
||||||
|
* @type int $number Number increment used for multiples of the same widget.
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* @return array $params
|
||||||
|
*/
|
||||||
|
function understrap_widget_classes( $params ) {
|
||||||
|
|
||||||
if ( isset( $sidebars_widgets_count[ $sidebar_id ] ) ) :
|
global $sidebars_widgets;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* When the corresponding filter is evaluated on the front end
|
||||||
|
* this takes into account that there might have been made other changes.
|
||||||
|
*/
|
||||||
|
$sidebars_widgets_count = apply_filters( 'sidebars_widgets', $sidebars_widgets );
|
||||||
|
|
||||||
|
// Only apply changes if sidebar ID is set and the widget's classes depend on the number of widgets in the sidebar.
|
||||||
|
if ( isset( $params[0]['id'] ) && strpos( $params[0]['before_widget'], 'dynamic-classes' ) ) {
|
||||||
|
$sidebar_id = $params[0]['id'];
|
||||||
$widget_count = count( $sidebars_widgets_count[ $sidebar_id ] );
|
$widget_count = count( $sidebars_widgets_count[ $sidebar_id ] );
|
||||||
$widget_classes = 'widget-count-' . count( $sidebars_widgets_count[ $sidebar_id ] );
|
|
||||||
if ( $widget_count % 4 == 0 || $widget_count > 6 ) :
|
$widget_classes = 'widget-count-' . $widget_count;
|
||||||
// Four widgets per row if there are exactly four or more than six
|
if ( 0 === $widget_count % 4 || $widget_count > 6 ) {
|
||||||
|
// Four widgets per row if there are exactly four or more than six.
|
||||||
$widget_classes .= ' col-md-3';
|
$widget_classes .= ' col-md-3';
|
||||||
elseif ( 6 == $widget_count ) :
|
} elseif ( 6 === $widget_count ) {
|
||||||
// If two widgets are published
|
// If two widgets are published.
|
||||||
$widget_classes .= ' col-md-2';
|
$widget_classes .= ' col-md-2';
|
||||||
elseif ( $widget_count >= 3 ) :
|
} elseif ( $widget_count >= 3 ) {
|
||||||
// Three widgets per row if there's three or more widgets
|
// Three widgets per row if there's three or more widgets.
|
||||||
$widget_classes .= ' col-md-4';
|
$widget_classes .= ' col-md-4';
|
||||||
elseif ( 2 == $widget_count ) :
|
} elseif ( 2 === $widget_count ) {
|
||||||
// If two widgets are published
|
// If two widgets are published.
|
||||||
$widget_classes .= ' col-md-6';
|
$widget_classes .= ' col-md-6';
|
||||||
elseif ( 1 == $widget_count ) :
|
} elseif ( 1 === $widget_count ) {
|
||||||
// If just on widget is active
|
// If just on widget is active.
|
||||||
$widget_classes .= ' col-md-12';
|
$widget_classes .= ' col-md-12';
|
||||||
endif;
|
}
|
||||||
return $widget_classes;
|
|
||||||
endif;
|
// Replace the placeholder class 'dynamic-classes' with the classes stored in $widget_classes.
|
||||||
|
$params[0]['before_widget'] = str_replace( 'dynamic-classes', $widget_classes, $params[0]['before_widget'] );
|
||||||
|
}
|
||||||
|
|
||||||
|
return $params;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
} // endif function_exists( 'understrap_widget_classes' ).
|
||||||
|
|
||||||
add_action( 'widgets_init', 'understrap_widgets_init' );
|
add_action( 'widgets_init', 'understrap_widgets_init' );
|
||||||
|
|
||||||
|
@ -51,65 +92,77 @@ if ( ! function_exists( 'understrap_widgets_init' ) ) {
|
||||||
* Initializes themes widgets.
|
* Initializes themes widgets.
|
||||||
*/
|
*/
|
||||||
function understrap_widgets_init() {
|
function understrap_widgets_init() {
|
||||||
register_sidebar( array(
|
register_sidebar(
|
||||||
'name' => __( 'Right Sidebar', 'understrap' ),
|
array(
|
||||||
'id' => 'right-sidebar',
|
'name' => __( 'Right Sidebar', 'understrap' ),
|
||||||
'description' => __( 'Right sidebar widget area', 'understrap' ),
|
'id' => 'right-sidebar',
|
||||||
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
|
'description' => __( 'Right sidebar widget area', 'understrap' ),
|
||||||
'after_widget' => '</aside>',
|
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
|
||||||
'before_title' => '<h3 class="widget-title">',
|
'after_widget' => '</aside>',
|
||||||
'after_title' => '</h3>',
|
'before_title' => '<h3 class="widget-title">',
|
||||||
) );
|
'after_title' => '</h3>',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
register_sidebar( array(
|
register_sidebar(
|
||||||
'name' => __( 'Left Sidebar', 'understrap' ),
|
array(
|
||||||
'id' => 'left-sidebar',
|
'name' => __( 'Left Sidebar', 'understrap' ),
|
||||||
'description' => __( 'Left sidebar widget area', 'understrap' ),
|
'id' => 'left-sidebar',
|
||||||
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
|
'description' => __( 'Left sidebar widget area', 'understrap' ),
|
||||||
'after_widget' => '</aside>',
|
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
|
||||||
'before_title' => '<h3 class="widget-title">',
|
'after_widget' => '</aside>',
|
||||||
'after_title' => '</h3>',
|
'before_title' => '<h3 class="widget-title">',
|
||||||
) );
|
'after_title' => '</h3>',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
register_sidebar( array(
|
register_sidebar(
|
||||||
'name' => __( 'Hero Slider', 'understrap' ),
|
array(
|
||||||
'id' => 'hero',
|
'name' => __( 'Hero Slider', 'understrap' ),
|
||||||
'description' => __( 'Hero slider area. Place two or more widgets here and they will slide!', 'understrap' ),
|
'id' => 'hero',
|
||||||
'before_widget' => '<div class="carousel-item">',
|
'description' => __( 'Hero slider area. Place two or more widgets here and they will slide!', 'understrap' ),
|
||||||
'after_widget' => '</div>',
|
'before_widget' => '<div class="carousel-item">',
|
||||||
'before_title' => '',
|
'after_widget' => '</div>',
|
||||||
'after_title' => '',
|
'before_title' => '',
|
||||||
) );
|
'after_title' => '',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
register_sidebar( array(
|
register_sidebar(
|
||||||
'name' => __( 'Hero Canvas', 'understrap' ),
|
array(
|
||||||
'id' => 'herocanvas',
|
'name' => __( 'Hero Canvas', 'understrap' ),
|
||||||
'description' => __( 'Full size canvas hero area for Bootstrap and other custom HTML markup', 'understrap' ),
|
'id' => 'herocanvas',
|
||||||
'before_widget' => '',
|
'description' => __( 'Full size canvas hero area for Bootstrap and other custom HTML markup', 'understrap' ),
|
||||||
'after_widget' => '',
|
'before_widget' => '',
|
||||||
'before_title' => '',
|
'after_widget' => '',
|
||||||
'after_title' => '',
|
'before_title' => '',
|
||||||
) );
|
'after_title' => '',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
register_sidebar( array(
|
register_sidebar(
|
||||||
'name' => __( 'Top Full', 'understrap' ),
|
array(
|
||||||
'id' => 'statichero',
|
'name' => __( 'Top Full', 'understrap' ),
|
||||||
'description' => __( 'Full top widget with dynamic grid', 'understrap' ),
|
'id' => 'statichero',
|
||||||
'before_widget' => '<div id="%1$s" class="static-hero-widget %2$s '. understrap_slbd_count_widgets( 'statichero' ) .'">',
|
'description' => __( 'Full top widget with dynamic grid', 'understrap' ),
|
||||||
'after_widget' => '</div><!-- .static-hero-widget -->',
|
'before_widget' => '<div id="%1$s" class="footer-widget %2$s dynamic-classes">',
|
||||||
'before_title' => '<h3 class="widget-title">',
|
'after_widget' => '</div><!-- .static-hero-widget -->',
|
||||||
'after_title' => '</h3>',
|
'before_title' => '<h3 class="widget-title">',
|
||||||
) );
|
'after_title' => '</h3>',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
register_sidebar( array(
|
register_sidebar(
|
||||||
'name' => __( 'Footer Full', 'understrap' ),
|
array(
|
||||||
'id' => 'footerfull',
|
'name' => __( 'Footer Full', 'understrap' ),
|
||||||
'description' => __( 'Full sized footer widget with dynamic grid', 'understrap' ),
|
'id' => 'footerfull',
|
||||||
'before_widget' => '<div id="%1$s" class="footer-widget %2$s '. understrap_slbd_count_widgets( 'footerfull' ) .'">',
|
'description' => __( 'Full sized footer widget with dynamic grid', 'understrap' ),
|
||||||
'after_widget' => '</div><!-- .footer-widget -->',
|
'before_widget' => '<div id="%1$s" class="footer-widget %2$s dynamic-classes">',
|
||||||
'before_title' => '<h3 class="widget-title">',
|
'after_widget' => '</div><!-- .footer-widget -->',
|
||||||
'after_title' => '</h3>',
|
'before_title' => '<h3 class="widget-title">',
|
||||||
) );
|
'after_title' => '</h3>',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
} // endif function_exists( 'understrap_widgets_init' ).
|
} // endif function_exists( 'understrap_widgets_init' ).
|
||||||
|
|
Reference in New Issue