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..cd404a5 --- /dev/null +++ b/lib/adapters/because.php @@ -0,0 +1,107 @@ + 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 + ) + )) + )); + + + if( is_wp_error($request) ) { + error_log("ERROR FETCHING JSON FOR BECAUSE"); + return false; + } + + $body = wp_remote_retrieve_body( $request ); + $campaigns = json_decode($body)->data->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_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 + ) + )) + )); + + + if( is_wp_error($request) ) { + error_log("ERROR FETCHING JSON FOR BECAUSE"); + return false; + } + + $body = wp_remote_retrieve_body( $request ); + $campaigns = json_decode($body)->data->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(); + } } ?>
ACF Post Type: (Leave blank to not use ACF)