PluginProbe
Powerkit – Supercharge your WordPress Site / 2.4.4
Powerkit – Supercharge your WordPress Site v2.4.4
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.4.4, at extensions/connect/class-powerkit-connect.php

1,081 lines 36.5 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 ) && 1 === count( $ids_business ) ) {
376
377 $profile = reset( $ids_business );
378
379 update_option( 'powerkit_connect_instagram_app_type', 'business' );
380 update_option( 'powerkit_connect_instagram_app_access_token', $ig_access_token );
381 update_option( 'powerkit_connect_instagram_app_user_id', $profile['id'] );
382 update_option( 'powerkit_connect_instagram_app_username', $profile['username'] );
383 update_option( 'powerkit_connect_instagram_app_refresh_time', time() + ( DAY_IN_SECONDS * 30 ) );
384
385 $this->location_reset_cache( 'instagram' );
386
387 } elseif ( is_array( $ids_business ) && $ids_business ) {
388 $form_business = true;
389 ?>
390 <form method="post" id="form_business">
391 <?php wp_nonce_field(); ?>
392
393 <h3><?php esc_html_e( 'Instagram Business Profiles for This Account', 'powerkit' ); ?></h3>
394
395 <p><?php esc_html_e( 'Please select the profile that you’d like to connect:', 'powerkit' ); ?></p>
396
397 <?php
398 $id_counter = 0;
399 foreach ( $ids_business as $item ) {
400 $id_counter++;
401
402 $data = array(
403 'status' => 200,
404 'type' => $ig_type,
405 'access_token' => $ig_access_token,
406 'user_id' => $item['id'],
407 'username' => $item['username'],
408 );
409
410 // Generate marker.
411 $data['marker'] = md5( uniqid( 'connect', true ) );
412
413 // Set API data.
414 $api_data = base64_encode( json_encode( $data ) );
415 ?>
416 <label>
417 <input type="radio" name="api_data" <?php checked( $id_counter, 1 ); ?> value="<?php echo esc_attr( $api_data ); ?>">
418
419 <?php echo esc_attr( $item['name'] ); ?> (<?php echo esc_attr( $item['id'] ); ?>)
420 </label>
421 <?php
422 }
423 ?>
424 <p class="submit">
425 <input class="button button-primary" id="save_business" type="submit" value="<?php esc_html_e( 'Connect', 'powerkit' ); ?>" />
426 </p>
427 </form>
428
429 <script>
430 window.onbeforeunload = function(e) {
431 return '<?php esc_html_e( 'You need to select a Business profile', 'powerkit' ); ?>';
432 };
433
434 document.getElementById( 'form_business' ).onsubmit = function(e) {
435 window.onbeforeunload = null;
436
437 return true;
438 };
439 </script>
440 <?php
441 }
442 }
443 ?>
444
445 <?php
446 // Display information or sign buttons.
447 if ( powerkit_connect( 'instagram_app_access_token' ) ) {
448 ?>
449
450 <?php if ( 'business' === powerkit_connect( 'instagram_app_type' ) ) { ?>
451 <h3><?php esc_html_e( 'Business Instagram Account', 'powerkit' ); ?></h3>
452 <?php } else { ?>
453 <h3><?php esc_html_e( 'Personal Instagram Account', 'powerkit' ); ?></h3>
454 <?php } ?>
455
456 <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>
457
458 <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>
459
460 <form method="post" class="form-logout">
461 <?php wp_nonce_field(); ?>
462
463 <input type="hidden" name="logout_account_type" value="instagram">
464
465 <p class="submit">
466 <input class="button button-primary" name="logout_account" type="submit" value="<?php esc_html_e( 'Disconnect', 'powerkit' ); ?>" />
467 </p>
468 </form>
469
470 <?php } elseif ( ! isset( $form_business ) ) { ?>
471
472 <h3><?php esc_html_e( 'Personal Account', 'powerkit' ); ?></h3>
473
474 <div class="notice inline">
475 <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>
476 </div>
477
478 <p><?php esc_html_e( 'Used for displaying user feeds from a Personal Instagram account.', 'powerkit' ); ?></p>
479
480 <?php
481 $app_authorize = add_query_arg(
482 array(
483 'app_id' => powerkit_connect( 'instagram_app_id' ),
484 'redirect_uri' => rawurlencode( powerkit_connect( 'instagram_app_url' ) ),
485 'response_type' => 'code',
486 'scope' => 'user_profile,user_media',
487 'state' => base64_encode( sprintf( '%s&tab=%s', powerkit_get_page_url( $this->slug ), $tab ) ),
488 ),
489 'https://www.instagram.com/oauth/authorize'
490 );
491 ?>
492
493 <p>
494 <a class="button button-primary" href="<?php echo esc_url( $app_authorize ); ?>">
495 <?php esc_html_e( 'Connect', 'powerkit' ); ?>
496 </a>
497 </p>
498
499 <hr>
500
501 <h3><?php esc_html_e( 'Business Account', 'powerkit' ); ?></h3>
502
503 <div class="notice inline">
504 <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>
505 </div>
506
507 <p><?php esc_html_e( 'Used for displaying user feeds from an Instagram Business or Creator account.', 'powerkit' ); ?></p>
508
509 <?php
510 $app_authorize = add_query_arg(
511 array(
512 'client_id' => powerkit_connect( 'instagram_app_fb_client_id' ),
513 'redirect_uri' => rawurlencode( powerkit_connect( 'instagram_app_fb_url' ) ),
514 'scope' => 'manage_pages,instagram_basic',
515 'state' => base64_encode( sprintf( '%s&tab=%s', powerkit_get_page_url( $this->slug ), $tab ) ),
516 ),
517 'https://www.facebook.com/dialog/oauth'
518 );
519 ?>
520
521 <p>
522 <a class="button button-primary" href="<?php echo esc_url( $app_authorize ); ?>">
523 <?php esc_html_e( 'Connect', 'powerkit' ); ?>
524 </a>
525 </p>
526 <?php
527 }
528 ?>
529
530 <hr>
531
532 <form class="basic" method="post">
533 <h3><?php esc_html_e( 'Manual Settings', 'powerkit' ); ?></h3>
534
535 <table class="form-table">
536 <tbody>
537 <tr>
538 <th scope="row">
539 <label for="powerkit_connect_instagram_following" class="title">
540 <?php esc_html_e( 'Following', 'powerkit' ); ?>
541 </label>
542 </th>
543 <td>
544 <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' ) ); ?>" />
545 </td>
546 </tr>
547 <tr>
548 <th scope="row">
549 <label for="powerkit_connect_instagram_custom_followers" class="title">
550 <?php esc_html_e( 'Followers', 'powerkit' ); ?>
551 </label>
552 </th>
553 <td>
554 <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' ) ); ?>" />
555 <p class="description">
556 <?php esc_html_e( 'The number of followers is automatically retrieved from Instagram for Business accounts.', 'powerkit' ); ?>
557 <br>
558 <?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' ) ); ?>
559 </p>
560 </td>
561 </tr>
562 <tr>
563 <th scope="row">
564 <label for="powerkit_connect_instagram_custom_name" class="title">
565 <?php esc_html_e( 'Name', 'powerkit' ); ?>
566 </label>
567 </th>
568 <td>
569 <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' ) ); ?>" />
570 <p class="description"><?php esc_html_e( 'Name is automatically retrieved from Instagram for Business accounts.', 'powerkit' ); ?></p>
571 </td>
572 </tr>
573 <tr>
574 <th scope="row">
575 <label for="powerkit_connect_instagram_custom_avatar" class="title">
576 <?php esc_html_e( 'Profile Image', 'powerkit' ); ?>
577 </label>
578 </th>
579 <td>
580 <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' ) ); ?>" />
581 <p class="description"><?php esc_html_e( 'Avatar is automatically retrieved from Instagram for Business accounts.', 'powerkit' ); ?></p>
582 </td>
583 </tr>
584 </tbody>
585 </table>
586
587 <?php wp_nonce_field(); ?>
588
589 <p class="submit">
590 <input class="button button-primary" name="save_instagram_settings" type="submit" value="<?php esc_html_e( 'Save settings', 'powerkit' ); ?>" />
591 </p>
592 </form>
593 <?php
594 }
595
596 /**
597 * Facebook custom tab
598 *
599 * @param string $tab The name of tab.
600 */
601 public function facebook_custom_tab( $tab ) {
602
603 $api_data = $this->api_get_data( 'facebook-connect' );
604
605 // Update connect settigs.
606 if ( is_array( $api_data ) && ! empty( $api_data ) && 'facebook-connect' === $api_data['type'] ) {
607 $fb_status = isset( $api_data['status'] ) && $api_data['status'] ? $api_data['status'] : null;
608 $fb_content = isset( $api_data['content'] ) && $api_data['content'] ? $api_data['content'] : null;
609 $fb_access_token = isset( $api_data['access_token'] ) && $api_data['access_token'] ? $api_data['access_token'] : null;
610 $fb_accounts = isset( $api_data['ids_accounts'] ) && $api_data['ids_accounts'] ? $api_data['ids_accounts'] : array();
611
612 if ( 200 === $fb_status && $fb_access_token ) {
613 update_option( 'powerkit_connect_facebook_app_access_token', $fb_access_token );
614 update_option( 'powerkit_connect_facebook_app_accounts', $fb_accounts );
615 update_option( 'powerkit_connect_facebook_app_refresh', true );
616
617 $this->location_reset_cache( 'facebook' );
618 }
619 }
620
621 // Notice.
622 if ( isset( $fb_status ) && ( 200 === $fb_status ) ) {
623 ?>
624 <div class="notice notice-success is-dismissible">
625 <p><?php echo wp_kses_post( wp_unslash( $this->api_get_message( $fb_status ) ) ); ?></p>
626 </div>
627 <?php
628 } elseif ( isset( $fb_status ) && $fb_status ) {
629 ?>
630 <div class="notice notice-error is-dismissible">
631 <p><?php echo wp_kses_post( wp_unslash( $this->api_get_message( $fb_status ) ) ); ?></p>
632 </div>
633 <?php
634 }
635 ?>
636
637 <?php
638 // Display information or sign buttons.
639 if ( powerkit_connect( 'facebook_app_access_token' ) ) {
640 ?>
641
642 <h3><?php esc_html_e( 'Facebook Account', 'powerkit' ); ?></h3>
643
644 <p><span class="tab-badge-success"><?php esc_html_e( '✓ Account successfully connected', 'powerkit' ); ?></span></p>
645
646 <?php
647 $accounts = powerkit_connect( 'facebook_app_accounts' );
648
649 if ( $accounts ) {
650 $usernames = array();
651
652 foreach ( $accounts as $account ) {
653 if ( isset( $account['username'] ) ) {
654 $usernames[] = sprintf( '<code>%s</code>', $account['username'] );
655 } else {
656 $usernames[] = sprintf( '<code>%s</code>', $account['id'] );
657 }
658 }
659
660 if ( 1 === count( $usernames ) ) {
661 ?>
662 <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>
663 <?php
664 } else {
665 ?>
666 <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>
667 <?php
668
669 }
670 }
671 ?>
672
673 <form method="post" class="form-logout">
674 <?php wp_nonce_field(); ?>
675
676 <input type="hidden" name="logout_account_type" value="facebook">
677
678 <p class="submit">
679 <input class="button button-primary" name="logout_account" type="submit" value="<?php esc_html_e( 'Disconnect', 'powerkit' ); ?>" />
680 </p>
681 </form>
682 <?php } else { ?>
683
684 <h3><?php esc_html_e( 'Facebook Account', 'powerkit' ); ?></h3>
685
686 <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>
687
688 <?php
689 $app_authorize = add_query_arg(
690 array(
691 'client_id' => powerkit_connect( 'facebook_app_id' ),
692 'redirect_uri' => rawurlencode( powerkit_connect( 'facebook_app_url' ) ),
693 'scope' => 'manage_pages',
694 'state' => base64_encode( sprintf( '%s&tab=%s', powerkit_get_page_url( $this->slug ), $tab ) ),
695 ),
696 'https://www.facebook.com/dialog/oauth'
697 );
698 ?>
699
700 <p>
701 <a class="button button-primary" href="<?php echo esc_url( $app_authorize ); ?>">
702 <?php esc_html_e( 'Connect', 'powerkit' ); ?>
703 </a>
704 </p>
705 <?php
706 }
707 }
708
709 /**
710 * Facebook custom tab
711 *
712 * @param string $tab The name of tab.
713 */
714 public function twitter_custom_tab( $tab ) {
715
716 $api_data = $this->api_get_data( 'twitter-connect' );
717
718 // Update connect settigs.
719 if ( is_array( $api_data ) && ! empty( $api_data ) && 'twitter-connect' === $api_data['type'] ) {
720 $tw_status = isset( $api_data['status'] ) && $api_data['status'] ? $api_data['status'] : null;
721 $tw_user_id = isset( $api_data['user_id'] ) && $api_data['user_id'] ? $api_data['user_id'] : null;
722 $tw_screen_name = isset( $api_data['screen_name'] ) && $api_data['screen_name'] ? $api_data['screen_name'] : null;
723 $tw_oauth_token = isset( $api_data['oauth_token'] ) && $api_data['oauth_token'] ? $api_data['oauth_token'] : null;
724 $tw_oauth_token_secret = isset( $api_data['oauth_token_secret'] ) && $api_data['oauth_token_secret'] ? $api_data['oauth_token_secret'] : null;
725
726 if ( 200 === $tw_status && $tw_oauth_token && $tw_oauth_token_secret ) {
727 update_option( 'powerkit_connect_twitter_app_user_id', $tw_user_id );
728 update_option( 'powerkit_connect_twitter_app_screen_name', $tw_screen_name );
729 update_option( 'powerkit_connect_twitter_app_oauth_token', $tw_oauth_token );
730 update_option( 'powerkit_connect_twitter_app_oauth_token_secret', $tw_oauth_token_secret );
731
732 $this->location_reset_cache( 'twitter' );
733 }
734 }
735
736 // Notice.
737 if ( isset( $tw_status ) && ( 200 === $tw_status ) ) {
738 ?>
739 <div class="notice notice-success is-dismissible">
740 <p><?php echo wp_kses_post( wp_unslash( $this->api_get_message( $tw_status ) ) ); ?></p>
741 </div>
742 <?php
743 } elseif ( isset( $tw_status ) && $tw_status ) {
744 ?>
745 <div class="notice notice-error is-dismissible">
746 <p><?php echo wp_kses_post( wp_unslash( $this->api_get_message( $tw_status ) ) ); ?></p>
747 </div>
748 <?php
749 }
750 ?>
751
752 <?php
753 // Display information or sign buttons.
754 if ( powerkit_connect( 'twitter_app_oauth_token' ) ) {
755 ?>
756
757 <h3><?php esc_html_e( 'Twitter Account', 'powerkit' ); ?></h3>
758
759 <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>
760
761 <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>
762
763 <form method="post" class="form-logout">
764 <?php wp_nonce_field(); ?>
765
766 <input type="hidden" name="logout_account_type" value="twitter">
767
768 <p class="submit">
769 <input class="button button-primary" name="logout_account" type="submit" value="<?php esc_html_e( 'Disconnect', 'powerkit' ); ?>" />
770 </p>
771 </form>
772 <?php } else { ?>
773 <h3><?php esc_html_e( 'Twitter Account', 'powerkit' ); ?></h3>
774
775 <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>
776
777 <?php
778 $app_authorize = add_query_arg(
779 array(
780 'state' => rawurlencode( sprintf( '%s&tab=%s', powerkit_get_page_url( $this->slug ), $tab ) ),
781 ),
782 powerkit_connect( 'twitter_app_url' )
783 );
784 ?>
785
786 <p>
787 <a class="button button-primary" href="<?php echo esc_url( $app_authorize ); ?>">
788 <?php esc_html_e( 'Connect', 'powerkit' ); ?>
789 </a>
790 </p>
791 <?php
792 }
793 }
794
795 /**
796 * Settings save
797 */
798 public function save_options_page() {
799 if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'] ) ) { // Input var ok; sanitization ok.
800 return;
801 }
802
803 if ( isset( $_POST['save_settings'] ) ) { // Input var ok.
804 // Save social params.
805 $connect_list = apply_filters( 'powerkit_register_connect_list', array() );
806 foreach ( $connect_list as $item ) {
807 $id = $item['id'];
808 if ( isset( $item['fields'] ) && $item['fields'] ) {
809 foreach ( $item['fields'] as $field ) {
810 if ( isset( $_POST[ $field['key'] ] ) ) { // Input var ok.
811 update_option( $field['key'], sanitize_text_field( wp_unslash( $_POST[ $field['key'] ] ) ) ); // Input var ok.
812 }
813 }
814 }
815 }
816
817 self::reset_cache();
818
819 printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'The settings are saved.', 'powerkit' ) );
820 }
821
822 if ( isset( $_POST['save_instagram_settings'] ) ) { // Input var ok.
823 // Custom fields.
824 if ( isset( $_POST['powerkit_connect_instagram_following'] ) ) { // Input var ok.
825 update_option( 'powerkit_connect_instagram_following', sanitize_text_field( wp_unslash( $_POST['powerkit_connect_instagram_following'] ) ) ); // Input var ok.
826 }
827 if ( isset( $_POST['powerkit_connect_instagram_custom_followers'] ) ) { // Input var ok.
828 update_option( 'powerkit_connect_instagram_custom_followers', sanitize_text_field( wp_unslash( $_POST['powerkit_connect_instagram_custom_followers'] ) ) ); // Input var ok.
829 }
830 if ( isset( $_POST['powerkit_connect_instagram_custom_name'] ) ) { // Input var ok.
831 update_option( 'powerkit_connect_instagram_custom_name', sanitize_text_field( wp_unslash( $_POST['powerkit_connect_instagram_custom_name'] ) ) ); // Input var ok.
832 }
833 if ( isset( $_POST['powerkit_connect_instagram_custom_avatar'] ) ) { // Input var ok.
834 update_option( 'powerkit_connect_instagram_custom_avatar', sanitize_text_field( wp_unslash( $_POST['powerkit_connect_instagram_custom_avatar'] ) ) ); // Input var ok.
835 }
836
837 $this->location_reset_cache( 'instagram' );
838
839 printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'The settings are saved.', 'powerkit' ) );
840 }
841 }
842
843 /**
844 * Logout account
845 *
846 * @since 1.0.0
847 */
848 public function logout_account() {
849 if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'] ) ) { // Input var ok; sanitization ok.
850 return;
851 }
852
853 if ( isset( $_POST['logout_account'] ) ) { // Input var ok.
854
855 // Logout Instagram.
856 if ( isset( $_POST['logout_account_type'] ) && 'instagram' === $_POST['logout_account_type'] ) {
857 delete_option( 'powerkit_connect_instagram_app_type' );
858 delete_option( 'powerkit_connect_instagram_app_access_token' );
859 delete_option( 'powerkit_connect_instagram_app_user_id' );
860 delete_option( 'powerkit_connect_instagram_app_username' );
861 delete_option( 'powerkit_connect_instagram_app_refresh_time' );
862 }
863
864 // Logout Facebook.
865 if ( isset( $_POST['logout_account_type'] ) && 'facebook' === $_POST['logout_account_type'] ) {
866 delete_option( 'powerkit_connect_facebook_app_access_token' );
867 delete_option( 'powerkit_connect_facebook_app_accounts' );
868 }
869
870 // Logout Facebook.
871 if ( isset( $_POST['logout_account_type'] ) && 'twitter' === $_POST['logout_account_type'] ) {
872 delete_option( 'powerkit_connect_twitter_app_user_id' );
873 delete_option( 'powerkit_connect_twitter_app_screen_name' );
874 delete_option( 'powerkit_connect_twitter_app_oauth_token' );
875 delete_option( 'powerkit_connect_twitter_app_oauth_token_secret' );
876 }
877
878 if ( isset( $_POST['logout_account_type'] ) ) {
879 $this->location_reset_cache( $_POST['logout_account_type'] );
880 }
881
882 printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Account disabled successfully.', 'powerkit' ) );
883 }
884 }
885
886 /**
887 * Tab reset cache
888 *
889 * @param string $slug Slug of tab.
890 */
891 public function location_reset_cache( $slug ) {
892
893 // Base.
894 $list = array(
895 'powerkit_social_follow',
896 'powerkit_social_links_counter',
897 );
898
899 // Instagram.
900 if ( 'instagram' === $slug ) {
901 $list = array_merge(
902 $list,
903 array(
904 'powerkit_instagram_data',
905 'powerkit_instagram_recent',
906 )
907 );
908
909 }
910
911 // Twitter.
912 if ( 'twitter' === $slug ) {
913 $list = array_merge(
914 $list,
915 array(
916 'powerkit_twitter_data',
917 'powerkit_twitter_block_cache',
918 'powerkit_twitter_shortcode_cache',
919 'powerkit_twitter_widget_cache',
920 )
921 );
922 }
923
924 self::reset_cache( $list );
925 }
926
927 /**
928 * User reset cache
929 *
930 * @param bool $forcibly Force clear the cache.
931 */
932 public function user_reset_cache( $forcibly = false ) {
933 if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'] ) ) { // Input var ok; sanitization ok.
934 return;
935 }
936
937 if ( isset( $_POST['powerkit_reset_cache'] ) ) { // Input var ok.
938 self::reset_cache();
939 }
940 }
941
942 /**
943 * Ajax Reset cache
944 */
945 public function ajax_reset_cache() {
946 wp_verify_nonce( null );
947
948 $list = apply_filters( 'powerkit_ajax_reset_cache', array() );
949
950 if ( ! isset( $_POST['page'] ) ) { // Input var ok.
951 return false;
952 }
953
954 $page = sanitize_key( $_POST['page'] ); // Input var ok; sanitization ok.
955
956 if ( ! isset( $list[ $page ] ) ) {
957 return false;
958 }
959
960 self::reset_cache( $list[ $page ] );
961
962 die();
963 }
964
965 /**
966 * Cron refresh access token
967 */
968 public function cron_access_token_refresh() {
969 self::access_token_refresh( 'instagram' );
970 }
971
972 /**
973 * Refresh access token
974 *
975 * @param string $type The type.
976 * @param bool $should_attempt_refresh The refresh.
977 */
978 public static function access_token_refresh( $type, $should_attempt_refresh = false ) {
979
980 if ( 'instagram' === $type && 'personal' === powerkit_connect( 'instagram_app_type' ) ) {
981
982 if ( (int) get_option( 'powerkit_connect_instagram_app_refresh_time' ) < time() ) {
983 $should_attempt_refresh = true;
984 }
985
986 if ( $should_attempt_refresh ) {
987
988 update_option( 'powerkit_connect_instagram_app_refresh_time', time() + ( DAY_IN_SECONDS * 30 ) );
989
990 $remote_link = add_query_arg(
991 array(
992 'grant_type' => 'ig_refresh_token',
993 'access_token' => powerkit_connect( 'instagram_app_access_token' ),
994 ),
995 'https://graph.instagram.com/refresh_access_token'
996 );
997
998 $response = wp_safe_remote_get(
999 $remote_link,
1000 array(
1001 'timeout' => 3,
1002 )
1003 );
1004
1005 $response = wp_remote_retrieve_body( $response );
1006
1007 $data = json_decode( $response, true );
1008
1009 // Update settings.
1010 if ( isset( $data['access_token'] ) ) {
1011 update_option( 'powerkit_connect_instagram_app_access_token', $data['access_token'] );
1012 }
1013 }
1014 }
1015 }
1016
1017 /**
1018 * Reset cache
1019 *
1020 * @param array $list Reset list.
1021 */
1022 public static function reset_cache( $list = false ) {
1023 if ( is_array( $list ) ) {
1024
1025 $list = $list;
1026
1027 } elseif ( is_string( $list ) && $list ) {
1028
1029 $list = explode( ' ', $list );
1030
1031 } else {
1032 $list = apply_filters( 'powerkit_reset_cache', array() );
1033
1034 $puck = array();
1035
1036 foreach ( $list as $item ) {
1037 if ( is_array( $item ) ) {
1038 $puck = array_merge( $puck, $item );
1039 } elseif ( is_string( $item ) ) {
1040 $puck = array_merge( $puck, explode( ' ', $item ) );
1041 }
1042 }
1043
1044 $list = $puck;
1045 }
1046
1047 if ( $list ) {
1048 global $wpdb;
1049
1050 foreach ( $list as $option_name ) {
1051 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name LIKE '%%%s%%'", $option_name ) ); // db call ok; no-cache ok.
1052 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key LIKE '%%%s%%'", $option_name ) ); // db call ok; no-cache ok.
1053 }
1054
1055 printf( '<div id="message" class="updated fade"><p><strong>%s</strong></p></div>', esc_html__( 'Cache purged.', 'powerkit' ) );
1056 }
1057 }
1058
1059 /**
1060 * Register the stylesheets and JavaScript for the admin area.
1061 *
1062 * @param string $page Current page.
1063 */
1064 public function enqueue_scripts( $page ) {
1065 if ( is_customize_preview() || 'toplevel_page_powerkit_manager' === $page ) {
1066
1067 wp_enqueue_script( 'admin-powerkit-connect', plugin_dir_url( __FILE__ ) . 'js/admin-powerkit-connect.js', array( 'jquery' ), powerkit_get_setting( 'version' ), false );
1068
1069 wp_localize_script(
1070 'admin-powerkit-connect',
1071 'pk_connect',
1072 array(
1073 'ajax_url' => admin_url( 'admin-ajax.php' ),
1074 )
1075 );
1076 }
1077 }
1078 }
1079
1080 new Powerkit_Connect();
1081