Bpbbpst_Support_Stats

Buddy-bbPress Support Topic statistic Widget.

Defined (1)

The class is defined in the following location(s).

/includes/widgets.php  
  1. class Bpbbpst_Support_Stats extends WP_Widget { 
  2. /** 
  3. * Buddy-bbPress Support Topic statistic Widget 
  4. * @since 1.1 
  5. * @uses WP_Widget::__construct() to init the widget 
  6. * @uses add_filter() to filter bbPress topic query, breadcrumb and pagination 
  7. * @uses add_action() to enqueue widget style 
  8. */ 
  9. public function __construct() { 
  10.  
  11. $widget_ops = array( 'description' => __( 'Displays support topic global statistics or of the active forum', 'buddy-bbpress-support-topic' ) ); 
  12. parent::__construct( false, $name = __( 'Support Topics Stats', 'buddy-bbpress-support-topic' ), $widget_ops ); 
  13.  
  14. /** bbPress filters to handle breadcrumb, pagination and topics query */ 
  15. add_filter( 'bbp_after_has_topics_parse_args', array( $this, 'filter_topics_query_by_status' ), 20, 1 ); 
  16. add_filter( 'bbp_get_breadcrumb', array( $this, 'breadcrumb_for_status' ), 20, 3 ); 
  17. add_filter( 'bbp_topic_pagination', array( $this, 'pagination_for_status' ), 20, 1 ); 
  18.  
  19. /** finally add some style */ 
  20. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ), 10 ); 
  21.  
  22. /** 
  23. * Registers the widget 
  24. * @since 1.1 
  25. * @uses register_widget() to register the widget 
  26. */ 
  27. public static function register_widget() { 
  28. register_widget( 'Bpbbpst_Support_Stats' ); 
  29.  
  30. /** 
  31. * Displays the output, the statistics 
  32. * Depending if the widget is displayed on a single forum template 
  33. * it displays the current forum stats or the global stats. 
  34. * If on a single forum, add links to filter by support status the topics 
  35. * @since 2.0 
  36. * @param mixed $args Arguments 
  37. * @param array $instance Instance 
  38. * @uses bbp_get_forum_id() to get the forum id 
  39. * @uses bbp_is_topic_edit() to check if the topic is being edited 
  40. * @uses bbp_get_topic_forum_id() to get the forum parent of the edited topic 
  41. * @uses bbp_get_topic_id() to get the edited topic id 
  42. * @uses bpbbpst_support_statistics() To get the statistics according to its parameter 
  43. * @uses add_query_arg() to add the querystring to filter the support topics 
  44. * @uses bbp_get_forum_permalink() to get the forum permalink 
  45. * @uses esc_attr() to sanitize attributes 
  46. * @uses get_post_type_archive_link() to build the link to forum archives 
  47. * @uses bbp_get_forum_post_type() to get the forum post type 
  48. * @return string html output 
  49. */ 
  50. public function widget( $args, $instance ) { 
  51. if ( ! $instance['bpbbpst_title'] ) { 
  52. $instance['bpbbpst_title'] = __( 'Support Topics Stats', 'buddy-bbpress-support-topic' ); 
  53.  
  54. if ( ! $instance['show_forum_link'] ) { 
  55. $instance['show_forum_link'] = false; 
  56.  
  57. $forum_id = bbp_get_forum_id(); 
  58. $stats_params = ""; 
  59.  
  60. if ( empty( $forum_id ) && bbp_is_topic_edit() ) { 
  61. $forum_id = bbp_get_topic_forum_id( bbp_get_topic_id() ); 
  62.  
  63. if ( ! empty( $forum_id ) ) { 
  64.  
  65. $stats_params = array( 
  66. 'meta_query' => array( 
  67. array( 
  68. 'key' => '_bpbbpst_support_topic',  
  69. 'value' => 1,  
  70. 'type' => 'numeric',  
  71. 'compare' => '>=' 
  72. ),  
  73. array( 
  74. 'key' => '_bbp_forum_id',  
  75. 'value' => $forum_id,  
  76. 'compare' => '=' 
  77. ); 
  78.  
  79. echo $args['before_widget']; 
  80. echo $args['before_title'] . $instance['bpbbpst_title'] . $args['after_title']; 
  81.  
  82. $support_statistics = bpbbpst_support_statistics( $stats_params ); 
  83.  
  84. if ( ! empty( $support_statistics['total_support'] ) ) { 
  85.  
  86. $status_stats = $support_statistics['allstatus']; 
  87.  
  88. if ( ! is_array( $status_stats ) || count( $status_stats ) < 1 ) { 
  89. return false; 
  90.  
  91. $num_percent = $support_statistics['percent']; 
  92. $text_percent = __( 'Resolved so far', 'buddy-bbpress-support-topic' ); ?> 
  93.  
  94. <ul class="bpbbpst-widget"> 
  95. <li class="bpbbpst-percent"> 
  96. <span class="bpbbpst-text"><?php echo esc_html( $text_percent );?></span> <span class="bpbbpst-num"><?php echo esc_html( $num_percent );?></span> 
  97. </li> 
  98.  
  99. <?php foreach ( $status_stats as $key => $stat ) : 
  100.  
  101. $num = $stat['stat']; 
  102. $text = $stat['label']; 
  103. $class = $stat['front_class']; 
  104.  
  105. if ( ! empty( $forum_id ) ) { 
  106. $link = esc_url( add_query_arg( array( 'support_status' => $key ), bbp_get_forum_permalink() ) ); 
  107. $num = '<a href="'. $link .'" title="'. esc_attr( $text ) .'">'. esc_html( $num ) .'</a>' ; 
  108. $text = '<a href="'. $link .'" title="'. esc_attr( $text ) .'">'. esc_html( $text ) .'</a>' ; 
  109.  
  110. ?> 
  111.  
  112. <li class="bpbbpst-<?php echo $class;?> status"> 
  113. <span class="bpbbpst-num"><?php echo $num;?></span> <span class="bpbbpst-text"><?php echo $text;?></span> 
  114. </li> 
  115.  
  116. <?php endforeach;?> 
  117.  
  118. </ul> 
  119. <?php 
  120.  
  121. // Otherwise display there are no stats available! 
  122. } else { 
  123. printf( '<p>%s</p>',  
  124. esc_html__( 'No statisitcs available.' , 'buddy-bbpress-support-topic' ) 
  125. ); 
  126.  
  127. if ( ! is_bbpress() && ! empty( $instance['show_forum_link'] ) ) { 
  128. printf( '<p><a href="%1$s" title="%2$s">%3$s</a></p>',  
  129. esc_url( get_post_type_archive_link( bbp_get_forum_post_type() ) ),  
  130. esc_attr__( 'Go to forums' , 'buddy-bbpress-support-topic' ),  
  131. esc_html__( 'Go to forums' , 'buddy-bbpress-support-topic' ) 
  132. ); 
  133.  
  134. echo $args['after_widget']; 
  135.  
  136. /** 
  137. * Update the statistics widget options (title) 
  138. * @since 1.1 
  139. * @param array $new_instance The new instance options 
  140. * @param array $old_instance The old instance options 
  141. * @return array the instance 
  142. */ 
  143. public function update( $new_instance, $old_instance ) { 
  144. $instance = $old_instance; 
  145.  
  146. $instance['bpbbpst_title'] = strip_tags( $new_instance['bpbbpst_title'] ); 
  147.  
  148. if ( ! empty( $new_instance['show_forum_link'] ) ) { 
  149. $instance['show_forum_link'] = intval( $new_instance['show_forum_link'] ); 
  150. } else { 
  151. $instance['show_forum_link'] = 0; 
  152.  
  153. return $instance; 
  154.  
  155. /** 
  156. * Output the statistics widget options form 
  157. * @since 1.1 
  158. * @param $instance Instance 
  159. * @uses wp_parse_args() to merge instance defined arguments into defaults array 
  160. * @uses Bpbbpst_Support_Stats::get_field_id() To output the field id 
  161. * @uses Bpbbpst_Support_Stats::get_field_name() To output the field name 
  162. * @uses checked() to activate the checkbox if it needs to 
  163. * @return string html output 
  164. */ 
  165. public function form( $instance ) { 
  166. $defaults = array( 
  167. 'bpbbpst_title' => __( 'Support Topics Stats', 'buddy-bbpress-support-topic' ),  
  168. 'show_forum_link' => 0 
  169. ); 
  170.  
  171. $instance = wp_parse_args( (array) $instance, $defaults ); 
  172.  
  173. $bpbbpst_title = strip_tags( $instance['bpbbpst_title'] ); 
  174. $show_forum_link = strip_tags( $instance['show_forum_link'] ); 
  175. ?> 
  176. <p> 
  177. <label for="bpbbpst_title"><?php _e( 'Title:', 'buddy-bbpress-support-topic' ); ?> 
  178. <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%" /> 
  179. </label> 
  180. </p> 
  181. <label for="show_forum_link"> 
  182. <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' ); ?> 
  183. </label> 
  184. <p class="description"><?php _e( 'When not on a forum page, display a link to it', 'buddy-bbpress-support-topic' )?></p> 
  185. <?php 
  186.  
  187. /** 
  188. * Adds a meta_query to the topics query 
  189. * @since 1.1 
  190. * @param mixed $args Arguments 
  191. * @uses bbp_is_single_forum() to check it's a single forum page 
  192. * @return mixed $args the new arguments eventually including the meta_query 
  193. */ 
  194. public function filter_topics_query_by_status( $args = "" ) { 
  195. if ( ! bbp_is_single_forum() ) { 
  196. return $args; 
  197.  
  198. if ( empty( $_GET['support_status'] ) ) { 
  199. return $args; 
  200.  
  201. /** if meta_query exists, we dont want to break anything */ 
  202. if ( ! empty( $args['meta_query'] ) ) { 
  203. return $args; 
  204.  
  205. $args['meta_query'] = array( 
  206. array( 
  207. 'key' => '_bpbbpst_support_topic',  
  208. 'value' => intval( $_GET['support_status'] ),  
  209. 'compare' => '=' 
  210. ); 
  211.  
  212. $args['show_stickies'] = false; 
  213.  
  214. return $args; 
  215.  
  216. /** 
  217. * Adds the support status at the end of the breadcrumb 
  218. * @since 2.0 
  219. * @param string $trail the breadcrumb html 
  220. * @param array $crumbs the different element to show in the breadcrumb 
  221. * @param array $args arguments to organize the display of the breadcrumb 
  222. * @uses bbp_is_single_forum() to check it's a single forum page 
  223. * @uses bpbbpst_get_selected_support_status() to get the caption for the filtered status 
  224. * @uses bbp_get_forum_permalink() to get the forum's permalink 
  225. * @uses esc_html() to sanitize the status 
  226. * @return string $trail the new breadcrumb eventually including the support status 
  227. */ 
  228. public function breadcrumb_for_status( $trail, $crumbs, $args ) { 
  229. if ( ! bbp_is_single_forum() ) { 
  230. return $trail; 
  231.  
  232. if ( empty( $_GET['support_status'] ) ) { 
  233. return $trail; 
  234.  
  235. $last = $crumbs[ count($crumbs) -1 ]; 
  236. $sep = $args['sep']; 
  237.  
  238. if ( ! empty( $sep ) ) { 
  239. $sep = $args['sep_before'] . $sep . $args['sep_after']; 
  240.  
  241. $pad_sep = $args['pad_sep']; 
  242.  
  243. // Pad the separator 
  244. if ( ! empty( $pad_sep ) ) { 
  245. $sep = str_pad( $sep, strlen( $sep ) + ( (int) $pad_sep * 2 ), ' ', STR_PAD_BOTH ); 
  246.  
  247. $support_status = bpbbpst_get_selected_support_status( $_GET['support_status'] ); 
  248.  
  249. $trail = str_replace( $last, '<a href="'. bbp_get_forum_permalink() .'" >'. $last .'</a>' . $sep . esc_html( $support_status['sb-caption'] ), $trail ); 
  250.  
  251. return $trail; 
  252.  
  253. /** 
  254. * Adds the support status at the end of the querystring 
  255. * @since 1.1 
  256. * @param array pagination pagination settings 
  257. * @uses bbp_is_single_forum() to check it's a single forum page 
  258. * @return array pagination the new pagination settings including the support status 
  259. */ 
  260. public function pagination_for_status( $pagination = array() ) { 
  261. if ( ! bbp_is_single_forum() ) { 
  262. return $pagination; 
  263.  
  264. if ( empty( $_GET['support_status'] ) ) { 
  265. return $pagination; 
  266.  
  267. $pagination['base'] .= '?support_status=' .intval( $_GET['support_status'] ) ; 
  268.  
  269. return $pagination; 
  270.  
  271. /** 
  272. * Enqueue some style for the widget 
  273. * @since 2.0 
  274. * @uses wp_enqueue_style() to load the css file 
  275. * @uses bpbbpst_get_plugin_url() to get plugin's url 
  276. * @uses bpbbpst_get_plugin_version() to get plugin's version 
  277. */ 
  278. public function enqueue_style() { 
  279. wp_enqueue_style( 'bpbbpst-bbpress-widget-css', bpbbpst_get_plugin_url( 'css' ). 'bpbbpst-bbpress-widget.css', false, bpbbpst_get_plugin_version() );