Bpbbpst_Support_Stats
Buddy-bbPress Support Topic statistic Widget.
Defined (1)
The class is defined in the following location(s).
- /includes/widgets.php
- class Bpbbpst_Support_Stats extends WP_Widget {
- /**
- * Buddy-bbPress Support Topic statistic Widget
- *
- * @since 1.1
- *
- * @uses WP_Widget::__construct() to init the widget
- * @uses add_filter() to filter bbPress topic query, breadcrumb and pagination
- * @uses add_action() to enqueue widget style
- */
- public function __construct() {
- $widget_ops = array( 'description' => __( 'Displays support topic global statistics or of the active forum', 'buddy-bbpress-support-topic' ) );
- parent::__construct( false, $name = __( 'Support Topics Stats', 'buddy-bbpress-support-topic' ), $widget_ops );
- /** bbPress filters to handle breadcrumb, pagination and topics query */
- add_filter( 'bbp_after_has_topics_parse_args', array( $this, 'filter_topics_query_by_status' ), 20, 1 );
- add_filter( 'bbp_get_breadcrumb', array( $this, 'breadcrumb_for_status' ), 20, 3 );
- add_filter( 'bbp_topic_pagination', array( $this, 'pagination_for_status' ), 20, 1 );
- /** finally add some style */
- add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ), 10 );
- }
- /**
- * Registers the widget
- *
- * @since 1.1
- *
- * @uses register_widget() to register the widget
- */
- public static function register_widget() {
- register_widget( 'Bpbbpst_Support_Stats' );
- }
- /**
- * Displays the output, the statistics
- *
- * Depending if the widget is displayed on a single forum template
- * it displays the current forum stats or the global stats.
- * If on a single forum, add links to filter by support status the topics
- *
- * @since 2.0
- *
- * @param mixed $args Arguments
- * @param array $instance Instance
- * @uses bbp_get_forum_id() to get the forum id
- * @uses bbp_is_topic_edit() to check if the topic is being edited
- * @uses bbp_get_topic_forum_id() to get the forum parent of the edited topic
- * @uses bbp_get_topic_id() to get the edited topic id
- * @uses bpbbpst_support_statistics() To get the statistics according to its parameter
- * @uses add_query_arg() to add the querystring to filter the support topics
- * @uses bbp_get_forum_permalink() to get the forum permalink
- * @uses esc_attr() to sanitize attributes
- * @uses get_post_type_archive_link() to build the link to forum archives
- * @uses bbp_get_forum_post_type() to get the forum post type
- * @return string html output
- */
- public function widget( $args, $instance ) {
- if ( ! $instance['bpbbpst_title'] ) {
- $instance['bpbbpst_title'] = __( 'Support Topics Stats', 'buddy-bbpress-support-topic' );
- }
- if ( ! $instance['show_forum_link'] ) {
- $instance['show_forum_link'] = false;
- }
- $forum_id = bbp_get_forum_id();
- $stats_params = "";
- if ( empty( $forum_id ) && bbp_is_topic_edit() ) {
- $forum_id = bbp_get_topic_forum_id( bbp_get_topic_id() );
- }
- if ( ! empty( $forum_id ) ) {
- $stats_params = array(
- 'meta_query' => array(
- array(
- 'key' => '_bpbbpst_support_topic',
- 'value' => 1,
- 'type' => 'numeric',
- 'compare' => '>='
- ),
- array(
- 'key' => '_bbp_forum_id',
- 'value' => $forum_id,
- 'compare' => '='
- )
- )
- );
- }
- echo $args['before_widget'];
- echo $args['before_title'] . $instance['bpbbpst_title'] . $args['after_title'];
- $support_statistics = bpbbpst_support_statistics( $stats_params );
- if ( ! empty( $support_statistics['total_support'] ) ) {
- $status_stats = $support_statistics['allstatus'];
- if ( ! is_array( $status_stats ) || count( $status_stats ) < 1 ) {
- return false;
- }
- $num_percent = $support_statistics['percent'];
- $text_percent = __( 'Resolved so far', 'buddy-bbpress-support-topic' ); ?>
- <ul class="bpbbpst-widget">
- <li class="bpbbpst-percent">
- <span class="bpbbpst-text"><?php echo esc_html( $text_percent );?></span> <span class="bpbbpst-num"><?php echo esc_html( $num_percent );?></span>
- </li>
- <?php foreach ( $status_stats as $key => $stat ) :
- $num = $stat['stat'];
- $text = $stat['label'];
- $class = $stat['front_class'];
- if ( ! empty( $forum_id ) ) {
- $link = esc_url( add_query_arg( array( 'support_status' => $key ), bbp_get_forum_permalink() ) );
- $num = '<a href="'. $link .'" title="'. esc_attr( $text ) .'">'. esc_html( $num ) .'</a>' ;
- $text = '<a href="'. $link .'" title="'. esc_attr( $text ) .'">'. esc_html( $text ) .'</a>' ;
- }
- ?>
- <li class="bpbbpst-<?php echo $class;?> status">
- <span class="bpbbpst-num"><?php echo $num;?></span> <span class="bpbbpst-text"><?php echo $text;?></span>
- </li>
- <?php endforeach;?>
- </ul>
- <?php
- // Otherwise display there are no stats available!
- } else {
- printf( '<p>%s</p>',
- esc_html__( 'No statisitcs available.' , 'buddy-bbpress-support-topic' )
- );
- }
- if ( ! is_bbpress() && ! empty( $instance['show_forum_link'] ) ) {
- printf( '<p><a href="%1$s" title="%2$s">%3$s</a></p>',
- esc_url( get_post_type_archive_link( bbp_get_forum_post_type() ) ),
- esc_attr__( 'Go to forums' , 'buddy-bbpress-support-topic' ),
- esc_html__( 'Go to forums' , 'buddy-bbpress-support-topic' )
- );
- }
- echo $args['after_widget'];
- }
- /**
- * Update the statistics widget options (title)
- *
- * @since 1.1
- *
- * @param array $new_instance The new instance options
- * @param array $old_instance The old instance options
- * @return array the instance
- */
- public function update( $new_instance, $old_instance ) {
- $instance = $old_instance;
- $instance['bpbbpst_title'] = strip_tags( $new_instance['bpbbpst_title'] );
- if ( ! empty( $new_instance['show_forum_link'] ) ) {
- $instance['show_forum_link'] = intval( $new_instance['show_forum_link'] );
- } else {
- $instance['show_forum_link'] = 0;
- }
- return $instance;
- }
- /**
- * Output the statistics widget options form
- *
- * @since 1.1
- *
- * @param $instance Instance
- * @uses wp_parse_args() to merge instance defined arguments into defaults array
- * @uses Bpbbpst_Support_Stats::get_field_id() To output the field id
- * @uses Bpbbpst_Support_Stats::get_field_name() To output the field name
- * @uses checked() to activate the checkbox if it needs to
- * @return string html output
- */
- public function form( $instance ) {
- $defaults = array(
- 'bpbbpst_title' => __( 'Support Topics Stats', 'buddy-bbpress-support-topic' ),
- 'show_forum_link' => 0
- );
- $instance = wp_parse_args( (array) $instance, $defaults );
- $bpbbpst_title = strip_tags( $instance['bpbbpst_title'] );
- $show_forum_link = strip_tags( $instance['show_forum_link'] );
- ?>
- <p>
- <label for="bpbbpst_title"><?php _e( 'Title:', 'buddy-bbpress-support-topic' ); ?>
- <input class="widefat" id="<?php echo $this->get_field_id( 'bpbbpst_title' ); ?>" name="<?php echo $this->get_field_name( 'bpbbpst_title' ); ?>" type="text" value="<?php echo esc_attr( $bpbbpst_title ); ?>" style="width: 100%" />
- </label>
- </p>
- <label for="show_forum_link">
- <input id="<?php echo $this->get_field_id( 'show_forum_link' ); ?>" name="<?php echo $this->get_field_name( 'show_forum_link' ); ?>" type="checkbox" value="1" <?php checked( true, $show_forum_link );?> /> <?php _e( 'Show forum link', 'buddy-bbpress-support-topic' ); ?>
- </label>
- <p class="description"><?php _e( 'When not on a forum page, display a link to it', 'buddy-bbpress-support-topic' )?></p>
- <?php
- }
- /**
- * Adds a meta_query to the topics query
- *
- * @since 1.1
- *
- * @param mixed $args Arguments
- * @uses bbp_is_single_forum() to check it's a single forum page
- * @return mixed $args the new arguments eventually including the meta_query
- */
- public function filter_topics_query_by_status( $args = "" ) {
- if ( ! bbp_is_single_forum() ) {
- return $args;
- }
- if ( empty( $_GET['support_status'] ) ) {
- return $args;
- }
- /** if meta_query exists, we dont want to break anything */
- if ( ! empty( $args['meta_query'] ) ) {
- return $args;
- }
- $args['meta_query'] = array(
- array(
- 'key' => '_bpbbpst_support_topic',
- 'value' => intval( $_GET['support_status'] ),
- 'compare' => '='
- )
- );
- $args['show_stickies'] = false;
- return $args;
- }
- /**
- * Adds the support status at the end of the breadcrumb
- *
- * @since 2.0
- *
- * @param string $trail the breadcrumb html
- * @param array $crumbs the different element to show in the breadcrumb
- * @param array $args arguments to organize the display of the breadcrumb
- * @uses bbp_is_single_forum() to check it's a single forum page
- * @uses bpbbpst_get_selected_support_status() to get the caption for the filtered status
- * @uses bbp_get_forum_permalink() to get the forum's permalink
- * @uses esc_html() to sanitize the status
- * @return string $trail the new breadcrumb eventually including the support status
- */
- public function breadcrumb_for_status( $trail, $crumbs, $args ) {
- if ( ! bbp_is_single_forum() ) {
- return $trail;
- }
- if ( empty( $_GET['support_status'] ) ) {
- return $trail;
- }
- $last = $crumbs[ count($crumbs) -1 ];
- $sep = $args['sep'];
- if ( ! empty( $sep ) ) {
- $sep = $args['sep_before'] . $sep . $args['sep_after'];
- }
- $pad_sep = $args['pad_sep'];
- // Pad the separator
- if ( ! empty( $pad_sep ) ) {
- $sep = str_pad( $sep, strlen( $sep ) + ( (int) $pad_sep * 2 ), ' ', STR_PAD_BOTH );
- }
- $support_status = bpbbpst_get_selected_support_status( $_GET['support_status'] );
- $trail = str_replace( $last, '<a href="'. bbp_get_forum_permalink() .'" >'. $last .'</a>' . $sep . esc_html( $support_status['sb-caption'] ), $trail );
- return $trail;
- }
- /**
- * Adds the support status at the end of the querystring
- *
- * @since 1.1
- *
- * @param array pagination pagination settings
- * @uses bbp_is_single_forum() to check it's a single forum page
- * @return array pagination the new pagination settings including the support status
- */
- public function pagination_for_status( $pagination = array() ) {
- if ( ! bbp_is_single_forum() ) {
- return $pagination;
- }
- if ( empty( $_GET['support_status'] ) ) {
- return $pagination;
- }
- $pagination['base'] .= '?support_status=' .intval( $_GET['support_status'] ) ;
- return $pagination;
- }
- /**
- * Enqueue some style for the widget
- *
- * @since 2.0
- *
- * @uses wp_enqueue_style() to load the css file
- * @uses bpbbpst_get_plugin_url() to get plugin's url
- * @uses bpbbpst_get_plugin_version() to get plugin's version
- */
- public function enqueue_style() {
- wp_enqueue_style( 'bpbbpst-bbpress-widget-css', bpbbpst_get_plugin_url( 'css' ). 'bpbbpst-bbpress-widget.css', false, bpbbpst_get_plugin_version() );
- }
- }