PluginProbe
Powerkit – Supercharge your WordPress Site / 2.5.7
Powerkit – Supercharge your WordPress Site v2.5.7
3.1.1 3.1.0 3.0.9 3.0.8 trunk 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 All 87 releases
powerkit / modules / post-views / admin / class-powerkit-post-views-admin.php

class-powerkit-post-views-admin.php in Powerkit – Supercharge your WordPress Site 2.5.7, at modules/post-views/admin/class-powerkit-post-views-admin.php

395 lines 16.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 &amp; Services"', 'powerkit' ), 'post' ); ?></p>
78
79 <ol>
80 <li><?php echo wp_kses( __( 'Go to "APIs &amp; 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 &amp; 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' ); ?> &raquo;</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' ); ?> &raquo;</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' ); ?> &raquo;</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' ); ?> &raquo;</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 powerkit_uuid_hash();
206
207 // Check wpnonce.
208 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'] ) ) { // Input var ok; sanitization ok.
209 return;
210 }
211
212 global $wpdb;
213
214 $options = powerkit_post_views_options();
215
216 /** Save settings */
217 /** ------------------- */
218
219 if ( isset( $_REQUEST['save_settings'] ) ) { // Input var ok; sanitization ok.
220
221 if ( isset( $_REQUEST['powerkit_post_views_clientid'] ) ) { // Input var ok; sanitization ok.
222 $options['clientid'] = sanitize_text_field( $_REQUEST['powerkit_post_views_clientid'] ); // Input var ok; sanitization ok.
223 }
224
225 if ( isset( $_REQUEST['powerkit_post_views_psecret'] ) ) { // Input var ok; sanitization ok.
226 $options['psecret'] = sanitize_text_field( $_REQUEST['powerkit_post_views_psecret'] ); // Input var ok; sanitization ok.
227 }
228
229 if ( isset( $_REQUEST['powerkit_post_views_wid'] ) ) { // Input var ok; sanitization ok.
230 $options['wid'] = sanitize_text_field( $_REQUEST['powerkit_post_views_wid'] ); // Input var ok; sanitization ok.
231 }
232
233 if ( isset( $_REQUEST['powerkit_post_views_startdate'] ) ) { // Input var ok; sanitization ok.
234 $options['startdate'] = sanitize_text_field( $_REQUEST['powerkit_post_views_startdate'] ); // Input var ok; sanitization ok.
235 }
236
237 if ( isset( $_REQUEST['powerkit_post_views_defaultval'] ) ) { // Input var ok; sanitization ok.
238 $options['defaultval'] = sanitize_text_field( $_REQUEST['powerkit_post_views_defaultval'] ); // Input var ok; sanitization ok.
239 }
240
241 if ( isset( $_REQUEST['powerkit_post_views_metric'] ) ) { // Input var ok; sanitization ok.
242 $options['metric'] = sanitize_text_field( $_REQUEST['powerkit_post_views_metric'] ); // Input var ok; sanitization ok.
243 }
244
245 $options['column'] = ( isset( $_REQUEST['powerkit_post_views_column'] ) ); // Input var ok; sanitization ok.
246 $options['trailing'] = ( isset( $_REQUEST['powerkit_post_views_trailing'] ) ); // Input var ok; sanitization ok.
247
248 update_option( 'powerkit_post_views_options', $options );
249
250 printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Settings saved.', 'powerkit' ) );
251 }
252
253 /** Actions */
254 /** ------------------- */
255
256 if ( isset( $_GET['state'] ) && 'init' === $_GET['state'] ) { // Input var ok; sanitization ok.
257
258 $request = new WP_Http();
259
260 $result = $request->request( 'https://accounts.google.com/o/oauth2/token', array(
261 'method' => 'POST',
262 'body' => array(
263 'code' => sanitize_text_field( isset( $_GET['code'] ) ? $_GET['code'] : null ), // Input var ok; sanitization ok.
264 'client_id' => $options['clientid'],
265 'client_secret' => $options['psecret'],
266 'redirect_uri' => powerkit_get_page_url( $this->slug ),
267 'grant_type' => 'authorization_code',
268 ),
269 ) );
270
271 if ( ! is_array( $result ) || ! isset( $result['response']['code'] ) && 200 !== $result['response']['code'] ) {
272 ?>
273 <div id="message" class="error">
274 <p>
275 <?php esc_html_e( 'There was something wrong with Google!', 'powerkit' ); ?>
276 </p>
277 </div>
278 <?php
279 }
280
281 $tjson = json_decode( $result['body'] );
282
283 $options['token'] = $tjson->access_token;
284 $options['token_refresh'] = $tjson->refresh_token;
285 $options['expires'] = time() + $tjson->expires_in;
286
287 update_option( 'powerkit_post_views_options', $options );
288
289 $ijson = powerkit_post_views_api_call( 'https://www.googleapis.com/oauth2/v1/userinfo', array() );
290
291 $options['gid'] = $ijson->id;
292 $options['gmail'] = $ijson->email;
293
294 update_option( 'powerkit_post_views_options', $options );
295
296 if ( ! empty( $options['token'] ) && ! empty( $options['gmail'] ) ) {
297 ?>
298 <script>window.location = '<?php echo esc_url( powerkit_get_page_url( $this->slug ) ); ?> ';</script>
299 <?php
300 exit;
301 }
302 } elseif ( isset( $_GET['state'] ) && 'disconnect' === $_GET['state'] ) { // Input var ok; sanitization ok.
303
304 $options['gid'] = null;
305 $options['gmail'] = null;
306 $options['token'] = null;
307 $options['token_refresh'] = null;
308 $options['expires'] = null;
309 $options['defaultval'] = 0;
310
311 update_option( 'powerkit_post_views_options', $options );
312
313 } elseif ( isset( $_GET['state'] ) && 'clear-api' === $_GET['state'] ) { // Input var ok; sanitization ok.
314
315 $options['clientid'] = null;
316 $options['psecret'] = null;
317
318 update_option( 'powerkit_post_views_options', $options );
319
320 printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'API Keys removed.', 'powerkit' ) );
321
322 } elseif ( isset( $_GET['state'] ) && 'refresh-token' === $_GET['state'] ) { // Input var ok; sanitization ok.
323
324 powerkit_post_views_refresh_token();
325
326 printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Token refreshed successfully.', 'powerkit' ) );
327
328 } elseif ( isset( $_GET['state'] ) && 'reset-cache' === $_GET['state'] ) { // Input var ok; sanitization ok.
329
330 $wpdb->query( "UPDATE {$wpdb->prefix}pk_post_views SET period = 0" ); // db call ok; no-cache ok.
331
332 printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Cache flushed successfully.', 'powerkit' ) );
333 }
334 }
335
336 /**
337 * Filters the columns displayed in the Posts list table.
338 *
339 * @param array $post_columns An associative array of column headings.
340 */
341 public function column_views( $post_columns ) {
342
343 $options = powerkit_post_views_options();
344
345 if ( ! empty( $options['token'] ) && $options['column'] ) {
346
347 $post_columns['pk_post_views'] = esc_html__( 'Views', 'powerkit' );
348
349 }
350
351 return $post_columns;
352 }
353
354 /**
355 * Fires in each custom column in the Posts list table.
356 *
357 * @param string $column_name The name of the column to display.
358 * @param int $post_id The current post ID.
359 */
360 public function custom_column_views( $column_name, $post_id ) {
361
362 if ( 'pk_post_views' === $column_name ) {
363
364 echo powerkit_get_post_views( $post_id, true ); // XSS.
365 }
366 }
367
368 /**
369 * Add column style.
370 */
371 public function column_style() {
372 echo '<style>.column-pk_post_views { width: 120px; }</style>';
373 }
374
375 /**
376 * Output notice.
377 */
378 public function admin_notice() {
379
380 $options = powerkit_post_views_options();
381
382 if ( current_user_can( 'manage_options' ) ) {
383
384 if ( isset( $options['token'] ) && empty( $options['token'] ) ) {
385
386 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' ) . ' &rarr;</a></p></div>';
387
388 } elseif ( isset( $options['error'] ) && ! empty( $options['error'] ) ) {
389
390 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' ) . ' &rarr;</a></p></div>';
391 }
392 }
393 }
394 }
395