diff --git a/src/Admin/pages/social-feed-settings/account-setup.admin.php b/src/Admin/pages/social-feed-settings/account-setup.admin.php index b84f501..511be74 100644 --- a/src/Admin/pages/social-feed-settings/account-setup.admin.php +++ b/src/Admin/pages/social-feed-settings/account-setup.admin.php @@ -1,6 +1,8 @@ form()->user_feedback('User Info Has Been Updated !!!'); // Update user data. - if ( SimSocialFeed\InstagramData::is_request_ok() ) : - $ig_user_media = SimSocialFeed\InstagramData::user_media(); + if ( InstagramData::is_request_ok() ) : + $ig_user_media = InstagramData::user_media(); update_option( 'simsf_user_media', $ig_user_media->data ); echo $this->form()->user_feedback( 'Instagram Feed Has Been Updated !!!' ); endif; # check the user info - if ( ! SimSocialFeed\InstagramData::user_check() ) { + if ( ! InstagramData::user_check() ) { echo $this->form()->user_feedback('User Activation Failed.
Configuration data is missing or incorrect
Please check your user token !!!', 'error'); } @@ -126,7 +128,7 @@ ?> - + The Following Account is Active:
@@ -135,7 +137,7 @@ form()->user_feedback('Please Activate User ID.
Configuration data is missing or incorrect !!!', 'error'); } diff --git a/src/Admin/pages/social-feed-settings/refresh-access-token.admin.php b/src/Admin/pages/social-feed-settings/refresh-access-token.admin.php index 2da480f..0c5d46c 100644 --- a/src/Admin/pages/social-feed-settings/refresh-access-token.admin.php +++ b/src/Admin/pages/social-feed-settings/refresh-access-token.admin.php @@ -1,5 +1,8 @@ form()->user_feedback(SimSocialFeed\InstagramData::error_message(), 'error'); + if ( ! InstagramData::is_request_ok() ) : + echo $this->form()->user_feedback( InstagramData::error_message(), 'error'); endif; # UPDATE USER MEDIA - if ( SimSocialFeed\InstagramData::is_request_ok() ) : - $ig_user_media = SimSocialFeed\InstagramData::user_media(); + if ( InstagramData::is_request_ok() ) : + $ig_user_media = InstagramData::user_media(); update_option('simsf_user_media', $ig_user_media->data ); echo $this->form()->user_feedback('IG Feed Has Been Updated !!!'); endif; @@ -42,7 +46,7 @@ /** * only show if we have valid user */ - if ( SimSocialFeed\InstagramData::user_check() ) { + if ( InstagramData::user_check() ) { # submit button echo $this->form()->submit_button('Refresh Instagram Feed', 'primary large', 'refresh_instagram_feed'); } else { @@ -53,7 +57,7 @@ * get images */ echo '
'; - SimSocialFeed\InstagramFeed::admin_view(); + InstagramFeed::admin_view(); echo '
'; ?> diff --git a/src/InstagramData.php b/src/InstagramData.php index 90f478e..e73ff0a 100644 --- a/src/InstagramData.php +++ b/src/InstagramData.php @@ -122,10 +122,29 @@ public function expires( $expires_in = 5184000 ) { return $expires_date; } + /** + * lets only refresh after 24 hours. + */ + public static function is_24_hours_since() { + + if ( self::has_refresh() ) { + + $created_date = get_option( 'simsf_access_token' )['created_at']; + + $next_refresh_can = strtotime('+1 day', $created_date ); + + // check time since last refresh. + if ( time() >= $next_refresh_can ) { + return true; + } + return false; + } + } + /** * lets only refresh after 48 hours. */ - public static function is_48_hours_since_refresh() { + public static function is_48_hours_since() { if ( self::has_refresh() ) { @@ -142,15 +161,15 @@ public static function is_48_hours_since_refresh() { } /** - * 40 Days since last refresh. + * 30 Days since last refresh. */ - public static function is_40_days_since_refresh() { + public static function is_30_days_since() { if ( self::has_refresh() ) { $created_date = get_option( 'simsf_access_token' )['created_at']; - $next_refresh = $created_date + 40 * 24 * 3600; + $next_refresh = strtotime('+30 day', $created_date ); // check time since last refresh. if ( time() >= $next_refresh ) { @@ -161,60 +180,53 @@ public static function is_40_days_since_refresh() { } /** - * Automatic token refresh. + * 40 Days since last refresh. */ - public static function maybe_refresh_token() { + public static function is_40_days_since() { - if ( self::is_40_days_since_refresh() ) { + if ( self::has_refresh() ) { - self::refresh_token(); + $created_date = get_option( 'simsf_access_token' )['created_at']; + + $next_refresh = $created_date + 40 * 24 * 3600; - // notify admin user. - $admin_user = get_option( 'admin_email' ); - $subject = 'Re: Access Token update ' . wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); - $message = self::notification(); - wp_mail( $admin_user, $subject, $message ); + // check time since last refresh. + if ( time() >= $next_refresh ) { + return true; + } + return false; } } /** - * Notification for Access Token update. - * @return string + * 45 Days since last refresh. */ - public static function notification() { - - $message_text = __( - 'Hi, + public static function is_45_days_since() { - This notification confirms that your Instagram User Access Token Has Been Updated on ###SITENAME###. - - Token was updated: ###CREATED###, Token will expire: ###EXPIRES###. + if ( self::has_refresh() ) { - Refreshed tokens are valid for 60 days from the date at which they are refreshed. - The Sim Social Feed plugin will automatically refresh your Access Token before it expires. + $created_date = get_option( 'simsf_access_token' )['created_at']; - This email has been sent to ###ADMIN_EMAIL###. + $next_refresh = strtotime('+45 day', $created_date ); - Regards, - [Sim Social Feed] plugin Notification. + // check time since last refresh. + if ( time() >= $next_refresh ) { + return true; + } + return false; + } + } - You have received this email notification to update you about your ###SITENAME### website. - Sent from: ###SITEURL###' - ); + /** + * Automatic token refresh. + */ + public static function maybe_refresh_token() { - // message vars. - $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); - $token_created = self::token_created_date(); - $token_will_expire = self::token_expire_date(); + if ( self::is_45_days_since() ) { - // message - $message_text = str_replace( '###SITENAME###', $blog_name, $message_text ); - $message_text = str_replace( '###CREATED###', $token_created, $message_text ); - $message_text = str_replace( '###EXPIRES###', $token_will_expire, $message_text ); - $message_text = str_replace( '###ADMIN_EMAIL###', get_option( 'admin_email' ), $message_text ); - $message_text = str_replace( '###SITEURL###', home_url(), $message_text ); + self::refresh_token(); - return nl2br( $message_text ); + } } /** @@ -236,6 +248,46 @@ public static function refresh_token() { $user_token['expire_date'] = time() + $user_token['expires_in']; $user_token['created_at'] = time(); $user_token['refresh'] = true; + + /** + * Notify admin. + */ + $admin_user = get_option( 'admin_email' ); + $subject = 'Re: Access Token update ' . wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); + + // email message. + $message = __( + 'Hi, + + This notification confirms that your Instagram User Access Token Has Been Updated on ###SITENAME###. + + Token was updated: ###CREATED###, Token will expire: ###EXPIRES###. + + Refreshed tokens are valid for 60 days from the date at which they are refreshed. + The Sim Social Feed plugin will automatically refresh your Access Token before it expires. + + This email has been sent to ###ADMIN_EMAIL###. + + Regards, + [Sim Social Feed] plugin Notification. + + You have received this email notification to update you about your ###SITENAME### website.' + ); + + // message vars. + $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); + $token_created = date_i18n( get_option( 'date_format' ), $user_token['created_at'] ); + $token_will_expire = date_i18n( get_option( 'date_format' ), $user_token['expire_date'] ); + + // message. + $message = str_replace( '###SITENAME###', $blog_name, $message ); + $message = str_replace( '###CREATED###', $token_created, $message ); + $message = str_replace( '###EXPIRES###', $token_will_expire, $message ); + $message = str_replace( '###ADMIN_EMAIL###', get_option( 'admin_email' ), $message ); + + // send email. + wp_mail( $admin_user, $subject, $message ); + return $user_token; }