flamingo.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. ** Module for Flamingo plugin.
  4. ** http://wordpress.org/extend/plugins/flamingo/
  5. **/
  6. add_action( 'wpcf7_submit', 'wpcf7_flamingo_submit', 10, 2 );
  7. function wpcf7_flamingo_submit( $contact_form, $result ) {
  8. if ( ! class_exists( 'Flamingo_Contact' )
  9. or ! class_exists( 'Flamingo_Inbound_Message' ) ) {
  10. return;
  11. }
  12. if ( $contact_form->in_demo_mode() ) {
  13. return;
  14. }
  15. $cases = (array) apply_filters( 'wpcf7_flamingo_submit_if',
  16. array( 'spam', 'mail_sent', 'mail_failed' ) );
  17. if ( empty( $result['status'] )
  18. or ! in_array( $result['status'], $cases ) ) {
  19. return;
  20. }
  21. $submission = WPCF7_Submission::get_instance();
  22. if ( ! $submission
  23. or ! $posted_data = $submission->get_posted_data() ) {
  24. return;
  25. }
  26. if ( $submission->get_meta( 'do_not_store' ) ) {
  27. return;
  28. }
  29. $fields_senseless =
  30. $contact_form->scan_form_tags( array( 'feature' => 'do-not-store' ) );
  31. $exclude_names = array();
  32. foreach ( $fields_senseless as $tag ) {
  33. $exclude_names[] = $tag['name'];
  34. }
  35. $exclude_names[] = 'g-recaptcha-response';
  36. foreach ( $posted_data as $key => $value ) {
  37. if ( '_' == substr( $key, 0, 1 )
  38. or in_array( $key, $exclude_names ) ) {
  39. unset( $posted_data[$key] );
  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. $meta = array();
  46. $special_mail_tags = array( 'serial_number', 'remote_ip',
  47. 'user_agent', 'url', 'date', 'time', 'post_id', 'post_name',
  48. 'post_title', 'post_url', 'post_author', 'post_author_email',
  49. 'site_title', 'site_description', 'site_url', 'site_admin_email',
  50. 'user_login', 'user_email', 'user_display_name' );
  51. foreach ( $special_mail_tags as $smt ) {
  52. $meta[$smt] = apply_filters( 'wpcf7_special_mail_tags', '',
  53. sprintf( '_%s', $smt ), false );
  54. }
  55. $akismet = isset( $submission->akismet )
  56. ? (array) $submission->akismet : null;
  57. if ( 'mail_sent' == $result['status'] ) {
  58. $flamingo_contact = Flamingo_Contact::add( array(
  59. 'email' => $email,
  60. 'name' => $name,
  61. ) );
  62. }
  63. $post_meta = get_post_meta( $contact_form->id(), '_flamingo', true );
  64. $channel_id = isset( $post_meta['channel'] )
  65. ? (int) $post_meta['channel']
  66. : wpcf7_flamingo_add_channel(
  67. $contact_form->name(), $contact_form->title() );
  68. if ( $channel_id ) {
  69. if ( ! isset( $post_meta['channel'] )
  70. or $post_meta['channel'] !== $channel_id ) {
  71. $post_meta = empty( $post_meta ) ? array() : (array) $post_meta;
  72. $post_meta = array_merge( $post_meta, array(
  73. 'channel' => $channel_id,
  74. ) );
  75. update_post_meta( $contact_form->id(), '_flamingo', $post_meta );
  76. }
  77. $channel = get_term( $channel_id,
  78. Flamingo_Inbound_Message::channel_taxonomy );
  79. if ( ! $channel or is_wp_error( $channel ) ) {
  80. $channel = 'contact-form-7';
  81. } else {
  82. $channel = $channel->slug;
  83. }
  84. } else {
  85. $channel = 'contact-form-7';
  86. }
  87. $args = array(
  88. 'channel' => $channel,
  89. 'subject' => $subject,
  90. 'from' => trim( sprintf( '%s <%s>', $name, $email ) ),
  91. 'from_name' => $name,
  92. 'from_email' => $email,
  93. 'fields' => $posted_data,
  94. 'meta' => $meta,
  95. 'akismet' => $akismet,
  96. 'spam' => ( 'spam' == $result['status'] ),
  97. 'consent' => $submission->collect_consent(),
  98. );
  99. if ( $args['spam'] ) {
  100. $args['spam_log'] = $submission->get_spam_log();
  101. }
  102. if ( isset( $submission->recaptcha ) ) {
  103. $args['recaptcha'] = $submission->recaptcha;
  104. }
  105. $flamingo_inbound = Flamingo_Inbound_Message::add( $args );
  106. $result += array(
  107. 'flamingo_contact_id' =>
  108. empty( $flamingo_contact ) ? 0 : absint( $flamingo_contact->id ),
  109. 'flamingo_inbound_id' =>
  110. empty( $flamingo_inbound ) ? 0 : absint( $flamingo_inbound->id ),
  111. );
  112. do_action( 'wpcf7_after_flamingo', $result );
  113. }
  114. function wpcf7_flamingo_get_value( $field, $contact_form ) {
  115. if ( empty( $field )
  116. or empty( $contact_form ) ) {
  117. return false;
  118. }
  119. $value = '';
  120. if ( in_array( $field, array( 'email', 'name', 'subject' ) ) ) {
  121. $templates = $contact_form->additional_setting( 'flamingo_' . $field );
  122. if ( empty( $templates[0] ) ) {
  123. $template = sprintf( '[your-%s]', $field );
  124. } else {
  125. $template = trim( wpcf7_strip_quote( $templates[0] ) );
  126. }
  127. $value = wpcf7_mail_replace_tags( $template );
  128. }
  129. $value = apply_filters( 'wpcf7_flamingo_get_value', $value,
  130. $field, $contact_form );
  131. return $value;
  132. }
  133. function wpcf7_flamingo_add_channel( $slug, $name = '' ) {
  134. if ( ! class_exists( 'Flamingo_Inbound_Message' ) ) {
  135. return false;
  136. }
  137. $parent = term_exists( 'contact-form-7',
  138. Flamingo_Inbound_Message::channel_taxonomy );
  139. if ( ! $parent ) {
  140. $parent = wp_insert_term( __( 'Contact Form 7', 'contact-form-7' ),
  141. Flamingo_Inbound_Message::channel_taxonomy,
  142. array( 'slug' => 'contact-form-7' ) );
  143. if ( is_wp_error( $parent ) ) {
  144. return false;
  145. }
  146. }
  147. $parent = (int) $parent['term_id'];
  148. if ( ! is_taxonomy_hierarchical( Flamingo_Inbound_Message::channel_taxonomy ) ) {
  149. // backward compat for Flamingo 1.0.4 and lower
  150. return $parent;
  151. }
  152. if ( empty( $name ) ) {
  153. $name = $slug;
  154. }
  155. $channel = term_exists( $slug,
  156. Flamingo_Inbound_Message::channel_taxonomy,
  157. $parent );
  158. if ( ! $channel ) {
  159. $channel = wp_insert_term( $name,
  160. Flamingo_Inbound_Message::channel_taxonomy,
  161. array( 'slug' => $slug, 'parent' => $parent ) );
  162. if ( is_wp_error( $channel ) ) {
  163. return false;
  164. }
  165. }
  166. return (int) $channel['term_id'];
  167. }
  168. add_action( 'wpcf7_after_update', 'wpcf7_flamingo_update_channel', 10, 1 );
  169. function wpcf7_flamingo_update_channel( $contact_form ) {
  170. if ( ! class_exists( 'Flamingo_Inbound_Message' ) ) {
  171. return false;
  172. }
  173. $post_meta = get_post_meta( $contact_form->id(), '_flamingo', true );
  174. $channel = isset( $post_meta['channel'] )
  175. ? get_term( $post_meta['channel'],
  176. Flamingo_Inbound_Message::channel_taxonomy )
  177. : get_term_by( 'slug', $contact_form->name(),
  178. Flamingo_Inbound_Message::channel_taxonomy );
  179. if ( ! $channel or is_wp_error( $channel ) ) {
  180. return;
  181. }
  182. if ( $channel->name !== wp_unslash( $contact_form->title() ) ) {
  183. wp_update_term( $channel->term_id,
  184. Flamingo_Inbound_Message::channel_taxonomy,
  185. array(
  186. 'name' => $contact_form->title(),
  187. 'slug' => $contact_form->name(),
  188. 'parent' => $channel->parent,
  189. )
  190. );
  191. }
  192. }
  193. add_filter( 'wpcf7_special_mail_tags', 'wpcf7_flamingo_serial_number', 10, 3 );
  194. function wpcf7_flamingo_serial_number( $output, $name, $html ) {
  195. if ( '_serial_number' != $name ) {
  196. return $output;
  197. }
  198. if ( ! class_exists( 'Flamingo_Inbound_Message' )
  199. or ! method_exists( 'Flamingo_Inbound_Message', 'count' ) ) {
  200. return $output;
  201. }
  202. if ( ! $contact_form = WPCF7_ContactForm::get_current() ) {
  203. return $output;
  204. }
  205. $post_meta = get_post_meta( $contact_form->id(), '_flamingo', true );
  206. $channel_id = isset( $post_meta['channel'] )
  207. ? (int) $post_meta['channel']
  208. : wpcf7_flamingo_add_channel(
  209. $contact_form->name(), $contact_form->title() );
  210. if ( $channel_id ) {
  211. return 1 + (int) Flamingo_Inbound_Message::count(
  212. array( 'channel_id' => $channel_id ) );
  213. }
  214. return 0;
  215. }