PluginProbe
Powerkit – Supercharge your WordPress Site / 2.2.9
Powerkit – Supercharge your WordPress Site v2.2.9
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 / extensions / connect / class-powerkit-connect.php

class-powerkit-connect.php in Powerkit – Supercharge your WordPress Site 2.2.9, at extensions/connect/class-powerkit-connect.php

1,069 lines 35.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Connect
4 *
5 * @package Powerkit
6 * @subpackage Extensions
7 */
8
9 /**
10 * Init module
11 */
12 class Powerkit_Connect extends Powerkit_Module {
13
14 /**
15 * Register module
16 */
17 public function register() {
18 $this->name = 'connect';
19 $this->slug = 'connect';
20 $this->type = 'extension';
21 $this->category = 'basic';
22 $this->public = false;
23 $this->enabled = true;
24 }
25
26 /**
27 * Initialize module
28 */
29 public function initialize() {
30 add_action( 'powerkit_plugin_activation', array( $this, 'activation' ) );
31 add_action( 'powerkit_plugin_deactivation', array( $this, 'deactivation' ) );
32 add_action( 'event_access_token_refresh', array( $this, 'cron_access_token_refresh' ) );
33 add_action( 'admin_menu', array( $this, 'register_options_page' ) );
34 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
35 add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
36 add_action( 'wp_ajax_powerkit_reset_cache', array( $this, 'ajax_reset_cache' ) );
37 add_action( 'wp_ajax_nopriv_powerkit_reset_cache', array( $this, 'ajax_reset_cache' ) );
38 }
39
40 /**
41 * Plugin activation
42 *
43 * @param bool $networkwide The networkwide.
44 */
45 public function activation( $networkwide ) {
46 if ( ! wp_next_scheduled( 'event_access_token_refresh' ) ) {
47 wp_schedule_event( time(), 'daily', 'event_access_token_refresh' );
48 }
49 }
50
51 /**
52 * Plugin activation
53 *
54 * @param bool $networkwide The networkwide.
55 */
56 public function deactivation( $networkwide ) {
57 wp_clear_scheduled_hook( 'event_access_token_refresh' );
58 }
59
60 /**
61 * Register admin page
62 *
63 * @since 1.0.0
64 */
65 public function register_options_page() {
66 add_options_page( esc_html__( 'Connect', 'powerkit' ), esc_html__( 'Connect', 'powerkit' ), 'manage_options', powerkit_get_page_slug( $this->slug ), array( $this, 'build_options_page' ) );
67 }
68
69 /**
70 * Build admin page
71 *
72 * @since 1.0.0
73 */
74 public function build_options_page() {
75 if ( ! current_user_can( 'manage_options' ) ) {
76 wp_die( esc_html__( 'You do not have sufficient rights to view this page.', 'powerkit' ) );
77 }
78 $this->logout_account();
79 $this->save_options_page();
80 $this->user_reset_cache();
81 ?>
82
83 <div class="wrap">
84 <h1><?php esc_html_e( 'Connect', 'powerkit' ); ?></h1>
85
86 <div class="settings">
87 <?php $connect_list = apply_filters( 'powerkit_register_connect_list', array() ); ?>
88
89 <?php if ( $connect_list ) : ?>
90 <div class="tabs">
91 <?php
92 $connect_list = array_values( $connect_list );
93
94 $tab = isset( $_GET['tab'] ) ? esc_attr( $_GET['tab'] ) : $connect_list[0]['id']; // Input var ok; sanitization ok.
95 ?>
96
97 <nav class="nav-tab-wrapper woo-nav-tab-wrapper">
98 <?php
99 foreach ( $connect_list as $item ) {
100 $class = ( $item['id'] === $tab ) ? 'nav-tab-active' : ''; // Input var ok.
101 printf(
102 '<a class="nav-tab %4$s" href="%1$s&tab=%2$s">%3$s</a>',
103 esc_url( powerkit_get_page_url( $this->slug ) ),
104 esc_attr( $item['id'] ),
105 esc_html( $item['name'] ),
106 esc_attr( $class )
107 );
108 }
109 ?>
110 </nav>
111
112 <?php
113 foreach ( $connect_list as $item ) {
114 // Instagram tab.
115 if ( 'instagram' === $item['id'] && $item['id'] === $tab ) { // Input var ok.
116 ?>
117 <div id="tab-<?php echo esc_attr( $item['id'] ); ?>" class="tab-wrap">
118 <?php $this->instagram_custom_tab( $tab ); ?>
119 </div>
120 <?php
121 // Facebook tab.
122 } elseif ( 'facebook' === $item['id'] && $item['id'] === $tab ) { // Input var ok.
123 ?>
124 <div id="tab-<?php echo esc_attr( $item['id'] ); ?>" class="tab-wrap">
125 <?php $this->facebook_custom_tab( $tab ); ?>
126 </div>
127 <?php
128 // Twitter tab.
129 } elseif ( 'twitter' === $item['id'] && $item['id'] === $tab ) { // Input var ok.
130 ?>
131 <div id="tab-<?php echo esc_attr( $item['id'] ); ?>" class="tab-wrap">
132 <?php $this->twitter_custom_tab( $tab ); ?>
133 </div>
134 <?php
135 } elseif ( $item['id'] === $tab ) {
136 ?>
137 <form class="basic" method="post">
138 <div id="tab-<?php echo esc_attr( $item['id'] ); ?>" class="tab-wrap">
139 <!-- Render Fields -->
140 <?php
141 if ( isset( $item['fields'] ) && $item['fields'] ) {
142 ?>
143 <table class="form-table">
144 <tbody>
145 <?php
146 foreach ( $item['fields'] as $field ) {
147 ?>
148 <tr>
149 <th scope="row"><label class="title" for="<?php echo esc_attr( $field['key'] ); ?>"><?php echo esc_html( $field['caption'] ); ?></label></th>
150 <td>
151 <input class="regular-text" id="<?php echo esc_attr( $field['key'] ); ?>" name="<?php echo esc_attr( $field['key'] ); ?>" type="text" value="<?php echo esc_attr( powerkit_connect( $field['key'] ) ); ?>" />
152
153 <?php if ( isset( $field['instruction'] ) ) { ?>
154 <p><?php echo wp_kses_post( $field['instruction'] ); ?></p>
155 <?php } ?>
156 </td>
157 </tr>
158 <?php
159 }
160 ?>
161 </tbody>
162 </table>
163 <?php
164 }
165 ?>
166 </div>
167
168 <?php wp_nonce_field(); ?>
169
170 <p class="submit">
171 <input class="button button-primary" name="save_settings" type="submit" value="<?php esc_html_e( 'Save changes', 'powerkit' ); ?>" />
172 </p>
173 </form>
174 <?php
175 }
176 }
177 ?>
178 </div>
179
180 <hr>
181
182 <form method="post" class="form-reset">
183 <?php wp_nonce_field(); ?>
184
185 <p class="submit"><input class="button" name="powerkit_reset_cache" type="submit" value="<?php esc_html_e( 'Reset cache', 'powerkit' ); ?>" /></p>
186 </form>
187 <?php else : ?>
188 <p class="submit">
189 <?php esc_html_e( 'The list of social network settings is empty!!!', 'powerkit' ); ?>
190 </p>
191 <?php endif; ?>
192 </div>
193 </div>
194
195 <style>
196 .tab-badge-success {
197 display: inline-block;
198 border: 1px solid #46b450;
199 padding: 0.5rem 0.75rem;
200 color: #32373c;
201 font-weight: 600;
202 border-radius: 5px;
203 margin-top: 1rem;
204 }
205 </style>
206 <?php
207 }
208
209 /**
210 * Get API data
211 *
212 * @param mixed $type The type of data.
213 */
214 public function api_get_data( $type ) {
215
216 wp_verify_nonce( null );
217
218 if ( ! $type ) {
219 return;
220 }
221
222 // Set types.
223 $types = array();
224
225 if ( is_string( $type ) ) {
226 $types[] = $type;
227 } elseif ( is_array( $type ) ) {
228 $types = $type;
229 }
230
231 // Get data.
232 if ( isset( $_REQUEST['api_data'] ) ) { // Input var ok; sanitization ok.
233
234 $data = json_decode( base64_decode( $_REQUEST['api_data'] ), true );
235
236 if ( ! is_array( $data ) ) {
237 return;
238 }
239
240 if ( ! isset( $data['type'] ) || ! isset( $data['marker'] ) ) {
241 return;
242 }
243
244 // Check type.
245 if ( ! in_array( $data['type'], $types, true ) ) {
246 return;
247 }
248
249 // Check marker.
250 if ( get_transient( $data['marker'] ) ) {
251 return;
252 }
253
254 // Add marker to DB.
255 set_transient( $data['marker'], true, HOUR_IN_SECONDS );
256
257 return $data;
258 }
259 }
260
261 /**
262 * Get API message
263 *
264 * @param mixed $status The status of data.
265 */
266 public function api_get_message( $status ) {
267
268 $message = esc_html__( 'Something went wrong, please contact the plugin administrator.', 'powerkit' );
269
270 // Check type status.
271 if ( ! is_numeric( $status ) && is_string( $status ) ) {
272 if ( empty( $status ) ) {
273 $message = esc_html__( 'Failed to get status, please contact the plugin administrator.', 'powerkit' );
274 }
275
276 // If the status is a message.
277 $message = $status;
278 }
279
280 // Common.
281 if ( 200 === $status ) {
282 $message = esc_html__( 'Successfully connected.', 'powerkit' );
283 }
284 if ( 201 === $status ) {
285 $message = esc_html__( 'Successfully connected, please select a profile!', 'powerkit' );
286 }
287 if ( 401 === $status ) {
288 $message = esc_html__( 'Access code not available.', 'powerkit' );
289 }
290
291 // Instagram.
292 if ( 4101 === $status ) {
293 $message = esc_html__( "Can't Get Short-Lived Token.", 'powerkit' );
294 }
295 if ( 4102 === $status ) {
296 $message = esc_html__( "Can't Get Long-Lived Token.", 'powerkit' );
297 }
298
299 // Facebook.
300 if ( 4201 === $status ) {
301 $message = esc_html__( "Can't Get Short-Lived Token.", 'powerkit' );
302 }
303 if ( 4202 === $status ) {
304 $message = esc_html__( "Can't Get Long-Lived Token.", 'powerkit' );
305 }
306 if ( 4203 === $status || 4204 === $status ) {
307 $message = esc_html__( "Couldn't find Business Profile.", 'powerkit' );
308 }
309 if ( 4205 === $status || 4206 === $status ) {
310 $message = sprintf( __( 'No Facebook pages linked to your Instagram Business account were found. Please link a public Facebook page to your Instagram Business account and connect again. See <a target="_blank" href="%s">this article</a> for details.', 'powerkit' ), 'https://help.instagram.com/399237934150902' );
311 }
312
313 // Twitter.
314 if ( 4301 === $status ) {
315 $message = esc_html__( "Can't Get Request Token.", 'powerkit' );
316 }
317 if ( 4302 === $status ) {
318 $message = esc_html__( 'Something went wrong, try again or contact the plugin administrator.', 'powerkit' );
319 }
320
321 return $message;
322 }
323
324 /**
325 * Instagram custom tab
326 *
327 * @param string $tab The name of tab.
328 */
329 public function instagram_custom_tab( $tab ) {
330
331 $api_data = $this->api_get_data( array( 'instagram-connect', 'facebook-connect' ) );
332
333 // Update connect settigs.
334 if ( is_array( $api_data ) && ! empty( $api_data ) ) {
335 $ig_status = isset( $api_data['status'] ) && $api_data['status'] ? $api_data['status'] : null;
336 $ig_access_token = isset( $api_data['access_token'] ) && $api_data['access_token'] ? $api_data['access_token'] : null;
337 $ig_user_id = isset( $api_data['user_id'] ) && $api_data['user_id'] ? $api_data['user_id'] : null;
338 $ig_username = isset( $api_data['username'] ) && $api_data['username'] ? $api_data['username'] : null;
339
340 if ( ( 200 === $ig_status ) && $ig_access_token && $ig_user_id ) {
341 update_option( 'powerkit_connect_instagram_app_type', 'instagram-connect' === $api_data['type'] ? 'personal' : 'business' );
342 update_option( 'powerkit_connect_instagram_app_access_token', $ig_access_token );
343 update_option( 'powerkit_connect_instagram_app_user_id', $ig_user_id );
344 update_option( 'powerkit_connect_instagram_app_username', $ig_username ? $ig_username : $ig_user_id );
345 update_option( 'powerkit_connect_instagram_app_refresh_time', time() + ( DAY_IN_SECONDS * 30 ) );
346
347 $this->location_reset_cache( 'instagram' );
348 }
349 }
350
351 // Notice.
352 if ( isset( $ig_status ) && ( 200 === $ig_status || 201 === $ig_status ) ) {
353 ?>
354 <div class="notice notice-success is-dismissible">
355 <p><?php echo wp_kses_post( wp_unslash( $this->api_get_message( $ig_status ) ) ); ?></p>
356 </div>
357 <?php
358 } elseif ( isset( $ig_status ) && $ig_status ) {
359 ?>
360 <div class="notice notice-error is-dismissible">
361 <p><?php echo wp_kses_post( wp_unslash( $this->api_get_message( $ig_status ) ) ); ?></p>
362 </div>
363 <?php
364 }
365 ?>
366
367 <?php
368 // Select Business Profile.
369 if ( is_array( $api_data ) && ! empty( $api_data ) && 'facebook-connect' === $api_data['type'] ) {
370 $ig_type = isset( $api_data['type'] ) && $api_data['type'] ? $api_data['type'] : null;
371 $ig_status = isset( $api_data['status'] ) && $api_data['status'] ? $api_data['status'] : null;
372 $ids_business = isset( $api_data['ids_business'] ) && $api_data['ids_business'] ? $api_data['ids_business'] : null;
373 $ig_access_token = isset( $api_data['access_token'] ) && $api_data['access_token'] ? $api_data['access_token'] : null;
374
375 if ( is_array( $ids_business ) && $ids_business ) {
376 $form_business = true;
377 ?>
378 <form method="post" id="form_business">
379 <?php wp_nonce_field(); ?>
380
381 <h3><?php esc_html_e( 'Instagram Business Profiles for This Account', 'powerkit' ); ?></h3>
382
383 <p><?php esc_html_e( 'You need to select a Business profile below.', 'powerkit' ); ?></p>
384
385 <?php
386 $id_counter = 0;
387 foreach ( $ids_business as $item ) {
388 $id_counter++;
389
390 $data = array(
391 'status' => 200,
392 'type' => $ig_type,
393 'access_token' => $ig_access_token,
394 'user_id' => $item['id'],
395 'username' => $item['username'],
396 );
397
398 // Generate marker.
399 $data['marker'] = md5( uniqid( 'connect', true ) );
400
401 // Set API data.
402 $api_data = base64_encode( json_encode( $data ) );
403 ?>
404 <label>
405 <input type="radio" name="api_data" <?php checked( $id_counter, 1 ); ?> value="<?php echo esc_attr( $api_data ); ?>">
406
407 <?php echo esc_attr( $item['name'] ); ?> (<?php echo esc_attr( $item['id'] ); ?>)
408 </label>
409 <?php
410 }
411 ?>
412 <p class="submit">
413 <input class="button" id="save_business" type="submit" value="<?php esc_html_e( 'Select this profile', 'powerkit' ); ?>" />
414 </p>
415 </form>
416
417 <script>
418 window.onbeforeunload = function(e) {
419 return '<?php esc_html_e( 'You need to select a Business profile', 'powerkit' ); ?>';
420 };
421
422 document.getElementById( 'form_business' ).onsubmit = function(e) {
423 window.onbeforeunload = null;
424
425 return true;
426 };
427 </script>
428 <?php
429 }
430 }
431 ?>
432
433 <?php
434 // Display information or sign buttons.
435 if ( powerkit_connect( 'instagram_app_access_token' ) ) {
436 ?>
437
438 <?php if ( 'business' === powerkit_connect( 'instagram_app_type' ) ) { ?>
439 <h3><?php esc_html_e( 'Business Instagram Account', 'powerkit' ); ?></h3>
440 <?php } else { ?>
441 <h3><?php esc_html_e( 'Personal Instagram Account', 'powerkit' ); ?></h3>
442 <?php } ?>
443
444 <p><span class="tab-badge-success"><?php esc_html_e( '✓ Account', 'powerkit' ); ?> (<a href="https://www.instagram.com/<?php echo esc_attr( powerkit_connect( 'instagram_app_username' ) ); ?>" target="_blank"><?php echo esc_attr( powerkit_connect( 'instagram_app_username' ) ); ?></a>) <?php esc_html_e( 'successfully connected', 'powerkit' ); ?></span></p>
445
446 <p><?php esc_html_e( 'Your Instagram User ID:', 'powerkit' ); ?> <code><?php echo esc_attr( powerkit_connect( 'instagram_app_username' ) ); ?></code> <?php esc_html_e( 'Please use this ID in settings when requested.', 'powerkit' ); ?></p>
447
448 <form method="post" class="form-logout">
449 <?php wp_nonce_field(); ?>
450
451 <input type="hidden" name="logout_account_type" value="instagram">
452
453 <p class="submit">
454 <input class="button button-primary" name="logout_account" type="submit" value="<?php esc_html_e( 'Disconnect', 'powerkit' ); ?>" />
455 </p>
456 </form>
457
458 <?php } elseif ( ! isset( $form_business ) ) { ?>
459
460 <h3><?php esc_html_e( 'Personal Account', 'powerkit' ); ?></h3>
461
462 <div class="notice inline">
463 <p><?php echo sprintf( __( 'The number of followers, likes, comments and profile image are not available to Instagram personal accounts. Please add these <a href="%s" target="_blank">manually</a> in the Manual Settings.', 'powerkit' ), admin_url( 'options-general.php?page=powerkit_social_links' ) ); ?></p>
464 </div>
465
466 <p><?php esc_html_e( 'Used for displaying user feeds from a Personal Instagram account.', 'powerkit' ); ?></p>
467
468 <?php
469 $app_authorize = add_query_arg(
470 array(
471 'app_id' => powerkit_connect( 'instagram_app_id' ),
472 'redirect_uri' => rawurlencode( powerkit_connect( 'instagram_app_url' ) ),
473 'response_type' => 'code',
474 'scope' => 'user_profile,user_media',
475 'state' => base64_encode( sprintf( '%s&tab=%s', powerkit_get_page_url( $this->slug ), $tab ) ),
476 ),
477 'https://www.instagram.com/oauth/authorize'
478 );
479 ?>
480
481 <p>
482 <a class="button button-primary" href="<?php echo esc_url( $app_authorize ); ?>">
483 <?php esc_html_e( 'Connect', 'powerkit' ); ?>
484 </a>
485 </p>
486
487 <hr>
488
489 <h3><?php esc_html_e( 'Business Account', 'powerkit' ); ?></h3>
490
491 <div class="notice inline">
492 <p><?php echo sprintf( __( 'Please make sure you link your Instagram Business account to a Facebook page. This is required to display number of followers, likes, comments and profile image. Please follow <a href="%s" target="_blank">this link</a> to learn more about linking your Instagram account to a Facebook page.', 'powerkit' ), 'https://help.instagram.com/399237934150902' ); ?></p>
493 </div>
494
495 <p><?php esc_html_e( 'Used for displaying user feeds from an Instagram Business or Creator account.', 'powerkit' ); ?></p>
496
497 <?php
498 $app_authorize = add_query_arg(
499 array(
500 'client_id' => powerkit_connect( 'instagram_app_fb_client_id' ),
501 'redirect_uri' => rawurlencode( powerkit_connect( 'instagram_app_fb_url' ) ),
502 'scope' => 'manage_pages,instagram_basic',
503 'state' => base64_encode( sprintf( '%s&tab=%s', powerkit_get_page_url( $this->slug ), $tab ) ),
504 ),
505 'https://www.facebook.com/dialog/oauth'
506 );
507 ?>
508
509 <p>
510 <a class="button button-primary" href="<?php echo esc_url( $app_authorize ); ?>">
511 <?php esc_html_e( 'Connect', 'powerkit' ); ?>
512 </a>
513 </p>
514 <?php
515 }
516 ?>
517
518 <hr>
519
520 <form class="basic" method="post">
521 <h3><?php esc_html_e( 'Manual Settings', 'powerkit' ); ?></h3>
522
523 <table class="form-table">
524 <tbody>
525 <tr>
526 <th scope="row">
527 <label for="powerkit_connect_instagram_following" class="title">
528 <?php esc_html_e( 'Following', 'powerkit' ); ?>
529 </label>
530 </th>
531 <td>
532 <input class="regular-text" name="powerkit_connect_instagram_following" id="powerkit_connect_instagram_following" type="number" value="<?php echo esc_attr( get_option( 'powerkit_connect_instagram_following' ) ); ?>" />
533 </td>
534 </tr>
535 <tr>
536 <th scope="row">
537 <label for="powerkit_connect_instagram_custom_followers" class="title">
538 <?php esc_html_e( 'Followers', 'powerkit' ); ?>
539 </label>
540 </th>
541 <td>
542 <input class="regular-text" name="powerkit_connect_instagram_custom_followers" id="powerkit_connect_instagram_custom_followers" type="number" value="<?php echo esc_attr( get_option( 'powerkit_connect_instagram_custom_followers' ) ); ?>" />
543 <p class="description">
544 <?php esc_html_e( 'The number of followers is automatically retrieved from Instagram for Business accounts.', 'powerkit' ); ?>
545 <br>
546 <?php echo sprintf( __( 'Please note, this value is displayed in the Instagram feed only. You may change the number of followers in Social Links on <a href="%s" target="_blank">this page</a>.', 'powerkit' ), admin_url( 'options-general.php?page=powerkit_social_links' ) ); ?>
547 </p>
548 </td>
549 </tr>
550 <tr>
551 <th scope="row">
552 <label for="powerkit_connect_instagram_custom_name" class="title">
553 <?php esc_html_e( 'Name', 'powerkit' ); ?>
554 </label>
555 </th>
556 <td>
557 <input class="regular-text" name="powerkit_connect_instagram_custom_name" id="powerkit_connect_instagram_custom_name" type="text" value="<?php echo esc_attr( get_option( 'powerkit_connect_instagram_custom_name' ) ); ?>" />
558 <p class="description"><?php esc_html_e( 'Name is automatically retrieved from Instagram for Business accounts.', 'powerkit' ); ?></p>
559 </td>
560 </tr>
561 <tr>
562 <th scope="row">
563 <label for="powerkit_connect_instagram_custom_avatar" class="title">
564 <?php esc_html_e( 'Profile Image', 'powerkit' ); ?>
565 </label>
566 </th>
567 <td>
568 <input placeholder="https://example.com/avatar.jpg" class="regular-text" name="powerkit_connect_instagram_custom_avatar" id="powerkit_connect_instagram_custom_avatar" type="text" value="<?php echo esc_attr( get_option( 'powerkit_connect_instagram_custom_avatar' ) ); ?>" />
569 <p class="description"><?php esc_html_e( 'Avatar is automatically retrieved from Instagram for Business accounts.', 'powerkit' ); ?></p>
570 </td>
571 </tr>
572 </tbody>
573 </table>
574
575 <?php wp_nonce_field(); ?>
576
577 <p class="submit">
578 <input class="button button-primary" name="save_instagram_settings" type="submit" value="<?php esc_html_e( 'Save settings', 'powerkit' ); ?>" />
579 </p>
580 </form>
581 <?php
582 }
583
584 /**
585 * Facebook custom tab
586 *
587 * @param string $tab The name of tab.
588 */
589 public function facebook_custom_tab( $tab ) {
590
591 $api_data = $this->api_get_data( 'facebook-connect' );
592
593 // Update connect settigs.
594 if ( is_array( $api_data ) && ! empty( $api_data ) && 'facebook-connect' === $api_data['type'] ) {
595 $fb_status = isset( $api_data['status'] ) && $api_data['status'] ? $api_data['status'] : null;
596 $fb_content = isset( $api_data['content'] ) && $api_data['content'] ? $api_data['content'] : null;
597 $fb_access_token = isset( $api_data['access_token'] ) && $api_data['access_token'] ? $api_data['access_token'] : null;
598 $fb_accounts = isset( $api_data['ids_accounts'] ) && $api_data['ids_accounts'] ? $api_data['ids_accounts'] : array();
599
600 if ( 200 === $fb_status && $fb_access_token ) {
601 update_option( 'powerkit_connect_facebook_app_access_token', $fb_access_token );
602 update_option( 'powerkit_connect_facebook_app_accounts', $fb_accounts );
603 update_option( 'powerkit_connect_facebook_app_refresh', true );
604
605 $this->location_reset_cache( 'facebook' );
606 }
607 }
608
609 // Notice.
610 if ( isset( $fb_status ) && ( 200 === $fb_status ) ) {
611 ?>
612 <div class="notice notice-success is-dismissible">
613 <p><?php echo wp_kses_post( wp_unslash( $this->api_get_message( $fb_status ) ) ); ?></p>
614 </div>
615 <?php
616 } elseif ( isset( $fb_status ) && $fb_status ) {
617 ?>
618 <div class="notice notice-error is-dismissible">
619 <p><?php echo wp_kses_post( wp_unslash( $this->api_get_message( $fb_status ) ) ); ?></p>
620 </div>
621 <?php
622 }
623 ?>
624
625 <?php
626 // Display information or sign buttons.
627 if ( powerkit_connect( 'facebook_app_access_token' ) ) {
628 ?>
629
630 <h3><?php esc_html_e( 'Facebook Account', 'powerkit' ); ?></h3>
631
632 <p><span class="tab-badge-success"><?php esc_html_e( '✓ Account successfully connected', 'powerkit' ); ?></span></p>
633
634 <?php
635 $accounts = powerkit_connect( 'facebook_app_accounts' );
636
637 if ( $accounts ) {
638 $usernames = array();
639
640 foreach ( $accounts as $account ) {
641 if ( isset( $account['username'] ) ) {
642 $usernames[] = sprintf( '<code>%s</code>', $account['username'] );
643 } else {
644 $usernames[] = sprintf( '<code>%s</code>', $account['id'] );
645 }
646 }
647
648 if ( 1 === count( $usernames ) ) {
649 ?>
650 <p><?php esc_html_e( 'Your Facebook page ID:', 'powerkit' ); ?> <?php echo wp_kses_post( implode( ', ', $usernames ) ); ?><?php esc_html_e( '. Please use this ID in settings when requested.', 'powerkit' ); ?></p>
651 <?php
652 } else {
653 ?>
654 <p><?php esc_html_e( 'Available Facebook page IDs:', 'powerkit' ); ?> <?php echo wp_kses_post( implode( ', ', $usernames ) ); ?><?php esc_html_e( '. Please use one of these IDs in settings when requested.', 'powerkit' ); ?></p>
655 <?php
656
657 }
658 }
659 ?>
660
661 <form method="post" class="form-logout">
662 <?php wp_nonce_field(); ?>
663
664 <input type="hidden" name="logout_account_type" value="facebook">
665
666 <p class="submit">
667 <input class="button button-primary" name="logout_account" type="submit" value="<?php esc_html_e( 'Disconnect', 'powerkit' ); ?>" />
668 </p>
669 </form>
670 <?php } else { ?>
671
672 <h3><?php esc_html_e( 'Facebook Account', 'powerkit' ); ?></h3>
673
674 <p><?php echo sprintf( __( 'Connect your Facebook account to automatically fetch the number of followers of your Facebook page. You may also change this number manually on <a href="%s" target="_blank">this page</a>.', 'powerkit' ), admin_url( 'options-general.php?page=powerkit_social_links' ) ); ?></p>
675
676 <?php
677 $app_authorize = add_query_arg(
678 array(
679 'client_id' => powerkit_connect( 'facebook_app_id' ),
680 'redirect_uri' => rawurlencode( powerkit_connect( 'facebook_app_url' ) ),
681 'scope' => 'manage_pages',
682 'state' => base64_encode( sprintf( '%s&tab=%s', powerkit_get_page_url( $this->slug ), $tab ) ),
683 ),
684 'https://www.facebook.com/dialog/oauth'
685 );
686 ?>
687
688 <p>
689 <a class="button button-primary" href="<?php echo esc_url( $app_authorize ); ?>">
690 <?php esc_html_e( 'Connect', 'powerkit' ); ?>
691 </a>
692 </p>
693 <?php
694 }
695 }
696
697 /**
698 * Facebook custom tab
699 *
700 * @param string $tab The name of tab.
701 */
702 public function twitter_custom_tab( $tab ) {
703
704 $api_data = $this->api_get_data( 'twitter-connect' );
705
706 // Update connect settigs.
707 if ( is_array( $api_data ) && ! empty( $api_data ) && 'twitter-connect' === $api_data['type'] ) {
708 $tw_status = isset( $api_data['status'] ) && $api_data['status'] ? $api_data['status'] : null;
709 $tw_user_id = isset( $api_data['user_id'] ) && $api_data['user_id'] ? $api_data['user_id'] : null;
710 $tw_screen_name = isset( $api_data['screen_name'] ) && $api_data['screen_name'] ? $api_data['screen_name'] : null;
711 $tw_oauth_token = isset( $api_data['oauth_token'] ) && $api_data['oauth_token'] ? $api_data['oauth_token'] : null;
712 $tw_oauth_token_secret = isset( $api_data['oauth_token_secret'] ) && $api_data['oauth_token_secret'] ? $api_data['oauth_token_secret'] : null;
713
714 if ( 200 === $tw_status && $tw_oauth_token && $tw_oauth_token_secret ) {
715 update_option( 'powerkit_connect_twitter_app_user_id', $tw_user_id );
716 update_option( 'powerkit_connect_twitter_app_screen_name', $tw_screen_name );
717 update_option( 'powerkit_connect_twitter_app_oauth_token', $tw_oauth_token );
718 update_option( 'powerkit_connect_twitter_app_oauth_token_secret', $tw_oauth_token_secret );
719
720 $this->location_reset_cache( 'twitter' );
721 }
722 }
723
724 // Notice.
725 if ( isset( $tw_status ) && ( 200 === $tw_status ) ) {
726 ?>
727 <div class="notice notice-success is-dismissible">
728 <p><?php echo wp_kses_post( wp_unslash( $this->api_get_message( $tw_status ) ) ); ?></p>
729 </div>
730 <?php
731 } elseif ( isset( $tw_status ) && $tw_status ) {
732 ?>
733 <div class="notice notice-error is-dismissible">
734 <p><?php echo wp_kses_post( wp_unslash( $this->api_get_message( $tw_status ) ) ); ?></p>
735 </div>
736 <?php
737 }
738 ?>
739
740 <?php
741 // Display information or sign buttons.
742 if ( powerkit_connect( 'twitter_app_oauth_token' ) ) {
743 ?>
744
745 <h3><?php esc_html_e( 'Twitter Account', 'powerkit' ); ?></h3>
746
747 <p><span class="tab-badge-success"><?php esc_html_e( '✓ Account', 'powerkit' ); ?> (<a href="https://twitter.com/<?php echo esc_attr( powerkit_connect( 'twitter_app_screen_name' ) ); ?>" target="_blank"><?php echo esc_attr( powerkit_connect( 'twitter_app_screen_name' ) ); ?></a>) <?php esc_html_e( 'successfully connected', 'powerkit' ); ?></span></p>
748
749 <p><?php esc_html_e( 'Your Twitter User ID:', 'powerkit' ); ?> <code><?php echo esc_attr( powerkit_connect( 'twitter_app_screen_name' ) ); ?></code> <?php esc_html_e( 'Please use this ID in settings when requested.', 'powerkit' ); ?></p>
750
751 <form method="post" class="form-logout">
752 <?php wp_nonce_field(); ?>
753
754 <input type="hidden" name="logout_account_type" value="twitter">
755
756 <p class="submit">
757 <input class="button button-primary" name="logout_account" type="submit" value="<?php esc_html_e( 'Disconnect', 'powerkit' ); ?>" />
758 </p>
759 </form>
760 <?php } else { ?>
761 <h3><?php esc_html_e( 'Twitter Account', 'powerkit' ); ?></h3>
762
763 <p><?php echo sprintf( __( 'Connect your Twitter account to display your Twitter feed and the number of followers in Social Links. You may also change the number of followers manually on <a href="%s" target="_blank">this page</a>.', 'powerkit' ), admin_url( 'options-general.php?page=powerkit_social_links' ) ); ?></p>
764
765 <?php
766 $app_authorize = add_query_arg(
767 array(
768 'state' => rawurlencode( sprintf( '%s&tab=%s', powerkit_get_page_url( $this->slug ), $tab ) ),
769 ),
770 powerkit_connect( 'twitter_app_url' )
771 );
772 ?>
773
774 <p>
775 <a class="button button-primary" href="<?php echo esc_url( $app_authorize ); ?>">
776 <?php esc_html_e( 'Connect', 'powerkit' ); ?>
777 </a>
778 </p>
779 <?php
780 }
781 }
782
783 /**
784 * Settings save
785 */
786 public function save_options_page() {
787 if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'] ) ) { // Input var ok; sanitization ok.
788 return;
789 }
790
791 if ( isset( $_POST['save_settings'] ) ) { // Input var ok.
792 // Save social params.
793 $connect_list = apply_filters( 'powerkit_register_connect_list', array() );
794 foreach ( $connect_list as $item ) {
795 $id = $item['id'];
796 if ( isset( $item['fields'] ) && $item['fields'] ) {
797 foreach ( $item['fields'] as $field ) {
798 if ( isset( $_POST[ $field['key'] ] ) ) { // Input var ok.
799 update_option( $field['key'], sanitize_text_field( wp_unslash( $_POST[ $field['key'] ] ) ) ); // Input var ok.
800 }
801 }
802 }
803 }
804
805 self::reset_cache();
806
807 printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'The settings are saved.', 'powerkit' ) );
808 }
809
810 if ( isset( $_POST['save_instagram_settings'] ) ) { // Input var ok.
811 // Custom fields.
812 if ( isset( $_POST['powerkit_connect_instagram_following'] ) ) { // Input var ok.
813 update_option( 'powerkit_connect_instagram_following', sanitize_text_field( wp_unslash( $_POST['powerkit_connect_instagram_following'] ) ) ); // Input var ok.
814 }
815 if ( isset( $_POST['powerkit_connect_instagram_custom_followers'] ) ) { // Input var ok.
816 update_option( 'powerkit_connect_instagram_custom_followers', sanitize_text_field( wp_unslash( $_POST['powerkit_connect_instagram_custom_followers'] ) ) ); // Input var ok.
817 }
818 if ( isset( $_POST['powerkit_connect_instagram_custom_name'] ) ) { // Input var ok.
819 update_option( 'powerkit_connect_instagram_custom_name', sanitize_text_field( wp_unslash( $_POST['powerkit_connect_instagram_custom_name'] ) ) ); // Input var ok.
820 }
821 if ( isset( $_POST['powerkit_connect_instagram_custom_avatar'] ) ) { // Input var ok.
822 update_option( 'powerkit_connect_instagram_custom_avatar', sanitize_text_field( wp_unslash( $_POST['powerkit_connect_instagram_custom_avatar'] ) ) ); // Input var ok.
823 }
824
825 $this->location_reset_cache( 'instagram' );
826
827 printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'The settings are saved.', 'powerkit' ) );
828 }
829 }
830
831 /**
832 * Logout account
833 *
834 * @since 1.0.0
835 */
836 public function logout_account() {
837 if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'] ) ) { // Input var ok; sanitization ok.
838 return;
839 }
840
841 if ( isset( $_POST['logout_account'] ) ) { // Input var ok.
842
843 // Logout Instagram.
844 if ( isset( $_POST['logout_account_type'] ) && 'instagram' === $_POST['logout_account_type'] ) {
845 delete_option( 'powerkit_connect_instagram_app_type' );
846 delete_option( 'powerkit_connect_instagram_app_access_token' );
847 delete_option( 'powerkit_connect_instagram_app_user_id' );
848 delete_option( 'powerkit_connect_instagram_app_username' );
849 delete_option( 'powerkit_connect_instagram_app_refresh_time' );
850 }
851
852 // Logout Facebook.
853 if ( isset( $_POST['logout_account_type'] ) && 'facebook' === $_POST['logout_account_type'] ) {
854 delete_option( 'powerkit_connect_facebook_app_access_token' );
855 delete_option( 'powerkit_connect_facebook_app_accounts' );
856 }
857
858 // Logout Facebook.
859 if ( isset( $_POST['logout_account_type'] ) && 'twitter' === $_POST['logout_account_type'] ) {
860 delete_option( 'powerkit_connect_twitter_app_user_id' );
861 delete_option( 'powerkit_connect_twitter_app_screen_name' );
862 delete_option( 'powerkit_connect_twitter_app_oauth_token' );
863 delete_option( 'powerkit_connect_twitter_app_oauth_token_secret' );
864 }
865
866 if ( isset( $_POST['logout_account_type'] ) ) {
867 $this->location_reset_cache( $_POST['logout_account_type'] );
868 }
869
870 printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Account disabled successfully.', 'powerkit' ) );
871 }
872 }
873
874 /**
875 * Tab reset cache
876 *
877 * @param string $slug Slug of tab.
878 */
879 public function location_reset_cache( $slug ) {
880
881 // Base.
882 $list = array(
883 'powerkit_social_follow',
884 'powerkit_social_links_counter',
885 );
886
887 // Instagram.
888 if ( 'instagram' === $slug ) {
889 $list = array_merge(
890 $list,
891 array(
892 'powerkit_instagram_data',
893 'powerkit_instagram_recent',
894 )
895 );
896
897 }
898
899 // Twitter.
900 if ( 'twitter' === $slug ) {
901 $list = array_merge(
902 $list,
903 array(
904 'powerkit_twitter_data',
905 'powerkit_twitter_block_cache',
906 'powerkit_twitter_shortcode_cache',
907 'powerkit_twitter_widget_cache',
908 )
909 );
910 }
911
912 self::reset_cache( $list );
913 }
914
915 /**
916 * User reset cache
917 *
918 * @param bool $forcibly Force clear the cache.
919 */
920 public function user_reset_cache( $forcibly = false ) {
921 if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'] ) ) { // Input var ok; sanitization ok.
922 return;
923 }
924
925 if ( isset( $_POST['powerkit_reset_cache'] ) ) { // Input var ok.
926 self::reset_cache();
927 }
928 }
929
930 /**
931 * Ajax Reset cache
932 */
933 public function ajax_reset_cache() {
934 wp_verify_nonce( null );
935
936 $list = apply_filters( 'powerkit_ajax_reset_cache', array() );
937
938 if ( ! isset( $_POST['page'] ) ) { // Input var ok.
939 return false;
940 }
941
942 $page = sanitize_key( $_POST['page'] ); // Input var ok; sanitization ok.
943
944 if ( ! isset( $list[ $page ] ) ) {
945 return false;
946 }
947
948 self::reset_cache( $list[ $page ] );
949
950 die();
951 }
952
953 /**
954 * Cron refresh access token
955 */
956 public function cron_access_token_refresh() {
957 self::access_token_refresh( 'instagram' );
958 }
959
960 /**
961 * Refresh access token
962 *
963 * @param string $type The type.
964 * @param bool $should_attempt_refresh The refresh.
965 */
966 public static function access_token_refresh( $type, $should_attempt_refresh = false ) {
967
968 if ( 'instagram' === $type && 'personal' === powerkit_connect( 'instagram_app_type' ) ) {
969
970 if ( (int) get_option( 'powerkit_connect_instagram_app_refresh_time' ) < time() ) {
971 $should_attempt_refresh = true;
972 }
973
974 if ( $should_attempt_refresh ) {
975
976 update_option( 'powerkit_connect_instagram_app_refresh_time', time() + ( DAY_IN_SECONDS * 30 ) );
977
978 $remote_link = add_query_arg(
979 array(
980 'grant_type' => 'ig_refresh_token',
981 'access_token' => powerkit_connect( 'instagram_app_access_token' ),
982 ),
983 'https://graph.instagram.com/refresh_access_token'
984 );
985
986 $response = wp_safe_remote_get(
987 $remote_link,
988 array(
989 'timeout' => 3,
990 )
991 );
992
993 $response = wp_remote_retrieve_body( $response );
994
995 $data = json_decode( $response, true );
996
997 // Update settings.
998 if ( isset( $data['access_token'] ) ) {
999 update_option( 'powerkit_connect_instagram_app_access_token', $data['access_token'] );
1000 }
1001 }
1002 }
1003 }
1004
1005 /**
1006 * Reset cache
1007 *
1008 * @param array $list Reset list.
1009 */
1010 public static function reset_cache( $list = false ) {
1011 if ( is_array( $list ) ) {
1012
1013 $list = $list;
1014
1015 } elseif ( is_string( $list ) && $list ) {
1016
1017 $list = explode( ' ', $list );
1018
1019 } else {
1020 $list = apply_filters( 'powerkit_reset_cache', array() );
1021
1022 $puck = array();
1023
1024 foreach ( $list as $item ) {
1025 if ( is_array( $item ) ) {
1026 $puck = array_merge( $puck, $item );
1027 } elseif ( is_string( $item ) ) {
1028 $puck = array_merge( $puck, explode( ' ', $item ) );
1029 }
1030 }
1031
1032 $list = $puck;
1033 }
1034
1035 if ( $list ) {
1036 global $wpdb;
1037
1038 foreach ( $list as $option_name ) {
1039 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name LIKE '%%%s%%'", $option_name ) ); // db call ok; no-cache ok.
1040 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key LIKE '%%%s%%'", $option_name ) ); // db call ok; no-cache ok.
1041 }
1042
1043 printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Cache purged.', 'powerkit' ) );
1044 }
1045 }
1046
1047 /**
1048 * Register the stylesheets and JavaScript for the admin area.
1049 *
1050 * @param string $page Current page.
1051 */
1052 public function enqueue_scripts( $page ) {
1053 if ( is_customize_preview() || 'toplevel_page_powerkit_manager' === $page ) {
1054
1055 wp_enqueue_script( 'admin-powerkit-connect', plugin_dir_url( __FILE__ ) . 'js/admin-powerkit-connect.js', array( 'jquery' ), powerkit_get_setting( 'version' ), false );
1056
1057 wp_localize_script(
1058 'admin-powerkit-connect',
1059 'pk_connect',
1060 array(
1061 'ajax_url' => admin_url( 'admin-ajax.php' ),
1062 )
1063 );
1064 }
1065 }
1066 }
1067
1068 new Powerkit_Connect();
1069