PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/contact-form-7/includes/classes.php

https://github.com/konscript/schroderpartners
PHP | 578 lines | 411 code | 157 blank | 10 comment | 89 complexity | fc174c2483e76b159a245b1e9451f3fe MD5 | raw file
  1. <?php
  2. class WPCF7_ContactForm {
  3. var $initial = false;
  4. var $id;
  5. var $title;
  6. var $unit_tag;
  7. var $responses_count = 0;
  8. var $scanned_form_tags;
  9. var $posted_data;
  10. var $uploaded_files = array();
  11. var $skip_mail = false;
  12. // Return true if this form is the same one as currently POSTed.
  13. function is_posted() {
  14. if ( ! isset( $_POST['_wpcf7_unit_tag'] ) || empty( $_POST['_wpcf7_unit_tag'] ) )
  15. return false;
  16. if ( $this->unit_tag == $_POST['_wpcf7_unit_tag'] )
  17. return true;
  18. return false;
  19. }
  20. function clear_post() {
  21. $fes = $this->form_scan_shortcode();
  22. foreach ( $fes as $fe ) {
  23. if ( ! isset( $fe['name'] ) || empty( $fe['name'] ) )
  24. continue;
  25. $name = $fe['name'];
  26. if ( isset( $_POST[$name] ) )
  27. unset( $_POST[$name] );
  28. }
  29. }
  30. /* Generating Form HTML */
  31. function form_html() {
  32. $form = '<div class="wpcf7" id="' . $this->unit_tag . '">';
  33. $url = wpcf7_get_request_uri();
  34. if ( $frag = strstr( $url, '#' ) )
  35. $url = substr( $url, 0, -strlen( $frag ) );
  36. $url .= '#' . $this->unit_tag;
  37. $url = apply_filters( 'wpcf7_form_action_url', $url );
  38. $enctype = apply_filters( 'wpcf7_form_enctype', '' );
  39. $class = apply_filters( 'wpcf7_form_class_attr', 'wpcf7-form' );
  40. $form .= '<form action="' . esc_url_raw( $url ) . '" method="post"'
  41. . ' class="' . esc_attr( $class ) . '"' . $enctype . '>' . "\n";
  42. $form .= '<div style="display: none;">' . "\n";
  43. $form .= '<input type="hidden" name="_wpcf7" value="'
  44. . esc_attr( $this->id ) . '" />' . "\n";
  45. $form .= '<input type="hidden" name="_wpcf7_version" value="'
  46. . esc_attr( WPCF7_VERSION ) . '" />' . "\n";
  47. $form .= '<input type="hidden" name="_wpcf7_unit_tag" value="'
  48. . esc_attr( $this->unit_tag ) . '" />' . "\n";
  49. $form .= '</div>' . "\n";
  50. $form .= $this->form_elements();
  51. if ( ! $this->responses_count )
  52. $form .= $this->form_response_output();
  53. $form .= '</form>';
  54. $form .= '</div>';
  55. return $form;
  56. }
  57. function form_response_output() {
  58. $class = 'wpcf7-response-output';
  59. $content = '';
  60. if ( $this->is_posted() ) { // Post response output for non-AJAX
  61. if ( isset( $_POST['_wpcf7_mail_sent'] ) && $_POST['_wpcf7_mail_sent']['id'] == $this->id ) {
  62. if ( $_POST['_wpcf7_mail_sent']['ok'] ) {
  63. $class .= ' wpcf7-mail-sent-ok';
  64. $content = $_POST['_wpcf7_mail_sent']['message'];
  65. } else {
  66. $class .= ' wpcf7-mail-sent-ng';
  67. if ( $_POST['_wpcf7_mail_sent']['spam'] )
  68. $class .= ' wpcf7-spam-blocked';
  69. $content = $_POST['_wpcf7_mail_sent']['message'];
  70. }
  71. } elseif ( isset( $_POST['_wpcf7_validation_errors'] ) && $_POST['_wpcf7_validation_errors']['id'] == $this->id ) {
  72. $class .= ' wpcf7-validation-errors';
  73. $content = $this->message( 'validation_error' );
  74. }
  75. } else {
  76. $class .= ' wpcf7-display-none';
  77. }
  78. $class = ' class="' . $class . '"';
  79. return '<div' . $class . '>' . $content . '</div>';
  80. }
  81. function validation_error( $name ) {
  82. if ( ! $this->is_posted() )
  83. return '';
  84. if ( ! isset( $_POST['_wpcf7_validation_errors']['messages'][$name] ) )
  85. return '';
  86. $ve = trim( $_POST['_wpcf7_validation_errors']['messages'][$name] );
  87. if ( ! empty( $ve ) ) {
  88. $ve = '<span class="wpcf7-not-valid-tip-no-ajax">' . esc_html( $ve ) . '</span>';
  89. return apply_filters( 'wpcf7_validation_error', $ve, $name, $this );
  90. }
  91. return '';
  92. }
  93. /* Form Elements */
  94. function form_do_shortcode() {
  95. global $wpcf7_shortcode_manager;
  96. $form = $this->form;
  97. if ( WPCF7_AUTOP ) {
  98. $form = $wpcf7_shortcode_manager->normalize_shortcode( $form );
  99. $form = wpcf7_autop( $form );
  100. }
  101. $form = $wpcf7_shortcode_manager->do_shortcode( $form );
  102. $this->scanned_form_tags = $wpcf7_shortcode_manager->scanned_tags;
  103. return $form;
  104. }
  105. function form_scan_shortcode( $cond = null ) {
  106. global $wpcf7_shortcode_manager;
  107. if ( ! empty( $this->scanned_form_tags ) ) {
  108. $scanned = $this->scanned_form_tags;
  109. } else {
  110. $scanned = $wpcf7_shortcode_manager->scan_shortcode( $this->form );
  111. $this->scanned_form_tags = $scanned;
  112. }
  113. if ( empty( $scanned ) )
  114. return null;
  115. if ( ! is_array( $cond ) || empty( $cond ) )
  116. return $scanned;
  117. for ( $i = 0, $size = count( $scanned ); $i < $size; $i++ ) {
  118. if ( isset( $cond['type'] ) ) {
  119. if ( is_string( $cond['type'] ) && ! empty( $cond['type'] ) ) {
  120. if ( $scanned[$i]['type'] != $cond['type'] ) {
  121. unset( $scanned[$i] );
  122. continue;
  123. }
  124. } elseif ( is_array( $cond['type'] ) ) {
  125. if ( ! in_array( $scanned[$i]['type'], $cond['type'] ) ) {
  126. unset( $scanned[$i] );
  127. continue;
  128. }
  129. }
  130. }
  131. if ( isset( $cond['name'] ) ) {
  132. if ( is_string( $cond['name'] ) && ! empty( $cond['name'] ) ) {
  133. if ( $scanned[$i]['name'] != $cond['name'] ) {
  134. unset ( $scanned[$i] );
  135. continue;
  136. }
  137. } elseif ( is_array( $cond['name'] ) ) {
  138. if ( ! in_array( $scanned[$i]['name'], $cond['name'] ) ) {
  139. unset( $scanned[$i] );
  140. continue;
  141. }
  142. }
  143. }
  144. }
  145. return array_values( $scanned );
  146. }
  147. function form_elements() {
  148. return apply_filters( 'wpcf7_form_elements', $this->form_do_shortcode() );
  149. }
  150. /* Validate */
  151. function validate() {
  152. $fes = $this->form_scan_shortcode();
  153. $result = array( 'valid' => true, 'reason' => array() );
  154. foreach ( $fes as $fe ) {
  155. $result = apply_filters( 'wpcf7_validate_' . $fe['type'], $result, $fe );
  156. }
  157. return $result;
  158. }
  159. /* Mail */
  160. function mail() {
  161. $fes = $this->form_scan_shortcode();
  162. foreach ( $fes as $fe ) {
  163. if ( empty( $fe['name'] ) )
  164. continue;
  165. $name = $fe['name'];
  166. $pipes = $fe['pipes'];
  167. $value = $_POST[$name];
  168. if ( WPCF7_USE_PIPE && is_a( $pipes, 'WPCF7_Pipes' ) && ! $pipes->zero() ) {
  169. if ( is_array( $value) ) {
  170. $new_value = array();
  171. foreach ( $value as $v ) {
  172. $new_value[] = $pipes->do_pipe( stripslashes( $v ) );
  173. }
  174. $value = $new_value;
  175. } else {
  176. $value = $pipes->do_pipe( stripslashes( $value ) );
  177. }
  178. }
  179. $this->posted_data[$name] = $value;
  180. }
  181. if ( $this->in_demo_mode() )
  182. $this->skip_mail = true;
  183. do_action_ref_array( 'wpcf7_before_send_mail', array( &$this ) );
  184. if ( $this->skip_mail )
  185. return true;
  186. if ( $this->compose_and_send_mail( $this->mail ) ) {
  187. $additional_mail = array();
  188. if ( $this->mail_2['active'] )
  189. $additional_mail[] = $this->mail_2;
  190. $additional_mail = apply_filters_ref_array( 'wpcf7_additional_mail',
  191. array( $additional_mail, &$this ) );
  192. foreach ( $additional_mail as $mail )
  193. $this->compose_and_send_mail( $mail );
  194. return true;
  195. }
  196. return false;
  197. }
  198. function compose_and_send_mail( $mail_template ) {
  199. $regex = '/\[\s*([a-zA-Z_][0-9a-zA-Z:._-]*)\s*\]/';
  200. $use_html = (bool) $mail_template['use_html'];
  201. $callback = array( &$this, 'mail_callback' );
  202. $callback_html = array( &$this, 'mail_callback_html' );
  203. $subject = preg_replace_callback( $regex, $callback, $mail_template['subject'] );
  204. $sender = preg_replace_callback( $regex, $callback, $mail_template['sender'] );
  205. $recipient = preg_replace_callback( $regex, $callback, $mail_template['recipient'] );
  206. $additional_headers =
  207. preg_replace_callback( $regex, $callback, $mail_template['additional_headers'] );
  208. if ( $use_html ) {
  209. $body = preg_replace_callback( $regex, $callback_html, $mail_template['body'] );
  210. $body = wpautop( $body );
  211. } else {
  212. $body = preg_replace_callback( $regex, $callback, $mail_template['body'] );
  213. }
  214. $attachments = array();
  215. foreach ( (array) $this->uploaded_files as $name => $path ) {
  216. if ( false === strpos( $mail_template['attachments'], "[${name}]" ) || empty( $path ) )
  217. continue;
  218. $attachments[] = $path;
  219. }
  220. extract( apply_filters_ref_array( 'wpcf7_mail_components', array(
  221. compact( 'subject', 'sender', 'body', 'recipient', 'additional_headers', 'attachments' ),
  222. &$this ) ) );
  223. $headers = "From: $sender\n";
  224. if ( $use_html )
  225. $headers .= "Content-Type: text/html\n";
  226. $headers .= trim( $additional_headers ) . "\n";
  227. return @wp_mail( $recipient, $subject, $body, $headers, $attachments );
  228. }
  229. function mail_callback_html( $matches ) {
  230. return $this->mail_callback( $matches, true );
  231. }
  232. function mail_callback( $matches, $html = false ) {
  233. if ( isset( $this->posted_data[$matches[1]] ) ) {
  234. $submitted = $this->posted_data[$matches[1]];
  235. if ( is_array( $submitted ) )
  236. $replaced = join( ', ', $submitted );
  237. else
  238. $replaced = $submitted;
  239. if ( $html ) {
  240. $replaced = strip_tags( $replaced );
  241. $replaced = wptexturize( $replaced );
  242. }
  243. $replaced = apply_filters( 'wpcf7_mail_tag_replaced', $replaced, $submitted );
  244. return stripslashes( $replaced );
  245. }
  246. if ( $special = apply_filters( 'wpcf7_special_mail_tags', '', $matches[1] ) )
  247. return $special;
  248. return $matches[0];
  249. }
  250. /* Message */
  251. function message( $status ) {
  252. $messages = $this->messages;
  253. $message = isset( $messages[$status] ) ? $messages[$status] : '';
  254. return apply_filters( 'wpcf7_display_message', $message, $status );
  255. }
  256. /* Additional settings */
  257. function additional_setting( $name, $max = 1 ) {
  258. $tmp_settings = (array) explode( "\n", $this->additional_settings );
  259. $count = 0;
  260. $values = array();
  261. foreach ( $tmp_settings as $setting ) {
  262. if ( preg_match('/^([a-zA-Z0-9_]+)\s*:(.*)$/', $setting, $matches ) ) {
  263. if ( $matches[1] != $name )
  264. continue;
  265. if ( ! $max || $count < (int) $max ) {
  266. $values[] = trim( $matches[2] );
  267. $count += 1;
  268. }
  269. }
  270. }
  271. return $values;
  272. }
  273. function in_demo_mode() {
  274. $settings = $this->additional_setting( 'demo_mode', false );
  275. foreach ( $settings as $setting ) {
  276. if ( in_array( $setting, array( 'on', 'true', '1' ) ) )
  277. return true;
  278. }
  279. return false;
  280. }
  281. /* Upgrade */
  282. function upgrade() {
  283. if ( ! isset( $this->mail['recipient'] ) )
  284. $this->mail['recipient'] = get_option( 'admin_email' );
  285. if ( ! is_array( $this->messages ) )
  286. $this->messages = array();
  287. foreach ( wpcf7_messages() as $key => $arr ) {
  288. if ( ! isset( $this->messages[$key] ) )
  289. $this->messages[$key] = $arr['default'];
  290. }
  291. }
  292. /* Save */
  293. function save() {
  294. $postarr = array(
  295. 'ID' => (int) $this->id,
  296. 'post_type' => 'wpcf7_contact_form',
  297. 'post_status' => 'publish',
  298. 'post_title' => $this->title );
  299. $post_id = wp_insert_post( $postarr );
  300. if ( $post_id ) {
  301. update_post_meta( $post_id, 'form', $this->form );
  302. update_post_meta( $post_id, 'mail', $this->mail );
  303. update_post_meta( $post_id, 'mail_2', $this->mail_2 );
  304. update_post_meta( $post_id, 'messages', $this->messages );
  305. update_post_meta( $post_id, 'additional_settings', $this->additional_settings );
  306. if ( $this->initial ) {
  307. $this->initial = false;
  308. $this->id = $post_id;
  309. do_action_ref_array( 'wpcf7_after_create', array( &$this ) );
  310. } else {
  311. do_action_ref_array( 'wpcf7_after_update', array( &$this ) );
  312. }
  313. do_action_ref_array( 'wpcf7_after_save', array( &$this ) );
  314. }
  315. return $post_id;
  316. }
  317. function copy() {
  318. $new = new WPCF7_ContactForm();
  319. $new->initial = true;
  320. $new->title = $this->title . '_copy';
  321. $new->form = $this->form;
  322. $new->mail = $this->mail;
  323. $new->mail_2 = $this->mail_2;
  324. $new->messages = $this->messages;
  325. $new->additional_settings = $this->additional_settings;
  326. $new = apply_filters_ref_array( 'wpcf7_copy', array( &$new, &$this ) );
  327. return $new;
  328. }
  329. function delete() {
  330. if ( $this->initial )
  331. return;
  332. wp_delete_post( $this->id, true );
  333. $this->initial = true;
  334. $this->id = null;
  335. }
  336. }
  337. function wpcf7_contact_form( $id ) {
  338. $post = get_post( $id );
  339. if ( empty( $post ) || 'wpcf7_contact_form' != get_post_type( $post ) )
  340. return false;
  341. $contact_form = new WPCF7_ContactForm();
  342. $contact_form->id = $post->ID;
  343. $contact_form->title = $post->post_title;
  344. $contact_form->form = get_post_meta( $post->ID, 'form', true );
  345. $contact_form->mail = get_post_meta( $post->ID, 'mail', true );
  346. $contact_form->mail_2 = get_post_meta( $post->ID, 'mail_2', true );
  347. $contact_form->messages = get_post_meta( $post->ID, 'messages', true );
  348. $contact_form->additional_settings = get_post_meta( $post->ID, 'additional_settings', true );
  349. $contact_form->upgrade();
  350. $contact_form = apply_filters_ref_array( 'wpcf7_contact_form', array( &$contact_form ) );
  351. return $contact_form;
  352. }
  353. function wpcf7_get_contact_form_by_old_id( $old_id ) {
  354. global $wpdb;
  355. $q = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_old_cf7_unit_id'"
  356. . $wpdb->prepare( " AND meta_value = %d", $old_id );
  357. if ( $new_id = $wpdb->get_var( $q ) )
  358. return wpcf7_contact_form( $new_id );
  359. }
  360. function wpcf7_contact_form_default_pack( $locale = null ) {
  361. // For backward compatibility
  362. return wpcf7_get_contact_form_default_pack( array( 'locale' => $locale ) );
  363. }
  364. function wpcf7_get_contact_form_default_pack( $args = '' ) {
  365. global $l10n;
  366. $defaults = array( 'locale' => null, 'title' => '' );
  367. $args = wp_parse_args( $args, $defaults );
  368. $locale = $args['locale'];
  369. $title = $args['title'];
  370. if ( $locale && $locale != get_locale() ) {
  371. $mo_orig = $l10n['wpcf7'];
  372. unset( $l10n['wpcf7'] );
  373. if ( 'en_US' != $locale ) {
  374. $mofile = wpcf7_plugin_path( 'languages/wpcf7-' . $locale . '.mo' );
  375. if ( ! load_textdomain( 'wpcf7', $mofile ) ) {
  376. $l10n['wpcf7'] = $mo_orig;
  377. unset( $mo_orig );
  378. }
  379. }
  380. }
  381. $contact_form = new WPCF7_ContactForm();
  382. $contact_form->initial = true;
  383. $contact_form->title = ( $title ? $title : __( 'Untitled', 'wpcf7' ) );
  384. $contact_form->form = wpcf7_get_default_template( 'form' );
  385. $contact_form->mail = wpcf7_get_default_template( 'mail' );
  386. $contact_form->mail_2 = wpcf7_get_default_template( 'mail_2' );
  387. $contact_form->messages = wpcf7_get_default_template( 'messages' );
  388. $contact_form->additional_settings = wpcf7_get_default_template( 'additional_settings' );
  389. if ( isset( $mo_orig ) )
  390. $l10n['wpcf7'] = $mo_orig;
  391. $contact_form = apply_filters_ref_array( 'wpcf7_contact_form_default_pack',
  392. array( &$contact_form, $args ) );
  393. return $contact_form;
  394. }
  395. function wpcf7_get_current_contact_form() {
  396. global $wpcf7_contact_form;
  397. if ( ! is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) )
  398. return null;
  399. return $wpcf7_contact_form;
  400. }
  401. function wpcf7_is_posted() {
  402. if ( ! $contact_form = wpcf7_get_current_contact_form() )
  403. return false;
  404. return $contact_form->is_posted();
  405. }
  406. function wpcf7_get_validation_error( $name ) {
  407. if ( ! $contact_form = wpcf7_get_current_contact_form() )
  408. return '';
  409. return $contact_form->validation_error( $name );
  410. }
  411. function wpcf7_get_message( $status ) {
  412. if ( ! $contact_form = wpcf7_get_current_contact_form() )
  413. return '';
  414. return $contact_form->message( $status );
  415. }
  416. function wpcf7_scan_shortcode( $cond = null ) {
  417. if ( ! $contact_form = wpcf7_get_current_contact_form() )
  418. return null;
  419. return $contact_form->form_scan_shortcode( $cond );
  420. }
  421. ?>