273 lines
9.5 KiB
PHP
273 lines
9.5 KiB
PHP
<?php
|
|
|
|
class CRED_credit_mailchimp extends CRED_base {
|
|
|
|
public function __construct() {
|
|
// Le hook admin_menu est maintenant géré par le factory
|
|
}
|
|
|
|
public function init() {
|
|
|
|
}
|
|
|
|
public function add_admin_menu() {
|
|
add_submenu_page(
|
|
'credit-manager',
|
|
'Mailchimp',
|
|
'Mailchimp',
|
|
'manage_options',
|
|
'credit-mailchimp',
|
|
array($this, 'render_settings_page')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Enregistre les réglages et champs via la Settings API
|
|
*/
|
|
public function register_settings() {
|
|
register_setting(
|
|
'cred_mailchimp_options_group',
|
|
'cred_mailchimp_options',
|
|
array(
|
|
'type' => 'array',
|
|
'sanitize_callback' => array($this, 'sanitize_options'),
|
|
'default' => array(
|
|
'api_key' => '',
|
|
'server_prefix' => '',
|
|
'primary_list_id' => ''
|
|
)
|
|
)
|
|
);
|
|
|
|
add_settings_section(
|
|
'cred_mailchimp_main_section',
|
|
__('Paramètres de connexion', 'esi-creditdirect'),
|
|
function() {
|
|
echo '<p>' . esc_html__('Renseignez la clé API et le préfixe serveur (ex: us19) comme indiqué par Mailchimp.', 'esi-creditdirect') . '</p>';
|
|
},
|
|
'credit-mailchimp'
|
|
);
|
|
|
|
add_settings_field(
|
|
'cred_mailchimp_api_key',
|
|
__('Clé API', 'esi-creditdirect'),
|
|
array($this, 'field_api_key_cb'),
|
|
'credit-mailchimp',
|
|
'cred_mailchimp_main_section'
|
|
);
|
|
|
|
add_settings_field(
|
|
'cred_mailchimp_server_prefix',
|
|
__('Préfixe serveur', 'esi-creditdirect'),
|
|
array($this, 'field_server_prefix_cb'),
|
|
'credit-mailchimp',
|
|
'cred_mailchimp_main_section'
|
|
);
|
|
|
|
add_settings_field(
|
|
'cred_mailchimp_primary_list',
|
|
__('Liste principale (Audience)', 'esi-creditdirect'),
|
|
array($this, 'field_primary_list_cb'),
|
|
'credit-mailchimp',
|
|
'cred_mailchimp_main_section'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Affiche la page de réglages
|
|
*/
|
|
public function render_settings_page() {
|
|
$options = $this->get_options();
|
|
include plugin_dir_path(__FILE__) . '../../templates/admin/mailchimp_settings.php';
|
|
}
|
|
|
|
/**
|
|
* Callback du champ API Key
|
|
*/
|
|
public function field_api_key_cb() {
|
|
$options = $this->get_options();
|
|
$value = isset($options['api_key']) ? $options['api_key'] : '';
|
|
echo '<input type="text" class="regular-text" id="cred_mailchimp_api_key" name="cred_mailchimp_options[api_key]" value="' . esc_attr($value) . '" placeholder="YOUR_API_KEY">';
|
|
}
|
|
|
|
/**
|
|
* Callback du champ Server Prefix
|
|
*/
|
|
public function field_server_prefix_cb() {
|
|
$options = $this->get_options();
|
|
$value = isset($options['server_prefix']) ? $options['server_prefix'] : '';
|
|
echo '<input type="text" class="regular-text" id="cred_mailchimp_server_prefix" name="cred_mailchimp_options[server_prefix]" value="' . esc_attr($value) . '" placeholder="us19">';
|
|
}
|
|
|
|
/**
|
|
* Callback du champ Liste principale (audience)
|
|
*/
|
|
public function field_primary_list_cb() {
|
|
$options = $this->get_options();
|
|
$selected = isset($options['primary_list_id']) ? $options['primary_list_id'] : '';
|
|
|
|
$client = $this->get_client();
|
|
if ($client === null) {
|
|
echo '<em>' . esc_html__('Renseignez d\'abord la clé API et le préfixe serveur, puis enregistrez.', 'esi-creditdirect') . '</em>';
|
|
echo '<br />';
|
|
echo '<input type="text" class="regular-text" name="cred_mailchimp_options[primary_list_id]" value="' . esc_attr($selected) . '" placeholder="Audience ID (ex: a1b2c3d4)">';
|
|
return;
|
|
}
|
|
|
|
$lists = array();
|
|
try {
|
|
$resp = $client->lists->getAllLists(array('count' => 1000));
|
|
if (is_array($resp) && isset($resp['lists']) && is_array($resp['lists'])) {
|
|
foreach ($resp['lists'] as $list) {
|
|
if (isset($list['id']) && isset($list['name'])) {
|
|
$lists[] = array(
|
|
'id' => (string) $list['id'],
|
|
'name' => (string) $list['name']
|
|
);
|
|
}
|
|
}
|
|
}
|
|
} catch (\Throwable $e) {
|
|
echo '<div class="notice notice-error"><p>' . esc_html(sprintf(__('Erreur lors du chargement des audiences: %s', 'esi-creditdirect'), $e->getMessage())) . '</p></div>';
|
|
}
|
|
|
|
if (empty($lists)) {
|
|
echo '<em>' . esc_html__('Aucune audience trouvée ou erreur. Vous pouvez saisir un ID manuellement.', 'esi-creditdirect') . '</em>';
|
|
echo '<br />';
|
|
echo '<input type="text" class="regular-text" name="cred_mailchimp_options[primary_list_id]" value="' . esc_attr($selected) . '" placeholder="Audience ID (ex: a1b2c3d4)">';
|
|
return;
|
|
}
|
|
|
|
echo '<select id="cred_mailchimp_primary_list" name="cred_mailchimp_options[primary_list_id]">';
|
|
echo '<option value="">' . esc_html__('— Sélectionner —', 'esi-creditdirect') . '</option>';
|
|
foreach ($lists as $list) {
|
|
$isSel = selected($selected, $list['id'], false);
|
|
echo '<option value="' . esc_attr($list['id']) . '" ' . $isSel . '>' . esc_html($list['name'] . ' (' . $list['id'] . ')') . '</option>';
|
|
}
|
|
echo '</select>';
|
|
}
|
|
|
|
/**
|
|
* Sanitize des options
|
|
*/
|
|
public function sanitize_options($options) {
|
|
$sanitized = array();
|
|
$sanitized['api_key'] = isset($options['api_key']) ? sanitize_text_field($options['api_key']) : '';
|
|
$sanitized['server_prefix'] = isset($options['server_prefix']) ? sanitize_text_field($options['server_prefix']) : '';
|
|
$sanitized['primary_list_id'] = isset($options['primary_list_id']) ? sanitize_text_field($options['primary_list_id']) : '';
|
|
return $sanitized;
|
|
}
|
|
|
|
/**
|
|
* Récupère les options du plugin
|
|
*/
|
|
private function get_options() {
|
|
$defaults = array(
|
|
'api_key' => '',
|
|
'server_prefix' => '',
|
|
'primary_list_id' => ''
|
|
);
|
|
$options = get_option('cred_mailchimp_options', array());
|
|
if (!is_array($options)) {
|
|
$options = array();
|
|
}
|
|
return array_merge($defaults, $options);
|
|
}
|
|
|
|
/**
|
|
* Retourne une instance configurée du client Mailchimp Marketing
|
|
* ou null si les informations de connexion sont incomplètes.
|
|
*/
|
|
public function get_client() {
|
|
$options = $this->get_options();
|
|
$apiKey = isset($options['api_key']) ? trim($options['api_key']) : '';
|
|
$server = isset($options['server_prefix']) ? trim($options['server_prefix']) : '';
|
|
|
|
if ($apiKey === '' || $server === '') {
|
|
return null;
|
|
}
|
|
|
|
|
|
if (!class_exists('\\MailchimpMarketing\\ApiClient')) {
|
|
return null;
|
|
}
|
|
|
|
|
|
|
|
$client = new \MailchimpMarketing\ApiClient();
|
|
$client->setConfig(array(
|
|
'apiKey' => $apiKey,
|
|
'server' => $server
|
|
));
|
|
|
|
return $client;
|
|
}
|
|
|
|
/**
|
|
* AJAX: ping Mailchimp pour valider la connexion
|
|
*/
|
|
public function ajax_ping() {
|
|
check_ajax_referer('cred_mailchimp_ping', 'nonce');
|
|
if (!current_user_can('manage_options')) {
|
|
wp_send_json_error(array('message' => __('Permissions insuffisantes', 'esi-creditdirect')), 403);
|
|
}
|
|
|
|
$client = $this->get_client();
|
|
if ($client === null) {
|
|
wp_send_json_error(array('message' => __('Configuration incomplète (clé API/préfixe serveur).', 'esi-creditdirect')), 400);
|
|
}
|
|
|
|
try {
|
|
$resp = $client->ping->get();
|
|
wp_send_json_success(array('response' => $resp));
|
|
} catch (\Throwable $e) {
|
|
wp_send_json_error(array('message' => $e->getMessage()), 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Abonne ou met à jour un contact à partir d'un objet crédit.
|
|
* @param object $credit Objet avec au minimum les propriétés email, prenom, nom
|
|
*/
|
|
public function subscribe_from_credit($credit) {
|
|
if (!$credit || !isset($credit->email)) {
|
|
return;
|
|
}
|
|
|
|
$email = trim((string)$credit->email);
|
|
if ($email === '' || !is_email($email)) {
|
|
return;
|
|
}
|
|
|
|
$options = get_option('cred_mailchimp_options', array());
|
|
$listId = isset($options['primary_list_id']) ? trim((string)$options['primary_list_id']) : '';
|
|
if ($listId === '') {
|
|
return;
|
|
}
|
|
|
|
$client = $this->get_client();
|
|
if ($client === null) {
|
|
return;
|
|
}
|
|
|
|
$firstName = isset($credit->prenom) ? (string)$credit->prenom : '';
|
|
$lastName = isset($credit->nom) ? (string)$credit->nom : '';
|
|
|
|
$subscriberHash = md5(strtolower($email));
|
|
$body = array(
|
|
'email_address' => $email,
|
|
'status' => 'subscribed',
|
|
'status_if_new' => 'subscribed',
|
|
'merge_fields' => array(
|
|
'FNAME' => $firstName,
|
|
'LNAME' => $lastName
|
|
)
|
|
);
|
|
|
|
try {
|
|
$client->lists->setListMember($listId, $subscriberHash, $body);
|
|
} catch (\Throwable $e) {
|
|
error_log('Mailchimp subscribe error (from credit): ' . $e->getMessage());
|
|
}
|
|
}
|
|
} |