| @@ -1,136 +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 | - 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 | -} | |
| 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 | +} | |