| 1 |
<?php |
| 2 |
/** |
| 3 |
* Helpers Post Views |
| 4 |
* |
| 5 |
* @package Powerkit |
| 6 |
* @subpackage Modules/Helper |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Google Analytics API Call |
| 11 |
* |
| 12 |
* @param string $url The url. |
| 13 |
* @param array $params The params. |
| 14 |
* @param bool $url_encode The url encode. |
| 15 |
*/ |
| 16 |
function powerkit_post_views_api_call( $url, $params = array(), $url_encode = true ) { |
| 17 |
|
| 18 |
$options = powerkit_post_views_options(); |
| 19 |
|
| 20 |
if ( time() >= $options['expires'] ) { |
| 21 |
|
| 22 |
$options = powerkit_post_views_refresh_token(); |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
$qs = '?access_token=' . rawurlencode( $options['token'] ); |
| 27 |
|
| 28 |
foreach ( $params as $k => $v ) { |
| 29 |
|
| 30 |
$qs .= '&' . $k . '=' . ( $url_encode ? rawurlencode( $v ) : $v ); |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
$request = new WP_Http(); |
| 35 |
|
| 36 |
$result = $request->request( $url . $qs ); |
| 37 |
|
| 38 |
$json = new stdClass(); |
| 39 |
|
| 40 |
$options['error'] = null; |
| 41 |
|
| 42 |
if ( is_array( $result ) && isset( $result['response']['code'] ) && 200 === $result['response']['code'] ) { |
| 43 |
|
| 44 |
$json = json_decode( $result['body'] ); |
| 45 |
|
| 46 |
update_option( 'powerkit_post_views_options', $options ); |
| 47 |
|
| 48 |
return $json; |
| 49 |
|
| 50 |
} else { |
| 51 |
|
| 52 |
if ( is_array( $result ) && isset( $result['response']['code'] ) && 403 === $result['response']['code'] ) { |
| 53 |
|
| 54 |
$json = json_decode( $result['body'], true ); |
| 55 |
|
| 56 |
$options['error'] = $json['error']['errors'][0]['message']; |
| 57 |
|
| 58 |
update_option( 'powerkit_post_views_options', $options ); |
| 59 |
|
| 60 |
} else { |
| 61 |
$options['error'] = esc_html__( 'Failed to get data of Google Analytics.', 'powerkit' ); |
| 62 |
|
| 63 |
update_option( 'powerkit_post_views_options', $options ); |
| 64 |
} |
| 65 |
|
| 66 |
return new stdClass(); |
| 67 |
} |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Google Analytics refresh token. |
| 73 |
*/ |
| 74 |
function powerkit_post_views_refresh_token() { |
| 75 |
|
| 76 |
$options = powerkit_post_views_options(); |
| 77 |
|
| 78 |
/* If the token has expired, we create it again */ |
| 79 |
if ( ! empty( $options['token_refresh'] ) ) { |
| 80 |
|
| 81 |
$request = new WP_Http(); |
| 82 |
|
| 83 |
$result = $request->request( |
| 84 |
'https://accounts.google.com/o/oauth2/token', array( |
| 85 |
'method' => 'POST', |
| 86 |
'body' => array( |
| 87 |
'client_id' => $options['clientid'], |
| 88 |
'client_secret' => $options['psecret'], |
| 89 |
'refresh_token' => $options['token_refresh'], |
| 90 |
'grant_type' => 'refresh_token', |
| 91 |
), |
| 92 |
) |
| 93 |
); |
| 94 |
|
| 95 |
$options['error'] = null; |
| 96 |
|
| 97 |
if ( is_array( $result ) && isset( $result['response']['code'] ) && 200 === $result['response']['code'] ) { |
| 98 |
|
| 99 |
$tjson = json_decode( $result['body'] ); |
| 100 |
|
| 101 |
$request = new WP_Http(); |
| 102 |
|
| 103 |
$result = $request->request( 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=' . rawurlencode( $tjson->access_token ) ); |
| 104 |
|
| 105 |
if ( is_array( $result ) && isset( $result['response']['code'] ) && 200 === $result['response']['code'] ) { |
| 106 |
|
| 107 |
$ijson = json_decode( $result['body'] ); |
| 108 |
|
| 109 |
$options['token'] = $tjson->access_token; |
| 110 |
|
| 111 |
if ( isset( $tjson->refresh_token ) && ! empty( $tjson->refresh_token ) ) { |
| 112 |
$options['token_refresh'] = $tjson->refresh_token; |
| 113 |
} |
| 114 |
|
| 115 |
$options['expires'] = time() + $tjson->expires_in; |
| 116 |
$options['gid'] = $ijson->id; |
| 117 |
|
| 118 |
update_option( 'powerkit_post_views_options', $options ); |
| 119 |
|
| 120 |
} elseif ( is_array( $result ) && isset( $result['response']['code'] ) && 403 === $result['response']['code'] ) { |
| 121 |
|
| 122 |
$json = json_decode( $result['body'], true ); |
| 123 |
|
| 124 |
$options['error'] = $json['error']['errors'][0]['message']; |
| 125 |
|
| 126 |
update_option( 'powerkit_post_views_options', $options ); |
| 127 |
} else { |
| 128 |
$options['error'] = esc_html__( 'Failed to update token of Google Analytics.', 'powerkit' ); |
| 129 |
|
| 130 |
update_option( 'powerkit_post_views_options', $options ); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
return $options; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Get post views settings. |
| 140 |
*/ |
| 141 |
function powerkit_post_views_options() { |
| 142 |
|
| 143 |
$options = get_option( 'powerkit_post_views_options', array() ); |
| 144 |
|
| 145 |
$options['clientid'] = isset( $options['clientid'] ) ? $options['clientid'] : null; |
| 146 |
$options['psecret'] = isset( $options['psecret'] ) ? $options['psecret'] : null; |
| 147 |
$options['gid'] = isset( $options['gid'] ) ? $options['gid'] : null; |
| 148 |
$options['gmail'] = isset( $options['gmail'] ) ? $options['gmail'] : null; |
| 149 |
$options['token'] = isset( $options['token'] ) ? $options['token'] : null; |
| 150 |
$options['defaultval'] = isset( $options['defaultval'] ) ? $options['defaultval'] : 0; |
| 151 |
$options['token_refresh'] = isset( $options['token_refresh'] ) ? $options['token_refresh'] : null; |
| 152 |
$options['expires'] = isset( $options['expires'] ) ? $options['expires'] : null; |
| 153 |
$options['wid'] = isset( $options['wid'] ) ? $options['wid'] : null; |
| 154 |
$options['column'] = isset( $options['column'] ) ? $options['column'] : false; |
| 155 |
$options['trailing'] = isset( $options['trailing'] ) ? $options['trailing'] : false; |
| 156 |
$options['metric'] = isset( $options['metric'] ) ? $options['metric'] : 'ga:pageviews'; |
| 157 |
|
| 158 |
if ( ! isset( $options['startdate'] ) ) { |
| 159 |
$options['startdate'] = date( 'Y-m-d', strtotime( '-1 year' ) ); |
| 160 |
} |
| 161 |
|
| 162 |
return $options; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Get cache time |
| 167 |
* |
| 168 |
* @since 1.0.0 |
| 169 |
* @param string|int $post_id Post ID. |
| 170 |
*/ |
| 171 |
function powerkit_post_views_get_cache_time( $post_id = false ) { |
| 172 |
|
| 173 |
// Post Id. |
| 174 |
if ( ! $post_id ) { |
| 175 |
$post_id = get_the_ID(); |
| 176 |
} |
| 177 |
|
| 178 |
// Post age in seconds. |
| 179 |
$post_age = floor( intval( date( 'U' ) ) - intval( get_post_time( 'U', true, $post_id ) ) ); |
| 180 |
|
| 181 |
$two_months_period = apply_filters( 'powerkit_post_views_two_months', 5184000 ); |
| 182 |
$three_weeks_period = apply_filters( 'powerkit_post_views_three_weeks', 1814400 ); |
| 183 |
|
| 184 |
if ( isset( $post_age ) && $post_age > $two_months_period ) { |
| 185 |
|
| 186 |
// Post older than 60 days - expire cache after 12 hours. |
| 187 |
$seconds = apply_filters( 'powerkit_post_views_refresh_60_days', 43200 ); |
| 188 |
|
| 189 |
} elseif ( isset( $post_age ) && $post_age > $three_weeks_period ) { |
| 190 |
|
| 191 |
// Post older than 21 days - expire cache after 4 hours. |
| 192 |
$seconds = apply_filters( 'powerkit_post_views_refresh_21_days', 14400 ); |
| 193 |
|
| 194 |
} else { |
| 195 |
|
| 196 |
// Expire cache after one hour. |
| 197 |
$seconds = apply_filters( 'powerkit_post_views_refresh_1_hour', 3600 ); |
| 198 |
} |
| 199 |
|
| 200 |
return $seconds; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Get no-cached post views |
| 205 |
* |
| 206 |
* @param int $post_id The post id. |
| 207 |
*/ |
| 208 |
function powerkit_get_nocached_post_views( $post_id ) { |
| 209 |
if ( empty( $post_id ) ) { |
| 210 |
$post_id = get_the_ID(); |
| 211 |
} |
| 212 |
|
| 213 |
global $wpdb; |
| 214 |
|
| 215 |
$query = 'SELECT * FROM ' . $wpdb->prefix . 'pk_post_views WHERE id = ' . $post_id . ' AND type = 1'; |
| 216 |
|
| 217 |
// Get cached data. |
| 218 |
$post_views = wp_cache_get( md5( $query ), 'pk-get-no-post-views' ); |
| 219 |
|
| 220 |
// Cached data not found? |
| 221 |
if ( false === $post_views ) { |
| 222 |
$post_row = $wpdb->get_row( $query ); |
| 223 |
|
| 224 |
if ( $post_row ) { |
| 225 |
$post_views = (float) $post_row->count; |
| 226 |
|
| 227 |
// Set the cache expiration, 5 minutes by default. |
| 228 |
$expire = absint( apply_filters( 'pk_object_cache_expire', 5 * 60, 'post-views' ) ); |
| 229 |
|
| 230 |
wp_cache_add( md5( $query ), $post_views, 'pk-get-no-post-views', $expire ); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
return $post_views; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Get cached post views |
| 239 |
* |
| 240 |
* @param int $post_id The post id. |
| 241 |
*/ |
| 242 |
function powerkit_get_cached_post_views( $post_id ) { |
| 243 |
if ( empty( $post_id ) ) { |
| 244 |
$post_id = get_the_ID(); |
| 245 |
} |
| 246 |
|
| 247 |
global $wpdb; |
| 248 |
|
| 249 |
$query = 'SELECT * FROM ' . $wpdb->prefix . 'pk_post_views WHERE id = ' . $post_id . ' AND type = 1'; |
| 250 |
|
| 251 |
// Get cached data. |
| 252 |
$post_views = wp_cache_get( md5( $query ), 'pk-get-post-views' ); |
| 253 |
|
| 254 |
// Cached data not found? |
| 255 |
if ( false === $post_views ) { |
| 256 |
$post_row = $wpdb->get_row( $query ); |
| 257 |
|
| 258 |
if ( $post_row && ( intval( date( 'U' ) ) < intval( $post_row->period ) ) ) { |
| 259 |
$post_views = (float) $post_row->count; |
| 260 |
|
| 261 |
// Set the cache expiration, 5 minutes by default. |
| 262 |
$expire = absint( apply_filters( 'pk_object_cache_expire', 5 * 60, 'post-views' ) ); |
| 263 |
|
| 264 |
wp_cache_add( md5( $query ), $post_views, 'pk-get-post-views', $expire ); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
return $post_views; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Set cached post views |
| 273 |
* |
| 274 |
* @param int $post_id The post id. |
| 275 |
* @param int $count The count. |
| 276 |
*/ |
| 277 |
function powerkit_set_cached_post_views( $post_id, $count ) { |
| 278 |
global $wpdb; |
| 279 |
|
| 280 |
$period = powerkit_post_views_get_cache_time( $post_id ) + intval( date( 'U' ) ); |
| 281 |
|
| 282 |
$count = (float) $count; |
| 283 |
|
| 284 |
return $wpdb->query( |
| 285 |
$wpdb->prepare( |
| 286 |
'INSERT INTO ' . $wpdb->prefix . 'pk_post_views (id, type, period, count) |
| 287 |
VALUES (%1$d, %2$d, %3$s, %4$d) |
| 288 |
ON DUPLICATE KEY UPDATE period = %3$s, count = %4$d', $post_id, 1, $period, $count |
| 289 |
) |
| 290 |
); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Get post views |
| 295 |
* |
| 296 |
* @param int $post_id The post id. |
| 297 |
* @param array $format The format. |
| 298 |
*/ |
| 299 |
function powerkit_get_post_views( $post_id = null, $format = true ) { |
| 300 |
|
| 301 |
if ( ! $post_id ) { |
| 302 |
$post_id = get_the_ID(); |
| 303 |
} |
| 304 |
|
| 305 |
if ( ! $post_id ) { |
| 306 |
return apply_filters( 'powerkit_get_post_views', $options['defaultval'], $post_id, $format ); |
| 307 |
} |
| 308 |
|
| 309 |
$options = powerkit_post_views_options(); |
| 310 |
|
| 311 |
// Set start date. |
| 312 |
$start_date = $options['startdate']; |
| 313 |
|
| 314 |
// Get permalink. |
| 315 |
$basename = basename( get_permalink( $post_id ) ); |
| 316 |
|
| 317 |
if ( $options['trailing'] ) { |
| 318 |
$basename .= '/'; |
| 319 |
} |
| 320 |
|
| 321 |
$permalink = '/' . $basename; |
| 322 |
|
| 323 |
// Get post date. |
| 324 |
$post_date = get_the_date( 'Y-m-d', $post_id ); |
| 325 |
|
| 326 |
// Check if the published date is earlier than default start date. |
| 327 |
if ( strtotime( $post_date ) > strtotime( $options['startdate'] ) ) { |
| 328 |
$start_date = $post_date; |
| 329 |
} |
| 330 |
|
| 331 |
// Get cached post views. |
| 332 |
$result = powerkit_get_cached_post_views( $post_id ); |
| 333 |
|
| 334 |
if ( false === $result || '' === $result ) { |
| 335 |
|
| 336 |
if ( empty( $options['token'] ) ) { |
| 337 |
return apply_filters( 'powerkit_get_post_views', $options['defaultval'], $post_id, $format ); |
| 338 |
} |
| 339 |
|
| 340 |
$json = powerkit_post_views_api_call( |
| 341 |
'https://www.googleapis.com/analytics/v3/data/ga', array( |
| 342 |
'ids' => 'ga:' . $options['wid'], |
| 343 |
'filters' => 'ga:pagePath=@' . $permalink, |
| 344 |
'max-results' => 1000, |
| 345 |
'metrics' => $options['metric'], |
| 346 |
'start-date' => $start_date, |
| 347 |
'end-date' => date( 'Y-m-d' ), |
| 348 |
), false |
| 349 |
); |
| 350 |
|
| 351 |
if ( isset( $json->totalsForAllResults->{$options['metric']} ) ) { |
| 352 |
|
| 353 |
$total_result = $json->totalsForAllResults->{$options['metric']}; |
| 354 |
|
| 355 |
// Set cached views. |
| 356 |
powerkit_set_cached_post_views( $post_id, $total_result ); |
| 357 |
|
| 358 |
$result = $total_result; |
| 359 |
} else { |
| 360 |
|
| 361 |
$value = $options['defaultval']; |
| 362 |
|
| 363 |
// If we have an old value let's put that instead of the default one in case of an error. |
| 364 |
$db_value = powerkit_get_nocached_post_views( $post_id ); |
| 365 |
|
| 366 |
if ( false !== $db_value && '' !== $db_value ) { |
| 367 |
$value = $db_value; |
| 368 |
} |
| 369 |
|
| 370 |
// Set cached views. |
| 371 |
powerkit_set_cached_post_views( $post_id, $value ); |
| 372 |
|
| 373 |
$result = $value; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
$result = apply_filters( 'powerkit_get_post_views', $result, $post_id, $format ); |
| 378 |
|
| 379 |
return ( $format ) ? number_format_i18n( (float) $result ) : $result; |
| 380 |
} |
| 381 |
|