Skip to content
Open
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
8 changes: 8 additions & 0 deletions campaigns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand All @@ -74,6 +75,13 @@ function campaigns_settings_page() {
</tr>
</table>

<table class="form-table">
<tr valign="top">
<th scope="row">BeCause URL</th>
<td><input type='text' name='because_url' value="<?php echo esc_attr(get_option('because_url')) ?>" style="width: 60%;"></td>
</tr>
</table>

<table class="form-table">
<tr valign="top">
<th scope="row">ACF Post Type: (Leave blank to not use ACF)</th>
Expand Down
107 changes: 107 additions & 0 deletions lib/adapters/because.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/**
* @package Campaigns
* @version 0.1
*/

class Because {

public static function sync() {
$because_url = get_option('because_url');
$because_json_url = $because_url . '/api/campaigns/v1/campaigns/query';
$page = 0;

$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;

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;

}

}
7 changes: 7 additions & 0 deletions lib/sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

?>