Plugin Directory

source: contact-form-7/tags/5.9.6/modules/sendinblue/contact-form-properties.php

Last change on this file was 3043884, checked in by takayukister, 4 months ago

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

File size: 7.3 KB
Line 
1<?php
2
3add_filter(
4        'wpcf7_pre_construct_contact_form_properties',
5        'wpcf7_sendinblue_register_property',
6        10, 2
7);
8
9/**
10 * Registers the sendinblue contact form property.
11 */
12function wpcf7_sendinblue_register_property( $properties, $contact_form ) {
13        $service = WPCF7_Sendinblue::get_instance();
14
15        if ( $service->is_active() ) {
16                $properties += array(
17                        'sendinblue' => array(),
18                );
19        }
20
21        return $properties;
22}
23
24
25add_action(
26        'wpcf7_save_contact_form',
27        'wpcf7_sendinblue_save_contact_form',
28        10, 3
29);
30
31/**
32 * Saves the sendinblue property value.
33 */
34function wpcf7_sendinblue_save_contact_form( $contact_form, $args, $context ) {
35        $service = WPCF7_Sendinblue::get_instance();
36
37        if ( ! $service->is_active() ) {
38                return;
39        }
40
41        $prop = (array) ( $_POST['wpcf7-sendinblue'] ?? array() );
42
43        $prop = wp_parse_args(
44                $prop,
45                array(
46                        'enable_contact_list' => false,
47                        'contact_lists' => array(),
48                        'enable_transactional_email' => false,
49                        'email_template' => 0,
50                )
51        );
52
53        $prop['contact_lists'] = array_map( 'absint', $prop['contact_lists'] );
54
55        $prop['email_template'] = absint( $prop['email_template'] );
56
57        $contact_form->set_properties( array(
58                'sendinblue' => $prop,
59        ) );
60}
61
62
63add_filter(
64        'wpcf7_editor_panels',
65        'wpcf7_sendinblue_editor_panels',
66        10, 1
67);
68
69/**
70 * Builds the editor panel for the sendinblue property.
71 */
72function wpcf7_sendinblue_editor_panels( $panels ) {
73        $service = WPCF7_Sendinblue::get_instance();
74
75        if ( ! $service->is_active() ) {
76                return $panels;
77        }
78
79        $contact_form = WPCF7_ContactForm::get_current();
80
81        $prop = wp_parse_args(
82                $contact_form->prop( 'sendinblue' ),
83                array(
84                        'enable_contact_list' => false,
85                        'contact_lists' => array(),
86                        'enable_transactional_email' => false,
87                        'email_template' => 0,
88                )
89        );
90
91        $editor_panel = static function () use ( $prop, $service ) {
92
93                $description = sprintf(
94                        esc_html(
95                                __( "You can set up the Brevo integration here. For details, see %s.", 'contact-form-7' )
96                        ),
97                        wpcf7_link(
98                                __( 'https://contactform7.com/sendinblue-integration/', 'contact-form-7' ),
99                                __( 'Brevo integration', 'contact-form-7' )
100                        )
101                );
102
103                $lists = wpcf7_sendinblue_get_lists();
104                $templates = $service->get_templates();
105
106?>
107<h2><?php echo esc_html( __( 'Brevo', 'contact-form-7' ) ); ?></h2>
108
109<fieldset>
110        <legend><?php echo $description; ?></legend>
111
112        <table class="form-table" role="presentation">
113                <tbody>
114                        <tr class="<?php echo $prop['enable_contact_list'] ? '' : 'inactive'; ?>">
115                                <th scope="row">
116                <?php
117
118                echo esc_html( __( 'Contact lists', 'contact-form-7' ) );
119
120                ?>
121                                </th>
122                                <td>
123                                        <fieldset>
124                                                <legend class="screen-reader-text">
125                <?php
126
127                echo esc_html( __( 'Contact lists', 'contact-form-7' ) );
128
129                ?>
130                                                </legend>
131                                                <label for="wpcf7-sendinblue-enable-contact-list">
132                                                        <input type="checkbox" name="wpcf7-sendinblue[enable_contact_list]" id="wpcf7-sendinblue-enable-contact-list" value="1" <?php checked( $prop['enable_contact_list'] ); ?> />
133                <?php
134
135                echo esc_html(
136                        __( "Add form submitters to your contact lists", 'contact-form-7' )
137                );
138
139                ?>
140                                                </label>
141                                        </fieldset>
142                                </td>
143                        </tr>
144                        <tr>
145                                <th scope="row"></th>
146                                <td>
147                                        <fieldset>
148                <?php
149
150                if ( $lists ) {
151                        echo sprintf(
152                                '<legend>%1$s</legend>',
153                                esc_html( __( 'Select lists to which contacts are added:', 'contact-form-7' ) )
154                        );
155
156                        echo '<ul>';
157
158                        foreach ( $lists as $list ) {
159                                echo sprintf(
160                                        '<li><label><input %1$s /> %2$s</label></li>',
161                                        wpcf7_format_atts( array(
162                                                'type' => 'checkbox',
163                                                'name' => 'wpcf7-sendinblue[contact_lists][]',
164                                                'value' => $list['id'],
165                                                'checked' => in_array( $list['id'], $prop['contact_lists'] ),
166                                        ) ),
167                                        esc_html( $list['name'] )
168                                );
169                        }
170
171                        echo '</ul>';
172                } else {
173                        echo sprintf(
174                                '<legend>%1$s</legend>',
175                                esc_html( __( 'You have no contact list yet.', 'contact-form-7' ) )
176                        );
177                }
178
179                ?>
180                                        </fieldset>
181                <?php
182
183                echo sprintf(
184                        '<p><a %1$s>%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a></p>',
185                        wpcf7_format_atts( array(
186                                'href' => 'https://my.sendinblue.com/lists',
187                                'target' => '_blank',
188                                'rel' => 'external noreferrer noopener',
189                        ) ),
190                        esc_html( __( 'Manage your contact lists', 'contact-form-7' ) ),
191                        esc_html( __( '(opens in a new tab)', 'contact-form-7' ) )
192                );
193
194                ?>
195                                </td>
196                        </tr>
197                        <tr class="<?php echo $prop['enable_transactional_email'] ? '' : 'inactive'; ?>">
198                                <th scope="row">
199                <?php
200
201                echo esc_html( __( 'Welcome email', 'contact-form-7' ) );
202
203                ?>
204                                </th>
205                                <td>
206                                        <fieldset>
207                                                <legend class="screen-reader-text">
208                <?php
209
210                echo esc_html( __( 'Welcome email', 'contact-form-7' ) );
211
212                ?>
213                                                </legend>
214                                                <label for="wpcf7-sendinblue-enable-transactional-email">
215                                                        <input type="checkbox" name="wpcf7-sendinblue[enable_transactional_email]" id="wpcf7-sendinblue-enable-transactional-email" value="1" <?php checked( $prop['enable_transactional_email'] ); ?> />
216                <?php
217
218                echo esc_html(
219                        __( "Send a welcome email to new contacts", 'contact-form-7' )
220                );
221
222                ?>
223                                                </label>
224                                        </fieldset>
225                                </td>
226                        </tr>
227                        <tr>
228                                <th scope="row"></th>
229                                <td>
230                                        <fieldset>
231                <?php
232
233                if ( $templates ) {
234                        echo sprintf(
235                                '<legend>%1$s</legend>',
236                                esc_html( __( 'Select an email template:', 'contact-form-7' ) )
237                        );
238
239                        echo '<select name="wpcf7-sendinblue[email_template]">';
240
241                        echo sprintf(
242                                '<option %1$s>%2$s</option>',
243                                wpcf7_format_atts( array(
244                                        'value' => 0,
245                                        'selected' => 0 === $prop['email_template'],
246                                ) ),
247                                esc_html( __( '&mdash; Select &mdash;', 'contact-form-7' ) )
248                        );
249
250                        foreach ( $templates as $template ) {
251                                echo sprintf(
252                                        '<option %1$s>%2$s</option>',
253                                        wpcf7_format_atts( array(
254                                                'value' => $template['id'],
255                                                'selected' => $prop['email_template'] === $template['id'],
256                                        ) ),
257                                        esc_html( $template['name'] )
258                                );
259                        }
260
261                        echo '</select>';
262                } else {
263                        echo sprintf(
264                                '<legend>%1$s</legend>',
265                                esc_html( __( 'You have no active email template yet.', 'contact-form-7' ) )
266                        );
267                }
268
269                ?>
270                                        </fieldset>
271                <?php
272
273                echo sprintf(
274                        '<p><a %1$s>%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a></p>',
275                        wpcf7_format_atts( array(
276                                'href' => 'https://my.sendinblue.com/camp/lists/template',
277                                'target' => '_blank',
278                                'rel' => 'external noreferrer noopener',
279                        ) ),
280                        esc_html( __( 'Manage your email templates', 'contact-form-7' ) ),
281                        esc_html( __( '(opens in a new tab)', 'contact-form-7' ) )
282                );
283
284                ?>
285                                </td>
286                        </tr>
287                </tbody>
288        </table>
289</fieldset>
290<?php
291        };
292
293        $panels += array(
294                'sendinblue-panel' => array(
295                        'title' => __( 'Brevo', 'contact-form-7' ),
296                        'callback' => $editor_panel,
297                ),
298        );
299
300        return $panels;
301}
302
303
304/**
305 * Retrieves contact lists from Brevo's database.
306 */
307function wpcf7_sendinblue_get_lists() {
308        static $lists = array();
309
310        $service = WPCF7_Sendinblue::get_instance();
311
312        if ( ! empty( $lists ) or ! $service->is_active() ) {
313                return $lists;
314        }
315
316        $limit = 50;
317        $offset = 0;
318
319        while ( count( $lists ) < $limit * 10 ) {
320                $lists_next = (array) $service->get_lists( array(
321                        'limit' => $limit,
322                        'offset' => $offset,
323                ) );
324
325                if ( ! empty( $lists_next ) ) {
326                        $lists = array_merge( $lists, $lists_next );
327                }
328
329                if ( count( $lists_next ) < $limit ) {
330                        break;
331                }
332
333                $offset += $limit;
334        }
335
336        return $lists;
337}
Note: See TracBrowser for help on using the repository browser.