Skip to content

Instantly share code, notes, and snippets.

@willybahuaud
Created January 29, 2022 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willybahuaud/2fbbbeb7b7c80cff9957979ad28abe25 to your computer and use it in GitHub Desktop.
Save willybahuaud/2fbbbeb7b7c80cff9957979ad28abe25 to your computer and use it in GitHub Desktop.
Constructeur de module d’emails dont les contenus sont administrables dans ACF
<?php
add_action( 'admin_menu', 'register_option_page', 10 );
function register_option_page() {
if( function_exists( 'acf_add_options_page' ) ) {
acf_add_options_page( array(
'page_title' => 'Réglages des emails',
'menu_title' => 'Réglages',
'menu_slug' => 'emails-settings',
'capability' => 'manage_options',
'redirect' => false,
) );
}
}
add_action( 'admin_init', 'load_acf_fields', 9 );
add_action( 'init', 'load_acf_fields', 9 );
function load_acf_fields() {
if ( function_exists('acf_add_local_field_group') ):
$format = array(
// ici on peut créer de nouveaux mails
array(
'title' => 'Exemple de mail',
'slug' => 'email_user_demo',
'placeholders' => array(
'%accueil%',
'%user_id%',
'%display_name%',
'%prenom%',
'%email%',
'%login%',
),
'to' => true,
),
);
// Plus touche à la suite
$fields_emails = array();
foreach ( $format as $k => $args ) {
$fields_emails[] = array (
'key' => 'field_' . substr( md5( $args['slug'] . 'tab' ), 0, 13 ),
'label' => ucfirst( $args['title'] ),
'name' => '',
'type' => 'tab',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'placement' => 'top',
'endpoint' => 0,
);
if ( ! empty( $args['to'] ) ) {
$fields_emails[] = array (
'key' => 'field_' . substr( md5( $args['slug'] . 'dest' ), 0, 13 ),
'label' => 'Destinataire de l’email de ' . $args['title'],
'name' => $args['slug'] . '_destinataire',
'type' => 'email',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '50',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
);
}
$fields_emails[] = array (
'key' => 'field_' . substr( md5( $args['slug'] . 'title' ), 0, 13 ),
'label' => 'Titre de l’email de ' . $args['title'],
'name' => $args['slug'] . '_title',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => ! empty( $args['to'] ) ? '50' : '100',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
);
$fields_emails[] = array (
'key' => 'field_' . substr( md5( $args['slug'] . 'from_name' ), 0, 13 ),
'label' => 'Nom de l’expéditeur',
'name' => $args['slug'] . '_from_name',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '50',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
);
$fields_emails[] = array (
'key' => 'field_' . substr( md5( $args['slug'] . 'from_email' ), 0, 13 ),
'label' => 'Addresse email de l’expéditeur',
'name' => $args['slug'] . '_from_email',
'type' => 'email',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '50',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
);
$fields_emails[] = array (
'key' => 'field_' . substr( md5( $args['slug'] ), 0, 13 ),
'label' => 'Email de ' . $args['title'],
'name' => $args['slug'],
'type' => 'textarea',
'instructions' => wp_sprintf( __( 'Vous pouvez utiliser les tags %l dans le corps du message.', 'w-espace-client' ), array_map( function ($el) {
return "<code>{$el}</code>";
} , $args['placeholders'] ) ),
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'rows' => 20,
'tabs' => 'all',
'toolbar' => 'full',
'media_upload' => 1,
'delay' => 1,
);
}
acf_add_local_field_group(array (
'key' => 'group_5956912dbe16c',
'title' => 'Formats d’emails',
'fields' => $fields_emails,
'location' => array (
array (
array (
'param' => 'options_page',
'operator' => '==',
'value' => 'emails-settings',
),
),
),
'menu_order' => 10,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
));
endif;
}
<?php
// Cette fonction peut être appelée quand on veut dans le template
function send_user_email_demo( $user_id ) {
$wrapper_title = 'options_email_user_demo';
if ( $email_inscription = get_option( $wrapper_title, '' ) ) {
$email_inscription = base64_decode( $email_inscription );
}
$email_subject = get_option( "{$wrapper_title}_from_name", sprintf( __( 'Nouveau message de %s', 'w-espace-client' ), get_bloginfo( 'name' ) ) );
$user = new WP_User( $user_id );
$email = get_option( "{$wrapper_title}_destinataire", $user->user_email );
$emetteur_name = sanitize_text_field( get_option( "{$wrapper_title}_from_name", get_bloginfo( 'name' ) ) );
$emetteur_email = sanitize_email( get_option( "{$wrapper_title}_from_email", get_option( 'admin_email' ) ) );
$emetteur = "{$emetteur_name} <{$emetteur_email}>";
// les tags doivent matcher ceux proposés dans les champs ACF
$replace = array(
'%accueil%' => get_permalink( get_option( 'page_on_front' ) ),
'%user_id%' => $user->ID,
'%display_name%' => $user->display_name,
'%prenom%' => $user->first_name,
'%email%' => $user->email,
'%login%' => $user->user_login,
);
$mail = str_ireplace( array_keys( $replace ), array_values( $replace ), $email_inscription );
$email_subject = str_ireplace( array_keys( $replace ), array_values( $replace ), $email_subject );
wp_mail( $email, $email_subject, $mail, array(
"From: {$emetteur}",
"Reply-To: {$emetteur}",
'Content-type: text/html',
) );
}
<?php
// on base_encode le contenu du mail par défaut (pour eviter tout formattage via ACF)
function is_special_mail_textarea( $name ) {
$mails = array(
'options_email_user_demo',
);
return in_array( $name, $mails );
}
add_filter( 'acf/update_value/type=textarea', 'need_sanitize_mail' , 10, 3);
function need_sanitize_mail( $value, $post_id, $field ) {
if ( $post_id === 'options' && is_special_mail_textarea( $field['name'] ) ) {
$value = base64_encode( stripslashes( $value ) );
}
return $value;
}
add_filter( 'acf/load_value/type=textarea', 'need_unsanitize_mail' , 10, 3);
function need_unsanitize_mail( $value, $post_id, $field ) {
if ( $post_id === 'options' && is_special_mail_textarea( $field['name'] ) ) {
$value = base64_decode( $value );
}
return $value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment