mircobabini
10/15/2013 - 7:51 PM

Lists the inactive templates on a MailChimp account and permits the user to recover them one by one.

Lists the inactive templates on a MailChimp account and permits the user to recover them one by one.

<?
/**
 * Lists only the inactive templates and
 * permits the user to recover them one by one
 * 
 * @author Mirco Babini <mirkolofio@gmail.com>
 * 
 * Based on MailChimp APIv2
 */

// https://github.com/drewm/mailchimp-api/
require 'MailChimp.class.php';

// obtain an api key or create one: https://admin.mailchimp.com/account/api/
$MailChimp = new MailChimp ('YOUR-API-KEY');
$result = $MailChimp->call ('/templates/list', array (
	'types'     => array (),
		'filters'   => array (
			'include_inactive'  => true,
			'inactive_only'     => true,
		),
	));

$lists = array ();
foreach ($result['user'] as $list) {
	echo $list['id'] . ': ' . $list['name'] . PHP_EOL;
	
	$lists[ $list['id'] ] = $list;
}

while (true) {
	echo 'Specify a list-id to recover or \'exit\' to close: ';
	$line = fgets (fopen ("php://stdin","r"));
	
	if (trim ($line) == 'exit') {
		exit;
	}
	
	$line = trim (preg_replace ('/\s+/', ' ', $line));
	if (isset ($lists[$line])) {
		$result = $MailChimp->call ('/templates/undel', array (
			'template_id'   => $line,
		));
	}
}