PluginProbe
Getwid – Gutenberg Blocks / 3.0.2
Getwid – Gutenberg Blocks v3.0.2
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
← All changes | includes/instagram-token-manager.php +136 -123 1.9.13.0.2 View file →
@@ -1,123 +1,136 @@
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 -}
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 + add_filter( 'pre_update_option_getwid_instagram_token', [ $this, 'filter_token_before_save' ], 10, 2 );
17 + }
18 +
19 + public function time_scheduled_event( $schedules ) {
20 +
21 + if ( !isset($schedules['two_weeks']) ) {
22 + /*
23 + * https://developers.facebook.com/docs/instagram-basic-display-api/guides/long-lived-access-tokens/
24 + */
25 + $schedules[ 'two_weeks' ] = [
26 + 'interval' => WEEK_IN_SECONDS * 2,
27 + 'display' => 'Once in Two Weeks'
28 + ];
29 + }
30 +
31 + return $schedules;
32 + }
33 +
34 + public function schedule_token_refresh_event() {
35 + if ( ! wp_next_scheduled( 'getwid_refresh_instagram_token' ) ) {
36 + wp_schedule_event( time(), 'two_weeks', 'getwid_refresh_instagram_token' );
37 + }
38 + }
39 +
40 + public function update_option( $option_name, $old_value, $value ) {
41 + if ( $option_name === 'getwid_instagram_token' ) {
42 + delete_option( 'getwid_instagram_token_cron_error_message' );
43 +
44 + if ( $value === '' ) {
45 + $this->clear_scheduled_event();
46 + }
47 + }
48 + }
49 +
50 + public function filter_token_before_save( $value, $old_value ) {
51 +
52 + if ( $value !== '' ) {
53 + $encryption = new StringEncryption();
54 + $value = $encryption->encrypt( $value );
55 + }
56 +
57 + return $value;
58 + }
59 +
60 + public function clear_scheduled_event() {
61 + $timestamp = wp_next_scheduled( 'getwid_refresh_instagram_token' );
62 +
63 + if ( $timestamp ) {
64 + wp_unschedule_event( $timestamp, 'getwid_refresh_instagram_token' );
65 + }
66 + }
67 +
68 + public function refresh_instagram_token() {
69 + $encryption = new StringEncryption();
70 + $getwid_instagram_token = $encryption->decrypt( get_option( 'getwid_instagram_token', '' ) );
71 +
72 + if ( ! empty( $getwid_instagram_token ) ) {
73 + $api_req = 'https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&access_token=' . $getwid_instagram_token;
74 + $response = wp_remote_get( $api_req );
75 +
76 + if ( is_wp_error( $response ) ) {
77 + update_option( 'getwid_instagram_token_cron_error_message', $response->get_error_message() );
78 + } else {
79 + $response_body = json_decode( wp_remote_retrieve_body( $response ), false );
80 +
81 + if ( $response_body && json_last_error() === JSON_ERROR_NONE ) {
82 + if ( isset( $response_body->error ) ) {
83 + update_option( 'getwid_instagram_token_cron_error_message', $response_body->error->message );
84 + } else {
85 + delete_option( 'getwid_instagram_token_cron_error_message' );
86 +
87 + if ( ! empty( $response_body->access_token ) ) {
88 + // Update token
89 + update_option( 'getwid_instagram_token', $response_body->access_token );
90 + // Delete cache data
91 + delete_transient( 'getwid_instagram_response_data' );
92 + // Schedule token refresh
93 + $this->schedule_token_refresh_event();
94 + }
95 + }
96 + } else {
97 + update_option( 'getwid_instagram_token_cron_error_message', __( 'Error in json_decode.', 'getwid' ) );
98 + }
99 + }
100 + }
101 + }
102 +
103 + public function getwid_instagram_notice_token_error() {
104 + $instagram_token_error_message = get_option( 'getwid_instagram_token_cron_error_message' );
105 +
106 + if ( ! empty( $instagram_token_error_message ) ) {
107 + ?>
108 + <div class="notice notice-error">
109 + <p>
110 + <?php
111 + echo esc_html( sprintf(
112 + //translators: %s is an error message
113 + __( 'An error occurred while updating Instagram access token: %s', 'getwid' ),
114 + $instagram_token_error_message
115 + ) );
116 + ?>
117 + </p>
118 + </div>
119 + <?php
120 + }
121 + }
122 +
123 + public function error_message() {
124 + global $pagenow;
125 +
126 + $is_getwid_url = isset( $_GET['page'] ) && sanitize_text_field( wp_unslash( $_GET['page'] ) ) == 'getwid';
127 +
128 + $is_getwid_settings_page = $pagenow == 'options-general.php' && $is_getwid_url;
129 +
130 + if ( $is_getwid_settings_page && current_user_can( 'manage_options' ) ) {
131 + if ( get_option( 'getwid_instagram_token_cron_error_message' ) !== '' ) {
132 + add_action( 'admin_notices', [ $this, 'getwid_instagram_notice_token_error' ] );
133 + }
134 + }
135 + }
136 +}