Skip to content

Commit

Permalink
2.7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
devuri committed Jan 25, 2021
1 parent 10ec456 commit ff2712c
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 56 deletions.
16 changes: 9 additions & 7 deletions src/Admin/pages/social-feed-settings/account-setup.admin.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

// @codingStandardsIgnoreFile TODO fix phpcs
use SimSocialFeed\InstagramData;

// @codingStandardsIgnoreFile TODO fix phpcs

/**
* Update the Token
Expand Down Expand Up @@ -40,19 +42,19 @@
}

// update user info
$igsf_profile = SimSocialFeed\InstagramData::user_profile();
$igsf_profile = InstagramData::user_profile();
update_option('simsf_user', $igsf_profile);
echo $this->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. <br> Configuration data is missing or incorrect <br> Please check your user token !!!', 'error');
}

Expand Down Expand Up @@ -126,7 +128,7 @@


?></form>
<?php if ( SimSocialFeed\InstagramData::user_check() ): ?>
<?php if ( InstagramData::user_check() ): ?>
The Following Account is Active:
<br>
<strong><?php echo get_option('simsf_user')['id']; ?></strong>
Expand All @@ -135,7 +137,7 @@
<?php endif;

# check the user info
if ( ! SimSocialFeed\InstagramData::user_check() ) {
if ( ! InstagramData::user_check() ) {
echo $this->form()->user_feedback('Please Activate User ID. <br> Configuration data is missing or incorrect !!!', 'error');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php
// @codingStandardsIgnoreFile TODO fix phpcs

use SimSocialFeed\InstagramData;

// @codingStandardsIgnoreFile TODO fix phpcs

// Save Data
if ( isset( $_POST['ig_token_update'] ) ) :
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php
// @codingStandardsIgnoreFile TODO fix phpcs

use SimSocialFeed\InstagramData;
use SimSocialFeed\InstagramFeed;

// @codingStandardsIgnoreFile TODO fix phpcs

// Save Data
if ( isset( $_POST['refresh_instagram_feed'] ) ) :
Expand All @@ -12,13 +16,13 @@
}

# SOMETHING WENT WRONG WITH THE REQUEST
if ( ! SimSocialFeed\InstagramData::is_request_ok() ) :
echo $this->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;
Expand All @@ -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 {
Expand All @@ -53,7 +57,7 @@
* get images
*/
echo '<hr/>';
SimSocialFeed\InstagramFeed::admin_view();
InstagramFeed::admin_view();
echo '<hr/>';

?></form>
Expand Down
134 changes: 93 additions & 41 deletions src/InstagramData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) {

Expand All @@ -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 ) {
Expand All @@ -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 );
}
}

/**
Expand All @@ -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;
}

Expand Down

0 comments on commit ff2712c

Please sign in to comment.