| 1 |
<?php |
| 2 |
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
if( ! class_exists('MWTSA_Admin_Stats') ) { |
| 5 |
|
| 6 |
class MWTSA_Admin_Stats { |
| 7 |
|
| 8 |
public $can_see_stats; |
| 9 |
public $can_update_options; |
| 10 |
|
| 11 |
public function __construct() { |
| 12 |
$this->set_constants(); |
| 13 |
|
| 14 |
add_action( 'admin_menu', array( $this, 'add_admin_menu' ) ); |
| 15 |
add_action( 'admin_enqueue_scripts', array( $this, 'load_admin_style' ) ); |
| 16 |
} |
| 17 |
|
| 18 |
private function set_constants() { |
| 19 |
$this->can_see_stats = array( |
| 20 |
'administrator' |
| 21 |
); |
| 22 |
|
| 23 |
$this->can_update_options = array( |
| 24 |
'administrator' |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
public function add_admin_menu() { |
| 29 |
add_submenu_page( 'index.php', __( 'Search Analytics', 'mwt-search-analytics' ), __( 'Search Analytics', 'mwt-search-analytics' ), 'manage_options', __FILE__, array( &$this, 'render_stats_page' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
public function load_admin_style( $hook ) { |
| 33 |
global $mwtsa; |
| 34 |
|
| 35 |
if($hook != 'dashboard_page_mwt-search-analytics/admin/includes/class.stats') { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
wp_enqueue_style( 'mwtsa-stats-style', $mwtsa->plugin_admin_url . 'assets/css/stats-style.css', array(), $mwtsa->version ); |
| 40 |
} |
| 41 |
|
| 42 |
public function render_stats_page() { |
| 43 |
?> |
| 44 |
<div class="wrap mwtsa-wrapper"> |
| 45 |
<h2><?php _e('Search history', 'search-meter') ?></h2> |
| 46 |
|
| 47 |
<div class="table-group"> |
| 48 |
<h3><?php _e('24 hours history') ?></h3> |
| 49 |
|
| 50 |
<?php $this->display_history_table( 1, 'day' ) ?> |
| 51 |
</div> |
| 52 |
<div class="table-group"> |
| 53 |
<h3><?php _e('7 days history') ?></h3> |
| 54 |
|
| 55 |
<?php $this->display_history_table( 1, 'week' ) ?> |
| 56 |
</div> |
| 57 |
<div class="table-group"> |
| 58 |
<h3><?php _e('30 days history') ?></h3> |
| 59 |
|
| 60 |
<?php $this->display_history_table( 1, 'month' ) ?> |
| 61 |
</div> |
| 62 |
</div> |
| 63 |
<?php |
| 64 |
} |
| 65 |
|
| 66 |
public function get_history_data( $since = 1, $unit = 'day', $min_posts = 0, $limit = 50 ) { |
| 67 |
global $wpdb; |
| 68 |
|
| 69 |
if ( in_array( $unit, array( 'day', 'minute', 'week', 'month' ) ) ) { |
| 70 |
$unit = strtoupper( $unit ); |
| 71 |
} |
| 72 |
|
| 73 |
$history = $wpdb->get_results( |
| 74 |
"SELECT t.term, COUNT( h.id ) as `count`, AVG( h.count_posts ) as average_posts |
| 75 |
FROM {$wpdb->prefix}mwt_search_terms as t |
| 76 |
JOIN {$wpdb->prefix}mwt_search_history as h ON t.id = h.term_id |
| 77 |
WHERE DATE_SUB( CURDATE(), INTERVAL $since $unit ) <= h.datetime |
| 78 |
GROUP BY h.term_id |
| 79 |
ORDER BY `count` DESC, t.term DESC |
| 80 |
LIMIT 50 |
| 81 |
", 'ARRAY_A' |
| 82 |
); |
| 83 |
|
| 84 |
return $history; |
| 85 |
} |
| 86 |
|
| 87 |
public function display_history_table( $since = 1, $unit = 'day', $min_posts = 0, $limit = 50 ) { |
| 88 |
|
| 89 |
$history = $this->get_history_data( $since, $unit, $min_posts, $limit ); |
| 90 |
|
| 91 |
if( ! empty( $history ) ) { |
| 92 |
?> |
| 93 |
<table> |
| 94 |
<tr class="table-head"> |
| 95 |
<th class="col-term"><?php _e('Term') ?></th> |
| 96 |
<th class="col-searches"><?php _e('No. of searches ') ?></th> |
| 97 |
<th class="col-results"><?php _e('Average no. of results') ?></th> |
| 98 |
</tr> |
| 99 |
<?php $alt = false; ?> |
| 100 |
<?php foreach ( $history as $row ) : ?> |
| 101 |
<?php $class = ( $row['average_posts'] < 0 ) ? 'low-posts' : ''; |
| 102 |
if( $alt ) { |
| 103 |
$class .= 'alt'; |
| 104 |
} |
| 105 |
|
| 106 |
$alt = ! $alt; |
| 107 |
?> |
| 108 |
<tr class="<?php echo $class ?>"> |
| 109 |
<td><?php echo $row['term'] ?></td> |
| 110 |
<td><?php echo $row['count'] ?></td> |
| 111 |
<td><?php echo $row['average_posts'] ?></td> |
| 112 |
</tr> |
| 113 |
<?php endforeach; ?> |
| 114 |
</table> |
| 115 |
<?php |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return new MWTSA_Admin_Stats(); |
| 122 |
|
| 123 |
|
| 124 |
|