PluginProbe
Getwid – Gutenberg Blocks / 1.8.7
Getwid – Gutenberg Blocks v1.8.7
3.0.2 3.0.1 3.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 trunk 1.8.7 1.9.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
getwid / includes / instagram-token-manager.php

instagram-token-manager.php in Getwid – Gutenberg Blocks 1.8.7, at includes/instagram-token-manager.php

124 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Getwid;
4
5 class InstagramTokenManager {
6
7 public function __construct() {
8
9 // Action hook to execute when the event is run
10 add_action( 'getwid_refresh_instagram_token', [ $this, 'refresh_instagram_token' ] );
11 add_filter( 'cron_schedules', [ $this , 'time_scheduled_event' ] );
12
13 add_action( 'update_option', [ $this, 'update_option' ], 10, 3 );
14 add_action( 'admin_init', [ $this, 'error_message' ] );
15 }
16
17 public function time_scheduled_event( $schedules ) {
18
19 if ( !isset($schedules['two_weeks']) ) {
20 /*
21 * https://developers.facebook.com/docs/instagram-basic-display-api/guides/long-lived-access-tokens/
22 */
23 $schedules[ 'two_weeks' ] = [
24 'interval' => WEEK_IN_SECONDS * 2,
25 'display' => 'Once in Two Weeks'
26 ];
27 }
28
29 return $schedules;
30 }
31
32 public function schedule_token_refresh_event() {
33 if ( ! wp_next_scheduled( 'getwid_refresh_instagram_token' ) ) {
34 wp_schedule_event( time(), 'two_weeks', 'getwid_refresh_instagram_token' );
35 }
36 }
37
38 public function update_option( $option_name, $old_value, $value ) {
39 if ( $option_name === 'getwid_instagram_token' ) {
40 delete_option( 'getwid_instagram_token_cron_error_message' );
41
42 if ( $value === '' ) {
43 $this->clear_scheduled_event();
44 }
45 }
46 }
47
48 public function clear_scheduled_event() {
49 $timestamp = wp_next_scheduled( 'getwid_refresh_instagram_token' );
50
51 if ( $timestamp ) {
52 wp_unschedule_event( $timestamp, 'getwid_refresh_instagram_token' );
53 }
54 }
55
56 public function refresh_instagram_token() {
57 $getwid_instagram_token = get_option( 'getwid_instagram_token' );
58
59 if ( ! empty( $getwid_instagram_token ) ) {
60 $api_req = 'https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&access_token=' . $getwid_instagram_token;
61 $response = wp_remote_get( $api_req );
62
63 if ( is_wp_error( $response ) ) {
64 update_option( 'getwid_instagram_token_cron_error_message', $response->get_error_message() );
65 } else {
66 $response_body = json_decode( wp_remote_retrieve_body( $response ), false );
67
68 if ( $response_body && json_last_error() === JSON_ERROR_NONE ) {
69 if ( isset( $response_body->error ) ) {
70 update_option( 'getwid_instagram_token_cron_error_message', $response_body->error->message );
71 } else {
72 delete_option( 'getwid_instagram_token_cron_error_message' );
73
74 if ( ! empty( $response_body->access_token ) ) {
75 // Update token
76 update_option( 'getwid_instagram_token', $response_body->access_token );
77 // Delete cache data
78 delete_transient( 'getwid_instagram_response_data' );
79 // Schedule token refresh
80 $this->schedule_token_refresh_event();
81 }
82 }
83 } else {
84 update_option( 'getwid_instagram_token_cron_error_message', __( 'Error in json_decode.', 'getwid' ) );
85 }
86 }
87 }
88 }
89
90 public function getwid_instagram_notice_token_error() {
91 $instagram_token_error_message = get_option( 'getwid_instagram_token_cron_error_message' );
92
93 if ( ! empty( $instagram_token_error_message ) ) {
94 ?>
95 <div class="notice notice-error">
96 <p>
97 <?php
98 echo esc_html( sprintf(
99 //translators: %s is an error message
100 __( 'An error occurred while updating Instagram access token: %s', 'getwid' ),
101 $instagram_token_error_message
102 ) );
103 ?>
104 </p>
105 </div>
106 <?php
107 }
108 }
109
110 public function error_message() {
111 global $pagenow;
112
113 $is_getwid_url = isset( $_GET['page'] ) && sanitize_text_field( wp_unslash( $_GET['page'] ) ) == 'getwid';
114
115 $is_getwid_settings_page = $pagenow == 'options-general.php' && $is_getwid_url;
116
117 if ( $is_getwid_settings_page && current_user_can( 'manage_options' ) ) {
118 if ( get_option( 'getwid_instagram_token_cron_error_message' ) !== '' ) {
119 add_action( 'admin_notices', [ $this, 'getwid_instagram_notice_token_error' ] );
120 }
121 }
122 }
123 }
124