Skip to content

Instantly share code, notes, and snippets.

@seriusokhatsky
Created August 17, 2016 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seriusokhatsky/dd995f363542438ebabd743d029104c0 to your computer and use it in GitHub Desktop.
Save seriusokhatsky/dd995f363542438ebabd743d029104c0 to your computer and use it in GitHub Desktop.
<?php
class Opened_Topics_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'widget_display_topics', // Base ID
__( 'Recent Topics', 'bbpress' ), // Name
array( 'description' => __( 'A list of recent topics, replied by customers.', 'bbpress' ), ) // Args
);
}
public function widget( $args = array(), $instance = array() ) {
if( ! SS_Envato_API()->user->is_staff() ) return;
// Get widget settings
$settings = $this->parse_settings( $instance );
// Typical WordPress filter
$settings['title'] = apply_filters( 'widget_title', $settings['title'], $instance, $this->id_base );
// bbPress filter
$settings['title'] = apply_filters( 'bbp_topic_widget_title', $settings['title'], $instance, $this->id_base );
$author_exclude = array(
'',
);
// How do we want to order our results?
$topics_query = array(
'post_type' => bbp_get_topic_post_type(),
'post_parent' => $settings['parent_forum'],
'posts_per_page' => (int) $settings['max_shown'],
'post_status' => array( bbp_get_public_status_id() ),
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'meta_key' => '_bbp_last_active_time',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => '_bbp_forum_id',
'value' => bbp_get_forum_id()
),
),
);
// get_post_meta( $reply_id, '_bbp_anonymous_name', true );
// Note: private and hidden forums will be excluded via the
// bbp_pre_get_posts_normalize_forum_visibility action and function.
$widget_query = new WP_Query( $topics_query );
// Bail if no topics are found
if ( ! $widget_query->have_posts() ) {
return;
}
?>
<div class="widget_display_topics">
<?php
echo $args['before_widget'];
if ( !empty( $settings['title'] ) ) {
echo $args['before_title'] . $settings['title'] . $args['after_title'];
} ?>
<ul>
<?php while ( $widget_query->have_posts() ) :
$widget_query->the_post();
$topic_id = bbp_get_topic_id( $widget_query->post->ID );
$author_link = '';
$reply_id = bbp_get_topic_last_reply_id($topic_id);
$author_id = bbp_get_reply_author_id( $reply_id );
$reply_author = bbp_get_reply_author( $reply_id );
$author_link = '<strong>' . $reply_author . '</strong>';
if( in_array($reply_author, $author_exclude) ) continue;
// Maybe get the topic author
if ( ! empty( $settings['show_user'] ) ) :
//$author_link = bbp_get_topic_author_link( array( 'post_id' => $topic_id, 'type' => 'both', 'size' => 14 ) );
endif; ?>
<li>
<a class="bbp-forum-title" href="<?php echo esc_url( bbp_get_reply_url( $reply_id ) ); ?>"><?php bbp_topic_title( $topic_id ); ?></a>
<?php if ( ! empty( $author_link ) ) : ?>
<?php printf( _x( 'by %1$s', 'widgets', 'bbpress' ), '<span class="topic-author">' . $author_link . '</span>' ); ?>
<?php endif; ?>
<?php if ( ! empty( $settings['show_date'] ) ) : ?>
<div><?php bbp_topic_last_active_time( $topic_id ); ?></div>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php echo $args['after_widget'];?>
<div class="clear"></div>
</div>
<?php
// Reset the $post global
wp_reset_postdata();
}
public function update( $new_instance = array(), $old_instance = array() ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['order_by'] = strip_tags( $new_instance['order_by'] );
$instance['parent_forum'] = sanitize_text_field( $new_instance['parent_forum'] );
$instance['show_date'] = (bool) $new_instance['show_date'];
$instance['show_user'] = (bool) $new_instance['show_user'];
$instance['max_shown'] = (int) $new_instance['max_shown'];
// Force to any
if ( !empty( $instance['parent_forum'] ) && !is_numeric( $instance['parent_forum'] ) ) {
$instance['parent_forum'] = 'any';
}
return $instance;
}
public function form( $instance = array() ) {
// Get widget settings
$settings = $this->parse_settings( $instance ); ?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'bbpress' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $settings['title'] ); ?>" /></label></p>
<p><label for="<?php echo $this->get_field_id( 'max_shown' ); ?>"><?php _e( 'Maximum topics to show:', 'bbpress' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_shown' ); ?>" name="<?php echo $this->get_field_name( 'max_shown' ); ?>" type="text" value="<?php echo esc_attr( $settings['max_shown'] ); ?>" /></label></p>
<p>
<label for="<?php echo $this->get_field_id( 'parent_forum' ); ?>"><?php _e( 'Parent Forum ID:', 'bbpress' ); ?>
<input class="widefat" id="<?php echo $this->get_field_id( 'parent_forum' ); ?>" name="<?php echo $this->get_field_name( 'parent_forum' ); ?>" type="text" value="<?php echo esc_attr( $settings['parent_forum'] ); ?>" />
</label>
<br />
<small><?php _e( '"0" to show only root - "any" to show all', 'bbpress' ); ?></small>
</p>
<p><label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Show post date:', 'bbpress' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" <?php checked( true, $settings['show_date'] ); ?> value="1" /></label></p>
<p><label for="<?php echo $this->get_field_id( 'show_user' ); ?>"><?php _e( 'Show topic author:', 'bbpress' ); ?> <input type="checkbox" id="<?php echo $this->get_field_id( 'show_user' ); ?>" name="<?php echo $this->get_field_name( 'show_user' ); ?>" <?php checked( true, $settings['show_user'] ); ?> value="1" /></label></p>
<?php
}
public function parse_settings( $instance = array() ) {
return bbp_parse_args( $instance, array(
'title' => __( 'Recent Topics', 'bbpress' ),
'max_shown' => 5,
'show_date' => false,
'show_user' => false,
'parent_forum' => 'any',
'order_by' => false
), 'topic_widget_settings' );
}
}
add_action( 'widgets_init', function(){
register_widget( 'Opened_Topics_Widget' );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment