Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonTheAdams committed Nov 6, 2020
2 parents caf1b5f + 4beb988 commit 9632154
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 31 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

## 1.0.1 - 2020-11-06

### Fixed
- ActiveCampaign should only query the API for lists and tags when viewing wp-admin pages containing those fields. [#3]
4 changes: 2 additions & 2 deletions give-activecampaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: Easily display an ActiveCampaign opt-in option within your donation forms.
* Author: GiveWP
* Author URI: https://givewp.com/
* Version: 1.0.0
* Version: 1.0.1
* Text Domain: give-activecampaign
* Domain Path: languages
*/
Expand All @@ -17,7 +17,7 @@

// Plugin constants.
if ( ! defined( 'GIVE_ACTIVECAMPAIGN_VERSION' ) ) {
define( 'GIVE_ACTIVECAMPAIGN_VERSION', '1.0.0' );
define( 'GIVE_ACTIVECAMPAIGN_VERSION', '1.0.1' );
}
if ( ! defined( 'GIVE_ACTIVECAMPAIGN_MIN_GIVE_VER' ) ) {
define( 'GIVE_ACTIVECAMPAIGN_MIN_GIVE_VER', '2.7.0' );
Expand Down
83 changes: 54 additions & 29 deletions includes/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@
/**
* Get the lists from ActiveCampaign
*
* @return array
* @return array|bool
*/
function give_get_activecampaign_lists() {

if ( ! give_activecampaign_should_load_api_fields() ) {
return false;
}

$api_url = give_get_option( 'give_activecampaign_apiurl', false );
$api_key = give_get_option( 'give_activecampaign_api', false );

if ( ! $api_url || ! $api_key ) {
return array();
return [];
}

$ac = new ActiveCampaign( $api_url, $api_key );

$lists = $ac->api( 'list/list', array( 'ids' => 'all' ) );
$lists = $ac->api( 'list/list', [ 'ids' => 'all' ] );

if ( (int) $lists->success ) {

// We need to cast the object to an array because ActiveCampaign returns invalid JSON.
$lists = (array) $lists;

$output = array();
$output = [];

foreach ( $lists as $key => $list ) {
if ( ! is_numeric( $key ) ) {
Expand All @@ -35,33 +39,37 @@ function give_get_activecampaign_lists() {

return $output;
} else {
return array();
return [];
}
}

/**
* Pull tags into
*
* @return array
* @return array|bool
*/
function give_get_activecampaign_tags() {

if ( ! give_activecampaign_should_load_api_fields() ) {
return false;
}

$api_url = give_get_option( 'give_activecampaign_apiurl', false );
$api_key = give_get_option( 'give_activecampaign_api', false );

if ( ! $api_url || ! $api_key ) {
return array();
return [];
}

$ac = new ActiveCampaign( $api_url, $api_key );

$tags = $ac->api( 'tags/list', array( 'ids' => 'all' ) );
$tags = $ac->api( 'tags/list', [ 'ids' => 'all' ] );

$tags = json_decode( $tags, true );

if ( ! empty( $tags ) ) {

$output = array();
$output = [];

foreach ( $tags as $key => $tag ) {
if ( ! is_numeric( $key ) ) {
Expand All @@ -74,11 +82,28 @@ function give_get_activecampaign_tags() {
return $output;

} else {
return array();
return [];
}

}

/**
* Should the API fields load in
*
* @since 1.0.1
* @return bool
*/
function give_activecampaign_should_load_api_fields() {
$post = isset( $_GET['post'] ) ? $_GET['post'] : false;
$post = $post ? get_post( $post ) : false;

$is_settings_screen = ( isset( $_GET['tab'] ) && 'activecampaign' === $_GET['tab'] );
$is_form_edit_screen = ( $post && 'give_forms' === $post->post_type );

// This function only runs when viewing the option on the settings screen or metabox.
return $is_settings_screen || $is_form_edit_screen;
}

/**
*
* Display the opt-in checkbox oon donation forms.
Expand All @@ -104,23 +129,23 @@ function give_activecampaign_display_optin( $form_id ) {
// Is label and checked by default customized per form?
if ( 'customized' === $form_display_option ) {
$optin_label = give_get_meta( $form_id, 'give_activecampaign_label', true );
$checked = give_get_meta( $form_id, 'give_activecampaign_checkbox_default', true );
$checked = give_get_meta( $form_id, 'give_activecampaign_checkbox_default', true );
} else {
$optin_label = give_get_option( 'give_activecampaign_label', esc_html__( 'Subscribe to our newsletter', 'give-activecampaign' ) );
$checked = give_get_option( 'give_activecampaign_checkbox_default', false );
$checked = give_get_option( 'give_activecampaign_checkbox_default', false );
}

ob_start(); ?>
<fieldset id="give_activecampaign_<?php echo $form_id; ?>" class="give-activecampaign-fieldset">
<label for="give_activecampaign_<?php echo $form_id; ?>_signup" class="give-activecampaign-optin-label">
<input name="give_activecampaign_signup"
class="give-activecampaign-optin-input"
id="give_activecampaign_<?php echo $form_id; ?>_signup"
type="checkbox" <?php echo( 'on' === $checked ? 'checked="checked"' : '' ); ?>/>
<span class="give-activecampaign-message-text"><?php echo $optin_label; ?></span>
</label>

</fieldset>
<fieldset id="give_activecampaign_<?php echo $form_id; ?>" class="give-activecampaign-fieldset">
<label for="give_activecampaign_<?php echo $form_id; ?>_signup" class="give-activecampaign-optin-label">
<input name="give_activecampaign_signup"
class="give-activecampaign-optin-input"
id="give_activecampaign_<?php echo $form_id; ?>_signup"
type="checkbox" <?php echo( 'on' === $checked ? 'checked="checked"' : '' ); ?>/>
<span class="give-activecampaign-message-text"><?php echo $optin_label; ?></span>
</label>

</fieldset>
<?php
echo ob_get_clean();
}
Expand All @@ -138,12 +163,12 @@ function give_activecampaign_donation_metabox_notification( $payment_id ) {
$opt_in_meta = give_get_meta( $payment_id, '_give_activecampaign_donation_optin_status', false );

if ( $opt_in_meta ) { ?>
<div class="give-admin-box-inside">
<p>
<span class="label"><?php _e( 'ActiveCampaign', 'give-activecampaign' ); ?>:</span>&nbsp;
<span><?php _e( 'Opted-in', 'give-activecampaign' ); ?></span>
</p>
</div>
<div class="give-admin-box-inside">
<p>
<span class="label"><?php _e( 'ActiveCampaign', 'give-activecampaign' ); ?>:</span>&nbsp;
<span><?php _e( 'Opted-in', 'give-activecampaign' ); ?></span>
</p>
</div>
<?php }

}
Expand All @@ -156,7 +181,7 @@ function give_activecampaign_donation_metabox_notification( $payment_id ) {
*/
function give_activecampaign_enqueue_admin_scripts() {
if ( give_is_admin_page() ) {
wp_register_script( 'give-activecampaign-admin', GIVE_ACTIVECAMPAIGN_URL . 'assets/js/give-activecampaign-admin.js', array( 'give-admin-scripts' ),
wp_register_script( 'give-activecampaign-admin', GIVE_ACTIVECAMPAIGN_URL . 'assets/js/give-activecampaign-admin.js', [ 'give-admin-scripts' ],
GIVE_ACTIVECAMPAIGN_VERSION, false );
wp_enqueue_script( 'give-activecampaign-admin' );
}
Expand Down
3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@ Automatic updates should work like a charm; as always though, ensure you backup

== Changelog ==

= 1.0.1: November 6th, 2020 =
* Fix: Reduce calls to Active Campaign to only when necessary

= 1.0.0: August 11th, 2020 =
* Initial plugin release. Yippee!

0 comments on commit 9632154

Please sign in to comment.