Пример #1
0
function AC_CreateCampaignFromPost($api, $postID, $listID, $interestGroupName, $categoryTemplateID)
{
    // Does the user only want to create campaigns once?
    if ('1' == get_option(WP88_MC_CREATE_CAMPAIGN_ONCE)) {
        if ('1' == get_post_meta($postID, WP88_MC_CAMPAIGN_CREATED, true)) {
            return '-1';
        }
        // Don't create the campaign again!
    }
    // Get the info on this post
    $post = get_post($postID);
    // If the post is somehow in an unsupported state (sometimes from email
    // posts), then just skip the post.
    if ('pending' == $post->post_status || 'draft' == $post->post_status || 'private' == $post->post_status) {
        return '-1';
        // Don't create the campaign yet.
    }
    // Get info on the list
    $filters = array();
    $filters['list_id'] = $listID;
    $lists = $api->lists($filters);
    $list = $lists['data'][0];
    // Time to start creating the campaign...
    // First, create the options array
    $htmlContentTag = 'html';
    $options = array();
    $options['list_id'] = $listID;
    $options['subject'] = $post->post_title;
    $options['from_email'] = $list['default_from_email'];
    $options['from_name'] = $list['default_from_name'];
    $options['to_email'] = '*|FNAME|*';
    $options['tracking'] = array('opens' => true, 'html_clicks' => true, 'text_clicks' => false);
    $options['authenticate'] = true;
    // See if a template should be used
    if (0 != strcmp($categoryTemplateID, WP88_NONE)) {
        $options['template_id'] = $categoryTemplateID;
        // 'main' is the name of the section that will be replaced.  This is a
        // hardcoded decision.  Keeps things simple.  To view the sections of
        // a template, use MailChimp's templateInfo() function.  For more
        // information, go here:
        // http://apidocs.mailchimp.com/api/1.3/templateinfo.func.php
        // You need the campaign ID.  That can be retrieved with campaigns().
        $htmlContentTag = 'html_main';
    }
    // Start generating content
    $content = array();
    $postContent = '';
    // Get the excerpt option; if on, then show the excerpt
    if ('1' === get_option(WP88_MC_CAMPAIGN_EXCERPT_ONLY)) {
        if (0 == strlen($post->post_excerpt)) {
            // Handmade function which mimics wp_trim_excerpt() (that function won't operate
            // on a non-empty string)
            $postContent = AC_TrimExcerpt($post->post_content);
        } else {
            $postContent = apply_filters('the_excerpt', $post->post_excerpt);
            // Add on a "Read the post" link here
            $permalink = get_permalink($postID);
            $postContent .= "<p>Read the post <a href=\"{$permalink}\">here</a>.</p>";
            // See http://codex.wordpress.org/Function_Reference/the_content, which
            // suggests adding this code:
            $postContent = str_replace(']]>', ']]&gt;', $postContent);
        }
        // Set the text content variables
        $content['text'] = strip_tags($postContent);
    } else {
        // Run the full text through the content plugins
        $contentPlugins = new ACContentPlugins();
        $postContent = $contentPlugins->ConvertShortcode($post->post_content);
        // Text version isn't run through the content plugins
        $textPostContent = apply_filters('the_content', $post->post_content);
        $content['text'] = strip_tags($textPostContent);
    }
    // Set the content variables
    $content[$htmlContentTag] = $postContent;
    // Segmentation, if any (Interest groups)
    $segment_opts = NULL;
    if (0 != strcmp($interestGroupName, WP88_ANY)) {
        $group = $api->listInterestGroupings($listID);
        if (NULL != $group) {
            $interestID = $group[0]['id'];
            $conditions = array();
            $conditions[] = array('field' => "interests-{$interestID}", 'op' => 'all', 'value' => $interestGroupName);
            $segment_opts = array('match' => 'all', 'conditions' => $conditions);
        }
    }
    // More info here:  http://apidocs.mailchimp.com/api/1.3/campaigncreate.func.php
    $result = $api->campaignCreate('regular', $options, $content, $segment_opts);
    if ($api->errorCode) {
        // Set latest activity - displayed in the admin panel
        update_option(WP88_MC_LAST_CAMPAIGN_ERROR, "Problem with campaign with title '{$post->post_title}'.  Error Code: {$api->errorCode}, Message: {$api->errorMessage}");
        $result = "0";
    } else {
        // Set latest activity
        update_option(WP88_MC_LAST_CAMPAIGN_ERROR, "Your latest campaign created is titled '{$post->post_title}' with ID: {$result}");
        // Mark this post as having a campaign created from it.
        add_post_meta($postID, WP88_MC_CAMPAIGN_CREATED, '1');
    }
    // Done
    return $result;
}