From 38e19b7c10dc4e01a1083370ca87621d42f5b07b Mon Sep 17 00:00:00 2001 From: Devin Walker Date: Wed, 14 Oct 2020 13:55:53 -0700 Subject: [PATCH 1/5] fix: add condition before requesting API --- includes/helpers.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/includes/helpers.php b/includes/helpers.php index 57c8da9..0b674d9 100644 --- a/includes/helpers.php +++ b/includes/helpers.php @@ -3,10 +3,23 @@ /** * Get the lists from ActiveCampaign * - * @return array + * @return array|bool */ function give_get_activecampaign_lists() { + global $post; + + $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. + if ( ! $is_settings_screen && ! $is_form_edit_screen ) { + return false; + } + $api_url = give_get_option( 'give_activecampaign_apiurl', false ); $api_key = give_get_option( 'give_activecampaign_api', false ); @@ -104,10 +117,10 @@ 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(); ?> @@ -116,7 +129,7 @@ function give_activecampaign_display_optin( $form_id ) { /> + type="checkbox" /> From 5e182fb1c00d04567c00929e0d158658a16c568a Mon Sep 17 00:00:00 2001 From: Devin Walker Date: Wed, 14 Oct 2020 19:06:20 -0700 Subject: [PATCH 2/5] add new give_activecampaign_should_load_api_fields() function to determine when to load API fields --- includes/helpers.php | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/includes/helpers.php b/includes/helpers.php index 0b674d9..9a9b21f 100644 --- a/includes/helpers.php +++ b/includes/helpers.php @@ -7,16 +7,7 @@ */ function give_get_activecampaign_lists() { - global $post; - - $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. - if ( ! $is_settings_screen && ! $is_form_edit_screen ) { + if ( ! give_activecampaign_should_load_api_fields() ) { return false; } @@ -55,10 +46,14 @@ function give_get_activecampaign_lists() { /** * 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 ); @@ -92,6 +87,29 @@ function give_get_activecampaign_tags() { } +/** + * Should the API fields load in + * + * @return false + * @since 1.0.1 + */ +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. + if ( $is_settings_screen || $is_form_edit_screen ) { + return true; + } + + return false; + +} + /** * * Display the opt-in checkbox oon donation forms. From 3e519fe0e122a150f441474a3ae85eb938900c58 Mon Sep 17 00:00:00 2001 From: Devin Walker Date: Wed, 14 Oct 2020 19:10:36 -0700 Subject: [PATCH 3/5] chore: add change log --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..a34cadf --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +# 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] + +### Fixed +- ActiveCampaign should only query the API for lists and tags when viewing wp-admin pages containing those fields. [#3] From 51abf51c4c9e026dd060db8fe828a55b60e892d0 Mon Sep 17 00:00:00 2001 From: Jason Adams Date: Thu, 15 Oct 2020 15:56:06 -0700 Subject: [PATCH 4/5] refactor: minor cleanup --- includes/helpers.php | 60 ++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/includes/helpers.php b/includes/helpers.php index 9a9b21f..5961784 100644 --- a/includes/helpers.php +++ b/includes/helpers.php @@ -15,19 +15,19 @@ function give_get_activecampaign_lists() { $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 ) ) { @@ -39,7 +39,7 @@ function give_get_activecampaign_lists() { return $output; } else { - return array(); + return []; } } @@ -58,18 +58,18 @@ function give_get_activecampaign_tags() { $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 ) ) { @@ -82,7 +82,7 @@ function give_get_activecampaign_tags() { return $output; } else { - return array(); + return []; } } @@ -90,11 +90,10 @@ function give_get_activecampaign_tags() { /** * Should the API fields load in * - * @return false * @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; @@ -102,12 +101,7 @@ function give_activecampaign_should_load_api_fields() { $is_form_edit_screen = ( $post && 'give_forms' === $post->post_type ); // This function only runs when viewing the option on the settings screen or metabox. - if ( $is_settings_screen || $is_form_edit_screen ) { - return true; - } - - return false; - + return $is_settings_screen || $is_form_edit_screen; } /** @@ -142,16 +136,16 @@ function give_activecampaign_display_optin( $form_id ) { } ob_start(); ?> -
- - -
+
+ + +
-
-

- :  - -

-
+
+

+ :  + +

+
Date: Fri, 6 Nov 2020 08:30:38 -0800 Subject: [PATCH 5/5] chore: prepare for 1.0.1 release --- CHANGELOG.md | 4 +++- give-activecampaign.php | 4 ++-- readme.txt | 3 +++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a34cadf..b4e8dab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,9 @@ 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] +## 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] diff --git a/give-activecampaign.php b/give-activecampaign.php index 9da31c8..ab0d71f 100644 --- a/give-activecampaign.php +++ b/give-activecampaign.php @@ -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 */ @@ -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' ); diff --git a/readme.txt b/readme.txt index 10c8dd2..525a2fc 100644 --- a/readme.txt +++ b/readme.txt @@ -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!