Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add options fallback #12

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion assets/js/admin-ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ jQuery(document).ready(function ($) {
$('.give-aweber-list-wrap').empty().append(res.data.lists);
}

refresh_button.hide();
//refresh_button.hide();
spinner.removeClass('is-active');
}
},
error: function (res) {
console.error({aweberListErrorResponse: res});
}
});
});
Expand Down
75 changes: 69 additions & 6 deletions give-aweber.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/

//Define constants.
use Give\Log\Log;

if ( ! defined( 'GIVE_AWEBER_VERSION' ) ) {
define( 'GIVE_AWEBER_VERSION', '1.0.4' );
}
Expand Down Expand Up @@ -442,6 +444,8 @@ public function render_metabox() {
$override_option = get_post_meta( $post->ID, '_give_' . $this->id . '_override_option', true );
$checked_option = get_post_meta( $post->ID, '_give_' . $this->id . '_checked_default', true );



//Start the buffer.
ob_start(); ?>

Expand Down Expand Up @@ -625,6 +629,9 @@ public function save_metabox( $post_id ) {
public function get_lists() {

$lists_data = get_transient( 'give_aweber_lists' );
$lists_not_found = array(
'unknown' => __( 'No lists found', 'give-aweber' )
);

if ( false === $lists_data ) {

Expand All @@ -633,7 +640,7 @@ public function get_lists() {
$aweber = $this->get_authenticated_instance();

if ( ! is_object( $aweber ) || false === ( $secrets = get_option( 'give_aweber_secrets' ) ) ) {
return array();
return $lists_not_found;
}

$account = $aweber->getAccount( $secrets['access_key'], $secrets['access_secret'] );
Expand All @@ -648,7 +655,7 @@ public function get_lists() {

} catch ( Exception $e ) {

$this->lists = array();
$this->lists = $lists_not_found;

}

Expand Down Expand Up @@ -781,7 +788,8 @@ public function show_subscribe_checkbox( $form_id ) {
*/
public function get_authenticated_instance() {

$authorization_code = isset( $this->give_options['give_aweber_api'] ) ? trim( $this->give_options['give_aweber_api'] ) : '';
//$authorization_code = isset( $this->give_options['give_aweber_api'] ) ? trim( $this->give_options['give_aweber_api'] ) : '';
$authorization_code = give_get_option('give_aweber_api', '');

$msg = '';
if ( ! empty( $authorization_code ) ) {
Expand All @@ -798,14 +806,26 @@ public function get_authenticated_instance() {
$msg = $options;

try {
if (!$this->validate_auth_code()){
throw new \RuntimeException('AWeber API Error: Authorization code does not match stored credentials.');
}

$api = new AWeberAPI( $options['consumer_key'], $options['consumer_secret'] );

} catch ( AWeberAPIException $exc ) {

$api = false;

}
} catch ( RuntimeException $exc ) {
Log::error( 'AWeber API Error (Exception): ' . $exc->getMessage() );

delete_transient( 'give_aweber_lists' );
give_delete_option('give_aweber_list');
delete_option( 'give_aweber_secrets' );

$api = false;

}

return $api;

Expand All @@ -817,6 +837,8 @@ public function get_authenticated_instance() {

} catch ( AWeberAPIException $exc ) {

Log::error( 'AWeber API Error (AWeberAPIException): ' . $exc->getMessage() );

list( $consumer_key, $consumer_secret, $access_key, $access_secret ) = null;

//make error messages customer friendly.
Expand All @@ -826,10 +848,12 @@ public function get_authenticated_instance() {
$error_code = " ($descr)";

} catch ( AWeberOAuthDataMissing $exc ) {
Log::error( 'AWeber API Error (AWeberOAuthDataMissing): ' . $exc->getMessage() );

list( $consumer_key, $consumer_secret, $access_key, $access_secret ) = null;

} catch ( AWeberException $exc ) {
Log::error( 'AWeber API Error (AWeberException): ' . $exc->getMessage() );

list( $consumer_key, $consumer_secret, $access_key, $access_secret ) = null;

Expand All @@ -848,7 +872,7 @@ public function get_authenticated_instance() {
$msg .= __( 'Authorization code entered was:', 'give-aweber' ) . '<br />' . $authorization_code;
}

$msg .= __( 'Please make sure you entered the complete authorization code and try again.', 'give-aweber' );
$msg .= '<br />' . __( 'Please make sure you entered the complete authorization code and try again.', 'give-aweber' );

$msg .= '</div>';

Expand All @@ -865,6 +889,8 @@ public function get_authenticated_instance() {
}
}
} else {
delete_transient( 'give_aweber_lists' );
give_delete_option('give_aweber_list');
delete_option( 'give_aweber_secrets' );
}

Expand All @@ -881,8 +907,8 @@ public function get_authenticated_instance() {
* @param $value
*/
public function aweber_list_select( $field, $value ) {

$lists = $this->get_lists();
$give_aweber_response = get_option('give_aweber_response');

ob_start();
?>
Expand All @@ -893,6 +919,9 @@ public function aweber_list_select( $field, $value ) {
</label>
</th>
<td scope="row" class="">
<?php if ( empty( give_get_option('give_aweber_api') ) ) : ?>
<p><?php echo __( 'Please enter your AWeber Authorization Code in the settings tab above.', 'give-aweber' ); ?></p>
<?php else: ?>
<select class="give-select give-aweber-list-select" name="<?php echo "{$field['id']}"; ?>"
id="<?php echo "{$field['id']}"; ?>">
<?php echo $this->get_list_options( $lists, $value ); ?>
Expand All @@ -904,6 +933,12 @@ public function aweber_list_select( $field, $value ) {
<span class="give-spinner spinner"></span>

<p class="give-field-description"><?php echo "{$field['desc']}"; ?></p>
<?php if ( ! empty( $give_aweber_response ) ) : ?>
<div class="give-aweber-response">
<?php echo $give_aweber_response; ?>
</div>
<?php endif; ?>
<?php endif; ?>
</td>
</tr>
<?php
Expand Down Expand Up @@ -1087,6 +1122,34 @@ public function get_environment_warning() {
return $is_working;
}

/**
* @unreleased
* @return bool
*/
public function validate_auth_code(){
if ( ! class_exists( 'AWeberAPI' ) ) {
require_once( GIVE_AWEBER_PATH . '/aweber/aweber_api.php' );
}

$authorization_code = give_get_option('give_aweber_api', '');
$options = get_option( 'give_aweber_secrets' );

if (empty($options) || empty($authorization_code)){
return false;
}

try {
list($consumer_key, $consumer_secret) = AWeberAPI::getDataFromAweberID(
$authorization_code
);

return !($consumer_secret !== $options['consumer_secret'] || $consumer_key !== $options['consumer_key']);
} catch (Exception $exc) {
Log::error('AWeber API Error (Exception): ' . $exc->getMessage());
return false;
}
}

}

/**
Expand Down