/wp-content/plugins/invite-anyone/admin/admin-stats.php

https://github.com/voidit/nycga2 · PHP · 422 lines · 273 code · 81 blank · 68 comment · 28 complexity · 76d832efc8b1dd4832fca2063bad8b79 MD5 · raw file

  1. <?php
  2. /**
  3. * The Stats class for Invite Anyone
  4. *
  5. * @package Invite Anyone
  6. * @since 0.9
  7. */
  8. class Invite_Anyone_Stats {
  9. var $time_periods;
  10. var $params;
  11. var $posts;
  12. var $date_sql;
  13. /**
  14. * PHP 4 constructor
  15. *
  16. * @package Invite Anyone
  17. * @since 0.9
  18. */
  19. function invite_anyone_stats() {
  20. $this->__construct();
  21. }
  22. /**
  23. * PHP 5 constructor
  24. *
  25. * @package Invite Anyone
  26. * @since 0.9
  27. */
  28. function __construct() {
  29. $this->setup_time_periods();
  30. $this->setup_get_params();
  31. $this->get_posts();
  32. }
  33. function setup_time_periods() {
  34. // $time_periods are in seconds
  35. $this->time_periods = apply_filters( 'invite_anyone_stats_time_periods', array(
  36. 60*60*24 => array(
  37. 'name' => __( '24 Hours', 'bp-invite-anyone' ),
  38. ),
  39. 60*60*24*3 => array(
  40. 'name' => __( '3 Days', 'bp-invite-anyone' ),
  41. ),
  42. 60*60*24*7 => array(
  43. 'name' => __( '1 Week', 'bp-invite-anyone' ),
  44. ),
  45. 60*60*24*28 => array(
  46. 'name' => __( '4 Weeks', 'bp-invite-anyone' ),
  47. ),
  48. 60*60*24*30*3 => array(
  49. 'name' => __( '3 Months', 'bp-invite-anyone' ),
  50. ),
  51. 0 => array(
  52. 'name' => __( 'All Time', 'bp-invite-anyone' ),
  53. )
  54. ) );
  55. }
  56. /**
  57. * Gets the stats params out of the $_GET global
  58. *
  59. * @package Invite Anyone
  60. * @since 0.9
  61. */
  62. function setup_get_params() {
  63. $params = array();
  64. if ( isset( $_REQUEST['user_id'] ) ) {
  65. $params['user_id'] = (int)$_REQUEST['user_id'];
  66. }
  67. $this->params = $params;
  68. }
  69. /**
  70. * Gets posts, based on the params provided
  71. *
  72. * @package Invite Anyone
  73. * @since 0.9
  74. */
  75. function get_posts() {
  76. global $wpdb;
  77. // $posts is a multidimensional array, containing all different time periods
  78. $posts = array();
  79. foreach( $this->time_periods as $tp => $period ) {
  80. $invite = new Invite_Anyone_Invitation;
  81. // Will be populated out of $this->params. Defaults to none?
  82. $args = array(
  83. 'posts_per_page' => '-1',
  84. 'status' => 'pending,draft,future,publish,trash'
  85. );
  86. // Create the date filter
  87. if ( $tp ) {
  88. $since = time() - $tp;
  89. $this->date_sql = $wpdb->prepare( " AND post_date > %s", date( 'Y-m-d H:i:s', $since ) );
  90. add_filter( 'posts_where_paged', array( $this, 'where_filter' ) );
  91. }
  92. $invites = $invite->get( $args );
  93. // Remove the filter
  94. if ( $tp ) {
  95. remove_filter( 'posts_where_paged', array( $this, 'where_filter' ) );
  96. }
  97. $period['total_count'] = 0;
  98. $period['accepted_count'] = 0;
  99. $period['total_count_cs'] = 0;
  100. $period['accepted_count_cs'] = 0;
  101. $period['unique_emails'] = 0;
  102. $period['unique_inviters'] = 0;
  103. $period['unique_emails'] = array();
  104. $period['unique_inviters'] = array();
  105. if ( $invites->have_posts() ) {
  106. while ( $invites->have_posts() ) {
  107. $invites->the_post();
  108. // Increase the total count
  109. $period['total_count']++;
  110. $author_key = get_the_author_ID();
  111. // If it's a new sender, add them to $unique_inviters
  112. if ( !isset( $period['unique_inviters'][$author_key] ) ) {
  113. $period['unique_inviters'][$author_key] = array(
  114. 'overall' => array(
  115. 'sent' => 0,
  116. 'accepted' => 0
  117. ),
  118. 'cloudsponge' => array(
  119. 'sent' => 0,
  120. 'accepted' => 0
  121. )
  122. );
  123. }
  124. // Bump the inviter's count
  125. $period['unique_inviters'][$author_key]['overall']['sent']++;
  126. // Is it accepted?
  127. $accepted = get_post_meta( get_the_ID(), 'bp_ia_accepted', true );
  128. if ( $accepted ) {
  129. // Total accepted count
  130. $period['accepted_count']++;
  131. // Author's accepted count
  132. $period['unique_inviters'][$author_key]['overall']['accepted']++;
  133. }
  134. // Is it a CloudSponge invite?
  135. $is_cloudsponge = get_post_meta( get_the_ID(), 'bp_ia_is_cloudsponge', true );
  136. if ( __( 'Yes', 'bp-invite-anyone' ) == $is_cloudsponge ) {
  137. $period['total_count_cs']++;
  138. // Author count
  139. $period['unique_inviters'][$author_key]['cloudsponge']['sent']++;
  140. if ( $accepted ) {
  141. // Total accepted count
  142. $period['accepted_count_cs']++;
  143. // Author's accepted count
  144. $period['unique_inviters'][$author_key]['cloudsponge']['accepted']++;
  145. }
  146. }
  147. }
  148. }
  149. // With all the data tallied, we can come up with some percentages
  150. // Overall acceptance rate
  151. if ( $period['total_count'] ) {
  152. $period['acceptance_rate'] = round( ( $period['accepted_count'] / $period['total_count'] ) * 100 );
  153. $period['acceptance_rate'] .= '%';
  154. } else {
  155. $period['acceptance_rate'] = __( 'n/a', 'bp-invite-anyone' );
  156. }
  157. // CS percentage
  158. if ( $period['total_count'] ) {
  159. $period['cs_percentage'] = round( ( $period['total_count_cs'] / $period['total_count'] ) * 100 );
  160. $period['cs_percentage'] .= '%';
  161. } else {
  162. $period['cs_percentage'] = __( 'n/a', 'bp-invite-anyone' );
  163. }
  164. // CS acceptance rate
  165. if ( $period['total_count_cs'] ) {
  166. $period['acceptance_rate_cs'] = round( ( $period['accepted_count_cs'] / $period['total_count_cs'] ) * 100 );
  167. $period['acceptance_rate_cs'] .= '%';
  168. } else {
  169. $period['acceptance_rate_cs'] = __( 'n/a', 'bp-invite-anyone' );
  170. }
  171. // Find the most active user
  172. $leader_user_id_pct = 0;
  173. $leader_val_pct = 0;
  174. $leader_user_id_num = 0;
  175. $leader_val_num = 0;
  176. $leader_user_id_pct_cs = 0;
  177. $leader_val_pct_cs = 0;
  178. $leader_user_id_num_cs = 0;
  179. $leader_val_num_cs = 0;
  180. foreach ( $period['unique_inviters'] as $user_id => $u ) {
  181. // Overall
  182. if ( $u['overall']['sent'] ) {
  183. if ( $u['overall']['sent'] >= $leader_val_num ) {
  184. $leader_user_id_num = $user_id;
  185. $leader_val_num = $u['overall']['sent'];
  186. }
  187. if ( ( $u['overall']['accepted'] / $u['overall']['sent'] ) >= $leader_val_pct ) {
  188. $leader_user_id_pct = $user_id;
  189. $leader_val_pct = $u['overall']['accepted'] / $u['overall']['sent'] * 100;
  190. }
  191. }
  192. // CloudSponge
  193. if ( $u['cloudsponge']['sent'] ) {
  194. if ( $u['cloudsponge']['sent'] >= $leader_val_num_cs ) {
  195. $leader_user_id_num_cs = $user_id;
  196. $leader_val_num_cs = $u['cloudsponge']['sent'];
  197. }
  198. if ( ( $u['cloudsponge']['accepted'] / $u['cloudsponge']['sent'] ) >= $leader_val_pct_cs ) {
  199. $leader_user_id_pct_cs = $user_id;
  200. $leader_val_pct_cs = $u['cloudsponge']['accepted'] / $u['cloudsponge']['sent'] * 100;
  201. }
  202. }
  203. }
  204. $period['top_users']['top_user_num'] = array(
  205. 'user_id' => $leader_user_id_num ? $leader_user_id_num : false,
  206. 'sent' => $leader_val_num ? $leader_val_num : false
  207. );
  208. $period['top_users']['top_user_pct'] = array(
  209. 'user_id' => $leader_user_id_pct ? $leader_user_id_pct : false,
  210. 'accepted' => $leader_val_pct ? round( $leader_val_pct ) . '%' : '-'
  211. );
  212. $period['top_users']['top_user_num_cs'] = array(
  213. 'user_id' => $leader_user_id_num_cs ? $leader_user_id_num_cs : false,
  214. 'sent' => $leader_val_num_cs ? $leader_val_num_cs : false
  215. );
  216. $period['top_users']['top_user_pct_cs'] = array(
  217. 'user_id' => $leader_user_id_pct_cs ? $leader_user_id_pct_cs : false,
  218. 'accepted' => $leader_val_pct_cs ? round( $leader_val_pct_cs ) . '%' : '-'
  219. );
  220. // Fetch userlinks
  221. foreach( $period['top_users'] as $key => $top_user ) {
  222. $link = bp_core_get_userlink( $top_user['user_id'] );
  223. $period['top_users'][$key]['user_link'] = $link;
  224. }
  225. $this->time_periods[$tp] = $period;
  226. }
  227. }
  228. /**
  229. * Add the date_sql filter to the where clause of the post query
  230. *
  231. * @package Invite Anyone
  232. * @since 0.9
  233. *
  234. * @param str $where Where clause from WP_Query
  235. * @param str $where Where clause with date_sql appended
  236. */
  237. function where_filter( $where ) {
  238. $where .= $this->date_sql;
  239. return $where;
  240. }
  241. /**
  242. * Displays the admin panel markup
  243. *
  244. * @package Invite Anyone
  245. * @since 0.9
  246. */
  247. function display() {
  248. ?>
  249. <table class="widefat ia-stats">
  250. <thead><tr>
  251. <th scope="col" class="in-the-last">
  252. <?php _e( 'In the last...', 'bp-invite-anyone' ) ?>
  253. </th>
  254. <th scope="col">
  255. <?php _e( 'Total Sent', 'bp-invite-anyone' ) ?>
  256. </th>
  257. <th scope="col">
  258. <?php _e( 'Total Accepted', 'bp-invite-anyone' ) ?>
  259. </th>
  260. <th scope="col">
  261. <?php _e( 'Acceptance Rate', 'bp-invite-anyone' ) ?>
  262. </th>
  263. <th scope="col" class="top-inviter">
  264. <?php _e( 'Top Inviter (by #)', 'bp-invite-anyone' ) ?>
  265. </th>
  266. <th scope="col" class="top-inviter">
  267. <?php _e( 'Top Inviter (by % accepted)', 'bp-invite-anyone' ) ?>
  268. </th>
  269. <?php if ( defined( 'INVITE_ANYONE_CS_ENABLED' ) && INVITE_ANYONE_CS_ENABLED ) : ?>
  270. <th scope="col">
  271. <?php _e( 'Total Sent (CloudSponge)', 'bp-invite-anyone' ) ?>
  272. </th>
  273. <th scope="col">
  274. <?php _e( 'Total Accepted (CloudSponge)', 'bp-invite-anyone' ) ?>
  275. </th>
  276. <th scope="col">
  277. <?php _e( 'Acceptance Rate (CloudSponge)', 'bp-invite-anyone' ) ?>
  278. </th>
  279. <th scope="col">
  280. <?php _e( 'CloudSponge Usage', 'bp-invite-anyone' ) ?>
  281. </th>
  282. <th scope="col" class="top-inviter">
  283. <?php _e( 'Top Inviter (by #) (CloudSponge)', 'bp-invite-anyone' ) ?>
  284. </th>
  285. <th scope="col" class="top-inviter">
  286. <?php _e( 'Top Inviter (by % accepted) (CloudSponge)', 'bp-invite-anyone' ) ?>
  287. </th>
  288. <?php endif ?>
  289. </tr></thead>
  290. <tbody>
  291. <?php foreach( $this->time_periods as $tp => $period ) : ?>
  292. <tr>
  293. <th scope="row">
  294. <?php echo esc_html( $period['name'] ) ?>
  295. </th>
  296. <td>
  297. <?php echo esc_html( $period['total_count'] ) ?>
  298. </td>
  299. <td>
  300. <?php echo esc_html( $period['accepted_count'] ) ?>
  301. </td>
  302. <td>
  303. <?php echo esc_html( $period['acceptance_rate'] ) ?>
  304. </td>
  305. <td>
  306. <?php echo $period['top_users']['top_user_num']['user_link'] ?> <span class="description"><?php printf( __( '(%d sent)', 'bp-invite-anyone' ), $period['top_users']['top_user_num']['sent'] ) ?></span>
  307. </td>
  308. <td>
  309. <?php echo $period['top_users']['top_user_pct']['user_link'] ?> <span class="description"><?php printf( __( '(%s accepted)', 'bp-invite-anyone' ), $period['top_users']['top_user_pct']['accepted'] ) ?></span>
  310. </td>
  311. <?php if ( defined( 'INVITE_ANYONE_CS_ENABLED' ) && INVITE_ANYONE_CS_ENABLED ) : ?>
  312. <td>
  313. <?php echo esc_html( $period['total_count_cs'] ) ?>
  314. </td>
  315. <td>
  316. <?php echo esc_html( $period['accepted_count_cs'] ) ?>
  317. </td>
  318. <td>
  319. <?php echo esc_html( $period['acceptance_rate_cs'] ) ?>
  320. </td>
  321. <td>
  322. <?php echo esc_html( $period['cs_percentage'] ) ?>
  323. </td>
  324. <td>
  325. <?php echo $period['top_users']['top_user_num_cs']['user_link'] ?> <span class="description"><?php printf( __( '(%d sent)', 'bp-invite-anyone' ), $period['top_users']['top_user_num_cs']['sent'] ) ?></span>
  326. </td>
  327. <td>
  328. <?php echo $period['top_users']['top_user_pct_cs']['user_link'] ?> <span class="description"><?php printf( __( '(%s accepted)', 'bp-invite-anyone' ), $period['top_users']['top_user_pct_cs']['accepted'] ) ?></span>
  329. </td>
  330. <?php endif ?>
  331. </tr>
  332. <?php endforeach ?>
  333. </tbody>
  334. </table>
  335. <?php if ( defined( 'INVITE_ANYONE_CS_ENABLED' ) && INVITE_ANYONE_CS_ENABLED ) : ?>
  336. <p class="description"><strong>Note:</strong> CloudSponge data has only been recorded since Invite Anyone v0.9.</p>
  337. <?php endif ?>
  338. <?php
  339. }
  340. }
  341. ?>