class-contact-form-7.php
4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
defined('ABSPATH') or exit;
/**
* Class MC4WP_Contact_Form_7_Integration
*
* @ignore
*/
class MC4WP_Contact_Form_7_Integration extends MC4WP_Integration
{
/**
* @var string
*/
public $name = 'Contact Form 7';
/**
* @var string
*/
public $description = 'Subscribes people from Contact Form 7 forms.';
/**
* Add hooks
*/
public function add_hooks()
{
add_action('wpcf7_init', array( $this, 'init' ));
add_action('wpcf7_mail_sent', array( $this, 'process' ), 1);
add_action('wpcf7_posted_data', array( $this, 'alter_cf7_data' ));
}
/**
* Registers the CF7 shortcode
*
* @return boolean
*/
public function init()
{
if (function_exists('wpcf7_add_form_tag')) {
wpcf7_add_form_tag('mc4wp_checkbox', array( $this, 'shortcode' ));
} else {
wpcf7_add_shortcode('mc4wp_checkbox', array( $this, 'shortcode' ));
}
return true;
}
/**
* @{inheritdoc}
*
* Contact Form 7 listens to the following triggers.
*
* - _mc4wp_subscribe_contact-form-7
* - mc4wp-subscribe
*
* @return bool
*/
public function checkbox_was_checked()
{
$data = $this->get_data();
return ( isset($data[ $this->checkbox_name ]) && (int) $data[ $this->checkbox_name ] === 1 )
|| ( isset($data['mc4wp-subscribe']) && (int) $data['mc4wp-subscribe'] === 1 );
}
/**
* Alter Contact Form 7 data.
*
* Adds mc4wp_checkbox to post data so users can use `mc4wp_checkbox` in their email templates
*
* @param array $data
* @return array
*/
public function alter_cf7_data($data = array())
{
$data['mc4wp_checkbox'] = $this->checkbox_was_checked() ? __('Yes', 'mailchimp-for-wp') : __('No', 'mailchimp-for-wp');
return $data;
}
/**
* Subscribe from Contact Form 7 Forms
*
* @todo improve smart guessing based on selected Mailchimp lists
*
* @param WPCF7_ContactForm $cf7_form
* @return bool
*/
public function process($cf7_form)
{
// was sign-up checkbox checked?
if (! $this->checkbox_was_checked()) {
return false;
}
$parser = new MC4WP_Field_Guesser($this->get_data());
$data = $parser->combine(array( 'guessed', 'namespaced' ));
// do nothing if no email was found
if (empty($data['EMAIL'])) {
$this->get_log()->warning(sprintf('%s > Unable to find EMAIL field.', $this->name));
return false;
}
return $this->subscribe($data, $cf7_form->id());
}
/**
* Return the shortcode output
*
* @return string
*/
public function shortcode($args = array())
{
if (! empty($args['labels'][0])) {
$this->options['label'] = $args['labels'][0];
}
if (isset($args['options'])) {
// check for default:0 or default:1 to set the checked attribute
if (in_array('default:1', $args['options'], true)) {
$this->options['precheck'] = true;
} elseif (in_array('default:0', $args['options'], true)) {
$this->options['precheck'] = false;
}
}
// disable paragraph wrap because CF7 defaults to `wpautop`
$this->options['wrap_p'] = 0;
return $this->get_checkbox_html();
}
/**
* @return bool
*/
public function is_installed()
{
return function_exists('wpcf7_contact_form');
}
/**
* @since 3.0
* @return array
*/
public function get_ui_elements()
{
return array_diff(parent::get_ui_elements(), array( 'enabled', 'implicit' ));
}
/**
* @param int $object_id
* @since 3.0
* @return string
*/
public function get_object_link($object_id)
{
// for backwards compatibility, not all CF7 sign-ups have an object id
if (empty($object_id)) {
return '';
}
// Return empty string if CF7 is no longer activated.
if (! function_exists('wpcf7_contact_form')) {
return '';
}
$form = wpcf7_contact_form($object_id);
if (! is_object($form)) {
return '';
}
return sprintf('<a href="%s">%s</a>', admin_url('admin.php?page=wpcf7&post=' . $object_id), $form->title());
}
}