Skip to content

Instantly share code, notes, and snippets.

@chriskoelle
Last active August 29, 2015 14:07
Show Gist options
  • Save chriskoelle/9c4b0f36d9174b9d38c3 to your computer and use it in GitHub Desktop.
Save chriskoelle/9c4b0f36d9174b9d38c3 to your computer and use it in GitHub Desktop.
Display a list of past MailChimp campaigns
<?php
class NH_MailChimp_Archive {
private $_args, $max_page,
public function __construct($args = array()) {
global $paged;
$defaults = array(
'apikey' => false,
'start' => 0,
'limit' => 12,
'sort_field' => 'send_time',
'filters' => array(
'template_id' => false,
'list_id' => false,
'status' => 'sent',
)
);
$request = array_replace_recursive( $defaults, $args );
// Cleanup the filters array
$request['filters'] = array_filter($request['filters']);
$this->_args = $request;
add_shortcode( 'mailchimp_archive', array($this, 'archive_shortcode') );
}
/**
* Fetch a list of campaigns using MailChimp's API
* @return array/bool An array of campaigns or false on failure
*/
public function fetch_campaigns() {
if(!$this->apikey) return;
// echo '<pre>'.print_r($this->_args, true).'</pre>'; die();
$return = false;
$mc_url = 'https://us1.api.mailchimp.com/2.0/campaigns/list.json/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $mc_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $this->_args ));
$result = curl_exec($ch);
if(!curl_errno($ch)) {
$numpages = ceil($result->total / $this->limit);
$this->start++;
$result = json_decode($result);
if(is_null($this->max_page)) $this->max_page = ceil($result->total / $this->limit);
$return = $result->data;
}
curl_close($ch);
return $return;
}
/**
* Helper function to add pagination to the bottom of the shortcode
* @return string HTML output for pagination
*/
public function archive_pagination() {
global $post;
$link = get_permalink($post->ID);
$output = '<ul class="wp_page_numbers">';
$output .= sprintf('<li class="page_info">Page %s of %s</li>', $this->start, $this->max_page);
for($i = 1; $i <= $this->max_page; $i++):
$output .= sprintf('<li%s><a href="%s">%s</a></li>',
($this->start === $i ? ' class="active_page"' : ''),
($i === 1 ? $link : $link .$i.'/'),
$i
);
endfor;
$output .= '</ul>';
return $output;
}
/**
* Shortcode for listing out past MailChimp campaigns
* @param array $atts Shortcode atts
* @return string HTML output for shortcode
*/
public function archive_shortcode($atts) {
// extract(shortcode_atts( array(
// ), $atts ));
$page = get_query_var('page') ? get_query_var('page') : 1;
$this->start = $page - 1;
$output = '';
$before = '<ul class="mailchimp-archive">';
$after = '</ul>';
$list = '';
if($campaigns = $this->fetch_campaigns()):
$pagination = $this->archive_pagination();
foreach($campaigns as $c):
$list .= sprintf('<li><a href="%s" title="%s" target="_blank"><strong>%s</strong> <span class="campaign-date">%s</span></a></li>',
$c->archive_url,
esc_attr($c->subject),
$c->subject,
date('F jS, Y', strtotime($c->send_time))
);
endforeach;
$output = $before . $list . $after . $pagination;
$ouput = apply_filters( 'mailchimp_archive_output', $output, $list, $before, $after, $pagination, $campaigns );
endif;
return $output;
}
/**
* Function for retrieving list names and IDs
* Useful for finding the correct list_id for fetch_campaigns function
*/
public function fetch_lists() {
$mc_url = 'https://us1.api.mailchimp.com/2.0/lists/list.json/';
$request = array(
'apikey' => $this->apikey
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $mc_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode( $request ));
$result = curl_exec($ch);
if(!curl_errno($ch)) {
echo '<pre>'.print_r(json_decode($result), true).'</pre>'; die();
}
curl_close($ch);
}
// Magic Functions
public function __set($property, $value) {
return $this->_args[$property] = $value;
}
public function __get($property){
return array_key_exists($property, $this->_args) ? $this->_args[$property] : null;
}
}
$mc_archive = new MailChimp_Archive_401(array(
'apikey' => 'API KEY',
'filters' => array(
'template_id' => 00000000,
'list_id' => 'LIST ID'
)
));
// $mc_archive->fetch_lists();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment