Skip to content

Instantly share code, notes, and snippets.

@chlab
Last active August 29, 2015 14:14
Show Gist options
  • Save chlab/ad608eafe2e1262140fb to your computer and use it in GitHub Desktop.
Save chlab/ad608eafe2e1262140fb to your computer and use it in GitHub Desktop.
Smarter BuddyPress sitewide notices widget that doesn't display the widget when there are no notices to show
<?php
/**
* Smarter version of the buddypress sitewide notices widget
*
* Fetches the notices in an output buffer and does not display widget when
* there is no content to display.
*
* Sadly this widget is necessary because buddypress only finds out if there
* are notices to be displayed while displaying them.
*
* This code would extend BP_Messages_Sitewide_Notices_Widget, but it's constructor
* is hardwired to register itself so I had to copy all it's methods too :(
*
* I'm using the buddypress textdomain as that probably makes more sense than
* registering my own.
*
* @see BP_Messages_Sitewide_Notices_Widget
* @see bp_message_get_notices()
*/
class Smart_Sitewide_Notices_Widget extends WP_Widget {
/**
* Register widget
*/
function __construct() {
parent::__construct(
'smart_sitewide_notices_widget',
__( 'Smart BuddyPress Sitewide Notices', 'buddypress' ),
array(
'classname' => 'widget_smart_sitewide_notices_widget buddypress widget',
'description' => __( 'Display Sitewide Notices posted by the site administrator', 'buddypress' ),
)
);
}
/**
* Render the widget.
*
* @see WP_Widget::widget() for a description of parameters.
*
* @param array $args See {@WP_Widget::widget()}.
* @param array $args See {@WP_Widget::widget()}.
*/
public function widget( $args, $instance ) {
if ( ! is_user_logged_in() ) {
return;
}
// Don't display the widget if there are no notices to show
$notices = BP_Messages_Notice::get_active();
if ( empty( $notices ) ) {
return;
}
extract( $args );
$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
$title = apply_filters( 'widget_title', $title, $instance );
// fetch notices in an output buffer and don't display them if there's nothing there
ob_start();
bp_message_get_notices();
$notices = ob_get_clean();
if (!strlen($notices)) {
return;
}
printf('%s%s%s%s<div class="bp-site-wide-message">%s</div>%s',
$before_widget,
$before_title,
$title,
$after_title,
$notices,
$after_widget
);
}
/**
* Process the saved settings for the widget.
*
* @see WP_Widget::update() for a description of parameters and
* return values.
*
* @param array $new_instance See {@WP_Widget::update()}.
* @param array $old_instance See {@WP_Widget::update()}.
* @return array $instance See {@WP_Widget::update()}.
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
/**
* Render the settings form for Appearance > Widgets.
*
* @see WP_Widget::form() for a description of parameters.
*
* @param array $instance See {@WP_Widget::form()}.
*/
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array(
'title' => '',
) );
$title = strip_tags( $instance['title'] );
printf('<p>
<label for="%s">%s</label>
<input class="widefat" id="%s" name="%s" type="text" value="%s" />
</p>
',
$this->get_field_id( 'title' ),
__( 'Title:', 'buddypress' ),
$this->get_field_id( 'title' ),
$this->get_field_name( 'title' ),
esc_attr( $title )
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment