first commit
This commit is contained in:
@@ -0,0 +1,447 @@
|
||||
<?php
|
||||
|
||||
add_filter(
|
||||
'wpcf7_pre_construct_contact_form_properties',
|
||||
'wpcf7_sendinblue_register_property',
|
||||
10, 2
|
||||
);
|
||||
|
||||
/**
|
||||
* Registers the sendinblue contact form property.
|
||||
*/
|
||||
function wpcf7_sendinblue_register_property( $properties, $contact_form ) {
|
||||
$service = WPCF7_Sendinblue::get_instance();
|
||||
|
||||
if ( $service->is_active() ) {
|
||||
$properties += array(
|
||||
'sendinblue' => array(),
|
||||
);
|
||||
}
|
||||
|
||||
return $properties;
|
||||
}
|
||||
|
||||
|
||||
add_action(
|
||||
'wpcf7_save_contact_form',
|
||||
'wpcf7_sendinblue_save_contact_form',
|
||||
10, 3
|
||||
);
|
||||
|
||||
/**
|
||||
* Saves the sendinblue property value.
|
||||
*/
|
||||
function wpcf7_sendinblue_save_contact_form( $contact_form, $args, $context ) {
|
||||
$service = WPCF7_Sendinblue::get_instance();
|
||||
|
||||
if ( ! $service->is_active() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$prop = wp_parse_args(
|
||||
(array) wpcf7_superglobal_post( 'wpcf7-sendinblue', array() ),
|
||||
array(
|
||||
'enable_contact_list' => false,
|
||||
'contact_lists' => array(),
|
||||
'enable_transactional_email' => false,
|
||||
'email_template' => 0,
|
||||
)
|
||||
);
|
||||
|
||||
$prop['contact_lists'] = array_map( 'absint', $prop['contact_lists'] );
|
||||
|
||||
$prop['email_template'] = absint( $prop['email_template'] );
|
||||
|
||||
$contact_form->set_properties( array(
|
||||
'sendinblue' => $prop,
|
||||
) );
|
||||
}
|
||||
|
||||
|
||||
add_filter(
|
||||
'wpcf7_editor_panels',
|
||||
'wpcf7_sendinblue_editor_panels',
|
||||
10, 1
|
||||
);
|
||||
|
||||
/**
|
||||
* Builds the editor panel for the sendinblue property.
|
||||
*/
|
||||
function wpcf7_sendinblue_editor_panels( $panels ) {
|
||||
$service = WPCF7_Sendinblue::get_instance();
|
||||
|
||||
if ( ! $service->is_active() ) {
|
||||
return $panels;
|
||||
}
|
||||
|
||||
$contact_form = WPCF7_ContactForm::get_current();
|
||||
|
||||
$prop = wp_parse_args(
|
||||
$contact_form->prop( 'sendinblue' ),
|
||||
array(
|
||||
'enable_contact_list' => false,
|
||||
'contact_lists' => array(),
|
||||
'enable_transactional_email' => false,
|
||||
'email_template' => 0,
|
||||
)
|
||||
);
|
||||
|
||||
$editor_panel = static function () use ( $prop, $service ) {
|
||||
|
||||
$description = sprintf(
|
||||
esc_html(
|
||||
/* translators: %s: link labeled 'Brevo integration' */
|
||||
__( 'You can set up the Brevo integration here. For details, see %s.', 'contact-form-7' )
|
||||
),
|
||||
wpcf7_link(
|
||||
__( 'https://contactform7.com/sendinblue-integration/', 'contact-form-7' ),
|
||||
__( 'Brevo integration', 'contact-form-7' )
|
||||
)
|
||||
);
|
||||
|
||||
$lists = wpcf7_sendinblue_get_lists();
|
||||
$templates = $service->get_templates();
|
||||
|
||||
$formatter = new WPCF7_HTMLFormatter();
|
||||
|
||||
$formatter->append_start_tag( 'h2' );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( 'Brevo', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->end_tag( 'h2' );
|
||||
|
||||
$formatter->append_start_tag( 'fieldset' );
|
||||
|
||||
$formatter->append_start_tag( 'legend' );
|
||||
|
||||
$formatter->append_preformatted( $description );
|
||||
|
||||
$formatter->end_tag( 'legend' );
|
||||
|
||||
$formatter->append_start_tag( 'table', array(
|
||||
'class' => 'form-table',
|
||||
'role' => 'presentation',
|
||||
) );
|
||||
|
||||
$formatter->append_start_tag( 'tbody' );
|
||||
|
||||
$formatter->append_start_tag( 'tr', array(
|
||||
'class' => $prop['enable_contact_list'] ? '' : 'inactive',
|
||||
) );
|
||||
|
||||
$formatter->append_start_tag( 'th', array(
|
||||
'scope' => 'row',
|
||||
) );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( 'Contact lists', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->append_start_tag( 'td' );
|
||||
|
||||
$formatter->append_start_tag( 'fieldset' );
|
||||
|
||||
$formatter->append_start_tag( 'legend', array(
|
||||
'class' => 'screen-reader-text',
|
||||
) );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( 'Contact lists', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->end_tag( 'legend' );
|
||||
|
||||
$formatter->append_start_tag( 'label', array(
|
||||
'for' => 'wpcf7-sendinblue-enable-contact-list',
|
||||
) );
|
||||
|
||||
$formatter->append_start_tag( 'input', array(
|
||||
'type' => 'checkbox',
|
||||
'name' => 'wpcf7-sendinblue[enable_contact_list]',
|
||||
'id' => 'wpcf7-sendinblue-enable-contact-list',
|
||||
'value' => '1',
|
||||
'checked' => $prop['enable_contact_list'],
|
||||
) );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( 'Add form submitters to your contact lists', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->end_tag( 'tr' );
|
||||
|
||||
$formatter->append_start_tag( 'tr' );
|
||||
|
||||
$formatter->append_start_tag( 'th', array(
|
||||
'scope' => 'row',
|
||||
) );
|
||||
|
||||
$formatter->append_start_tag( 'td' );
|
||||
|
||||
$formatter->append_start_tag( 'fieldset' );
|
||||
|
||||
if ( $lists ) {
|
||||
$formatter->append_start_tag( 'legend' );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( 'Select lists to which contacts are added:', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->end_tag( 'legend' );
|
||||
|
||||
$formatter->append_start_tag( 'ul' );
|
||||
|
||||
foreach ( $lists as $list ) {
|
||||
$formatter->append_start_tag( 'li' );
|
||||
$formatter->append_start_tag( 'label' );
|
||||
|
||||
$formatter->append_start_tag( 'input', array(
|
||||
'type' => 'checkbox',
|
||||
'name' => 'wpcf7-sendinblue[contact_lists][]',
|
||||
'value' => $list['id'],
|
||||
'checked' => in_array( $list['id'], $prop['contact_lists'] ),
|
||||
) );
|
||||
|
||||
$formatter->append_whitespace();
|
||||
|
||||
$formatter->append_preformatted( esc_html( $list['name'] ) );
|
||||
|
||||
$formatter->end_tag( 'li' );
|
||||
}
|
||||
|
||||
$formatter->end_tag( 'ul' );
|
||||
|
||||
} else {
|
||||
$formatter->append_start_tag( 'legend' );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( 'You have no contact list yet.', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->end_tag( 'legend' );
|
||||
}
|
||||
|
||||
$formatter->end_tag( 'fieldset' );
|
||||
|
||||
$formatter->append_start_tag( 'p' );
|
||||
|
||||
$formatter->append_start_tag( 'a', array(
|
||||
'href' => 'https://my.sendinblue.com/lists',
|
||||
'target' => '_blank',
|
||||
'rel' => 'external noreferrer noopener',
|
||||
) );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( 'Manage your contact lists', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->append_whitespace();
|
||||
|
||||
$formatter->append_start_tag( 'span', array(
|
||||
'class' => 'screen-reader-text',
|
||||
) );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( '(opens in a new tab)', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->end_tag( 'span' );
|
||||
|
||||
$formatter->append_start_tag( 'span', array(
|
||||
'aria-hidden' => 'true',
|
||||
'class' => 'dashicons dashicons-external',
|
||||
) );
|
||||
|
||||
$formatter->end_tag( 'p' );
|
||||
|
||||
$formatter->end_tag( 'tr' );
|
||||
|
||||
$formatter->append_start_tag( 'tr', array(
|
||||
'class' => $prop['enable_transactional_email'] ? '' : 'inactive',
|
||||
) );
|
||||
|
||||
$formatter->append_start_tag( 'th', array(
|
||||
'scope' => 'row',
|
||||
) );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( 'Welcome email', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->append_start_tag( 'td' );
|
||||
|
||||
$formatter->append_start_tag( 'fieldset' );
|
||||
|
||||
$formatter->append_start_tag( 'legend', array(
|
||||
'class' => 'screen-reader-text',
|
||||
) );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( 'Welcome email', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->end_tag( 'legend' );
|
||||
|
||||
$formatter->append_start_tag( 'label', array(
|
||||
'for' => 'wpcf7-sendinblue-enable-transactional-email',
|
||||
) );
|
||||
|
||||
$formatter->append_start_tag( 'input', array(
|
||||
'type' => 'checkbox',
|
||||
'name' => 'wpcf7-sendinblue[enable_transactional_email]',
|
||||
'id' => 'wpcf7-sendinblue-enable-transactional-email',
|
||||
'value' => '1',
|
||||
'checked' => $prop['enable_transactional_email'],
|
||||
) );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( 'Send a welcome email to new contacts', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->end_tag( 'fieldset' );
|
||||
|
||||
$formatter->end_tag( 'tr' );
|
||||
|
||||
$formatter->append_start_tag( 'tr' );
|
||||
|
||||
$formatter->append_start_tag( 'th', array(
|
||||
'scope' => 'row',
|
||||
) );
|
||||
|
||||
$formatter->append_start_tag( 'td' );
|
||||
|
||||
$formatter->append_start_tag( 'fieldset' );
|
||||
|
||||
if ( $templates ) {
|
||||
$formatter->append_start_tag( 'legend' );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( 'Select an email template:', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->end_tag( 'legend' );
|
||||
|
||||
$formatter->append_start_tag( 'select', array(
|
||||
'name' => 'wpcf7-sendinblue[email_template]',
|
||||
) );
|
||||
|
||||
$formatter->append_start_tag( 'option', array(
|
||||
'value' => 0,
|
||||
'selected' => 0 === $prop['email_template'],
|
||||
) );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( '— Select —', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->end_tag( 'option' );
|
||||
|
||||
foreach ( $templates as $template ) {
|
||||
$formatter->append_start_tag( 'option', array(
|
||||
'value' => $template['id'],
|
||||
'selected' => $prop['email_template'] === $template['id'],
|
||||
) );
|
||||
|
||||
$formatter->append_preformatted( esc_html( $template['name'] ) );
|
||||
|
||||
$formatter->end_tag( 'option' );
|
||||
}
|
||||
|
||||
$formatter->end_tag( 'select' );
|
||||
|
||||
} else {
|
||||
$formatter->append_start_tag( 'legend' );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( 'You have no active email template yet.', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->end_tag( 'legend' );
|
||||
}
|
||||
|
||||
$formatter->end_tag( 'fieldset' );
|
||||
|
||||
$formatter->append_start_tag( 'p' );
|
||||
|
||||
$formatter->append_start_tag( 'a', array(
|
||||
'href' => 'https://my.sendinblue.com/camp/lists/template',
|
||||
'target' => '_blank',
|
||||
'rel' => 'external noreferrer noopener',
|
||||
) );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( 'Manage your email templates', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->append_whitespace();
|
||||
|
||||
$formatter->append_start_tag( 'span', array(
|
||||
'class' => 'screen-reader-text',
|
||||
) );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( '(opens in a new tab)', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->end_tag( 'span' );
|
||||
|
||||
$formatter->append_start_tag( 'span', array(
|
||||
'aria-hidden' => 'true',
|
||||
'class' => 'dashicons dashicons-external',
|
||||
) );
|
||||
|
||||
$formatter->end_tag( 'p' );
|
||||
|
||||
$formatter->end_tag( 'tr' );
|
||||
|
||||
$formatter->end_tag( 'table' );
|
||||
|
||||
$formatter->print();
|
||||
};
|
||||
|
||||
$panels += array(
|
||||
'sendinblue-panel' => array(
|
||||
'title' => __( 'Brevo', 'contact-form-7' ),
|
||||
'callback' => $editor_panel,
|
||||
),
|
||||
);
|
||||
|
||||
return $panels;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves contact lists from Brevo's database.
|
||||
*/
|
||||
function wpcf7_sendinblue_get_lists() {
|
||||
static $lists = array();
|
||||
|
||||
$service = WPCF7_Sendinblue::get_instance();
|
||||
|
||||
if ( ! empty( $lists ) or ! $service->is_active() ) {
|
||||
return $lists;
|
||||
}
|
||||
|
||||
$limit = 50;
|
||||
$offset = 0;
|
||||
|
||||
while ( count( $lists ) < $limit * 10 ) {
|
||||
$lists_next = (array) $service->get_lists( array(
|
||||
'limit' => $limit,
|
||||
'offset' => $offset,
|
||||
) );
|
||||
|
||||
if ( ! empty( $lists_next ) ) {
|
||||
$lists = array_merge( $lists, $lists_next );
|
||||
}
|
||||
|
||||
if ( count( $lists_next ) < $limit ) {
|
||||
break;
|
||||
}
|
||||
|
||||
$offset += $limit;
|
||||
}
|
||||
|
||||
return $lists;
|
||||
}
|
||||
101
wp-content/plugins/contact-form-7/modules/sendinblue/doi.php
Normal file
101
wp-content/plugins/contact-form-7/modules/sendinblue/doi.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
/**
|
||||
* Double Opt-In Helper-related functions
|
||||
*
|
||||
* @link https://contactform7.com/doi-helper/
|
||||
*/
|
||||
|
||||
|
||||
add_action(
|
||||
'doihelper_init',
|
||||
'wpcf7_sendinblue_doi_register_agent',
|
||||
10, 0
|
||||
);
|
||||
|
||||
/**
|
||||
* Registers wpcf7_sendinblue as an agent.
|
||||
*/
|
||||
function wpcf7_sendinblue_doi_register_agent() {
|
||||
if ( ! function_exists( 'doihelper_register_agent' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
doihelper_register_agent( 'wpcf7_sendinblue', array(
|
||||
'optin_callback' => apply_filters(
|
||||
'wpcf7_sendinblue_doi_optin_callback',
|
||||
'wpcf7_sendinblue_doi_default_optin_callback'
|
||||
),
|
||||
'email_callback' => apply_filters(
|
||||
'wpcf7_sendinblue_doi_email_callback',
|
||||
'wpcf7_sendinblue_doi_default_email_callback'
|
||||
),
|
||||
) );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Default optin_callback function.
|
||||
*/
|
||||
function wpcf7_sendinblue_doi_default_optin_callback( $properties ) {
|
||||
$service = WPCF7_Sendinblue::get_instance();
|
||||
|
||||
if ( ! $service->is_active() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! empty( $properties['contact'] ) ) {
|
||||
$contact_id = $service->create_contact( $properties['contact'] );
|
||||
|
||||
if ( $contact_id and ! empty( $properties['email'] ) ) {
|
||||
$service->send_email( $properties['email'] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Default email_callback function.
|
||||
*/
|
||||
function wpcf7_sendinblue_doi_default_email_callback( $args ) {
|
||||
if ( ! isset( $args['token'] ) or ! isset( $args['email_to'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$site_title = wp_specialchars_decode(
|
||||
get_bloginfo( 'name' ),
|
||||
ENT_QUOTES
|
||||
);
|
||||
|
||||
$link = add_query_arg(
|
||||
array( 'doitoken' => $args['token'] ),
|
||||
home_url()
|
||||
);
|
||||
|
||||
$to = $args['email_to'];
|
||||
|
||||
$subject = sprintf(
|
||||
/* translators: %s: blog name */
|
||||
__( 'Opt-in confirmation from %s', 'contact-form-7' ),
|
||||
$site_title
|
||||
);
|
||||
|
||||
$message = sprintf(
|
||||
/* translators: 1: blog name, 2: confirmation link */
|
||||
__( 'Hello,
|
||||
|
||||
This is a confirmation email sent from %1$s.
|
||||
|
||||
We have received your submission to our web form, according to which you have allowed us to add you to our contact list. But, the process has not yet been completed. To complete it, please click the following link.
|
||||
|
||||
%2$s
|
||||
|
||||
If it was not your intention, or if you have no idea why you received this message, please do not click on the link, and ignore this message. We will never collect or use your personal data without your clear consent.
|
||||
|
||||
Sincerely,
|
||||
%1$s', 'contact-form-7' ),
|
||||
$site_title,
|
||||
$link
|
||||
);
|
||||
|
||||
wp_mail( $to, $subject, $message );
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
<?php
|
||||
/**
|
||||
* Brevo module main file
|
||||
*
|
||||
* @link https://contactform7.com/sendinblue-integration/
|
||||
*/
|
||||
|
||||
wpcf7_include_module_file( 'sendinblue/service.php' );
|
||||
wpcf7_include_module_file( 'sendinblue/contact-form-properties.php' );
|
||||
wpcf7_include_module_file( 'sendinblue/doi.php' );
|
||||
|
||||
|
||||
add_action( 'wpcf7_init', 'wpcf7_sendinblue_register_service', 10, 0 );
|
||||
|
||||
/**
|
||||
* Registers the Sendinblue service.
|
||||
*/
|
||||
function wpcf7_sendinblue_register_service() {
|
||||
$integration = WPCF7_Integration::get_instance();
|
||||
|
||||
$integration->add_service( 'sendinblue',
|
||||
WPCF7_Sendinblue::get_instance()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
add_action( 'wpcf7_submit', 'wpcf7_sendinblue_submit', 10, 2 );
|
||||
|
||||
/**
|
||||
* Callback to the wpcf7_submit action hook. Creates a contact
|
||||
* based on the submission.
|
||||
*/
|
||||
function wpcf7_sendinblue_submit( $contact_form, $result ) {
|
||||
if ( $contact_form->in_demo_mode() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$service = WPCF7_Sendinblue::get_instance();
|
||||
|
||||
if ( ! $service->is_active() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( empty( $result['posted_data_hash'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( empty( $result['status'] )
|
||||
or ! in_array( $result['status'], array( 'mail_sent', 'mail_failed' ), true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$submission = WPCF7_Submission::get_instance();
|
||||
|
||||
$consented = true;
|
||||
|
||||
foreach ( $contact_form->scan_form_tags( 'feature=name-attr' ) as $tag ) {
|
||||
if ( $tag->has_option( 'consent_for:sendinblue' )
|
||||
and null == $submission->get_posted_data( $tag->name ) ) {
|
||||
$consented = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $consented ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$prop = wp_parse_args(
|
||||
$contact_form->prop( 'sendinblue' ),
|
||||
array(
|
||||
'enable_contact_list' => false,
|
||||
'contact_lists' => array(),
|
||||
'enable_transactional_email' => false,
|
||||
'email_template' => 0,
|
||||
)
|
||||
);
|
||||
|
||||
if ( ! $prop['enable_contact_list'] ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$attributes = wpcf7_sendinblue_collect_parameters();
|
||||
|
||||
$params = array(
|
||||
'contact' => array(),
|
||||
'email' => array(),
|
||||
);
|
||||
|
||||
if ( ! empty( $attributes['EMAIL'] ) or ! empty( $attributes['SMS'] ) ) {
|
||||
$params['contact'] = apply_filters(
|
||||
'wpcf7_sendinblue_contact_parameters',
|
||||
array(
|
||||
'email' => $attributes['EMAIL'],
|
||||
'attributes' => (object) $attributes,
|
||||
'listIds' => (array) $prop['contact_lists'],
|
||||
'updateEnabled' => false,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( $prop['enable_transactional_email'] and $prop['email_template'] ) {
|
||||
$first_name = isset( $attributes['FIRSTNAME'] )
|
||||
? trim( $attributes['FIRSTNAME'] )
|
||||
: '';
|
||||
|
||||
$last_name = isset( $attributes['LASTNAME'] )
|
||||
? trim( $attributes['LASTNAME'] )
|
||||
: '';
|
||||
|
||||
if ( $first_name or $last_name ) {
|
||||
$email_to_name = sprintf(
|
||||
/* translators: 1: first name, 2: last name */
|
||||
_x( '%1$s %2$s', 'personal name', 'contact-form-7' ),
|
||||
$first_name,
|
||||
$last_name
|
||||
);
|
||||
} else {
|
||||
$email_to_name = '';
|
||||
}
|
||||
|
||||
$params['email'] = apply_filters(
|
||||
'wpcf7_sendinblue_email_parameters',
|
||||
array(
|
||||
'templateId' => absint( $prop['email_template'] ),
|
||||
'to' => array(
|
||||
array(
|
||||
'name' => $email_to_name,
|
||||
'email' => $attributes['EMAIL'],
|
||||
),
|
||||
),
|
||||
'params' => (object) $attributes,
|
||||
'tags' => array( 'Contact Form 7' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( is_email( $attributes['EMAIL'] ) ) {
|
||||
$token = null;
|
||||
|
||||
do_action_ref_array( 'wpcf7_doi', array(
|
||||
'wpcf7_sendinblue',
|
||||
array(
|
||||
'email_to' => $attributes['EMAIL'],
|
||||
'properties' => $params,
|
||||
),
|
||||
&$token,
|
||||
) );
|
||||
|
||||
if ( isset( $token ) ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $params['contact'] ) ) {
|
||||
$contact_id = $service->create_contact( $params['contact'] );
|
||||
|
||||
if ( $contact_id and ! empty( $params['email'] ) ) {
|
||||
$service->send_email( $params['email'] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Collects parameters for Sendinblue contact data based on submission.
|
||||
*
|
||||
* @return array Sendinblue contact parameters.
|
||||
*/
|
||||
function wpcf7_sendinblue_collect_parameters() {
|
||||
$params = array();
|
||||
|
||||
$submission = WPCF7_Submission::get_instance();
|
||||
|
||||
foreach ( (array) $submission->get_posted_data() as $name => $val ) {
|
||||
$name = strtoupper( $name );
|
||||
|
||||
if ( 'YOUR-' === substr( $name, 0, 5 ) ) {
|
||||
$name = substr( $name, 5 );
|
||||
}
|
||||
|
||||
if ( $val ) {
|
||||
$params += array(
|
||||
$name => $val,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $params['SMS'] ) ) {
|
||||
$sms = trim( implode( ' ', (array) $params['SMS'] ) );
|
||||
$sms = preg_replace( '/[#*].*$/', '', $sms ); // Remove extension
|
||||
|
||||
$is_international = false ||
|
||||
str_starts_with( $sms, '+' ) ||
|
||||
str_starts_with( $sms, '00' );
|
||||
|
||||
if ( $is_international ) {
|
||||
$sms = preg_replace( '/^[+0]+/', '', $sms );
|
||||
}
|
||||
|
||||
$sms = preg_replace( '/[^0-9]/', '', $sms );
|
||||
|
||||
if ( $is_international and 6 < strlen( $sms ) and strlen( $sms ) < 16 ) {
|
||||
$params['SMS'] = '+' . $sms;
|
||||
} else { // Invalid telephone number
|
||||
unset( $params['SMS'] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $params['NAME'] ) ) {
|
||||
$your_name = implode( ' ', (array) $params['NAME'] );
|
||||
$your_name = explode( ' ', $your_name );
|
||||
|
||||
if ( ! isset( $params['LASTNAME'] ) ) {
|
||||
$params['LASTNAME'] = implode(
|
||||
' ',
|
||||
array_slice( $your_name, 1 )
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! isset( $params['FIRSTNAME'] ) ) {
|
||||
$params['FIRSTNAME'] = implode(
|
||||
' ',
|
||||
array_slice( $your_name, 0, 1 )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$params = array_map(
|
||||
function ( $param ) {
|
||||
if ( is_array( $param ) ) {
|
||||
$param = wpcf7_array_flatten( $param );
|
||||
$param = reset( $param );
|
||||
}
|
||||
|
||||
return $param;
|
||||
},
|
||||
$params
|
||||
);
|
||||
|
||||
$params = apply_filters(
|
||||
'wpcf7_sendinblue_collect_parameters',
|
||||
$params
|
||||
);
|
||||
|
||||
return $params;
|
||||
}
|
||||
427
wp-content/plugins/contact-form-7/modules/sendinblue/service.php
Normal file
427
wp-content/plugins/contact-form-7/modules/sendinblue/service.php
Normal file
@@ -0,0 +1,427 @@
|
||||
<?php
|
||||
|
||||
if ( ! class_exists( 'WPCF7_Service' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
class WPCF7_Sendinblue extends WPCF7_Service {
|
||||
use WPCF7_Sendinblue_API;
|
||||
|
||||
private static $instance;
|
||||
private $api_key;
|
||||
|
||||
public static function get_instance() {
|
||||
if ( empty( self::$instance ) ) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
private function __construct() {
|
||||
$option = WPCF7::get_option( 'sendinblue' );
|
||||
|
||||
if ( isset( $option['api_key'] ) ) {
|
||||
$this->api_key = $option['api_key'];
|
||||
}
|
||||
}
|
||||
|
||||
public function get_title() {
|
||||
return __( 'Brevo', 'contact-form-7' );
|
||||
}
|
||||
|
||||
public function is_active() {
|
||||
return (bool) $this->get_api_key();
|
||||
}
|
||||
|
||||
public function get_api_key() {
|
||||
return $this->api_key;
|
||||
}
|
||||
|
||||
public function get_categories() {
|
||||
return array( 'email_marketing' );
|
||||
}
|
||||
|
||||
public function icon() {
|
||||
}
|
||||
|
||||
public function link() {
|
||||
echo wp_kses_data( wpcf7_link(
|
||||
'https://get.brevo.com/wpcf7-integration',
|
||||
'brevo.com'
|
||||
) );
|
||||
}
|
||||
|
||||
protected function log( $url, $request, $response ) {
|
||||
wpcf7_log_remote_request( $url, $request, $response );
|
||||
}
|
||||
|
||||
protected function menu_page_url( $args = '' ) {
|
||||
$args = wp_parse_args( $args, array() );
|
||||
|
||||
$url = menu_page_url( 'wpcf7-integration', false );
|
||||
$url = add_query_arg( array( 'service' => 'sendinblue' ), $url );
|
||||
|
||||
if ( ! empty( $args ) ) {
|
||||
$url = add_query_arg( $args, $url );
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
protected function save_data() {
|
||||
WPCF7::update_option( 'sendinblue', array(
|
||||
'api_key' => $this->api_key,
|
||||
) );
|
||||
}
|
||||
|
||||
protected function reset_data() {
|
||||
$this->api_key = null;
|
||||
$this->save_data();
|
||||
}
|
||||
|
||||
public function load( $action = '' ) {
|
||||
if (
|
||||
'setup' === $action and
|
||||
'POST' === wpcf7_superglobal_server( 'REQUEST_METHOD' )
|
||||
) {
|
||||
check_admin_referer( 'wpcf7-sendinblue-setup' );
|
||||
|
||||
if ( wpcf7_superglobal_post( 'reset' ) ) {
|
||||
$this->reset_data();
|
||||
$redirect_to = $this->menu_page_url( 'action=setup' );
|
||||
} else {
|
||||
$this->api_key = wpcf7_superglobal_post( 'api_key' );
|
||||
|
||||
$confirmed = $this->confirm_key();
|
||||
|
||||
if ( true === $confirmed ) {
|
||||
$redirect_to = $this->menu_page_url( array(
|
||||
'message' => 'success',
|
||||
) );
|
||||
|
||||
$this->save_data();
|
||||
} elseif ( false === $confirmed ) {
|
||||
$redirect_to = $this->menu_page_url( array(
|
||||
'action' => 'setup',
|
||||
'message' => 'unauthorized',
|
||||
) );
|
||||
} else {
|
||||
$redirect_to = $this->menu_page_url( array(
|
||||
'action' => 'setup',
|
||||
'message' => 'invalid',
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
wp_safe_redirect( $redirect_to );
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
public function admin_notice( $message = '' ) {
|
||||
if ( 'unauthorized' === $message ) {
|
||||
wp_admin_notice(
|
||||
sprintf(
|
||||
'<strong>%1$s</strong>: %2$s',
|
||||
__( 'Error', 'contact-form-7' ),
|
||||
__( 'You have not been authenticated. Make sure the provided API key is correct.', 'contact-form-7' )
|
||||
),
|
||||
array( 'type' => 'error' )
|
||||
);
|
||||
}
|
||||
|
||||
if ( 'invalid' === $message ) {
|
||||
wp_admin_notice(
|
||||
sprintf(
|
||||
'<strong>%1$s</strong>: %2$s',
|
||||
__( 'Error', 'contact-form-7' ),
|
||||
__( 'Invalid key values.', 'contact-form-7' )
|
||||
),
|
||||
array( 'type' => 'error' )
|
||||
);
|
||||
}
|
||||
|
||||
if ( 'success' === $message ) {
|
||||
wp_admin_notice(
|
||||
__( 'Settings saved.', 'contact-form-7' ),
|
||||
array( 'type' => 'success' )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function display( $action = '' ) {
|
||||
$formatter = new WPCF7_HTMLFormatter( array(
|
||||
'allowed_html' => array_merge( wpcf7_kses_allowed_html(), array(
|
||||
'form' => array(
|
||||
'action' => true,
|
||||
'method' => true,
|
||||
),
|
||||
) ),
|
||||
) );
|
||||
|
||||
$formatter->append_start_tag( 'p' );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( 'Store and organize your contacts while protecting user privacy on Brevo, the leading CRM & email marketing platform in Europe. Brevo offers unlimited contacts and advanced marketing features.', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->end_tag( 'p' );
|
||||
|
||||
$formatter->append_start_tag( 'p' );
|
||||
$formatter->append_start_tag( 'strong' );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
wpcf7_link(
|
||||
__( 'https://contactform7.com/sendinblue-integration/', 'contact-form-7' ),
|
||||
__( 'Brevo integration', 'contact-form-7' )
|
||||
)
|
||||
);
|
||||
|
||||
$formatter->end_tag( 'p' );
|
||||
|
||||
if ( $this->is_active() ) {
|
||||
$formatter->append_start_tag( 'p', array(
|
||||
'class' => 'dashicons-before dashicons-yes',
|
||||
) );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( 'Brevo is active on this site.', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->end_tag( 'p' );
|
||||
}
|
||||
|
||||
if ( 'setup' === $action ) {
|
||||
$formatter->call_user_func( function () {
|
||||
$this->display_setup();
|
||||
} );
|
||||
} else {
|
||||
$formatter->append_start_tag( 'p' );
|
||||
|
||||
$formatter->append_start_tag( 'a', array(
|
||||
'href' => esc_url( $this->menu_page_url( 'action=setup' ) ),
|
||||
'class' => 'button',
|
||||
) );
|
||||
|
||||
$formatter->append_preformatted(
|
||||
esc_html( __( 'Setup integration', 'contact-form-7' ) )
|
||||
);
|
||||
|
||||
$formatter->end_tag( 'p' );
|
||||
}
|
||||
|
||||
$formatter->print();
|
||||
}
|
||||
|
||||
private function display_setup() {
|
||||
$api_key = $this->get_api_key();
|
||||
|
||||
?>
|
||||
<form method="post" action="<?php echo esc_url( $this->menu_page_url( 'action=setup' ) ); ?>">
|
||||
<?php wp_nonce_field( 'wpcf7-sendinblue-setup' ); ?>
|
||||
<table class="form-table">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th scope="row"><label for="publishable"><?php echo esc_html( __( 'API key', 'contact-form-7' ) ); ?></label></th>
|
||||
<td><?php
|
||||
if ( $this->is_active() ) {
|
||||
echo esc_html( wpcf7_mask_password( $api_key, 4, 8 ) );
|
||||
echo sprintf(
|
||||
'<input type="hidden" value="%s" id="api_key" name="api_key" />',
|
||||
esc_attr( $api_key )
|
||||
);
|
||||
} else {
|
||||
echo sprintf(
|
||||
'<input type="text" aria-required="true" value="%s" id="api_key" name="api_key" class="regular-text code" />',
|
||||
esc_attr( $api_key )
|
||||
);
|
||||
}
|
||||
?></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php
|
||||
if ( $this->is_active() ) {
|
||||
submit_button(
|
||||
_x( 'Remove key', 'API keys', 'contact-form-7' ),
|
||||
'small', 'reset'
|
||||
);
|
||||
} else {
|
||||
submit_button( __( 'Save changes', 'contact-form-7' ) );
|
||||
}
|
||||
?>
|
||||
</form>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Trait for the Sendinblue API (v3).
|
||||
*
|
||||
* @link https://developers.sendinblue.com/reference
|
||||
*/
|
||||
trait WPCF7_Sendinblue_API {
|
||||
|
||||
|
||||
public function confirm_key() {
|
||||
$endpoint = 'https://api.sendinblue.com/v3/account';
|
||||
|
||||
$request = array(
|
||||
'headers' => array(
|
||||
'Accept' => 'application/json',
|
||||
'Content-Type' => 'application/json; charset=utf-8',
|
||||
'API-Key' => $this->get_api_key(),
|
||||
),
|
||||
);
|
||||
|
||||
$response = wp_remote_get( $endpoint, $request );
|
||||
$response_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
|
||||
if ( 200 === $response_code ) { // 200 OK
|
||||
return true;
|
||||
} elseif ( 401 === $response_code ) { // 401 Unauthorized
|
||||
return false;
|
||||
} elseif ( 400 <= $response_code ) {
|
||||
if ( WP_DEBUG ) {
|
||||
$this->log( $endpoint, $request, $response );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function get_lists( $options = '' ) {
|
||||
$options = wp_parse_args( $options, array(
|
||||
'limit' => 50,
|
||||
'offset' => 0,
|
||||
) );
|
||||
|
||||
$endpoint = add_query_arg(
|
||||
$options,
|
||||
'https://api.sendinblue.com/v3/contacts/lists'
|
||||
);
|
||||
|
||||
$request = array(
|
||||
'headers' => array(
|
||||
'Accept' => 'application/json',
|
||||
'Content-Type' => 'application/json; charset=utf-8',
|
||||
'API-Key' => $this->get_api_key(),
|
||||
),
|
||||
);
|
||||
|
||||
$response = wp_remote_get( $endpoint, $request );
|
||||
$response_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
|
||||
if ( 200 === $response_code ) { // 200 OK
|
||||
$response_body = wp_remote_retrieve_body( $response );
|
||||
$response_body = json_decode( $response_body, true );
|
||||
|
||||
if ( empty( $response_body['lists'] ) ) {
|
||||
return array();
|
||||
} else {
|
||||
return (array) $response_body['lists'];
|
||||
}
|
||||
} elseif ( 400 <= $response_code ) {
|
||||
if ( WP_DEBUG ) {
|
||||
$this->log( $endpoint, $request, $response );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function get_templates() {
|
||||
$endpoint = add_query_arg(
|
||||
array(
|
||||
'templateStatus' => 'true',
|
||||
'limit' => 100,
|
||||
'offset' => 0,
|
||||
),
|
||||
'https://api.sendinblue.com/v3/smtp/templates'
|
||||
);
|
||||
|
||||
$request = array(
|
||||
'headers' => array(
|
||||
'Accept' => 'application/json',
|
||||
'Content-Type' => 'application/json; charset=utf-8',
|
||||
'API-Key' => $this->get_api_key(),
|
||||
),
|
||||
);
|
||||
|
||||
$response = wp_remote_get( $endpoint, $request );
|
||||
$response_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
|
||||
if ( 200 === $response_code ) { // 200 OK
|
||||
$response_body = wp_remote_retrieve_body( $response );
|
||||
$response_body = json_decode( $response_body, true );
|
||||
|
||||
if ( empty( $response_body['templates'] ) ) {
|
||||
return array();
|
||||
} else {
|
||||
return (array) $response_body['templates'];
|
||||
}
|
||||
} elseif ( 400 <= $response_code ) {
|
||||
if ( WP_DEBUG ) {
|
||||
$this->log( $endpoint, $request, $response );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function create_contact( $properties ) {
|
||||
$endpoint = 'https://api.sendinblue.com/v3/contacts';
|
||||
|
||||
$request = array(
|
||||
'headers' => array(
|
||||
'Accept' => 'application/json',
|
||||
'Content-Type' => 'application/json; charset=utf-8',
|
||||
'API-Key' => $this->get_api_key(),
|
||||
),
|
||||
'body' => wp_json_encode( $properties ),
|
||||
);
|
||||
|
||||
$response = wp_remote_post( $endpoint, $request );
|
||||
$response_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
|
||||
if ( in_array( $response_code, array( 201, 204 ), true ) ) {
|
||||
$contact_id = wp_remote_retrieve_body( $response );
|
||||
return $contact_id;
|
||||
} elseif ( 400 <= $response_code ) {
|
||||
if ( WP_DEBUG ) {
|
||||
$this->log( $endpoint, $request, $response );
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function send_email( $properties ) {
|
||||
$endpoint = 'https://api.sendinblue.com/v3/smtp/email';
|
||||
|
||||
$request = array(
|
||||
'headers' => array(
|
||||
'Accept' => 'application/json',
|
||||
'Content-Type' => 'application/json; charset=utf-8',
|
||||
'API-Key' => $this->get_api_key(),
|
||||
),
|
||||
'body' => wp_json_encode( $properties ),
|
||||
);
|
||||
|
||||
$response = wp_remote_post( $endpoint, $request );
|
||||
$response_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
|
||||
if ( 201 === $response_code ) { // 201 Transactional email sent
|
||||
$message_id = wp_remote_retrieve_body( $response );
|
||||
return $message_id;
|
||||
} elseif ( 400 <= $response_code ) {
|
||||
if ( WP_DEBUG ) {
|
||||
$this->log( $endpoint, $request, $response );
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user