Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bypass empty BU Text widgets in class count increment via filter #96

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

# unreleased
- Fixes #45 New filters added to correct widget count classes for each widget.
- Created new filter `responsive_is_widget_empty` to help check for empty widgets.
- Added `r_is_bu_text_widget_empty()` using is `responsive_is_widget_empty` to check for empty BU Text Widgets.
- Added `r_bu_text_widget_loaded()` to conditionally load `r_is_bu_text_widget_empty()` only if plugin is loaded.
- Added `r_is_bu_links_widget_empty()` using is `responsive_is_widget_empty` to check for empty BU Links Widgets.
- Added `r_bu_link_widget_loaded()` to conditionally load `r_is_bu_links_widget_empty()` only if plugin is loaded.

# 2.1.8

- Added new template tag, `responsive_the_title()`, intended to output the page title
Expand Down
74 changes: 74 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,80 @@ function r_enqueue_fancy_gallery() {
wp_enqueue_style( 'lightgallery' );
}

/**
* Filter used to prevent incrementing CSS widget class count if a BU Text Widget is empty.
*
* @since 2.1.9
*
* @param bool $is_widget_empty Defaults to false, assumes widget has content.
* @param array $params An array of Widget options info.
*
* @return bool $is_widget_empty The status of content for the widget.
*/
function r_is_bu_text_widget_empty( $is_widget_empty, $params ) {
$widget_name = $params[0]['widget_name'];

if ( 'BU Text' === $widget_name ) {
$widget_instance = $params[1]['number'];
$meta_key = '_bu_text_widget_' . $widget_instance;
$widget_meta = get_post_meta( get_the_ID(), $meta_key, true );

if ( empty( $widget_meta['content'] ) ) {
$is_widget_empty = true;
}
}
return $is_widget_empty;
}

/**
* Adds an empty widget check filter if the BU Text Widget plugin is active.
*
* @since 2.1.9
*/
function r_bu_text_widget_loaded() {
if ( is_plugin_active( 'bu-text-widget/bu-text-widget.php' ) ) {
add_filter( 'responsive_is_widget_empty', 'r_is_bu_text_widget_empty', 10, 2 );
}
}
add_action( 'after_setup_theme', 'r_bu_text_widget_loaded' );

/**
* Filter used to prevent incrementing CSS widget class count if a BU Links Widget is empty.
*
* @since 2.1.9
*
* @param bool $is_widget_empty Defaults to false, assumes widget has content.
* @param array $params An array of Widget options info.
*
* @return bool $is_widget_empty The status of content for the widget.
*/
function r_is_bu_links_widget_empty( $is_widget_empty, $params ) {
$widget_name = $params[0]['widget_name'];

if ( 'BU Links' === $widget_name ) {
$widget_instance = $params[1]['number'];
$meta_key = '_bu_links_' . $widget_instance;
$widget_meta = get_post_meta( get_the_ID(), $meta_key, true );

if ( ! is_array( $widget_meta ) ) {
$is_widget_empty = true;
}
}
return $is_widget_empty;
}

/**
* Adds an empty widget check filter if the BU Link plugin is active.
*
* @since 2.1.9
*/
function r_bu_link_widget_loaded() {
if ( is_plugin_active( 'link-lists/link-lists.php' ) ) {
add_filter( 'responsive_is_widget_empty', 'r_is_bu_links_widget_empty', 10, 2 );
}
}
add_action( 'after_setup_theme', 'r_bu_link_widget_loaded' );

/**
* Remove the news template when BU_News_Page_Template does not exist.
*
Expand Down
16 changes: 16 additions & 0 deletions inc/extras.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ function responsive_widget_counts( $params ) {
return $params;
}

/**
* Filters the $is_widget_empty variable.
*
* @since 2.1.9
*
* @params bool $is_widget_empty The empty/full status of the widget content.
*
* @params array $params An array of widget options.
*/
$is_widget_empty = apply_filters( 'responsive_is_widget_empty', $is_widget_empty = false, $params );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to specify $is_bu_widget_empty, or do you think a generic name would be fine here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose to be more generic there. The filter can be used for any type of widget, not just BU widgets.


// Don't increment static widget counter if widget is empty because it will not be displayed.
if ( $is_widget_empty ) {
return $params;
}

// Initialize or increment our static widget counter by one for this widget.
if ( array_key_exists( $current_sidebar, $widget_counter ) ) {
$widget_counter[ $current_sidebar ] ++;
Expand Down