public function render_meta_box()
 {
     $post = get_post();
     $settings = get_option('mailchimp_settings');
     $existing = mailchimp_tools_get_existing_campaign_data_for_post($post, false);
     $lists = $this->api->lists->getList();
     $segments = array();
     $groups = array();
     foreach ($lists['data'] as $list) {
         $list_segments = $this->api->lists->segments($list['id']);
         if (!empty($list_segments['saved'])) {
             $segments[$list['id']] = $list_segments['saved'];
         }
         try {
             $list_groups = $this->api->lists->interestGroupings($list['id']);
             if (!empty($list_groups)) {
                 $groups[$list['id']] = $list_groups;
             }
         } catch (Mailchimp_List_InvalidOption $e) {
             continue;
         }
     }
     $web_id = get_post_meta($post->ID, 'mailchimp_web_id', true);
     $mc_api_key_parts = explode('-', $settings['mailchimp_api_key']);
     $mc_api_endpoint = $mc_api_key_parts[1];
     $post_type_obj = get_post_type_object($this->post_type);
     $settings_key = $post_type_obj->name . '_mailchimp_settings';
     $saved_settings = get_option($settings_key, false);
     /**
      * Try to get existing group and subgroup data.
      *
      * Do this here instead of in the campaign-edit.php template
      * because it's fairly complex to parse the necessary info
      * from $existing data.
      */
     if (!empty($existing['segment_opts'])) {
         $existing_seg_opts = $existing['segment_opts'];
         if (isset($existing_seg_opts['conditions'])) {
             $conditions = $existing_seg_opts['conditions'][0];
             $saved_group_id = str_replace('interests-', '', $conditions['field']);
             $saved_subgroup_bit = $conditions['value'][0];
             $saved_group_settings = array('group' => array('saved_group_id' => $saved_group_id), 'subgroup' => array('saved_subgroup_bit' => $saved_subgroup_bit));
             /**
              * Use existing values for groups and subgroups if they were preset in $existing data
              */
             if (isset($saved_settings['segment'])) {
                 unset($saved_settings['segment']);
             }
             $saved_settings = wp_parse_args($saved_group_settings, $saved_settings);
         }
     }
     $context = array('lists' => $lists, 'segments' => $segments, 'groups' => $groups, 'templates' => $this->api->templates->getList(array('gallery' => false, 'base' => false), array('include_drag_and_drop' => true)), 'existing' => $existing, 'mc_api_endpoint' => $mc_api_endpoint, 'web_id' => $web_id, 'saved_settings' => $saved_settings);
     mailchimp_tools_render_template('campaign-edit.php', $context);
 }
 public function render_preview_page()
 {
     global $wp_query, $post;
     if (!isset($wp_query->query_vars['campaign_preview'])) {
         return false;
     }
     if (!isset($_GET['post_id'])) {
         return false;
     }
     $post = get_post($_GET['post_id']);
     setup_postdata($post);
     $existing = mailchimp_tools_get_existing_campaign_data_for_post($post);
     $html = apply_filters('the_content', $post->post_content);
     if ($existing['type'] == 'plaintext') {
         echo wp_strip_all_tags($post->post_content);
     } else {
         if ($existing['type'] == 'regular') {
             $template_source = mailchimp_tools_get_template_source($existing['template_id']);
             $doc = new DOMDocument();
             @$doc->loadHTML('<?xml encoding="UTF-8">' . $template_source);
             foreach ($doc->getElementsByTagName('*') as $element) {
                 if ($element->getAttribute('mc:edit') == 'body') {
                     $fragment = new DOMDocument();
                     @$fragment->loadHTML('<?xml encoding="UTF-8">' . $html);
                     $item = $fragment->getElementsByTagName('body')->item(0);
                     while ($element->childNodes->length) {
                         $element->removeChild($element->firstChild);
                     }
                     $element->appendChild($doc->importNode($item, true));
                     break;
                 }
             }
             echo $doc->saveHTML();
         }
     }
     wp_reset_postdata();
     die;
 }