Skip to content
Merged
3 changes: 1 addition & 2 deletions admin/class-convertkit-mm-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ private function check_credentials() {
// Remove from settings.
$this->settings->delete_credentials();

// Redirect to General screen, which will now show the ConvertKit_Settings_OAuth screen, because
// the Plugin has no access token.
// Reload settings screen, to reflect no credentials exist.
wp_safe_redirect(
add_query_arg(
array(
Expand Down
1 change: 1 addition & 0 deletions convertkit-membermouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@

// Load plugin files.
require CONVERTKIT_MM_PATH . 'includes/class-convertkit-mm-actions.php';
require CONVERTKIT_MM_PATH . 'includes/class-convertkit-mm-admin-notices.php';
require CONVERTKIT_MM_PATH . 'includes/class-convertkit-mm-api.php';
require CONVERTKIT_MM_PATH . 'includes/class-convertkit-mm-resource.php';
require CONVERTKIT_MM_PATH . 'includes/class-convertkit-mm-resource-custom-fields.php';
Expand Down
196 changes: 196 additions & 0 deletions includes/class-convertkit-mm-admin-notices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<?php
/**
* ConvertKit MemberMouse Admin Notices class.
*
* @package ConvertKit_MM
* @author Kit
*/

/**
* Add and remove persistent error messages across all
* WordPress Administration screens.
*
* @package ConvertKit_MM
* @author Kit
*/
class ConvertKit_MM_Admin_Notices {

/**
* The key prefix to use for stored notices
*
* @since 1.3.7
*
* @var string
*/
private $key_prefix = 'convertkit-mm-admin-notices';

/**
* Register output function to display persistent notices
* in the WordPress Administration, if any exist.
*
* @since 1.3.7
*/
public function __construct() {

add_action( 'admin_notices', array( $this, 'output' ) );

}

/**
* Output persistent notices in the WordPress Administration
*
* @since 1.3.7
*/
public function output() {

// Don't output if we don't have the required capabilities to fix the issue.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}

// Bail if no notices exist.
$notices = get_option( $this->key_prefix );
if ( ! $notices ) {
return;
}

// Output notices.
foreach ( $notices as $notice ) {
switch ( $notice ) {
case 'authorization_failed':
$api = new ConvertKit_MM_API( CONVERTKIT_MM_OAUTH_CLIENT_ID, CONVERTKIT_MM_OAUTH_CLIENT_REDIRECT_URI );
$output = sprintf(
'%s %s',
esc_html__( 'Kit for MemberMouse: Authorization failed. Please', 'convertkit-mm' ),
sprintf(
'<a href="%s">%s</a>',
esc_url( $api->get_oauth_url( admin_url( 'options-general.php?page=convertkit-mm' ), get_site_url() ) ),
esc_html__( 'connect your Kit account.', 'convertkit-mm' )
)
);
break;

default:
$output = '';

/**
* Define the text to output in an admin error notice.
*
* @since 1.3.7
*
* @param string $notice Admin notice name.
*/
$output = apply_filters( CONVERTKIT_MM_NAME . '_admin_notices_output_' . $notice, $output );
break;
}

// If no output defined, skip.
if ( empty( $output ) ) {
continue;
}
?>
<div class="notice notice-error">
<p>
<?php
echo wp_kses(
$output,
wp_kses_allowed_html( 'post' )
);
?>
</p>
</div>
<?php
}

}

/**
* Add a persistent notice for output in the WordPress Administration.
*
* @since 1.3.7
*
* @param string $notice Notice name.
* @return bool Notice saved successfully
*/
public function add( $notice ) {

// If no other persistent notices exist, add one now.
if ( ! $this->exist() ) {
return update_option( $this->key_prefix, array( $notice ) );
}

// Fetch existing persistent notices.
$notices = $this->get();

// Add notice to existing notices.
$notices[] = $notice;

// Remove any duplicate notices.
$notices = array_values( array_unique( $notices ) );

// Update and return.
return update_option( $this->key_prefix, $notices );

}

/**
* Returns all notices stored in the options table.
*
* @since 1.3.7
*
* @return array
*/
public function get() {

// Fetch all notices from the options table.
return get_option( $this->key_prefix );

}

/**
* Whether any persistent notices are stored in the option table.
*
* @since 1.3.7
*
* @return bool
*/
public function exist() {

if ( ! $this->get() ) {
return false;
}

return true;

}

/**
* Delete all persistent notices.
*
* @since 1.3.7
*
* @param string $notice Notice name.
* @return bool Success
*/
public function delete( $notice ) {

// If no persistent notices exist, there's nothing to delete.
if ( ! $this->exist() ) {
return false;
}

// Fetch existing persistent notices.
$notices = $this->get();

// Remove notice from existing notices.
$index = array_search( $notice, $notices, true );
if ( $index !== false ) {
unset( $notices[ $index ] );
}

// Update and return.
return update_option( $this->key_prefix, $notices );

}

}
8 changes: 4 additions & 4 deletions includes/class-convertkit-mm-api.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
/**
* ConvertKit API class for WPForms.
* ConvertKit API class for MemberMouse.
*
* @package ConvertKit_WPForms
* @package ConvertKit_MM
* @author ConvertKit
*/

/**
* ConvertKit API class for WPForms.
* ConvertKit API class for MemberMouse.
*
* @package ConvertKit_WPForms
* @package ConvertKit_MM
* @author ConvertKit
*/
class ConvertKit_MM_API extends ConvertKit_API_V4 {
Expand Down
2 changes: 1 addition & 1 deletion includes/class-convertkit-mm-resource-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* ConvertKit Tags Resource class.
*
* @package ConvertKit_WPForms
* @package ConvertKit_MM
* @author ConvertKit
*/

Expand Down
30 changes: 28 additions & 2 deletions includes/class-convertkit-mm-resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,34 @@ public function __construct() {
);
}

// Call parent initialization function.
parent::init();
// Get last query time and existing resources.
$this->last_queried = get_option( $this->settings_name . '_last_queried' );
$this->resources = get_option( $this->settings_name );

}

/**
* Fetches resources (custom fields, forms, sequences or tags) from the API, storing them in the options table
* with a last queried timestamp.
*
* If the refresh results in a 401, removes the access and refresh tokens from the settings.
*
* @since 1.3.7
*
* @return WP_Error|array
*/
public function refresh() {

// Call parent refresh method.
$result = parent::refresh();

// If an error occured, maybe delete credentials from the Plugin's settings
// if the error is a 401 unauthorized.
if ( is_wp_error( $result ) ) {
convertkit_mm_maybe_delete_credentials( $result );
}

return $result;

}

Expand Down
46 changes: 33 additions & 13 deletions includes/class-convertkit-mm-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ public function __construct() {
$this->settings = array_merge( $this->get_defaults(), $settings );
}

// Update Access Token when refreshed by the API class.
add_action( 'convertkit_api_get_access_token', array( $this, 'update_credentials' ), 10, 2 );
add_action( 'convertkit_api_refresh_token', array( $this, 'update_credentials' ), 10, 2 );

}

/**
Expand Down Expand Up @@ -122,6 +118,9 @@ public function has_api_key() {
*/
public function get_access_token() {

// Reload settings from options table, to ensure we have the latest tokens.
$this->refresh_settings();

// Return Access Token from settings.
return $this->settings['access_token'];

Expand Down Expand Up @@ -149,6 +148,9 @@ public function has_access_token() {
*/
public function get_refresh_token() {

// Reload settings from options table, to ensure we have the latest tokens.
$this->refresh_settings();

// Return Refresh Token from settings.
return $this->settings['refresh_token'];

Expand Down Expand Up @@ -287,16 +289,13 @@ public function get_bundle_cancellation_mapping( $id ) {
*
* @since 1.3.0
*
* @param array $result New Access Token, Refresh Token and Expiry.
* @param string $client_id OAuth Client ID used for the Access and Refresh Tokens.
* @param array $result New Access Token, Refresh Token and Expiry.
*/
public function update_credentials( $result, $client_id ) {
public function update_credentials( $result ) {

// Don't save these credentials if they're not for this Client ID.
// They're for another ConvertKit Plugin that uses OAuth.
if ( $client_id !== CONVERTKIT_MM_OAUTH_CLIENT_ID ) {
return;
}
// Remove any existing persistent notice.
$admin_notices = new ConvertKit_MM_Admin_Notices();
$admin_notices->delete( 'authorization_failed' );

$this->save(
array(
Expand Down Expand Up @@ -329,6 +328,9 @@ public function delete_credentials() {
)
);

// Clear any existing scheduled WordPress Cron event.
wp_clear_scheduled_hook( 'convertkit_mm_refresh_token' );

}

/**
Expand Down Expand Up @@ -410,7 +412,25 @@ public function save( $settings ) {
update_option( self::SETTINGS_NAME, array_merge( $this->get(), $settings ) );

// Reload settings in class, to reflect changes.
$this->settings = get_option( self::SETTINGS_NAME );
$this->refresh_settings();

}

/**
* Reloads settings from the options table so this instance has the latest values.
*
* @since 1.3.7
*/
private function refresh_settings() {

$settings = get_option( self::SETTINGS_NAME );

if ( ! $settings ) {
$this->settings = $this->get_defaults();
return;
}

$this->settings = array_merge( $this->get_defaults(), $settings );

}

Expand Down
5 changes: 3 additions & 2 deletions includes/class-convertkit-mm.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ConvertKit_MM {
public function __construct() {

// Initialize.
add_action( 'init', array( $this, 'init' ) );
add_action( 'init', array( $this, 'init' ), 1 );

}

Expand Down Expand Up @@ -85,7 +85,8 @@ private function initialize_admin() {
return;
}

$this->classes['admin'] = new ConvertKit_MM_Admin();
$this->classes['admin'] = new ConvertKit_MM_Admin();
$this->classes['admin_notices'] = new ConvertKit_MM_Admin_Notices();

/**
* Initialize integration classes for the WordPress Administration interface.
Expand Down
Loading