__('Email', 'agumail'),
'first_name' => __('First name', 'agumail'),
'last_name' => __('Last name', 'agumail'),
'full_name' => __('Full name', 'agumail'),
'phone' => __('Phone', 'agumail'),
'consent' => __('Consent', 'agumail'),
);
}
function agumail_cf7_register_property($properties, $contact_form = null) {
return (array) $properties + array(AGUMAIL_CF7_PROPERTY => array());
}
/** The form's settings: enabled ('1' or ''), tags, double_opt_in ('1' or '') and fields, part => tag name. */
function agumail_cf7_settings($contact_form) {
$saved = is_object($contact_form) && method_exists($contact_form, 'prop') ? $contact_form->prop(AGUMAIL_CF7_PROPERTY) : array();
$saved = is_array($saved) ? $saved : array();
$settings = array_merge(array('enabled' => '', 'tags' => '', 'double_opt_in' => '', 'fields' => array()), $saved);
$settings['fields'] = array_merge(array_fill_keys(array_keys(agumail_cf7_parts()), ''), is_array($settings['fields']) ? $settings['fields'] : array());
return $settings;
}
// ─────────────────────────────────────────────────────────────────────────────
// The tab.
// ─────────────────────────────────────────────────────────────────────────────
function agumail_cf7_editor_panels($panels) {
$panels = is_array($panels) ? $panels : array();
$panels['agumail-panel'] = array('title' => __('AguMail', 'agumail'), 'callback' => 'agumail_cf7_render_panel');
return $panels;
}
/** The form tags a part can point at: every named field but the submit button. */
function agumail_cf7_named_tags($contact_form) {
$tags = array();
foreach (is_object($contact_form) && method_exists($contact_form, 'scan_form_tags') ? (array) $contact_form->scan_form_tags() : array() as $tag) {
if (is_object($tag) && !empty($tag->name) && $tag->basetype !== 'submit') {
$tags[] = $tag;
}
}
return $tags;
}
function agumail_cf7_render_panel($contact_form) {
$settings = agumail_cf7_settings($contact_form);
$tags = agumail_cf7_named_tags($contact_form);
echo '
' . esc_html__('AguMail', 'agumail') . '
';
echo '';
}
/**
* Keeps the tab's settings on the form. Only when the tab was posted: a form
* saved another way (the REST API, a copy) keeps what it had. CF7 has checked
* the nonce and the merchant's capability before it saves.
*/
function agumail_cf7_save($contact_form, $args = array(), $context = 'save') {
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- verified by Contact Form 7 before it saves the form.
if (!isset($_POST['agumail-signup']) || !is_array($_POST['agumail-signup']) || !is_object($contact_form) || !method_exists($contact_form, 'set_properties')) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- as above.
$posted = map_deep(wp_unslash($_POST['agumail-signup']), 'sanitize_text_field');
$fields = array();
foreach (array_keys(agumail_cf7_parts()) as $part) {
$name = isset($posted['fields'][$part]) && is_string($posted['fields'][$part]) ? trim($posted['fields'][$part]) : '';
// A form tag name, as CF7 allows them.
$fields[$part] = preg_match('/^[A-Za-z][-A-Za-z0-9_:.]*$/', $name) ? $name : '';
}
$contact_form->set_properties(array(AGUMAIL_CF7_PROPERTY => array(
'enabled' => !empty($posted['enabled']) ? '1' : '',
'tags' => isset($posted['tags']) && is_string($posted['tags']) ? sanitize_text_field($posted['tags']) : '',
'double_opt_in' => !empty($posted['double_opt_in']) ? '1' : '',
'fields' => $fields,
)));
}
// ─────────────────────────────────────────────────────────────────────────────
// Submissions.
// ─────────────────────────────────────────────────────────────────────────────
function agumail_cf7_submit($contact_form, $result) {
try {
if (!is_object($contact_form) || !is_array($result) || !empty($result['demo_mode'])
|| (method_exists($contact_form, 'in_demo_mode') && $contact_form->in_demo_mode())) {
return;
}
if (empty($result['status']) || !in_array($result['status'], array('mail_sent', 'mail_failed'), true)) {
return;
}
$settings = agumail_cf7_settings($contact_form);
if ($settings['enabled'] !== '1' || !class_exists('WPCF7_Submission')) {
return;
}
$submission = WPCF7_Submission::get_instance();
$signup = $submission ? agumail_cf7_signup($contact_form, $submission, $settings) : null;
if ($signup && agumail_signup_enqueue('cf7', $signup['form'], $signup['data'])) {
$identity = agumail_signup_identity($signup['data']['email']);
if ($identity) {
agumail_signup_response_identity($identity);
}
}
} catch (Throwable $e) {
agumail_log('error', 'A Contact Form 7 signup was not queued: ' . $e->getMessage());
}
}
/** The signup in a submission, or null when there is none: no address, or a consent box left unticked. */
function agumail_cf7_signup($contact_form, $submission, array $settings) {
$types = array('email' => 'email', 'tel' => 'phone', 'acceptance' => 'consent');
$list = array();
$by_name = array();
foreach (agumail_cf7_named_tags($contact_form) as $tag) {
$by_name[$tag->name] = $tag;
$list[] = array(
'id' => $tag->name,
'type' => isset($types[$tag->basetype]) ? $types[$tag->basetype] : $tag->basetype,
'label' => '',
'value' => agumail_cf7_posted($submission, $tag->name),
// CF7 requires an acceptance box to be ticked unless it is marked optional.
'required' => $tag->basetype === 'acceptance' && !(method_exists($tag, 'has_option') && $tag->has_option('optional')),
);
}
$chosen = array_filter($settings['fields']);
if (!isset($chosen['consent'])) {
foreach ($by_name as $name => $tag) {
if ($tag->basetype === 'acceptance' && method_exists($tag, 'has_option') && $tag->has_option('consent_for:agumail')) {
$chosen['consent'] = $name;
break;
}
}
}
$parts = agumail_signup_detect_fields($list, $chosen);
if (!isset($parts['email'])) {
return null;
}
$consent_text = '';
if (isset($parts['consent'])) {
$tag = $by_name[$parts['consent']];
$ticked = agumail_signup_consent_ticked(agumail_cf7_posted($submission, $tag->name));
// An inverted box is ticked to say no.
if (method_exists($tag, 'has_option') && $tag->has_option('invert')) {
$ticked = !$ticked;
}
if (!$ticked) {
return null;
}
// The words next to the box, as CF7 records them for its own consent log.
$consent_text = trim((string) (!empty($tag->content) ? $tag->content : reset($tag->values)));
}
$value = function ($part) use ($parts, $submission) {
return isset($parts[$part]) ? agumail_cf7_posted($submission, $parts[$part]) : '';
};
$page = method_exists($submission, 'get_meta') ? $submission->get_meta('url') : '';
return array(
'form' => array('id' => (string) $contact_form->id(), 'name' => (string) $contact_form->title()),
'data' => array(
'email' => $value('email'),
'first_name' => $value('first_name'),
'last_name' => $value('last_name'),
'full_name' => $value('full_name'),
'phone' => $value('phone'),
'tags' => $settings['tags'],
'double_opt_in' => $settings['double_opt_in'] === '1',
'consent_text' => $consent_text,
'page_url' => is_string($page) ? $page : '',
),
);
}
/** What the visitor entered in a field, as one trimmed string (the first value of a list). */
function agumail_cf7_posted($submission, $name) {
$value = $submission->get_posted_data($name);
if (is_array($value)) {
$value = reset($value);
}
return is_scalar($value) ? trim((string) $value) : '';
}
/** Who signed up, for the AguMail script, in the answer to a submission that queued one. */
function agumail_cf7_feedback_response($response, $result = array()) {
$identity = agumail_signup_response_identity();
if ($identity && is_array($response)) {
$response['agumail'] = $identity;
}
return $response;
}