| 1 |
<?php |
| 2 |
/** |
| 3 |
* The admin-specific functionality of the module. |
| 4 |
* |
| 5 |
* @link https://codesupply.co |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @package Powerkit |
| 9 |
* @subpackage Modules/Admin |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* The admin-specific functionality of the module. |
| 14 |
*/ |
| 15 |
class Powerkit_Post_Views_Admin extends Powerkit_Module_Admin { |
| 16 |
|
| 17 |
/** |
| 18 |
* Initialize |
| 19 |
*/ |
| 20 |
public function initialize() { |
| 21 |
|
| 22 |
add_filter( 'init', function() { |
| 23 |
$post_types = get_post_types( array( |
| 24 |
'publicly_queryable' => 1, |
| 25 |
'_builtin' => false, |
| 26 |
) ); |
| 27 |
|
| 28 |
// Merge post types. |
| 29 |
$post_types = array_merge( array( |
| 30 |
'post' => 'post', |
| 31 |
), $post_types ); |
| 32 |
|
| 33 |
foreach ( $post_types as $post_type ) { |
| 34 |
add_filter( "manage_{$post_type}_posts_columns", array( $this, 'column_views' ) ); |
| 35 |
add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'custom_column_views' ), 6, 2 ); |
| 36 |
} |
| 37 |
} ); |
| 38 |
|
| 39 |
add_action( 'admin_menu', array( $this, 'register_options_page' ) ); |
| 40 |
add_action( 'admin_head', array( $this, 'column_style' ) ); |
| 41 |
add_action( 'admin_notices', array( $this, 'admin_notice' ) ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Register admin page |
| 46 |
*/ |
| 47 |
public function register_options_page() { |
| 48 |
add_options_page( esc_html__( 'Post Views', 'powerkit' ), esc_html__( 'Post Views', 'powerkit' ), 'manage_options', powerkit_get_page_slug( $this->slug ), array( $this, 'build_options_page' ) ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Build admin page |
| 53 |
*/ |
| 54 |
public function build_options_page() { |
| 55 |
|
| 56 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 57 |
wp_die( esc_html__( 'You do not have sufficient rights to view this page.', 'powerkit' ) ); |
| 58 |
} |
| 59 |
|
| 60 |
$this->process_options_page(); |
| 61 |
|
| 62 |
$options = powerkit_post_views_options(); |
| 63 |
?> |
| 64 |
<div class="wrap pk-wrap"> |
| 65 |
<h1><?php esc_html_e( 'Post Views Settings', 'powerkit' ); ?></h1> |
| 66 |
|
| 67 |
<div class="pk-settings"> |
| 68 |
<form method="post" action="<?php echo esc_url( powerkit_get_page_url( $this->slug ) ); ?>"> |
| 69 |
<table class="form-table"> |
| 70 |
<tbody> |
| 71 |
<?php |
| 72 |
if ( empty( $options['token'] ) ) { |
| 73 |
|
| 74 |
if ( empty( $options['clientid'] ) || empty( $options['psecret'] ) ) { |
| 75 |
?> |
| 76 |
|
| 77 |
<p><?php echo wp_kses( __( 'In order to connect to your Google Analytics Account, you need to create a new project in the <a href="https://console.developers.google.com/project" target="_blank">Google API Console</a> and activate the Analytics API in "APIs & Services"', 'powerkit' ), 'post' ); ?></p> |
| 78 |
|
| 79 |
<ol> |
| 80 |
<li><?php echo wp_kses( __( 'Go to "APIs & Services".', 'powerkit' ), 'post' ); ?></li> |
| 81 |
<li><?php echo wp_kses( __( 'On the tab "OAuth consent screen" register the application, select "User Type" (External) and click "Create".', 'powerkit' ), 'post' ); ?></li> |
| 82 |
<li><?php echo wp_kses( __( 'On the tab "OAuth consent screen" enter "Application name" and add your domain.', 'powerkit' ), 'post' ); ?></li> |
| 83 |
<li><?php echo wp_kses( sprintf( __( 'Then, create an OAuth Client ID in "APIs & Services > Credentials" (Select "Web application", enter this URL %s for the "Authorized redirect URIs" field). ', 'powerkit' ), '<code>' . powerkit_get_page_url( $this->slug ) . '</code>' ), 'post' ); ?></li> |
| 84 |
<li><?php echo wp_kses( __( 'Enter your access below and connect to Google Analytics (if you have received a notice - "This app is not verified", then you can continue by clicking on "Advanced" link and follow the instructions).', 'powerkit' ), 'post' ); ?></li> |
| 85 |
</ol> |
| 86 |
|
| 87 |
<!-- Client ID --> |
| 88 |
<tr> |
| 89 |
<th scope="row"><label for="powerkit_post_views_clientid"><?php esc_html_e( 'Client ID', 'powerkit' ); ?></label></th> |
| 90 |
<td><input class="regular-text" id="powerkit_post_views_clientid" name="powerkit_post_views_clientid" type="text" value="<?php echo esc_attr( $options['clientid'] ); ?>"></td> |
| 91 |
</tr> |
| 92 |
<!-- Client secret --> |
| 93 |
<tr> |
| 94 |
<th scope="row"><label for="powerkit_post_views_psecret"><?php esc_html_e( 'Client secret', 'powerkit' ); ?></label></th> |
| 95 |
<td><input class="regular-text" id="powerkit_post_views_psecret" name="powerkit_post_views_psecret" type="text" value="<?php echo esc_attr( $options['psecret'] ); ?>"></td> |
| 96 |
</tr> |
| 97 |
<?php |
| 98 |
} else { |
| 99 |
$googleapis_auth = add_query_arg( array( |
| 100 |
'client_id' => $options['clientid'], |
| 101 |
'redirect_uri' => powerkit_get_page_url( $this->slug ), |
| 102 |
'scope' => 'https://www.googleapis.com/auth/analytics.readonly+https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile&response_type=code&access_type=offline&state=init&approval_prompt=force', |
| 103 |
), 'https://accounts.google.com/o/oauth2/auth' ); |
| 104 |
?> |
| 105 |
<tr> |
| 106 |
<td colspan="2"> |
| 107 |
<p><a class="button" href="<?php echo esc_url( $googleapis_auth ); ?>"><?php esc_html_e( 'Connect to Google Analytics', 'powerkit' ); ?> »</a></p> |
| 108 |
|
| 109 |
<p><a class="button" href="<?php echo esc_url( powerkit_get_page_url( $this->slug . '&state=clear-api' ) ); ?>"><?php esc_html_e( 'Clear the API keys', 'powerkit' ); ?> »</a></p> |
| 110 |
</td> |
| 111 |
</tr> |
| 112 |
|
| 113 |
<tr><td colspan="2"><hr></td></tr> |
| 114 |
<?php |
| 115 |
} |
| 116 |
} else { |
| 117 |
?> |
| 118 |
<p><?php esc_html_e( 'You are connected to Google Analytics with the e-mail address', 'powerkit' ); ?> <?php echo esc_html( $options['gmail'] ); ?></p> |
| 119 |
|
| 120 |
<p><a href="<?php echo esc_url( powerkit_get_page_url( $this->slug . '&state=disconnect' ) ); ?>"><?php esc_html_e( 'Disconnect from Google Analytics', 'powerkit' ); ?> »</a></p> |
| 121 |
|
| 122 |
<p><a href="<?php echo esc_url( powerkit_get_page_url( $this->slug . '&state=reset-cache' ) ); ?>"><?php esc_html_e( 'Empty page views cache', 'powerkit' ); ?> »</a></p> |
| 123 |
|
| 124 |
<!-- Use this website to retrieve pageviews numbers --> |
| 125 |
<tr> |
| 126 |
<th scope="row"><label for="powerkit_post_views_wid"><?php esc_html_e( 'Use this website to retrieve pageviews numbers', 'powerkit' ); ?></label></th> |
| 127 |
<td> |
| 128 |
<select id="powerkit_post_views_wid" name="powerkit_post_views_wid"> |
| 129 |
<option value="" <?php selected( empty( $options['wid'] ), true ); ?>><?php esc_html_e( 'None', 'powerkit' ); ?></option> |
| 130 |
|
| 131 |
<?php |
| 132 |
$wjson = powerkit_post_views_api_call( 'https://www.googleapis.com/analytics/v3/management/accounts/~all/webproperties/~all/profiles', array() ); |
| 133 |
|
| 134 |
if ( is_array( $wjson->items ) ) { |
| 135 |
|
| 136 |
foreach ( $wjson->items as $item ) { |
| 137 |
|
| 138 |
if ( 'WEB' !== $item->type ) { |
| 139 |
continue; |
| 140 |
} |
| 141 |
?> |
| 142 |
<option value="<?php echo esc_attr( $item->id ); ?>" <?php selected( $options['wid'], $item->id ); ?>> |
| 143 |
<?php echo esc_html( $item->name .' (' . $item->websiteUrl . ')' ); ?> |
| 144 |
</option> |
| 145 |
<?php |
| 146 |
} |
| 147 |
} |
| 148 |
?> |
| 149 |
</select> |
| 150 |
</td> |
| 151 |
</tr> |
| 152 |
<!-- Metrics to retrieve --> |
| 153 |
<tr> |
| 154 |
<th scope="row"><label for="powerkit_post_views_metric"><?php esc_html_e( 'Metrics to retrieve', 'powerkit' ); ?></label></th> |
| 155 |
<td> |
| 156 |
<select id="powerkit_post_views_metric" name="powerkit_post_views_metric"> |
| 157 |
<option value="ga:pageviews" <?php selected( $options['metric'], 'ga:pageviews' ); ?>> |
| 158 |
<?php esc_html_e( 'Page views', 'powerkit' ); ?> |
| 159 |
</option> |
| 160 |
<option value="ga:uniquePageviews" <?php selected( $options['metric'], 'ga:uniquePageviews' ); ?>> |
| 161 |
<?php esc_html_e( 'Unique page views', 'powerkit' ); ?> |
| 162 |
</option> |
| 163 |
</select> |
| 164 |
</td> |
| 165 |
</tr> |
| 166 |
<!-- Start date for the analytics --> |
| 167 |
<tr> |
| 168 |
<th scope="row"><label for="powerkit_post_views_startdate"><?php esc_html_e( 'Start date for the analytics', 'powerkit' ); ?></label></th> |
| 169 |
<td><input class="regular-text" id="powerkit_post_views_startdate" name="powerkit_post_views_startdate" type="date" value="<?php echo esc_attr( $options['startdate'] ); ?>"></td> |
| 170 |
</tr> |
| 171 |
<!-- Default value when a count cannot be fetched --> |
| 172 |
<tr> |
| 173 |
<th scope="row"><label for="powerkit_post_views_defaultval"><?php esc_html_e( 'Default value when a count cannot be fetched', 'powerkit' ); ?></label></th> |
| 174 |
<td><input class="regular-text" id="powerkit_post_views_defaultval" name="powerkit_post_views_defaultval" type="text" value="<?php echo esc_attr( $options['defaultval'] ); ?>"></td> |
| 175 |
</tr> |
| 176 |
<!-- Display the Views column in Posts list --> |
| 177 |
<tr> |
| 178 |
<th scope="row"><label for="powerkit_post_views_column"><?php esc_html_e( 'Display the Views column in Posts list', 'powerkit' ); ?></label></th> |
| 179 |
<td><input class="regular-text" id="powerkit_post_views_column" name="powerkit_post_views_column" type="checkbox" value="true" <?php checked( (bool) $options['column'] ); ?>></td> |
| 180 |
</tr> |
| 181 |
<!-- Search pageviews slugs with trailing slash --> |
| 182 |
<tr> |
| 183 |
<th scope="row"><label for="powerkit_post_views_trailing"><?php esc_html_e( 'Search pageviews slugs with trailing slash', 'powerkit' ); ?></label></th> |
| 184 |
<td><input class="regular-text" id="powerkit_post_views_trailing" name="powerkit_post_views_trailing" type="checkbox" value="true" <?php checked( (bool) $options['trailing'] ); ?>></td> |
| 185 |
</tr> |
| 186 |
<?php |
| 187 |
} |
| 188 |
?> |
| 189 |
</tbody> |
| 190 |
</table> |
| 191 |
|
| 192 |
<?php wp_nonce_field(); ?> |
| 193 |
|
| 194 |
<p class="submit"><input class="button button-primary" name="save_settings" type="submit" value="<?php esc_html_e( 'Save changes', 'powerkit' ); ?>" /></p> |
| 195 |
</form> |
| 196 |
</div> |
| 197 |
</div> |
| 198 |
<?php |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Process options page |
| 203 |
*/ |
| 204 |
public function process_options_page() { |
| 205 |
wp_verify_nonce( null ); |
| 206 |
|
| 207 |
global $wpdb; |
| 208 |
|
| 209 |
$options = powerkit_post_views_options(); |
| 210 |
|
| 211 |
/** Save settings */ |
| 212 |
/** ------------------- */ |
| 213 |
|
| 214 |
if ( isset( $_POST['save_settings'] ) ) { // Input var ok; sanitization ok. |
| 215 |
|
| 216 |
if ( isset( $_POST['powerkit_post_views_clientid'] ) ) { // Input var ok; sanitization ok. |
| 217 |
$options['clientid'] = sanitize_text_field( $_POST['powerkit_post_views_clientid'] ); // Input var ok; sanitization ok. |
| 218 |
} |
| 219 |
|
| 220 |
if ( isset( $_POST['powerkit_post_views_psecret'] ) ) { // Input var ok; sanitization ok. |
| 221 |
$options['psecret'] = sanitize_text_field( $_POST['powerkit_post_views_psecret'] ); // Input var ok; sanitization ok. |
| 222 |
} |
| 223 |
|
| 224 |
if ( isset( $_POST['powerkit_post_views_wid'] ) ) { // Input var ok; sanitization ok. |
| 225 |
$options['wid'] = sanitize_text_field( $_POST['powerkit_post_views_wid'] ); // Input var ok; sanitization ok. |
| 226 |
} |
| 227 |
|
| 228 |
if ( isset( $_POST['powerkit_post_views_startdate'] ) ) { // Input var ok; sanitization ok. |
| 229 |
$options['startdate'] = sanitize_text_field( $_POST['powerkit_post_views_startdate'] ); // Input var ok; sanitization ok. |
| 230 |
} |
| 231 |
|
| 232 |
if ( isset( $_POST['powerkit_post_views_defaultval'] ) ) { // Input var ok; sanitization ok. |
| 233 |
$options['defaultval'] = sanitize_text_field( $_POST['powerkit_post_views_defaultval'] ); // Input var ok; sanitization ok. |
| 234 |
} |
| 235 |
|
| 236 |
if ( isset( $_POST['powerkit_post_views_metric'] ) ) { // Input var ok; sanitization ok. |
| 237 |
$options['metric'] = sanitize_text_field( $_POST['powerkit_post_views_metric'] ); // Input var ok; sanitization ok. |
| 238 |
} |
| 239 |
|
| 240 |
$options['column'] = ( isset( $_POST['powerkit_post_views_column'] ) ); // Input var ok; sanitization ok. |
| 241 |
$options['trailing'] = ( isset( $_POST['powerkit_post_views_trailing'] ) ); // Input var ok; sanitization ok. |
| 242 |
|
| 243 |
update_option( 'powerkit_post_views_options', $options ); |
| 244 |
|
| 245 |
printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Settings saved.', 'powerkit' ) ); |
| 246 |
} |
| 247 |
|
| 248 |
/** Actions */ |
| 249 |
/** ------------------- */ |
| 250 |
|
| 251 |
if ( isset( $_GET['state'] ) && 'init' === $_GET['state'] ) { // Input var ok; sanitization ok. |
| 252 |
|
| 253 |
$request = new WP_Http(); |
| 254 |
|
| 255 |
$result = $request->request( 'https://accounts.google.com/o/oauth2/token', array( |
| 256 |
'method' => 'POST', |
| 257 |
'body' => array( |
| 258 |
'code' => sanitize_text_field( isset( $_GET['code'] ) ? $_GET['code'] : null ), // Input var ok; sanitization ok. |
| 259 |
'client_id' => $options['clientid'], |
| 260 |
'client_secret' => $options['psecret'], |
| 261 |
'redirect_uri' => powerkit_get_page_url( $this->slug ), |
| 262 |
'grant_type' => 'authorization_code', |
| 263 |
), |
| 264 |
) ); |
| 265 |
|
| 266 |
if ( ! is_array( $result ) || ! isset( $result['response']['code'] ) && 200 !== $result['response']['code'] ) { |
| 267 |
?> |
| 268 |
<div id="message" class="error"> |
| 269 |
<p> |
| 270 |
<?php esc_html_e( 'There was something wrong with Google!', 'powerkit' ); ?> |
| 271 |
</p> |
| 272 |
</div> |
| 273 |
<?php |
| 274 |
} |
| 275 |
|
| 276 |
$tjson = json_decode( $result['body'] ); |
| 277 |
|
| 278 |
$options['token'] = $tjson->access_token; |
| 279 |
$options['token_refresh'] = $tjson->refresh_token; |
| 280 |
$options['expires'] = time() + $tjson->expires_in; |
| 281 |
|
| 282 |
update_option( 'powerkit_post_views_options', $options ); |
| 283 |
|
| 284 |
$ijson = powerkit_post_views_api_call( 'https://www.googleapis.com/oauth2/v1/userinfo', array() ); |
| 285 |
|
| 286 |
$options['gid'] = $ijson->id; |
| 287 |
$options['gmail'] = $ijson->email; |
| 288 |
|
| 289 |
update_option( 'powerkit_post_views_options', $options ); |
| 290 |
|
| 291 |
if ( ! empty( $options['token'] ) && ! empty( $options['gmail'] ) ) { |
| 292 |
?> |
| 293 |
<script>window.location = '<?php echo esc_url( powerkit_get_page_url( $this->slug ) ); ?> ';</script> |
| 294 |
<?php |
| 295 |
exit; |
| 296 |
} |
| 297 |
} elseif ( isset( $_GET['state'] ) && 'disconnect' === $_GET['state'] ) { // Input var ok; sanitization ok. |
| 298 |
|
| 299 |
$options['gid'] = null; |
| 300 |
$options['gmail'] = null; |
| 301 |
$options['token'] = null; |
| 302 |
$options['token_refresh'] = null; |
| 303 |
$options['expires'] = null; |
| 304 |
$options['defaultval'] = 0; |
| 305 |
|
| 306 |
update_option( 'powerkit_post_views_options', $options ); |
| 307 |
|
| 308 |
} elseif ( isset( $_GET['state'] ) && 'clear-api' === $_GET['state'] ) { // Input var ok; sanitization ok. |
| 309 |
|
| 310 |
$options['clientid'] = null; |
| 311 |
$options['psecret'] = null; |
| 312 |
|
| 313 |
update_option( 'powerkit_post_views_options', $options ); |
| 314 |
|
| 315 |
printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'API Keys removed.', 'powerkit' ) ); |
| 316 |
|
| 317 |
} elseif ( isset( $_GET['state'] ) && 'refresh-token' === $_GET['state'] ) { // Input var ok; sanitization ok. |
| 318 |
|
| 319 |
powerkit_post_views_refresh_token(); |
| 320 |
|
| 321 |
printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Token refreshed successfully.', 'powerkit' ) ); |
| 322 |
|
| 323 |
} elseif ( isset( $_GET['state'] ) && 'reset-cache' === $_GET['state'] ) { // Input var ok; sanitization ok. |
| 324 |
|
| 325 |
$wpdb->query( "UPDATE {$wpdb->prefix}pk_post_views SET period = 0" ); // db call ok; no-cache ok. |
| 326 |
|
| 327 |
printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Cache flushed successfully.', 'powerkit' ) ); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Filters the columns displayed in the Posts list table. |
| 333 |
* |
| 334 |
* @param array $post_columns An associative array of column headings. |
| 335 |
*/ |
| 336 |
public function column_views( $post_columns ) { |
| 337 |
|
| 338 |
$options = powerkit_post_views_options(); |
| 339 |
|
| 340 |
if ( ! empty( $options['token'] ) && $options['column'] ) { |
| 341 |
|
| 342 |
$post_columns['pk_post_views'] = esc_html__( 'Views', 'powerkit' ); |
| 343 |
|
| 344 |
} |
| 345 |
|
| 346 |
return $post_columns; |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Fires in each custom column in the Posts list table. |
| 351 |
* |
| 352 |
* @param string $column_name The name of the column to display. |
| 353 |
* @param int $post_id The current post ID. |
| 354 |
*/ |
| 355 |
public function custom_column_views( $column_name, $post_id ) { |
| 356 |
|
| 357 |
if ( 'pk_post_views' === $column_name ) { |
| 358 |
|
| 359 |
echo powerkit_get_post_views( get_the_ID(), true, true ); // XSS. |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Add column style. |
| 365 |
*/ |
| 366 |
public function column_style() { |
| 367 |
echo '<style>.column-pk_post_views { width: 120px; }</style>'; |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Output notice. |
| 372 |
*/ |
| 373 |
public function admin_notice() { |
| 374 |
|
| 375 |
$options = powerkit_post_views_options(); |
| 376 |
|
| 377 |
if ( current_user_can( 'manage_options' ) ) { |
| 378 |
|
| 379 |
if ( isset( $options['token'] ) && empty( $options['token'] ) ) { |
| 380 |
|
| 381 |
echo '<div class="error"><p>' . esc_html__( 'Google Post Views Warning: You have to (re)connect the plugin to your Google account.' ) . '<br><a href="' . esc_url( powerkit_get_page_url( $this->slug ) ) . '">' . esc_html__( 'Update settings', 'powerkit' ) . ' →</a></p></div>'; |
| 382 |
|
| 383 |
} elseif ( isset( $options['error'] ) && ! empty( $options['error'] ) ) { |
| 384 |
|
| 385 |
echo '<div class="error"><p>' . esc_html__( 'Google Post Views Error: ', 'powerkit' ) . wp_kses( $options['error'], 'post' ) . '<br><a href="' . esc_url( powerkit_get_page_url( $this->slug ) ) . '">' . esc_html__( 'Update settings', 'powerkit' ) . ' →</a></p></div>'; |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
|