Plugin Directory

source: contact-form-7/trunk/modules/sendinblue/sendinblue.php

Last change on this file was 3031315, checked in by takayukister, 6 months ago

Merge changes on GitHub https://github.com/takayukister/contact-form-7

File size: 5.0 KB
Line 
1<?php
2/**
3 * Brevo module main file
4 *
5 * @link https://contactform7.com/sendinblue-integration/
6 */
7
8wpcf7_include_module_file( 'sendinblue/service.php' );
9wpcf7_include_module_file( 'sendinblue/contact-form-properties.php' );
10wpcf7_include_module_file( 'sendinblue/doi.php' );
11
12
13add_action( 'wpcf7_init', 'wpcf7_sendinblue_register_service', 10, 0 );
14
15/**
16 * Registers the Sendinblue service.
17 */
18function wpcf7_sendinblue_register_service() {
19        $integration = WPCF7_Integration::get_instance();
20
21        $integration->add_service( 'sendinblue',
22                WPCF7_Sendinblue::get_instance()
23        );
24}
25
26
27add_action( 'wpcf7_submit', 'wpcf7_sendinblue_submit', 10, 2 );
28
29/**
30 * Callback to the wpcf7_submit action hook. Creates a contact
31 * based on the submission.
32 */
33function wpcf7_sendinblue_submit( $contact_form, $result ) {
34        if ( $contact_form->in_demo_mode() ) {
35                return;
36        }
37
38        $service = WPCF7_Sendinblue::get_instance();
39
40        if ( ! $service->is_active() ) {
41                return;
42        }
43
44        if ( empty( $result['posted_data_hash'] ) ) {
45                return;
46        }
47
48        if ( empty( $result['status'] )
49        or ! in_array( $result['status'], array( 'mail_sent', 'mail_failed' ) ) ) {
50                return;
51        }
52
53        $submission = WPCF7_Submission::get_instance();
54
55        $consented = true;
56
57        foreach ( $contact_form->scan_form_tags( 'feature=name-attr' ) as $tag ) {
58                if ( $tag->has_option( 'consent_for:sendinblue' )
59                and null == $submission->get_posted_data( $tag->name ) ) {
60                        $consented = false;
61                        break;
62                }
63        }
64
65        if ( ! $consented ) {
66                return;
67        }
68
69        $prop = wp_parse_args(
70                $contact_form->prop( 'sendinblue' ),
71                array(
72                        'enable_contact_list' => false,
73                        'contact_lists' => array(),
74                        'enable_transactional_email' => false,
75                        'email_template' => 0,
76                )
77        );
78
79        if ( ! $prop['enable_contact_list'] ) {
80                return;
81        }
82
83        $attributes = wpcf7_sendinblue_collect_parameters();
84
85        $params = array(
86                'contact' => array(),
87                'email' => array(),
88        );
89
90        if ( ! empty( $attributes['EMAIL'] ) or ! empty( $attributes['SMS'] ) ) {
91                $params['contact'] = apply_filters(
92                        'wpcf7_sendinblue_contact_parameters',
93                        array(
94                                'email' => $attributes['EMAIL'],
95                                'attributes' => (object) $attributes,
96                                'listIds' => (array) $prop['contact_lists'],
97                                'updateEnabled' => false,
98                        )
99                );
100        }
101
102        if ( $prop['enable_transactional_email'] and $prop['email_template'] ) {
103                $first_name = isset( $attributes['FIRSTNAME'] )
104                        ? trim( $attributes['FIRSTNAME'] )
105                        : '';
106
107                $last_name = isset( $attributes['LASTNAME'] )
108                        ? trim( $attributes['LASTNAME'] )
109                        : '';
110
111                if ( $first_name or $last_name ) {
112                        $email_to_name = sprintf(
113                                /* translators: 1: first name, 2: last name */
114                                _x( '%1$s %2$s', 'personal name', 'contact-form-7' ),
115                                $first_name,
116                                $last_name
117                        );
118                } else {
119                        $email_to_name = '';
120                }
121
122                $params['email'] = apply_filters(
123                        'wpcf7_sendinblue_email_parameters',
124                        array(
125                                'templateId' => absint( $prop['email_template'] ),
126                                'to' => array(
127                                        array(
128                                                'name' => $email_to_name,
129                                                'email' => $attributes['EMAIL'],
130                                        ),
131                                ),
132                                'params' => (object) $attributes,
133                                'tags' => array( 'Contact Form 7' ),
134                        )
135                );
136        }
137
138        if ( is_email( $attributes['EMAIL'] ) ) {
139                $token = null;
140
141                do_action_ref_array( 'wpcf7_doi', array(
142                        'wpcf7_sendinblue',
143                        array(
144                                'email_to' => $attributes['EMAIL'],
145                                'properties' => $params,
146                        ),
147                        &$token,
148                ) );
149
150                if ( isset( $token ) ) {
151                        return;
152                }
153        }
154
155        if ( ! empty( $params['contact'] ) ) {
156                $contact_id = $service->create_contact( $params['contact'] );
157
158                if ( $contact_id and ! empty( $params['email'] ) ) {
159                        $service->send_email( $params['email'] );
160                }
161        }
162}
163
164
165/**
166 * Collects parameters for Sendinblue contact data based on submission.
167 *
168 * @return array Sendinblue contact parameters.
169 */
170function wpcf7_sendinblue_collect_parameters() {
171        $params = array();
172
173        $submission = WPCF7_Submission::get_instance();
174
175        foreach ( (array) $submission->get_posted_data() as $name => $val ) {
176                $name = strtoupper( $name );
177
178                if ( 'YOUR-' == substr( $name, 0, 5 ) ) {
179                        $name = substr( $name, 5 );
180                }
181
182                if ( $val ) {
183                        $params += array(
184                                $name => $val,
185                        );
186                }
187        }
188
189        if ( isset( $params['SMS'] ) ) {
190                $sms = implode( ' ', (array) $params['SMS'] );
191                $sms = trim( $sms );
192
193                $plus = '+' == substr( $sms, 0, 1 ) ? '+' : '';
194                $sms = preg_replace( '/[^0-9]/', '', $sms );
195
196                if ( 6 < strlen( $sms ) and strlen( $sms ) < 18 ) {
197                        $params['SMS'] = $plus . $sms;
198                } else { // Invalid phone number
199                        unset( $params['SMS'] );
200                }
201        }
202
203        if ( isset( $params['NAME'] ) ) {
204                $your_name = implode( ' ', (array) $params['NAME'] );
205                $your_name = explode( ' ', $your_name );
206
207                if ( ! isset( $params['LASTNAME'] ) ) {
208                        $params['LASTNAME'] = implode(
209                                ' ',
210                                array_slice( $your_name, 1 )
211                        );
212                }
213
214                if ( ! isset( $params['FIRSTNAME'] ) ) {
215                        $params['FIRSTNAME'] = implode(
216                                ' ',
217                                array_slice( $your_name, 0, 1 )
218                        );
219                }
220        }
221
222        $params = array_map(
223                function ( $param ) {
224                        if ( is_array( $param ) ) {
225                                $param = wpcf7_array_flatten( $param );
226                                $param = reset( $param );
227                        }
228
229                        return $param;
230                },
231                $params
232        );
233
234        $params = apply_filters(
235                'wpcf7_sendinblue_collect_parameters',
236                $params
237        );
238
239        return $params;
240}
Note: See TracBrowser for help on using the repository browser.