akismet
11 months ago
constant-contact
9 months ago
recaptcha
9 months ago
sendinblue
9 months ago
stripe
9 months ago
turnstile
9 months ago
acceptance.php
11 months ago
checkbox.php
9 months ago
count.php
1 year ago
date.php
9 months ago
disallowed-list.php
9 months ago
doi-helper.php
4 years ago
file.php
11 months ago
flamingo.php
9 months ago
hidden.php
7 years ago
listo.php
2 years ago
number.php
9 months ago
quiz.php
11 months ago
really-simple-captcha.php
9 months ago
reflection.php
3 years ago
response.php
6 years ago
select.php
9 months ago
submit.php
11 months ago
text.php
9 months ago
textarea.php
11 months ago
flamingo.php
348 lines
| 1 | <?php |
| 2 | /** |
| 3 | ** Module for Flamingo plugin. |
| 4 | ** http://wordpress.org/extend/plugins/flamingo/ |
| 5 | **/ |
| 6 | |
| 7 | add_action( 'wpcf7_submit', 'wpcf7_flamingo_submit', 10, 2 ); |
| 8 | |
| 9 | function wpcf7_flamingo_submit( $contact_form, $result ) { |
| 10 | if ( |
| 11 | ! class_exists( 'Flamingo_Contact' ) or |
| 12 | ! class_exists( 'Flamingo_Inbound_Message' ) |
| 13 | ) { |
| 14 | return; |
| 15 | } |
| 16 | |
| 17 | if ( $contact_form->in_demo_mode() ) { |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | $cases = (array) apply_filters( 'wpcf7_flamingo_submit_if', |
| 22 | array( 'spam', 'mail_sent', 'mail_failed' ) |
| 23 | ); |
| 24 | |
| 25 | if ( |
| 26 | empty( $result['status'] ) or |
| 27 | ! in_array( $result['status'], $cases, true ) |
| 28 | ) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | $submission = WPCF7_Submission::get_instance(); |
| 33 | |
| 34 | if ( ! $submission or ! $posted_data = $submission->get_posted_data() ) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | if ( $submission->get_meta( 'do_not_store' ) ) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | // Exclude do-not-store form-tag values. |
| 43 | $posted_data = array_filter( |
| 44 | $posted_data, |
| 45 | static function ( $name ) use ( $contact_form ) { |
| 46 | return ! $contact_form->scan_form_tags( array( |
| 47 | 'name' => $name, |
| 48 | 'feature' => 'do-not-store', |
| 49 | ) ); |
| 50 | }, |
| 51 | ARRAY_FILTER_USE_KEY |
| 52 | ); |
| 53 | |
| 54 | $email = wpcf7_flamingo_get_value( 'email', $contact_form ); |
| 55 | $name = wpcf7_flamingo_get_value( 'name', $contact_form ); |
| 56 | $subject = wpcf7_flamingo_get_value( 'subject', $contact_form ); |
| 57 | |
| 58 | $meta = array(); |
| 59 | |
| 60 | $special_mail_tags = array( 'serial_number', 'remote_ip', |
| 61 | 'user_agent', 'url', 'date', 'time', 'post_id', 'post_name', |
| 62 | 'post_title', 'post_url', 'post_author', 'post_author_email', |
| 63 | 'site_title', 'site_description', 'site_url', 'site_admin_email', |
| 64 | 'user_login', 'user_email', 'user_display_name', |
| 65 | ); |
| 66 | |
| 67 | foreach ( $special_mail_tags as $smt ) { |
| 68 | $tagname = sprintf( '_%s', $smt ); |
| 69 | |
| 70 | $mail_tag = new WPCF7_MailTag( |
| 71 | sprintf( '[%s]', $tagname ), |
| 72 | $tagname, |
| 73 | '' |
| 74 | ); |
| 75 | |
| 76 | $meta[$smt] = apply_filters( 'wpcf7_special_mail_tags', null, |
| 77 | $tagname, false, $mail_tag |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | $timestamp = $submission->get_meta( 'timestamp' ); |
| 82 | |
| 83 | if ( $timestamp and $datetime = date_create( '@' . $timestamp ) ) { |
| 84 | $datetime->setTimezone( wp_timezone() ); |
| 85 | $last_contacted = $datetime->format( 'Y-m-d H:i:s' ); |
| 86 | } else { |
| 87 | $last_contacted = '0000-00-00 00:00:00'; |
| 88 | } |
| 89 | |
| 90 | if ( 'mail_sent' === $result['status'] ) { |
| 91 | $flamingo_contact = Flamingo_Contact::add( array( |
| 92 | 'email' => $email, |
| 93 | 'name' => $name, |
| 94 | 'last_contacted' => $last_contacted, |
| 95 | ) ); |
| 96 | } |
| 97 | |
| 98 | $post_meta = get_post_meta( $contact_form->id(), '_flamingo', true ); |
| 99 | |
| 100 | $channel_id = isset( $post_meta['channel'] ) |
| 101 | ? (int) $post_meta['channel'] |
| 102 | : wpcf7_flamingo_add_channel( |
| 103 | $contact_form->name(), |
| 104 | $contact_form->title() |
| 105 | ); |
| 106 | |
| 107 | if ( $channel_id ) { |
| 108 | if ( |
| 109 | ! isset( $post_meta['channel'] ) or |
| 110 | $post_meta['channel'] !== $channel_id |
| 111 | ) { |
| 112 | $post_meta = empty( $post_meta ) ? array() : (array) $post_meta; |
| 113 | $post_meta = array_merge( $post_meta, array( |
| 114 | 'channel' => $channel_id, |
| 115 | ) ); |
| 116 | |
| 117 | update_post_meta( $contact_form->id(), '_flamingo', $post_meta ); |
| 118 | } |
| 119 | |
| 120 | $channel = get_term( $channel_id, |
| 121 | Flamingo_Inbound_Message::channel_taxonomy |
| 122 | ); |
| 123 | |
| 124 | if ( ! $channel or is_wp_error( $channel ) ) { |
| 125 | $channel = 'contact-form-7'; |
| 126 | } else { |
| 127 | $channel = $channel->slug; |
| 128 | } |
| 129 | } else { |
| 130 | $channel = 'contact-form-7'; |
| 131 | } |
| 132 | |
| 133 | $args = array( |
| 134 | 'channel' => $channel, |
| 135 | 'status' => $submission->get_status(), |
| 136 | 'subject' => $subject, |
| 137 | 'from' => trim( sprintf( '%s <%s>', $name, $email ) ), |
| 138 | 'from_name' => $name, |
| 139 | 'from_email' => $email, |
| 140 | 'fields' => $posted_data, |
| 141 | 'meta' => $meta, |
| 142 | 'akismet' => $submission->pull( 'akismet' ), |
| 143 | 'spam' => ( 'spam' === $result['status'] ), |
| 144 | 'consent' => $submission->collect_consent(), |
| 145 | 'timestamp' => $timestamp, |
| 146 | 'posted_data_hash' => $submission->get_posted_data_hash(), |
| 147 | ); |
| 148 | |
| 149 | if ( $args['spam'] ) { |
| 150 | $args['spam_log'] = $submission->get_spam_log(); |
| 151 | } |
| 152 | |
| 153 | $args['recaptcha'] = $submission->pull( 'recaptcha' ); |
| 154 | |
| 155 | $args = apply_filters( 'wpcf7_flamingo_inbound_message_parameters', $args ); |
| 156 | |
| 157 | $flamingo_inbound = Flamingo_Inbound_Message::add( $args ); |
| 158 | |
| 159 | if ( empty( $flamingo_contact ) ) { |
| 160 | $flamingo_contact_id = 0; |
| 161 | } elseif ( method_exists( $flamingo_contact, 'id' ) ) { |
| 162 | $flamingo_contact_id = $flamingo_contact->id(); |
| 163 | } else { |
| 164 | $flamingo_contact_id = $flamingo_contact->id; |
| 165 | } |
| 166 | |
| 167 | if ( empty( $flamingo_inbound ) ) { |
| 168 | $flamingo_inbound_id = 0; |
| 169 | } elseif ( method_exists( $flamingo_inbound, 'id' ) ) { |
| 170 | $flamingo_inbound_id = $flamingo_inbound->id(); |
| 171 | } else { |
| 172 | $flamingo_inbound_id = $flamingo_inbound->id; |
| 173 | } |
| 174 | |
| 175 | $result += array( |
| 176 | 'flamingo_contact_id' => absint( $flamingo_contact_id ), |
| 177 | 'flamingo_inbound_id' => absint( $flamingo_inbound_id ), |
| 178 | ); |
| 179 | |
| 180 | do_action( 'wpcf7_after_flamingo', $result ); |
| 181 | } |
| 182 | |
| 183 | function wpcf7_flamingo_get_value( $field, $contact_form ) { |
| 184 | if ( empty( $field ) or empty( $contact_form ) ) { |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | $value = ''; |
| 189 | |
| 190 | if ( in_array( $field, array( 'email', 'name', 'subject' ), true ) ) { |
| 191 | $template = $contact_form->pref( 'flamingo_' . $field ); |
| 192 | |
| 193 | if ( null === $template ) { |
| 194 | $template = sprintf( '[your-%s]', $field ); |
| 195 | } else { |
| 196 | $template = trim( wpcf7_strip_quote( $template ) ); |
| 197 | } |
| 198 | |
| 199 | $value = wpcf7_mail_replace_tags( $template ); |
| 200 | } |
| 201 | |
| 202 | $value = apply_filters( 'wpcf7_flamingo_get_value', $value, |
| 203 | $field, $contact_form |
| 204 | ); |
| 205 | |
| 206 | return $value; |
| 207 | } |
| 208 | |
| 209 | function wpcf7_flamingo_add_channel( $slug, $name = '' ) { |
| 210 | if ( ! class_exists( 'Flamingo_Inbound_Message' ) ) { |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | $parent = term_exists( 'contact-form-7', |
| 215 | Flamingo_Inbound_Message::channel_taxonomy |
| 216 | ); |
| 217 | |
| 218 | if ( ! $parent ) { |
| 219 | $parent = wp_insert_term( __( 'Contact Form 7', 'contact-form-7' ), |
| 220 | Flamingo_Inbound_Message::channel_taxonomy, |
| 221 | array( 'slug' => 'contact-form-7' ) |
| 222 | ); |
| 223 | |
| 224 | if ( is_wp_error( $parent ) ) { |
| 225 | return false; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | $parent = (int) $parent['term_id']; |
| 230 | |
| 231 | if ( |
| 232 | ! is_taxonomy_hierarchical( Flamingo_Inbound_Message::channel_taxonomy ) |
| 233 | ) { |
| 234 | // backward compat for Flamingo 1.0.4 and lower |
| 235 | return $parent; |
| 236 | } |
| 237 | |
| 238 | if ( empty( $name ) ) { |
| 239 | $name = $slug; |
| 240 | } |
| 241 | |
| 242 | $channel = term_exists( $slug, |
| 243 | Flamingo_Inbound_Message::channel_taxonomy, |
| 244 | $parent |
| 245 | ); |
| 246 | |
| 247 | if ( ! $channel ) { |
| 248 | $channel = wp_insert_term( $name, |
| 249 | Flamingo_Inbound_Message::channel_taxonomy, |
| 250 | array( 'slug' => $slug, 'parent' => $parent ) |
| 251 | ); |
| 252 | |
| 253 | if ( is_wp_error( $channel ) ) { |
| 254 | return false; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return (int) $channel['term_id']; |
| 259 | } |
| 260 | |
| 261 | add_action( 'wpcf7_after_update', 'wpcf7_flamingo_update_channel', 10, 1 ); |
| 262 | |
| 263 | function wpcf7_flamingo_update_channel( $contact_form ) { |
| 264 | if ( ! class_exists( 'Flamingo_Inbound_Message' ) ) { |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | $post_meta = get_post_meta( $contact_form->id(), '_flamingo', true ); |
| 269 | |
| 270 | $channel = isset( $post_meta['channel'] ) |
| 271 | ? get_term( $post_meta['channel'], |
| 272 | Flamingo_Inbound_Message::channel_taxonomy |
| 273 | ) |
| 274 | : get_term_by( 'slug', $contact_form->name(), |
| 275 | Flamingo_Inbound_Message::channel_taxonomy |
| 276 | ); |
| 277 | |
| 278 | if ( ! $channel or is_wp_error( $channel ) ) { |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | if ( $channel->name !== wp_unslash( $contact_form->title() ) ) { |
| 283 | wp_update_term( $channel->term_id, |
| 284 | Flamingo_Inbound_Message::channel_taxonomy, |
| 285 | array( |
| 286 | 'name' => $contact_form->title(), |
| 287 | 'slug' => $contact_form->name(), |
| 288 | 'parent' => $channel->parent, |
| 289 | ) |
| 290 | ); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | |
| 295 | add_filter( 'wpcf7_special_mail_tags', 'wpcf7_flamingo_serial_number', 10, 4 ); |
| 296 | |
| 297 | /** |
| 298 | * Returns output string of a special mail-tag. |
| 299 | * |
| 300 | * @param string $output The string to be output. |
| 301 | * @param string $name The tag name of the special mail-tag. |
| 302 | * @param bool $html Whether the mail-tag is used in an HTML content. |
| 303 | * @param WPCF7_MailTag $mail_tag An object representation of the mail-tag. |
| 304 | * @return string Output of the given special mail-tag. |
| 305 | */ |
| 306 | function wpcf7_flamingo_serial_number( $output, $name, $html, $mail_tag = null ) { |
| 307 | if ( ! $mail_tag instanceof WPCF7_MailTag ) { |
| 308 | wpcf7_doing_it_wrong( |
| 309 | sprintf( '%s()', __FUNCTION__ ), |
| 310 | __( 'The fourth parameter ($mail_tag) must be an instance of the WPCF7_MailTag class.', 'contact-form-7' ), |
| 311 | '5.2.2' |
| 312 | ); |
| 313 | } |
| 314 | |
| 315 | if ( '_serial_number' !== $name ) { |
| 316 | return $output; |
| 317 | } |
| 318 | |
| 319 | if ( |
| 320 | ! class_exists( 'Flamingo_Inbound_Message' ) or |
| 321 | ! method_exists( 'Flamingo_Inbound_Message', 'count' ) |
| 322 | ) { |
| 323 | return $output; |
| 324 | } |
| 325 | |
| 326 | if ( ! $contact_form = WPCF7_ContactForm::get_current() ) { |
| 327 | return $output; |
| 328 | } |
| 329 | |
| 330 | $serial_number = 0; |
| 331 | |
| 332 | $post_meta = get_post_meta( $contact_form->id(), '_flamingo', true ); |
| 333 | |
| 334 | $channel_id = isset( $post_meta['channel'] ) |
| 335 | ? (int) $post_meta['channel'] |
| 336 | : wpcf7_flamingo_add_channel( |
| 337 | $contact_form->name(), $contact_form->title() |
| 338 | ); |
| 339 | |
| 340 | if ( $channel_id ) { |
| 341 | $serial_number = 1 + Flamingo_Inbound_Message::count( array( |
| 342 | 'channel_id' => $channel_id, |
| 343 | ) ); |
| 344 | } |
| 345 | |
| 346 | return (string) $serial_number; |
| 347 | } |
| 348 |