'; $faceb .= ''; $faceb .= ''; $twitr = ''; $twitr .= ''; $twitr .= ''; $share = '

'.$twitr.'   '.$faceb.'

'; return $share; } /** * get the individual sections in the MailChimp HTML format * */ public function mailchimp_sections( $parent_id, $section_id ) { // make sure I got a section ID if ( empty( $section_id ) || intval( $section_id ) != $section_id ) return; // grab variables $announce = get_post_meta( $parent_id, '_nd_announce', true ); $raw_title = get_post_field( 'post_title', $section_id, 'raw' ); $raw_words = get_post_field( 'post_content', $section_id, 'raw' ); $item_count = get_post_meta( $section_id, '_nd_section_order', true ); // add the optional announcement for the first item if present $raw_words = !empty( $announce ) && $item_count == 1 ? ''.$announce.'

'.$raw_words : $raw_words; // clean up and tweak $title = '

'.$item_count.'. '.$raw_title.'

'; $words = mb_convert_encoding( $raw_words, 'HTML-ENTITIES' ); $merge = $title.'
'.$words; return $merge; } /** * clean up the content for the text version of the newsletter * */ public function mailchimp_text_clean( $raw_words ) { // first, get it in HTML $raw_words = wpautop( $raw_words ); // do a double replace for plus signs to get a line break $raw_words = str_replace( "+", "+--", $raw_words ); $raw_words = str_replace( "+", "\n", $raw_words ); // get rid of whitespace $raw_words = trim( $raw_words ); // do some voodoo to make the links nicer and avoid stripping $raw_words = str_replace( array( '' ), array( '[ ', ' ] ' ) , $raw_words ); // now strip everything $raw_words = strip_tags( $raw_words ); return $raw_words; } /** * get the individual sections in the MailChimp text format * */ public function mailchimp_text_sections( $parent_id, $section_id ) { // make sure I got a section ID if ( empty( $section_id ) || intval( $section_id ) != $section_id ) return; // grab variables $announce = get_post_meta( $parent_id, '_nd_announce', true ); $raw_title = get_post_field( 'post_title', $section_id, 'raw' ); $item_count = get_post_meta( $section_id, '_nd_section_order', true ); // get the content and start getting wild with it. $raw_words = get_post_field( 'post_content', $section_id, 'raw' ); $srp_words = $this->mailchimp_text_clean( $raw_words ); // add the optional announcement for the first item if present $content = !empty( $announce ) && $item_count == 1 ? $announce."\n".$srp_words : $srp_words; // clean up and tweak $title = $item_count.'. '.$raw_title; $section = ''; $section .= $title; $section .= "\n"; $section .= "\n"; $section .= $content; $section .= "\n"; return $section; } /** * generate the text email for MailChimp * */ public function mailchimp_text( $parent_id ) { $sections = $this->get_section_ids( $parent_id ); if ( empty( $sections ) ) return; // generate my raw text $raw_text = ''; foreach ( $sections as $section_id ): $content = $this->mailchimp_text_sections( $parent_id, $section_id ); // bail if no content returned if ( empty( $content ) ) continue; // construct section block $raw_text .= $content; $raw_text .= "\n"; $raw_text .= "\n"; endforeach; // add the sign off $raw_text .= 'Thanks'."\n".'Dave Pell'."\n".'Managing Editor, Internet'; return $raw_text; } /** * generate the HTML setup for MailChimp * */ public function mailchimp_html( $parent_id ) { $sections = $this->get_section_ids( $parent_id ); if ( empty( $sections ) ) return; // generate my raw HTML $raw_html = ''; $raw_html .= '
'; 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 .= '

'; 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 = ''; $button .= ''.__( 'Send Test Email', 'ndp' ).''; $button .= ''; $button .= ''; $ret['success'] = true; $ret['mail_id'] = $data; $ret['pending'] = $parent_id; $ret['button'] = $button; $ret['process'] = date( 'M jS Y', time() ); $ret['message'] = __( 'The newsletter has been sent to MailChimp for editing.', 'ndp' ); echo json_encode($ret); die(); } // unknown error $ret['success'] = false; $ret['message'] = __( 'An unknown error occured. Please try again later.', 'ndp' ); echo json_encode($ret); die(); }