| @@ -1,13 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | -// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- weekly/monthly analytics aggregation; runs once per email send. | |
| 2 | + | |
| 3 | 3 | namespace WPDeveloper\BetterDocs\Admin; |
| 4 | 4 | |
| 5 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 6 | - exit; | |
| 7 | -} | |
| 8 | - | |
| 9 | - | |
| 10 | 5 | use WP_Error; |
| 11 | 6 | use WP_Query; |
| 12 | 7 | use stdClass; |
| 13 | 8 | use WPDeveloper\BetterDocs\Utils\Base; |
| @@ -96,21 +91,20 @@ | ||
| 96 | 91 | return self::$current_timestamp; |
| 97 | 92 | } |
| 98 | 93 | |
| 99 | 94 | public function create_date( $count = '-7days' ) { |
| 100 | - return gmdate( 'Y-m-d', strtotime( $count, self::timestamps() ) ); | |
| 95 | + return date( 'Y-m-d', strtotime( $count, self::timestamps() ) ); | |
| 101 | 96 | } |
| 102 | 97 | |
| 103 | 98 | public function get_views( $start_date, $end_date = null ) { |
| 104 | 99 | global $wpdb; |
| 100 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 105 | 101 | $query = $wpdb->get_results( |
| 106 | - $wpdb->prepare( | |
| 107 | - "SELECT sum(impressions) as views, sum(unique_visit) as unique_visit, sum(happy + sad + normal) as reactions | |
| 108 | - FROM {$wpdb->prefix}betterdocs_analytics | |
| 109 | - WHERE created_at BETWEEN %s AND %s", | |
| 110 | - (string) $start_date, | |
| 111 | - (string) $end_date | |
| 112 | - ) | |
| 102 | + " | |
| 103 | + SELECT sum(impressions) as views, sum(unique_visit) as unique_visit, sum(happy + sad + normal) as reactions | |
| 104 | + FROM {$wpdb->prefix}betterdocs_analytics | |
| 105 | + WHERE (created_at BETWEEN '" . $start_date . "' AND '" . $end_date . "') | |
| 106 | + " | |
| 113 | 107 | ); |
| 114 | 108 | return $query; |
| 115 | 109 | } |
| 116 | 110 | |
| @@ -115,16 +109,15 @@ | ||
| 115 | 109 | } |
| 116 | 110 | |
| 117 | 111 | public function get_search( $start_date, $end_date = null ) { |
| 118 | 112 | global $wpdb; |
| 113 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 119 | 114 | $query = $wpdb->get_results( |
| 120 | - $wpdb->prepare( | |
| 121 | - "SELECT SUM(count + not_found_count) as search_count, SUM(count) as search_found, SUM(not_found_count) as search_not_found_count | |
| 122 | - FROM {$wpdb->prefix}betterdocs_search_log as search_log | |
| 123 | - WHERE search_log.created_at BETWEEN %s AND %s", | |
| 124 | - (string) $start_date, | |
| 125 | - (string) $end_date | |
| 126 | - ) | |
| 115 | + " | |
| 116 | + SELECT SUM(count + not_found_count) as search_count, SUM(count) as search_found, SUM(not_found_count) as search_not_found_count | |
| 117 | + FROM {$wpdb->prefix}betterdocs_search_log as search_log | |
| 118 | + WHERE (search_log.created_at BETWEEN '" . $start_date . "' AND '" . $end_date . "') | |
| 119 | + " | |
| 127 | 120 | ); |
| 128 | 121 | |
| 129 | 122 | return $query; |
| 130 | 123 | } |
| @@ -148,22 +141,20 @@ | ||
| 148 | 141 | } |
| 149 | 142 | |
| 150 | 143 | public function get_search_keywords( $start_date, $end_date = null ) { |
| 151 | 144 | global $wpdb; |
| 145 | + $select = 'SELECT search_keyword.keyword, search_log.keyword_id, SUM(search_log.count + search_log.not_found_count) as total_search, SUM(search_log.count) as count, SUM(search_log.not_found_count) as not_found'; | |
| 146 | + $join = "FROM {$wpdb->prefix}betterdocs_search_keyword as search_keyword | |
| 147 | + JOIN {$wpdb->prefix}betterdocs_search_log as search_log on search_keyword.id = search_log.keyword_id"; | |
| 148 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 152 | 149 | $query = $wpdb->get_results( |
| 153 | - $wpdb->prepare( | |
| 154 | - "SELECT search_keyword.keyword, search_log.keyword_id, | |
| 155 | - SUM(search_log.count + search_log.not_found_count) as total_search, | |
| 156 | - SUM(search_log.count) as count, | |
| 157 | - SUM(search_log.not_found_count) as not_found | |
| 158 | - FROM {$wpdb->prefix}betterdocs_search_keyword as search_keyword | |
| 159 | - JOIN {$wpdb->prefix}betterdocs_search_log as search_log ON search_keyword.id = search_log.keyword_id | |
| 160 | - WHERE search_log.created_at BETWEEN %s AND %s | |
| 161 | - GROUP BY search_log.keyword_id | |
| 162 | - ORDER BY count DESC LIMIT 5", | |
| 163 | - (string) $start_date, | |
| 164 | - (string) $end_date | |
| 165 | - ) | |
| 150 | + " | |
| 151 | + {$select} | |
| 152 | + {$join} | |
| 153 | + WHERE (search_log.created_at BETWEEN '" . $start_date . "' AND '" . $end_date . "') | |
| 154 | + GROUP BY search_log.keyword_id | |
| 155 | + ORDER BY count DESC LIMIT 5 | |
| 156 | + " | |
| 166 | 157 | ); |
| 167 | 158 | |
| 168 | 159 | return $query; |
| 169 | 160 | } |
| @@ -200,25 +191,20 @@ | ||
| 200 | 191 | |
| 201 | 192 | public function get_leading_docs( $start_date, $end_date = null ) { |
| 202 | 193 | global $wpdb; |
| 203 | 194 | |
| 195 | + $select = 'SELECT docs.ID, docs.post_author, docs.post_title, SUM(analytics.impressions) as total_views, SUM(analytics.unique_visit) as total_unique_visit, SUM(analytics.happy + analytics.sad + analytics.normal) as total_reactions'; | |
| 196 | + $join = "FROM {$wpdb->prefix}posts as docs | |
| 197 | + JOIN {$wpdb->prefix}betterdocs_analytics as analytics on docs.ID = analytics.post_id"; | |
| 198 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 204 | 199 | $query = $wpdb->get_results( |
| 205 | - $wpdb->prepare( | |
| 206 | - "SELECT docs.ID, docs.post_author, docs.post_title, | |
| 207 | - SUM(analytics.impressions) as total_views, | |
| 208 | - SUM(analytics.unique_visit) as total_unique_visit, | |
| 209 | - SUM(analytics.happy + analytics.sad + analytics.normal) as total_reactions | |
| 210 | - FROM {$wpdb->posts} as docs | |
| 211 | - JOIN {$wpdb->prefix}betterdocs_analytics as analytics ON docs.ID = analytics.post_id | |
| 212 | - WHERE post_type = %s AND post_status = %s | |
| 213 | - AND analytics.created_at BETWEEN %s AND %s | |
| 214 | - GROUP BY analytics.post_id | |
| 215 | - ORDER BY total_views DESC LIMIT 5", | |
| 216 | - 'docs', | |
| 217 | - 'publish', | |
| 218 | - (string) $start_date, | |
| 219 | - (string) $end_date | |
| 220 | - ) | |
| 200 | + " | |
| 201 | + {$select} | |
| 202 | + {$join} | |
| 203 | + WHERE post_type = 'docs' AND post_status = 'publish' AND (analytics.created_at BETWEEN '" . $start_date . "' AND '" . $end_date . "') | |
| 204 | + GROUP BY analytics.post_id | |
| 205 | + ORDER BY total_views DESC LIMIT 5 | |
| 206 | + " | |
| 221 | 207 | ); |
| 222 | 208 | |
| 223 | 209 | $docs = []; |
| 224 | 210 | foreach ( $query as $key => $value ) { |
| @@ -471,9 +457,9 @@ | ||
| 471 | 457 | $days_ago = '1 day'; |
| 472 | 458 | break; |
| 473 | 459 | case 'betterdocs_monthly': |
| 474 | 460 | $initial_timestamp = strtotime( 'first day of last month', current_time( 'timestamp' ) ); |
| 475 | - $days_in_last_month = cal_days_in_month( CAL_GREGORIAN, (int) gmdate( 'm', $initial_timestamp ), (int) gmdate( 'Y', $initial_timestamp ) ); | |
| 461 | + $days_in_last_month = cal_days_in_month( CAL_GREGORIAN, date( 'm', $initial_timestamp ), date( 'Y', $initial_timestamp ) ); | |
| 476 | 462 | $days_ago = $days_in_last_month . ' days'; |
| 477 | 463 | break; |
| 478 | 464 | } |
| 479 | 465 | |