';
foreach ( $sections as $section_id ):
$content = $this->mailchimp_sections( $parent_id, $section_id );
$social = $this->mailchimp_sharing( $section_id );
// bail if no content returned
if ( empty( $content ) )
continue;
// do all the cleanup to get it in MailChimp format.
$content = preg_replace( '/^[ \t]*[\r\n]+/m', '', $content );
$content = str_replace( '+', '
+', $content );
$content = mb_convert_encoding( $content, 'HTML-ENTITIES' );
$content = trim( $content );
// construct section block
$raw_html .= '
';
$raw_html .= $content;
$raw_html .= $social;
$raw_html .= '
';
$raw_html .= '
';
endforeach;
// add the sign off
$raw_html .= '
Thanks,
Dave Pell
Managing Editor, Internet
';
$raw_html .= '
'; // end newsletter wrap
return $raw_html;
}
/**
* process the mailchimp creation
*/
public function mailchimp_submit() {
// get our values via $_POST
$parent_id = $_POST['post_id'];
$nonce = $_POST['nonce'];
check_ajax_referer( 'nd_mailchimp_nonce', 'nonce' );
// set up return array for ajax responses
$ret = array();
// check permissions first
if ( ! current_user_can( 'manage_options' ) ) {
$ret['success'] = false;
$ret['errcode'] = 'NO_PERMISSION';
$ret['message'] = __( 'You do not have access to this function. ', 'ndp' );
echo json_encode($ret);
die();
}
// check to make sure we got a post ID
if( !isset( $parent_id ) || empty( $parent_id ) ) {
$ret['success'] = false;
$ret['errcode'] = 'NO_POST_ID';
$ret['message'] = __( 'No post ID was provided.', 'ndp' );
echo json_encode($ret);
die();
}
// make sure our post ID was actually a number
if( intval( $parent_id ) != $parent_id ) {
$ret['success'] = false;
$ret['errcode'] = 'ID_NOT_VALID';
$ret['message'] = __( 'The post ID provided was not valid', 'ndp' );
echo json_encode($ret);
die();
}
// get all my HTML
$html_data = $this->mailchimp_html( $parent_id );
$text_data = $this->mailchimp_text( $parent_id );
if ( empty( $html_data ) ) {
$ret['success'] = false;
$ret['errcode'] = 'NO_HTML';
$ret['message'] = __( 'No HTML was generated.', 'ndp' );
echo json_encode($ret);
die();
}
if ( empty( $text_data ) ) {
$ret['success'] = false;
$ret['errcode'] = 'NO_TEXT';
$ret['message'] = __( 'No text was generated.', 'ndp' );
echo json_encode($ret);
die();
}
// get some metadata from the parent item
$item_meta = get_post_meta( $parent_id, '_nd_meta', true );
$fallback = date('F jS', time() )." - The Day's Most Fascinating News";
$subject = isset( $item_meta['title'] ) && !empty( $item_meta['title'] ) ? $item_meta['title'] : $fallback;
// grab our settings data
$mcdata = get_option('nd-mailchimp' );
if( !isset( $mcdata ) || empty( $mcdata ) ) {
$ret['success'] = false;
$ret['errcode'] = 'NO_MC_DATA';
$ret['message'] = __( 'The MailChimp settings have not been configured.', 'ndp' );
echo json_encode($ret);
die();
}
$apikey = isset( $mcdata['api-key'] ) ? $mcdata['api-key'] : '';
$listid = isset( $mcdata['list-id'] ) ? $mcdata['list-id'] : '';
$template = isset( $mcdata['template'] ) ? $mcdata['template'] : '';
$fromname = isset( $mcdata['from-name'] ) ? $mcdata['from-name'] : '';
$fromemail = isset( $mcdata['from-email'] ) ? $mcdata['from-email'] : '';
// check for empty stuff
if( empty( $apikey ) || empty( $listid ) || empty( $template ) || empty( $fromname ) || empty( $fromemail ) ) {
$ret['success'] = false;
$ret['errcode'] = 'NO_API_KEY';
$ret['message'] = __( 'No API key was provided.', 'ndp' );
echo json_encode($ret);
die();
}
// build Mailchimp query
$options = array(
'list_id' => $listid,
'subject' => $subject,
'from_email' => $fromemail,
'from_name' => $fromname,
'template_id' => $template,
'tracking' => array(
'opens' => true,
'html_clicks' => true,
'text_clicks' => false
),
'title' => $subject
);
$content = array(
'html_bodyContent' => $html_data,
'text' => $text_data
);
// pull in our MailChimp class file
require_once('lib/MCAPI.class.php');
$api = new MCAPI($apikey);
$data = $api->campaignCreate( 'regular', $options, $content );
// start our data testing
if( !isset( $data ) || empty( $data ) ) {
$ret['success'] = false;
$ret['message'] = __( 'MailChimp did not return any data.', 'ndp' );
echo json_encode($ret);
die();
}
if( is_wp_error( $data ) ) {
$ret['success'] = false;
$ret['message'] = __( 'There was an error with your request. Please try again.', 'ndp' );
echo json_encode($ret);
die();
}
// now process the actual return
if( isset( $data->errorCode ) ) {
$ret['success'] = false;
$ret['errcode'] = $data->errorCode;
$ret['message'] = $data->errorMessage;
echo json_encode($ret);
die();
}
if( !isset( $data->errorCode ) ) {
// update the parent item postmeta
$chimp['id'] = $data;
$chimp['status'] = 'complete';
$chimp['process'] = time();
update_post_meta( $parent_id, '_nd_chimp_meta', $chimp );
// build the send button for cloning
$button = '