From 2fc76361eb69eef316be876780d779f4f3effb4b Mon Sep 17 00:00:00 2001 From: Frances McMullin Date: Thu, 14 Nov 2024 16:29:53 +0000 Subject: [PATCH 1/3] Add support for pulling campaigns from BeCause --- campaigns.php | 8 ++++ lib/adapters/because.php | 87 ++++++++++++++++++++++++++++++++++++++++ lib/sync.php | 7 ++++ 3 files changed, 102 insertions(+) create mode 100644 lib/adapters/because.php diff --git a/campaigns.php b/campaigns.php index c58cad7..f8f864f 100644 --- a/campaigns.php +++ b/campaigns.php @@ -49,6 +49,7 @@ function campaigns_admin_menu() { function campaigns_admin_init() { register_setting('campaigns-settings', 'speakout_url'); register_setting('campaigns-settings', 'csl_url'); + register_setting('campaigns-settings', 'because_url'); register_setting('campaigns-settings', 'acf_post_type'); } @@ -74,6 +75,13 @@ function campaigns_settings_page() { + + + + + +
BeCause URL
+ diff --git a/lib/adapters/because.php b/lib/adapters/because.php new file mode 100644 index 0000000..6cd96a3 --- /dev/null +++ b/lib/adapters/because.php @@ -0,0 +1,87 @@ + array( + 'x-request-id' => 'af7c1fe6-d669-414e-b066-e9733f0de7a8', // dummy value to keep the API happy + ) + )); + + + if( is_wp_error($request) ) { + error_log("ERROR FETCHING JSON FOR BECAUSE"); + return false; + } + + $body = wp_remote_retrieve_body( $request ); + $campaigns = json_decode($body)->campaigns; + + while(!empty($campaigns)) { + require_once(CAMPAIGNS_BASE_DIR . 'lib/campaign.php'); + foreach($campaigns as $campaign) { + $max_actions = (is_null($campaign->maxActions) || $campaign->maxActions === "0") ? Because::campaign_target($campaign->actions) : $campaign->maxActions; + $campaign_data = [ + "external_id" => $campaign->id, + "created_at" => $campaign->createdAt, + "name" => $campaign->title, + "description" => $campaign->description, + "url" => $campaign->url, + "source" => "because", + "image" => $campaign->imageUrl, + "actions" => $campaign->actions, + "max_actions" => $max_actions, + ]; + + Campaign::add_or_update($campaign_data); + } + + $page++; + + $request = wp_remote_get( $because_json_url . '&page=' . $page, array( + 'headers' => array( + 'x-request-id' => 'af7c1fe6-d669-414e-b066-e9733f0de7a8', // dummy value to keep the API happy + ) + )); + + + if( is_wp_error($request) ) { + error_log("ERROR FETCHING JSON FOR BECAUSE"); + return false; + } + + $body = wp_remote_retrieve_body( $request ); + $campaigns = json_decode($body)->campaigns; + } + } + + /** + * This function was copied from Speakout to give a bogus campaign target as Because does not support this feature yet + */ + private static function campaign_target($c){ + $n = $c*(5.0/4.0); + $m = [2.0,2.5,2.0]; + $target = 100.0; + $i=0; + while ($n > $target){ + $target = $target * $m[$i%count($m)]; + $i = $i + 1; + } + + $target = (int)$target; + + return $target; + + } + +} \ No newline at end of file diff --git a/lib/sync.php b/lib/sync.php index 9ddd389..853f7c2 100644 --- a/lib/sync.php +++ b/lib/sync.php @@ -32,6 +32,13 @@ function sync_campaigns() { require_once(CAMPAIGNS_BASE_DIR . 'lib/adapters/csl.php'); CSL::sync(); } + + if( get_option('because_url') ) { + error_log('Syncing campaigns with BeCause'); + + require_once(CAMPAIGNS_BASE_DIR . 'lib/adapters/because.php'); + Because::sync(); + } } ?> From 3e642c6cd572b41f5eb7cf0fbbecd02139db030f Mon Sep 17 00:00:00 2001 From: Frances McMullin Date: Fri, 14 Feb 2025 11:30:03 +0000 Subject: [PATCH 2/3] Update for BeCause featured campaigns --- lib/adapters/because.php | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/adapters/because.php b/lib/adapters/because.php index 6cd96a3..15c04e0 100644 --- a/lib/adapters/because.php +++ b/lib/adapters/because.php @@ -9,13 +9,23 @@ class Because { public static function sync() { $because_url = get_option('because_url'); - $because_json_url = $because_url . '/campaigns/v1/campaigns?per_page=100'; + $because_json_url = $because_url . '/api/campaigns/v1/campaigns/query'; $page = 0; - $request = wp_remote_get( $because_json_url . '&page=' . $page, array( - 'headers' => array( + $request = wp_remote_post( $because_json_url, array( + 'headers' => array( 'x-request-id' => 'af7c1fe6-d669-414e-b066-e9733f0de7a8', // dummy value to keep the API happy - ) + ), + 'body' => json_encode(array( + 'filter' => array( + 'status' => 'STATUS_PUBLISHED', + 'isFeatured' => true + ), + 'pagination' => array( + 'perPage' => 100, + 'page' => $page + ) + )) )); @@ -48,10 +58,20 @@ public static function sync() { $page++; - $request = wp_remote_get( $because_json_url . '&page=' . $page, array( - 'headers' => array( - 'x-request-id' => 'af7c1fe6-d669-414e-b066-e9733f0de7a8', // dummy value to keep the API happy - ) + $request = wp_remote_post( $because_json_url, array( + 'headers' => array( + 'x-request-id' => 'af7c1fe6-d669-414e-b066-e9733f0de7a8', // dummy value to keep the API happy + ), + 'body' => json_encode(array( + 'filter' => array( + 'status' => 'STATUS_PUBLISHED', + 'isFeatured' => true + ), + 'pagination' => array( + 'perPage' => 100, + 'page' => $page + ) + )) )); From a0023b6dcbbfaef2353dff6a4c3445199a846522 Mon Sep 17 00:00:00 2001 From: Frances McMullin Date: Wed, 2 Apr 2025 18:35:51 +0100 Subject: [PATCH 3/3] Because API update --- lib/adapters/because.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/adapters/because.php b/lib/adapters/because.php index 15c04e0..cd404a5 100644 --- a/lib/adapters/because.php +++ b/lib/adapters/because.php @@ -35,7 +35,7 @@ public static function sync() { } $body = wp_remote_retrieve_body( $request ); - $campaigns = json_decode($body)->campaigns; + $campaigns = json_decode($body)->data->campaigns; while(!empty($campaigns)) { require_once(CAMPAIGNS_BASE_DIR . 'lib/campaign.php'); @@ -81,7 +81,7 @@ public static function sync() { } $body = wp_remote_retrieve_body( $request ); - $campaigns = json_decode($body)->campaigns; + $campaigns = json_decode($body)->data->campaigns; } }
ACF Post Type: (Leave blank to not use ACF)