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' => 
  117. isset( $_POST['_wpcf7_unit_tag'] ) ? $_POST['_wpcf7_unit_tag'] : '',  
  118. ); 
  119.  
  120. $contact_form = $this->contact_form; 
  121.  
  122. if ( ! $this->validate() ) { // Validation error occured 
  123. $this->status = 'validation_failed'; 
  124. $this->response = $contact_form->message( 'validation_error' ); 
  125.  
  126. } elseif ( ! $this->accepted() ) { // Not accepted terms 
  127. $this->status = 'acceptance_missing'; 
  128. $this->response = $contact_form->message( 'accept_terms' ); 
  129.  
  130. } elseif ( $this->spam() ) { // Spam! 
  131. $this->status = 'spam'; 
  132. $this->response = $contact_form->message( 'spam' ); 
  133.  
  134. } elseif ( $this->mail() ) { 
  135. $this->status = 'mail_sent'; 
  136. $this->response = $contact_form->message( 'mail_sent_ok' ); 
  137.  
  138. do_action( 'wpcf7_mail_sent', $contact_form ); 
  139.  
  140. } else { 
  141. $this->status = 'mail_failed'; 
  142. $this->response = $contact_form->message( 'mail_sent_ng' ); 
  143.  
  144. do_action( 'wpcf7_mail_failed', $contact_form ); 
  145.  
  146. $this->remove_uploaded_files(); 
  147.  
  148. return $this->status; 
  149.  
  150. private function get_remote_ip_addr() { 
  151. if ( isset( $_SERVER['REMOTE_ADDR'] ) 
  152. && WP_Http::is_ip_address( $_SERVER['REMOTE_ADDR'] ) ) { 
  153. return $_SERVER['REMOTE_ADDR']; 
  154.  
  155. return ''; 
  156.  
  157. private function validate() { 
  158. if ( $this->invalid_fields ) { 
  159. return false; 
  160.  
  161. require_once WPCF7_PLUGIN_DIR . '/includes/validation.php'; 
  162. $result = new WPCF7_Validation(); 
  163.  
  164. $tags = $this->contact_form->scan_form_tags(); 
  165.  
  166. foreach ( $tags as $tag ) { 
  167. $type = $tag['type']; 
  168. $result = apply_filters( "wpcf7_validate_{$type}", $result, $tag ); 
  169.  
  170. $result = apply_filters( 'wpcf7_validate', $result, $tags ); 
  171.  
  172. $this->invalid_fields = $result->get_invalid_fields(); 
  173.  
  174. return $result->is_valid(); 
  175.  
  176. private function accepted() { 
  177. return apply_filters( 'wpcf7_acceptance', true ); 
  178.  
  179. private function spam() { 
  180. $spam = false; 
  181.  
  182. $user_agent = (string) $this->get_meta( 'user_agent' ); 
  183.  
  184. if ( strlen( $user_agent ) < 2 ) { 
  185. $spam = true; 
  186.  
  187. if ( WPCF7_VERIFY_NONCE && ! $this->verify_nonce() ) { 
  188. $spam = true; 
  189.  
  190. if ( $this->blacklist_check() ) { 
  191. $spam = true; 
  192.  
  193. return apply_filters( 'wpcf7_spam', $spam ); 
  194.  
  195. private function verify_nonce() { 
  196. return wpcf7_verify_nonce( $_POST['_wpnonce'], $this->contact_form->id() ); 
  197.  
  198. private function blacklist_check() { 
  199. $target = wpcf7_array_flatten( $this->posted_data ); 
  200. $target[] = $this->get_meta( 'remote_ip' ); 
  201. $target[] = $this->get_meta( 'user_agent' ); 
  202.  
  203. $target = implode( "\n", $target ); 
  204.  
  205. return wpcf7_blacklist_check( $target ); 
  206.  
  207. /** Mail */ 
  208.  
  209. private function mail() { 
  210. $contact_form = $this->contact_form; 
  211.  
  212. do_action( 'wpcf7_before_send_mail', $contact_form ); 
  213.  
  214. $skip_mail = $this->skip_mail || ! empty( $contact_form->skip_mail ); 
  215. $skip_mail = apply_filters( 'wpcf7_skip_mail', $skip_mail, $contact_form ); 
  216.  
  217. if ( $skip_mail ) { 
  218. return true; 
  219.  
  220. $result = WPCF7_Mail::send( $contact_form->prop( 'mail' ), 'mail' ); 
  221.  
  222. if ( $result ) { 
  223. $additional_mail = array(); 
  224.  
  225. if ( ( $mail_2 = $contact_form->prop( 'mail_2' ) ) && $mail_2['active'] ) { 
  226. $additional_mail['mail_2'] = $mail_2; 
  227.  
  228. $additional_mail = apply_filters( 'wpcf7_additional_mail',  
  229. $additional_mail, $contact_form ); 
  230.  
  231. foreach ( $additional_mail as $name => $template ) { 
  232. WPCF7_Mail::send( $template, $name ); 
  233.  
  234. return true; 
  235.  
  236. return false; 
  237.  
  238. public function uploaded_files() { 
  239. return $this->uploaded_files; 
  240.  
  241. public function add_uploaded_file( $name, $file_path ) { 
  242. $this->uploaded_files[$name] = $file_path; 
  243.  
  244. if ( empty( $this->posted_data[$name] ) ) { 
  245. $this->posted_data[$name] = basename( $file_path ); 
  246.  
  247. public function remove_uploaded_files() { 
  248. foreach ( (array) $this->uploaded_files as $name => $path ) { 
  249. wpcf7_rmdir_p( $path ); 
  250. @rmdir( dirname( $path ) ); // remove parent dir if it's removable (empty). 
  251.  
  252. public function get_meta( $name ) { 
  253. if ( isset( $this->meta[$name] ) ) { 
  254. return $this->meta[$name];