chive__date">';
$html .= sprintf(
// translators: %s is the campaign send date.
esc_html__('(%s)', 'mailchimp-for-wp'),
esc_html(date_i18n(get_option('date_format'), $timestamp))
);
$html .= '';
}
}
$html .= '';
}
$html .= '';
return $html;
}
/**
* Fetch sent campaigns from the Mailchimp API, with transient caching.
*
* Results are cached for 1 hour per (count) combination to reduce API requests.
*
* @since 4.13.0
* @param int $count Maximum number of campaigns to retrieve.
* @return array|string
*/
private function get_campaigns($count)
{
$transient_key = 'mc4wp_campaigns_v2_' . $count;
$cached = get_transient($transient_key);
if (false !== $cached) {
return $cached;
}
try {
$api = mc4wp_get_api_v3();
$result = $api->get_campaigns([
'status' => 'sent',
'count' => $count,
'sort_field' => 'send_time',
'sort_dir' => 'DESC',
'fields' => 'campaigns.id,campaigns.settings.title,campaigns.settings.subject_line,campaigns.send_time,campaigns.long_archive_url',
]);
if (is_object($result) && isset($result->campaigns) && is_array($result->campaigns)) {
$campaigns = $result->campaigns;
} else {
return current_user_can('manage_options') ? '' : '';
}
} catch (MC4WP_API_Exception $e) {
// Log API errors gracefully, do not break the page.
mc4wp('log')->error('Campaign Archive: ' . $e->getMessage());
return current_user_can('manage_options') ? '' : '';
}
if (empty($campaigns)) {
return current_user_can('manage_options') ? '' : '';
}
set_transient($transient_key, $campaigns, HOUR_IN_SECONDS);
return $campaigns;
}
}