PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/class-supportflow-statistics.php

https://github.com/SupportFlow/supportflow
PHP | 411 lines | 405 code | 1 blank | 5 comment | 3 complexity | 7db4402456d06e3a5e0e6b678563d906 MD5 | raw file
  1. <?php
  2. /**
  3. * Show SupportFlow ticket statistics
  4. *
  5. * @since 0.1
  6. */
  7. defined( 'ABSPATH' ) or die( "Cheatin' uh?" );
  8. class SupportFlow_Statistics extends SupportFlow {
  9. public function __construct() {
  10. add_action( 'supportflow_after_setup_actions', array( $this, 'setup_actions' ) );
  11. }
  12. public function setup_actions() {
  13. add_action( 'admin_menu', array( $this, 'action_admin_menu' ) );
  14. }
  15. public function action_admin_menu() {
  16. $this->slug = 'sf_statistics';
  17. // Creates a submenu in SupportFlow menu
  18. add_submenu_page(
  19. 'edit.php?post_type=' . SupportFlow()->post_type,
  20. __( 'Statistics', 'supportflow' ),
  21. __( 'Statistics', 'supportflow' ),
  22. 'manage_options',
  23. $this->slug,
  24. array( $this, 'statistics_page' )
  25. );
  26. }
  27. /**
  28. * Display whole statistics page
  29. */
  30. public function statistics_page() {
  31. // Add JS and CSS code required by page
  32. $this->insert_css_code();
  33. $this->insert_script();
  34. ?>
  35. <div class="wrap">
  36. <h2><?php _e( 'Statistics', 'supportflow' ) ?></h2>
  37. <br />
  38. <div class="stat-box">
  39. <a class="toggle-link" href="#">
  40. <h3><?php _e( 'Overall statistics', 'supportflow' ) ?></h3>
  41. </a>
  42. <div class="toggle-content">
  43. <?php $this->show_overall_stats() ?>
  44. </div>
  45. </div>
  46. <div class="stat-box">
  47. <a class="toggle-link" href="#">
  48. <h3><?php _e( 'Tickets distribution by tag', 'supportflow' ) ?></h3>
  49. </a>
  50. <div class="toggle-content">
  51. <?php $this->show_tag_stats() ?>
  52. </div>
  53. </div>
  54. <div class="stat-box">
  55. <a class="toggle-link" href="#">
  56. <h3><?php _e( 'Daily statistics for last 30 days', 'supportflow' ) ?></h3>
  57. </a>
  58. <div class="toggle-content">
  59. <?php $this->show_30_days_stats() ?>
  60. </div>
  61. </div>
  62. <div class="stat-box">
  63. <a class="toggle-link" href="#">
  64. <h3><?php _e( 'Month statistics for last 12 months', 'supportflow' ) ?></h3>
  65. </a>
  66. <div class="toggle-content">
  67. <?php $this->show_12_month_stats() ?>
  68. </div>
  69. </div>
  70. <div class="stat-box">
  71. <a class="toggle-link" href="#">
  72. <h3><?php _e( 'Yearly statistics for last 5 years', 'supportflow' ) ?></h3>
  73. </a>
  74. <div class="toggle-content">
  75. <?php $this->show_5_year_stats() ?>
  76. </div>
  77. </div>
  78. </div>
  79. <?php
  80. }
  81. /*
  82. * Add CSS code required by statistics page
  83. */
  84. function insert_css_code() {
  85. ?>
  86. <style type="text/css">
  87. .stat-box .toggle-content {
  88. display: none;
  89. }
  90. </style>
  91. <?php
  92. }
  93. /*
  94. * Enqueue JS code required by statistics page
  95. */
  96. function insert_script() {
  97. $handle = SupportFlow()->enqueue_script( 'supportflow-statistics', 'toggle-links.js' );
  98. wp_localize_script( $handle, 'SFToggleLinks', array(
  99. 'expand' => __( 'Expand', 'supportflow' ),
  100. 'collapse' => __( 'Collapse', 'supportflow' ),
  101. ) );
  102. }
  103. /*
  104. * Show overall stats table.
  105. * Currently shows today, yesterday and overall (whole life) ticket stats
  106. */
  107. public function show_overall_stats() {
  108. $statistics_table = new SupportFlow_Table();
  109. $items = array();
  110. $statistics_table->set_columns( array(
  111. 'table_date' => __( 'Date', 'supportflow' ),
  112. 'table_new' => __( 'New tickets', 'supportflow' ),
  113. 'table_open' => __( 'Open tickets', 'supportflow' ),
  114. 'table_closed' => __( 'Closed tickets', 'supportflow' ),
  115. ) );
  116. $labels = array( 'Today', 'Yesterday' );
  117. for ( $i = 0; $i < 2; $i ++ ) {
  118. $date_time = strtotime( '-' . $i . ' days' );
  119. $link_date = date( "Ymd", $date_time );
  120. $link_value = date( "j-M-Y", $date_time );
  121. $post_date = array(
  122. 'year' => (int) date( "Y", $date_time ),
  123. 'month' => (int) date( "m", $date_time ),
  124. 'day' => (int) date( "j", $date_time ),
  125. );
  126. $items[] = $this->get_post_data_by_date( $post_date, $link_date, __( $labels[$i], 'supportflow' ) );
  127. }
  128. $items[] = $this->get_post_data_by_date( null, null, __( 'Total tickets', 'supportflow' ) );
  129. $statistics_table->set_data( $items );
  130. $statistics_table->display();
  131. }
  132. /*
  133. * Show tickets status per tag
  134. */
  135. public function show_tag_stats() {
  136. $statistics_table = new SupportFlow_Table();
  137. $items = array();
  138. $statistics_table->set_columns( array(
  139. 'table_tag' => __( 'Tag', 'supportflow' ),
  140. 'table_new' => __( 'New tickets', 'supportflow' ),
  141. 'table_open' => __( 'Open tickets', 'supportflow' ),
  142. 'table_closed' => __( 'Closed tickets', 'supportflow' ),
  143. ) );
  144. foreach ( get_terms( SupportFlow()->tags_tax, 'hide_empty=0' ) as $tag ) {
  145. $items[] = $this->get_post_data_by_tag( $tag->slug, $tag->slug, $tag->name );
  146. }
  147. $statistics_table->set_data( $items );
  148. $statistics_table->display();
  149. }
  150. /*
  151. * Shows stats of ticket created in last 30 days
  152. */
  153. public function show_30_days_stats() {
  154. $statistics_table = new SupportFlow_Table();
  155. $items = array();
  156. $statistics_table->set_columns( array(
  157. 'table_date' => __( 'Date', 'supportflow' ),
  158. 'table_new' => __( 'New tickets', 'supportflow' ),
  159. 'table_open' => __( 'Open tickets', 'supportflow' ),
  160. 'table_closed' => __( 'Closed tickets', 'supportflow' ),
  161. ) );
  162. for ( $i = 0; $i < 30; $i ++ ) {
  163. $date_time = strtotime( '-' . $i . ' days' );
  164. $post_date = array(
  165. 'year' => (int) date( "Y", $date_time ),
  166. 'month' => (int) date( "m", $date_time ),
  167. 'day' => (int) date( "j", $date_time ),
  168. );
  169. $link_date = date( "Ymd", $date_time );
  170. $link_value = date( "j-M-Y", $date_time );
  171. $items[] = $this->get_post_data_by_date( $post_date, $link_date, $link_value );
  172. }
  173. $statistics_table->set_data( $items );
  174. $statistics_table->display();
  175. }
  176. /*
  177. * Shows stats of ticket created in last 12 months
  178. */
  179. public function show_12_month_stats() {
  180. $statistics_table = new SupportFlow_Table();
  181. $items = array();
  182. $statistics_table->set_columns( array(
  183. 'table_date' => __( 'Date', 'supportflow' ),
  184. 'table_new' => __( 'New tickets', 'supportflow' ),
  185. 'table_open' => __( 'Open tickets', 'supportflow' ),
  186. 'table_closed' => __( 'Closed tickets', 'supportflow' ),
  187. ) );
  188. for ( $i = 0; $i < 12; $i ++ ) {
  189. $date_time = strtotime( '-' . $i . 'month' );
  190. $post_date = array(
  191. 'year' => (int) date( "Y", $date_time ),
  192. 'month' => (int) date( "m", $date_time ),
  193. );
  194. $link_date = date( "Ym", $date_time );
  195. $link_value = date( "M-Y", $date_time );
  196. $items[] = $this->get_post_data_by_date( $post_date, $link_date, $link_value );
  197. }
  198. $statistics_table->set_data( $items );
  199. $statistics_table->display();
  200. }
  201. /*
  202. * Shows stats of ticket created in last 5 years
  203. */
  204. public function show_5_year_stats() {
  205. $statistics_table = new SupportFlow_Table();
  206. $items = array();
  207. $statistics_table->set_columns( array(
  208. 'table_date' => __( 'Date', 'supportflow' ),
  209. 'table_new' => __( 'New tickets', 'supportflow' ),
  210. 'table_open' => __( 'Open tickets', 'supportflow' ),
  211. 'table_closed' => __( 'Closed tickets', 'supportflow' ),
  212. ) );
  213. for ( $i = 0; $i < 5; $i ++ ) {
  214. $date_time = strtotime( '-' . $i . 'year' );
  215. $post_date = array(
  216. 'year' => (int) date( "Y", $date_time ),
  217. );
  218. $link_date = date( "Y", $date_time );
  219. $link_value = date( "Y", $date_time );
  220. $items[] = $this->get_post_data_by_date( $post_date, $link_date, $link_value );
  221. }
  222. $statistics_table->set_data( $items );
  223. $statistics_table->display();
  224. }
  225. /**
  226. * Stats of post created on a particular date
  227. *
  228. * @param array $post_date Date in format like array('year'=>1994, 'month'=>12, 'day'=>5)
  229. * @param integer $link_date Date in format used in filtering by WP in all tickets page. e.g. 19941205
  230. * @param string $link_value Value to show user of link in table
  231. *
  232. * @return array Tickets stats created on a particular date. This array is directly usable in table
  233. */
  234. function get_post_data_by_date( $post_date = null, $link_date = null, $link_value = null ) {
  235. return array(
  236. 'table_date' => $link_value,
  237. 'table_new' => $this->get_post_link_by_date( $link_date, 'sf_new', $this->get_posts_count_by_date( 'sf_new', $post_date ) ),
  238. 'table_open' => $this->get_post_link_by_date( $link_date, 'sf_open', $this->get_posts_count_by_date( 'sf_open', $post_date ) ),
  239. 'table_closed' => $this->get_post_link_by_date( $link_date, 'sf_closed', $this->get_posts_count_by_date( 'sf_closed', $post_date ) ),
  240. );
  241. }
  242. /**
  243. * Stats of post created of a particular tag
  244. *
  245. * @param string $post_tag Slug of tag for which you want to get stats for
  246. * @param integer $link_tag Tag in format used in filtering by WP in all tickets page.
  247. * @param string $link_value Value to show user of link in table
  248. *
  249. * @return array Tickets stats created for a particular tag. This array is directly usable in table
  250. */
  251. function get_post_data_by_tag( $post_tag = null, $link_tag = null, $link_value = null ) {
  252. return array(
  253. 'table_tag' => $link_value,
  254. 'table_new' => $this->get_post_link_by_tag( $link_tag, 'sf_new', $this->get_posts_count_by_tag( 'sf_new', $post_tag ) ),
  255. 'table_open' => $this->get_post_link_by_tag( $link_tag, 'sf_open', $this->get_posts_count_by_tag( 'sf_open', $post_tag ) ),
  256. 'table_closed' => $this->get_post_link_by_tag( $link_tag, 'sf_closed', $this->get_posts_count_by_tag( 'sf_closed', $post_tag ) ),
  257. );
  258. }
  259. /**
  260. * Get count of post created on a particular date with particular post status.
  261. *
  262. * @param string $post_status Post status you want to get count. Using `*` to get count of all post statuses
  263. * @param type $date Date in format like array('year'=>1994, 'month'=>12, 'day'=>5)
  264. *
  265. * @return integer Count of posts
  266. */
  267. public function get_posts_count_by_date( $post_status = '*', $date = null ) {
  268. $args = array(
  269. 'post_type' => SupportFlow()->post_type,
  270. 'posts_per_page' => 1,
  271. 'post_parent' => 0,
  272. 'post_status' => $post_status,
  273. );
  274. if ( ! is_null( $date ) ) {
  275. $args['date_query'] = $date;
  276. }
  277. $wp_query = new WP_Query( $args );
  278. return (int) $wp_query->found_posts;
  279. }
  280. /**
  281. * Get count of post created of a particular tag with particular post status.
  282. *
  283. * @param string $post_status Post status you want to get count. Using `*` to get count of all post statuses
  284. * @param type $tag Slug of tag you want to get count of
  285. *
  286. * @return integer Count of posts
  287. */
  288. public function get_posts_count_by_tag( $post_status = '*', $tag = null ) {
  289. $args = array(
  290. 'post_type' => SupportFlow()->post_type,
  291. 'posts_per_page' => 1,
  292. 'post_parent' => 0,
  293. 'post_status' => $post_status,
  294. 'taxonomy' => SupportFlow()->tags_tax,
  295. );
  296. if ( ! is_null( $tag ) ) {
  297. $args['term'] = $tag;
  298. }
  299. $wp_query = new WP_Query( $args );
  300. return (int) $wp_query->found_posts;
  301. }
  302. /**
  303. * Generate a link that shows matching tickets in all ticket page
  304. *
  305. * @param integer $link_date Date in format used in filtering by WP in all tickets page. e.g. 19941205
  306. * @param string $post_status Post statuses that should be shown in all tickets page
  307. * @param string $link_value Value of hyperlink that should be shown to user
  308. *
  309. * @return string A hyperlink
  310. */
  311. function get_post_link_by_date( $link_date = null, $post_status = null, $link_value = null ) {
  312. $link = '<a href="edit.php?%s%s%s">%s</a>';
  313. $post_type = 'post_type=' . SupportFlow()->post_type;
  314. $post_status = is_null( $post_status ) ? '' : "&post_status=$post_status";
  315. $date = is_null( $link_date ) ? '' : "&m=$link_date";
  316. $value = is_null( $link_value ) ? '' : "$link_value";
  317. return sprintf( $link, $post_type, $post_status, $date, $value );
  318. }
  319. /**
  320. * Generate a link that shows matching tickets in all ticket page
  321. *
  322. * @param string $link_tag Slug of tag
  323. * @param string $post_status Post statuses that should be shown in all tickets page
  324. * @param string $link_value Value of hyperlink that should be shown to user
  325. *
  326. * @return string A hyperlink
  327. */
  328. function get_post_link_by_tag( $link_tag = null, $post_status = null, $link_value = null ) {
  329. $link = '<a href="edit.php?%s%s%s">%s</a>';
  330. $post_type = 'post_type=' . SupportFlow()->post_type;
  331. $post_status = is_null( $post_status ) ? '' : "&post_status=$post_status";
  332. $date = is_null( $link_tag ) ? '' : '&' . SupportFlow()->tags_tax . "=$link_tag";
  333. $value = is_null( $link_value ) ? '' : "$link_value";
  334. return sprintf( $link, $post_type, $post_status, $date, $value );
  335. }
  336. }
  337. SupportFlow()->extend->statistics = new SupportFlow_Statistics();