Skapa posts på mailchimp campaigns via api

<?php
// Mailchimp API credentials
const MAILCHIMP_API_KEY = '';  // Replace with your Mailchimp API Key
const MAILCHIMP_DATA_CENTER = '';  // Replace with your Mailchimp data center (e.g., 'us2')

// Function to fetch and create posts for sent Mailchimp campaigns
function create_campaign_posts_from_mailchimp() {
	if (empty(MAILCHIMP_API_KEY)) {
		error_log("Mailchimp API key is not set.");
		return;
	}

	// Mailchimp API URL to get sent campaigns
	$url = "https://" . MAILCHIMP_DATA_CENTER . ".api.mailchimp.com/3.0/campaigns?status=sent";

	// Initialize cURL
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HTTPHEADER, [
		"Authorization: Bearer " . MAILCHIMP_API_KEY,
		"Content-Type: application/json"
	]);

	// Execute cURL and get response
	$response = curl_exec($ch);
	curl_close($ch);

	// Decode JSON response
	$data = json_decode($response, true);

	// Check if campaigns exist
	if (!isset($data['campaigns']) || empty($data['campaigns'])) {
		error_log("No campaigns found.");
		return;
	}

	// Loop through campaigns and create posts
	foreach ($data['campaigns'] as $campaign) {
		create_campaign_post($campaign);
	}
}

// Function to create a post from a Mailchimp campaign
function create_campaign_post($campaign) {
	// Check if post with campaign ID already exists
	$existing_post = get_posts([
		'post_type'   => 'post',
		'meta_key'    => 'campaign_id',
		'meta_value'  => $campaign['id']
	]);

	if (!empty($existing_post)) {
		return;  // Post already exists
	}

	// Prepare post data
	$post_title = "Nyhetsbrev: " . sanitize_text_field($campaign['settings']['title']);
	$post_content = "
        <p><a href='" . esc_url($campaign['archive_url']) . "'>" . esc_html($campaign['settings']['title']) . "</a></p>
        <p>Sent Time: " . date("Y-m-d H:i", strtotime($campaign['send_time'])) . "</p>
        <p>Subject Line: " . esc_html($campaign['settings']['subject_line']) . "</p>
        <p>Recipients: " . esc_html($campaign['report_summary']['opens']) . " opens, " . esc_html($campaign['report_summary']['clicks']) . " clicks</p>
    ";
	$post_date = current_time('mysql');  // Set post date to current time

	// Insert the post into WordPress
	$new_post = [
		'post_title'   => $post_title,
		'post_content' => $post_content,
		'post_status'  => 'publish',
		'post_date'    => $post_date,
		'post_author'  => 1,  // Default admin user
		'meta_input'   => [
			'campaign_id' => $campaign['id']
		]
	];

	wp_insert_post($new_post);
}

// Cron job function to run daily
function schedule_mailchimp_campaign_cron() {
	if (!wp_next_scheduled('sync_mailchimp_campaigns_event')) {
		wp_schedule_event(time(), 'daily', 'sync_mailchimp_campaigns_event');
	}
}
add_action('wp', 'schedule_mailchimp_campaign_cron');

// Hook the cron job to our function
add_action('sync_mailchimp_campaigns_event', 'create_campaign_posts_from_mailchimp');

Författare: Erik

Erik har jobbat med webb professionellt sedan 2008. Från 2005 till 2008 studerades webb på ING/JTH och dessförinnan skapades webb på all fritid. Första sajten byggdes någon gång mellan 1996-1998.