/modules/flamingo.php

  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( $contactform, $result ) { 
  10. if ( ! class_exists( 'Flamingo_Contact' ) 
  11. || ! class_exists( 'Flamingo_Inbound_Message' ) ) { 
  12. return; 
  13.  
  14. if ( $contactform->in_demo_mode() 
  15. || $contactform->is_true( 'do_not_store' ) ) { 
  16. return; 
  17.  
  18. $cases = (array) apply_filters( 'wpcf7_flamingo_submit_if',  
  19. array( 'spam', 'mail_sent', 'mail_failed' ) ); 
  20.  
  21. if ( empty( $result['status'] ) 
  22. || ! in_array( $result['status'], $cases ) ) { 
  23. return; 
  24.  
  25. $submission = WPCF7_Submission::get_instance(); 
  26.  
  27. if ( ! $submission || ! $posted_data = $submission->get_posted_data() ) { 
  28. return; 
  29.  
  30. $fields_senseless = $contactform->scan_form_tags( 
  31. array( 'type' => array( 'captchar', 'quiz', 'acceptance' ) ) ); 
  32.  
  33. $exclude_names = array(); 
  34.  
  35. foreach ( $fields_senseless as $tag ) { 
  36. $exclude_names[] = $tag['name']; 
  37.  
  38. $exclude_names[] = 'g-recaptcha-response'; 
  39.  
  40. foreach ( $posted_data as $key => $value ) { 
  41. if ( '_' == substr( $key, 0, 1 ) || in_array( $key, $exclude_names ) ) { 
  42. unset( $posted_data[$key] ); 
  43.  
  44. $email = wpcf7_flamingo_get_value( 'email', $contactform ); 
  45. $name = wpcf7_flamingo_get_value( 'name', $contactform ); 
  46. $subject = wpcf7_flamingo_get_value( 'subject', $contactform ); 
  47.  
  48. $meta = array(); 
  49.  
  50. $special_mail_tags = array( 'remote_ip', 'user_agent', 'url',  
  51. 'date', 'time', 'post_id', 'post_name', 'post_title', 'post_url',  
  52. 'post_author', 'post_author_email' ); 
  53.  
  54. foreach ( $special_mail_tags as $smt ) { 
  55. $meta[$smt] = apply_filters( 'wpcf7_special_mail_tags',  
  56. '', '_' . $smt, false ); 
  57.  
  58. $akismet = isset( $submission->akismet ) ? (array) $submission->akismet : null; 
  59.  
  60. if ( 'mail_sent' == $result['status'] ) { 
  61. $flamingo_contact = Flamingo_Contact::add( array( 
  62. 'email' => $email,  
  63. 'name' => $name ) ); 
  64.  
  65. $channel_id = wpcf7_flamingo_add_channel( 
  66. $contactform->name(), $contactform->title() ); 
  67.  
  68. if ( $channel_id ) { 
  69. $channel = get_term( $channel_id,  
  70. Flamingo_Inbound_Message::channel_taxonomy ); 
  71.  
  72. if ( ! $channel || is_wp_error( $channel ) ) { 
  73. $channel = 'contact-form-7'; 
  74. } else { 
  75. $channel = $channel->slug; 
  76. } else { 
  77. $channel = 'contact-form-7'; 
  78.  
  79. $args = array( 
  80. 'channel' => $channel,  
  81. 'subject' => $subject,  
  82. 'from' => trim( sprintf( '%s <%s>', $name, $email ) ),  
  83. 'from_name' => $name,  
  84. 'from_email' => $email,  
  85. 'fields' => $posted_data,  
  86. 'meta' => $meta,  
  87. 'akismet' => $akismet,  
  88. 'spam' => ( 'spam' == $result['status'] ) ); 
  89.  
  90. $flamingo_inbound = Flamingo_Inbound_Message::add( $args ); 
  91.  
  92. $result += array( 
  93. 'flamingo_contact_id' => 
  94. empty( $flamingo_contact ) ? 0 : absint( $flamingo_contact->id ),  
  95. 'flamingo_inbound_id' => 
  96. empty( $flamingo_inbound ) ? 0 : absint( $flamingo_inbound->id ) ); 
  97.  
  98. do_action( 'wpcf7_after_flamingo', $result ); 
  99.  
  100. function wpcf7_flamingo_get_value( $field, $contactform ) { 
  101. if ( empty( $field ) || empty( $contactform ) ) { 
  102. return false; 
  103.  
  104. $value = ''; 
  105.  
  106. if ( in_array( $field, array( 'email', 'name', 'subject' ) ) ) { 
  107. $templates = $contactform->additional_setting( 'flamingo_' . $field ); 
  108.  
  109. if ( empty( $templates[0] ) ) { 
  110. $template = sprintf( '[your-%s]', $field ); 
  111. } else { 
  112. $template = trim( wpcf7_strip_quote( $templates[0] ) ); 
  113.  
  114. $value = wpcf7_mail_replace_tags( $template ); 
  115.  
  116. $value = apply_filters( 'wpcf7_flamingo_get_value', $value,  
  117. $field, $contactform ); 
  118.  
  119. return $value; 
  120.  
  121. function wpcf7_flamingo_add_channel( $slug, $name = '' ) { 
  122. if ( ! class_exists( 'Flamingo_Inbound_Message' ) ) { 
  123. return false; 
  124.  
  125. $parent = term_exists( 'contact-form-7',  
  126. Flamingo_Inbound_Message::channel_taxonomy ); 
  127.  
  128. if ( ! $parent ) { 
  129. $parent = wp_insert_term( __( 'Contact Form 7', 'contact-form-7' ),  
  130. Flamingo_Inbound_Message::channel_taxonomy,  
  131. array( 'slug' => 'contact-form-7' ) ); 
  132.  
  133. if ( is_wp_error( $parent ) ) { 
  134. return false; 
  135.  
  136. $parent = (int) $parent['term_id']; 
  137.  
  138. if ( ! is_taxonomy_hierarchical( Flamingo_Inbound_Message::channel_taxonomy ) ) { 
  139. // backward compat for Flamingo 1.0.4 and lower 
  140. return $parent; 
  141.  
  142. if ( empty( $name ) ) { 
  143. $name = $slug; 
  144.  
  145. $channel = term_exists( $slug,  
  146. Flamingo_Inbound_Message::channel_taxonomy,  
  147. $parent ); 
  148.  
  149. if ( ! $channel ) { 
  150. $channel = wp_insert_term( $name,  
  151. Flamingo_Inbound_Message::channel_taxonomy,  
  152. array( 'slug' => $slug, 'parent' => $parent ) ); 
  153.  
  154. if ( is_wp_error( $channel ) ) { 
  155. return false; 
  156.  
  157. return (int) $channel['term_id']; 
  158.  
  159. add_filter( 'wpcf7_special_mail_tags', 'wpcf7_flamingo_serial_number', 10, 3 ); 
  160.  
  161. function wpcf7_flamingo_serial_number( $output, $name, $html ) { 
  162. if ( '_serial_number' != $name ) { 
  163. return $output; 
  164.  
  165. if ( ! class_exists( 'Flamingo_Inbound_Message' ) 
  166. || ! method_exists( 'Flamingo_Inbound_Message', 'count' ) ) { 
  167. return $output; 
  168.  
  169. if ( ! $contact_form = WPCF7_ContactForm::get_current() ) { 
  170. return $output; 
  171.  
  172. $channel_id = wpcf7_flamingo_add_channel( 
  173. $contact_form->name(), $contact_form->title() ); 
  174.  
  175. if ( $channel_id ) { 
  176. return 1 + (int) Flamingo_Inbound_Message::count( 
  177. array( 'channel_id' => $channel_id ) ); 
  178.  
  179. return 0; 
.