WPCF7_Submission

The Contact Form 7 WPCF7 Submission class.

Defined (1)

The class is defined in the following location(s).

/includes/submission.php  
  1. class WPCF7_Submission { 
  2.  
  3. private static $instance; 
  4.  
  5. private $contact_form; 
  6. private $status = 'init'; 
  7. private $posted_data = array(); 
  8. private $uploaded_files = array(); 
  9. private $skip_mail = false; 
  10. private $response = ''; 
  11. private $invalid_fields = array(); 
  12. private $meta = array(); 
  13.  
  14. private function __construct() {} 
  15.  
  16. public static function get_instance( WPCF7_ContactForm $contact_form = null ) { 
  17. if ( empty( self::$instance ) ) { 
  18. if ( null == $contact_form ) { 
  19. return null; 
  20.  
  21. self::$instance = new self; 
  22. self::$instance->contact_form = $contact_form; 
  23. self::$instance->skip_mail = $contact_form->in_demo_mode(); 
  24. self::$instance->setup_posted_data(); 
  25. self::$instance->submit(); 
  26. } elseif ( null != $contact_form ) { 
  27. return null; 
  28.  
  29. return self::$instance; 
  30.  
  31. public function get_status() { 
  32. return $this->status; 
  33.  
  34. public function is( $status ) { 
  35. return $this->status == $status; 
  36.  
  37. public function get_response() { 
  38. return $this->response; 
  39.  
  40. public function get_invalid_field( $name ) { 
  41. if ( isset( $this->invalid_fields[$name] ) ) { 
  42. return $this->invalid_fields[$name]; 
  43. } else { 
  44. return false; 
  45.  
  46. public function get_invalid_fields() { 
  47. return $this->invalid_fields; 
  48.  
  49. public function get_posted_data( $name = '' ) { 
  50. if ( ! empty( $name ) ) { 
  51. if ( isset( $this->posted_data[$name] ) ) { 
  52. return $this->posted_data[$name]; 
  53. } else { 
  54. return null; 
  55.  
  56. return $this->posted_data; 
  57.  
  58. private function setup_posted_data() { 
  59. $posted_data = (array) $_POST; 
  60. $posted_data = array_diff_key( $posted_data, array( '_wpnonce' => '' ) ); 
  61. $posted_data = $this->sanitize_posted_data( $posted_data ); 
  62.  
  63. $tags = $this->contact_form->scan_form_tags(); 
  64.  
  65. foreach ( (array) $tags as $tag ) { 
  66. if ( empty( $tag['name'] ) ) { 
  67. continue; 
  68.  
  69. $name = $tag['name']; 
  70. $value = ''; 
  71.  
  72. if ( isset( $posted_data[$name] ) ) { 
  73. $value = $posted_data[$name]; 
  74.  
  75. $pipes = $tag['pipes']; 
  76.  
  77. if ( WPCF7_USE_PIPE 
  78. && $pipes instanceof WPCF7_Pipes 
  79. && ! $pipes->zero() ) { 
  80. if ( is_array( $value) ) { 
  81. $new_value = array(); 
  82.  
  83. foreach ( $value as $v ) { 
  84. $new_value[] = $pipes->do_pipe( wp_unslash( $v ) ); 
  85.  
  86. $value = $new_value; 
  87. } else { 
  88. $value = $pipes->do_pipe( wp_unslash( $value ) ); 
  89.  
  90. $posted_data[$name] = $value; 
  91.  
  92. $this->posted_data = apply_filters( 'wpcf7_posted_data', $posted_data ); 
  93.  
  94. return $this->posted_data; 
  95.  
  96. private function sanitize_posted_data( $value ) { 
  97. if ( is_array( $value ) ) { 
  98. $value = array_map( array( $this, 'sanitize_posted_data' ), $value ); 
  99. } elseif ( is_string( $value ) ) { 
  100. $value = wp_check_invalid_utf8( $value ); 
  101. $value = wp_kses_no_null( $value ); 
  102.  
  103. return $value; 
  104.  
  105. private function submit() { 
  106. if ( ! $this->is( 'init' ) ) { 
  107. return $this->status; 
  108.  
  109. $this->meta = array( 
  110. 'remote_ip' => $this->get_remote_ip_addr(),  
  111. 'user_agent' => isset( $_SERVER['HTTP_USER_AGENT'] ) 
  112. ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : '',  
  113. 'url' => preg_replace( '%(?<!:|/)/.*$%', '',  
  114. untrailingslashit( home_url() ) ) . wpcf7_get_request_uri(),  
  115. 'timestamp' => current_time( 'timestamp' ),  
  116. 'unit_tag' => isset( $_POST['_wpcf7_unit_tag'] ) 
  117. ? $_POST['_wpcf7_unit_tag'] : '' ); 
  118.  
  119. $contact_form = $this->contact_form; 
  120.  
  121. if ( ! $this->validate() ) { // Validation error occured 
  122. $this->status = 'validation_failed'; 
  123. $this->response = $contact_form->message( 'validation_error' ); 
  124.  
  125. } elseif ( ! $this->accepted() ) { // Not accepted terms 
  126. $this->status = 'acceptance_missing'; 
  127. $this->response = $contact_form->message( 'accept_terms' ); 
  128.  
  129. } elseif ( $this->spam() ) { // Spam! 
  130. $this->status = 'spam'; 
  131. $this->response = $contact_form->message( 'spam' ); 
  132.  
  133. } elseif ( $this->mail() ) { 
  134. $this->status = 'mail_sent'; 
  135. $this->response = $contact_form->message( 'mail_sent_ok' ); 
  136.  
  137. do_action( 'wpcf7_mail_sent', $contact_form ); 
  138.  
  139. } else { 
  140. $this->status = 'mail_failed'; 
  141. $this->response = $contact_form->message( 'mail_sent_ng' ); 
  142.  
  143. do_action( 'wpcf7_mail_failed', $contact_form ); 
  144.  
  145. $this->remove_uploaded_files(); 
  146.  
  147. return $this->status; 
  148.  
  149. private function get_remote_ip_addr() { 
  150. if ( isset( $_SERVER['REMOTE_ADDR'] ) 
  151. && WP_Http::is_ip_address( $_SERVER['REMOTE_ADDR'] ) ) { 
  152. return $_SERVER['REMOTE_ADDR']; 
  153.  
  154. return ''; 
  155.  
  156. private function validate() { 
  157. if ( $this->invalid_fields ) { 
  158. return false; 
  159.  
  160. require_once WPCF7_PLUGIN_DIR . '/includes/validation.php'; 
  161. $result = new WPCF7_Validation(); 
  162.  
  163. $tags = $this->contact_form->scan_form_tags(); 
  164.  
  165. foreach ( $tags as $tag ) { 
  166. $result = apply_filters( 'wpcf7_validate_' . $tag['type'],  
  167. $result, $tag ); 
  168.  
  169. $result = apply_filters( 'wpcf7_validate', $result, $tags ); 
  170.  
  171. $this->invalid_fields = $result->get_invalid_fields(); 
  172.  
  173. return $result->is_valid(); 
  174.  
  175. private function accepted() { 
  176. return apply_filters( 'wpcf7_acceptance', true ); 
  177.  
  178. private function spam() { 
  179. $spam = false; 
  180.  
  181. $user_agent = (string) $this->get_meta( 'user_agent' ); 
  182.  
  183. if ( strlen( $user_agent ) < 2 ) { 
  184. $spam = true; 
  185.  
  186. if ( WPCF7_VERIFY_NONCE && ! $this->verify_nonce() ) { 
  187. $spam = true; 
  188.  
  189. if ( $this->blacklist_check() ) { 
  190. $spam = true; 
  191.  
  192. return apply_filters( 'wpcf7_spam', $spam ); 
  193.  
  194. private function verify_nonce() { 
  195. return wpcf7_verify_nonce( $_POST['_wpnonce'], $this->contact_form->id() ); 
  196.  
  197. private function blacklist_check() { 
  198. $target = wpcf7_array_flatten( $this->posted_data ); 
  199. $target[] = $this->get_meta( 'remote_ip' ); 
  200. $target[] = $this->get_meta( 'user_agent' ); 
  201.  
  202. $target = implode( "\n", $target ); 
  203.  
  204. return wpcf7_blacklist_check( $target ); 
  205.  
  206. /** Mail */ 
  207.  
  208. private function mail() { 
  209. $contact_form = $this->contact_form; 
  210.  
  211. do_action( 'wpcf7_before_send_mail', $contact_form ); 
  212.  
  213. $skip_mail = $this->skip_mail || ! empty( $contact_form->skip_mail ); 
  214. $skip_mail = apply_filters( 'wpcf7_skip_mail', $skip_mail, $contact_form ); 
  215.  
  216. if ( $skip_mail ) { 
  217. return true; 
  218.  
  219. $result = WPCF7_Mail::send( $contact_form->prop( 'mail' ), 'mail' ); 
  220.  
  221. if ( $result ) { 
  222. $additional_mail = array(); 
  223.  
  224. if ( ( $mail_2 = $contact_form->prop( 'mail_2' ) ) && $mail_2['active'] ) { 
  225. $additional_mail['mail_2'] = $mail_2; 
  226.  
  227. $additional_mail = apply_filters( 'wpcf7_additional_mail',  
  228. $additional_mail, $contact_form ); 
  229.  
  230. foreach ( $additional_mail as $name => $template ) { 
  231. WPCF7_Mail::send( $template, $name ); 
  232.  
  233. return true; 
  234.  
  235. return false; 
  236.  
  237. public function uploaded_files() { 
  238. return $this->uploaded_files; 
  239.  
  240. public function add_uploaded_file( $name, $file_path ) { 
  241. $this->uploaded_files[$name] = $file_path; 
  242.  
  243. if ( empty( $this->posted_data[$name] ) ) { 
  244. $this->posted_data[$name] = basename( $file_path ); 
  245.  
  246. public function remove_uploaded_files() { 
  247. foreach ( (array) $this->uploaded_files as $name => $path ) { 
  248. wpcf7_rmdir_p( $path ); 
  249. @rmdir( dirname( $path ) ); // remove parent dir if it's removable (empty). 
  250.  
  251. public function get_meta( $name ) { 
  252. if ( isset( $this->meta[$name] ) ) { 
  253. return $this->meta[$name];