PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/Jezfx/synergy
PHP | 1084 lines | 777 code | 296 blank | 11 comment | 132 complexity | e132f814a0797ad01409ebbb5190c4f1 MD5 | raw file
Possible License(s): GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. class WPCF7_ContactForm {
  3. const post_type = 'wpcf7_contact_form';
  4. private static $found_items = 0;
  5. private static $current = null;
  6. private static $submission = array(); // result of submit() process
  7. public $initial = false;
  8. public $id;
  9. public $name;
  10. public $title;
  11. public $unit_tag;
  12. public $responses_count = 0;
  13. public $scanned_form_tags;
  14. public $posted_data;
  15. public $uploaded_files = array();
  16. public $skip_mail = false;
  17. public static function count() {
  18. return self::$found_items;
  19. }
  20. public static function set_current( self $obj ) {
  21. self::$current = $obj;
  22. }
  23. public static function get_current() {
  24. return self::$current;
  25. }
  26. public static function reset_current() {
  27. self::$current = null;
  28. }
  29. private static function add_submission_status( $id, $status ) {
  30. self::$submission[$id] = (array) $status;
  31. }
  32. private static function get_submission_status( $id ) {
  33. if ( isset( self::$submission[$id] ) ) {
  34. return (array) self::$submission[$id];
  35. }
  36. }
  37. private static function get_unit_tag( $id = 0 ) {
  38. static $global_count = 0;
  39. $global_count += 1;
  40. if ( in_the_loop() ) {
  41. $unit_tag = sprintf( 'wpcf7-f%1$d-p%2$d-o%3$d',
  42. absint( $id ), get_the_ID(), $global_count );
  43. } else {
  44. $unit_tag = sprintf( 'wpcf7-f%1$d-o%2$d',
  45. absint( $id ), $global_count );
  46. }
  47. return $unit_tag;
  48. }
  49. public static function register_post_type() {
  50. register_post_type( self::post_type, array(
  51. 'labels' => array(
  52. 'name' => __( 'Contact Forms', 'contact-form-7' ),
  53. 'singular_name' => __( 'Contact Form', 'contact-form-7' ) ),
  54. 'rewrite' => false,
  55. 'query_var' => false ) );
  56. }
  57. public static function find( $args = '' ) {
  58. $defaults = array(
  59. 'post_status' => 'any',
  60. 'posts_per_page' => -1,
  61. 'offset' => 0,
  62. 'orderby' => 'ID',
  63. 'order' => 'ASC' );
  64. $args = wp_parse_args( $args, $defaults );
  65. $args['post_type'] = self::post_type;
  66. $q = new WP_Query();
  67. $posts = $q->query( $args );
  68. self::$found_items = $q->found_posts;
  69. $objs = array();
  70. foreach ( (array) $posts as $post )
  71. $objs[] = new self( $post );
  72. return $objs;
  73. }
  74. public function __construct( $post = null ) {
  75. $this->initial = true;
  76. $this->form = '';
  77. $this->mail = array();
  78. $this->mail_2 = array();
  79. $this->messages = array();
  80. $this->additional_settings = '';
  81. $post = get_post( $post );
  82. if ( $post && self::post_type == get_post_type( $post ) ) {
  83. $this->initial = false;
  84. $this->id = $post->ID;
  85. $this->name = $post->post_name;
  86. $this->title = $post->post_title;
  87. $this->locale = get_post_meta( $post->ID, '_locale', true );
  88. $props = $this->get_properties();
  89. foreach ( $props as $prop => $value ) {
  90. if ( metadata_exists( 'post', $post->ID, '_' . $prop ) )
  91. $this->{$prop} = get_post_meta( $post->ID, '_' . $prop, true );
  92. else
  93. $this->{$prop} = get_post_meta( $post->ID, $prop, true );
  94. }
  95. $this->upgrade();
  96. }
  97. do_action_ref_array( 'wpcf7_contact_form', array( &$this ) );
  98. }
  99. public static function generate_default_package( $args = '' ) {
  100. global $l10n;
  101. $defaults = array( 'locale' => null, 'title' => '' );
  102. $args = wp_parse_args( $args, $defaults );
  103. $locale = $args['locale'];
  104. $title = $args['title'];
  105. if ( $locale ) {
  106. $mo_orig = $l10n['contact-form-7'];
  107. wpcf7_load_textdomain( $locale );
  108. }
  109. $contact_form = new self;
  110. $contact_form->initial = true;
  111. $contact_form->title =
  112. ( $title ? $title : __( 'Untitled', 'contact-form-7' ) );
  113. $contact_form->locale = ( $locale ? $locale : get_locale() );
  114. $props = $contact_form->get_properties();
  115. foreach ( $props as $prop => $value ) {
  116. $contact_form->{$prop} = wpcf7_get_default_template( $prop );
  117. }
  118. $contact_form = apply_filters( 'wpcf7_contact_form_default_pack',
  119. $contact_form, $args );
  120. if ( isset( $mo_orig ) ) {
  121. $l10n['contact-form-7'] = $mo_orig;
  122. }
  123. return $contact_form;
  124. }
  125. public function get_properties() {
  126. $prop_names = array( 'form', 'mail', 'mail_2', 'messages', 'additional_settings' );
  127. $properties = array();
  128. foreach ( $prop_names as $prop_name )
  129. $properties[$prop_name] = isset( $this->{$prop_name} ) ? $this->{$prop_name} : '';
  130. return apply_filters( 'wpcf7_contact_form_properties', $properties, $this );
  131. }
  132. // Return true if this form is the same one as currently POSTed.
  133. public function is_posted() {
  134. if ( ! isset( $_POST['_wpcf7_unit_tag'] ) || empty( $_POST['_wpcf7_unit_tag'] ) )
  135. return false;
  136. if ( $this->unit_tag == $_POST['_wpcf7_unit_tag'] )
  137. return true;
  138. return false;
  139. }
  140. public function clear_post() {
  141. $fes = $this->form_scan_shortcode();
  142. foreach ( $fes as $fe ) {
  143. if ( ! isset( $fe['name'] ) || empty( $fe['name'] ) )
  144. continue;
  145. $name = $fe['name'];
  146. if ( isset( $_POST[$name] ) )
  147. unset( $_POST[$name] );
  148. }
  149. }
  150. /* Generating Form HTML */
  151. public function form_html( $atts = array() ) {
  152. $atts = wp_parse_args( $atts, array(
  153. 'html_id' => '',
  154. 'html_name' => '',
  155. 'html_class' => '' ) );
  156. $this->unit_tag = self::get_unit_tag( $this->id );
  157. $html = '<div class="wpcf7" id="' . $this->unit_tag . '">' . "\n";
  158. $html .= $this->screen_reader_response() . "\n";
  159. $url = wpcf7_get_request_uri();
  160. if ( $frag = strstr( $url, '#' ) )
  161. $url = substr( $url, 0, -strlen( $frag ) );
  162. $url .= '#' . $this->unit_tag;
  163. $url = apply_filters( 'wpcf7_form_action_url', $url );
  164. $id_attr = apply_filters( 'wpcf7_form_id_attr',
  165. preg_replace( '/[^A-Za-z0-9:._-]/', '', $atts['html_id'] ) );
  166. $name_attr = apply_filters( 'wpcf7_form_name_attr',
  167. preg_replace( '/[^A-Za-z0-9:._-]/', '', $atts['html_name'] ) );
  168. $class = 'wpcf7-form';
  169. $result = self::get_submission_status( $this->id );
  170. if ( $this->is_posted() ) {
  171. if ( empty( $result['valid'] ) )
  172. $class .= ' invalid';
  173. elseif ( ! empty( $result['spam'] ) )
  174. $class .= ' spam';
  175. elseif ( ! empty( $result['mail_sent'] ) )
  176. $class .= ' sent';
  177. else
  178. $class .= ' failed';
  179. }
  180. if ( $atts['html_class'] ) {
  181. $class .= ' ' . $atts['html_class'];
  182. }
  183. if ( $this->in_demo_mode() ) {
  184. $class .= ' demo';
  185. }
  186. $class = explode( ' ', $class );
  187. $class = array_map( 'sanitize_html_class', $class );
  188. $class = array_filter( $class );
  189. $class = array_unique( $class );
  190. $class = implode( ' ', $class );
  191. $class = apply_filters( 'wpcf7_form_class_attr', $class );
  192. $enctype = apply_filters( 'wpcf7_form_enctype', '' );
  193. $novalidate = apply_filters( 'wpcf7_form_novalidate',
  194. wpcf7_support_html5() ? ' novalidate="novalidate"' : '' );
  195. $html .= '<form action="' . esc_url( $url ) . '" method="post"'
  196. . ( $id_attr ? ' id="' . esc_attr( $id_attr ) . '"' : '' )
  197. . ( $name_attr ? ' name="' . esc_attr( $name_attr ) . '"' : '' )
  198. . ' class="' . esc_attr( $class ) . '"'
  199. . $enctype . $novalidate . '>' . "\n";
  200. $html .= $this->form_hidden_fields();
  201. $html .= $this->form_elements();
  202. if ( ! $this->responses_count )
  203. $html .= $this->form_response_output();
  204. $html .= '</form>';
  205. $html .= '</div>';
  206. return $html;
  207. }
  208. public function form_hidden_fields() {
  209. $hidden_fields = array(
  210. '_wpcf7' => $this->id,
  211. '_wpcf7_version' => WPCF7_VERSION,
  212. '_wpcf7_locale' => $this->locale,
  213. '_wpcf7_unit_tag' => $this->unit_tag );
  214. if ( WPCF7_VERIFY_NONCE )
  215. $hidden_fields['_wpnonce'] = wpcf7_create_nonce( $this->id );
  216. $content = '';
  217. foreach ( $hidden_fields as $name => $value ) {
  218. $content .= '<input type="hidden"'
  219. . ' name="' . esc_attr( $name ) . '"'
  220. . ' value="' . esc_attr( $value ) . '" />' . "\n";
  221. }
  222. return '<div style="display: none;">' . "\n" . $content . '</div>' . "\n";
  223. }
  224. public function form_response_output() {
  225. $class = 'wpcf7-response-output';
  226. $role = '';
  227. $content = '';
  228. if ( $this->is_posted() ) { // Post response output for non-AJAX
  229. $result = self::get_submission_status( $this->id );
  230. if ( empty( $result['valid'] ) )
  231. $class .= ' wpcf7-validation-errors';
  232. elseif ( ! empty( $result['spam'] ) )
  233. $class .= ' wpcf7-spam-blocked';
  234. elseif ( ! empty( $result['mail_sent'] ) )
  235. $class .= ' wpcf7-mail-sent-ok';
  236. else
  237. $class .= ' wpcf7-mail-sent-ng';
  238. $role = 'alert';
  239. if ( ! empty( $result['message'] ) )
  240. $content = $result['message'];
  241. } else {
  242. $class .= ' wpcf7-display-none';
  243. }
  244. $atts = array(
  245. 'class' => trim( $class ),
  246. 'role' => trim( $role ) );
  247. $atts = wpcf7_format_atts( $atts );
  248. $output = sprintf( '<div %1$s>%2$s</div>',
  249. $atts, esc_html( $content ) );
  250. return apply_filters( 'wpcf7_form_response_output',
  251. $output, $class, $content, $this );
  252. }
  253. public function screen_reader_response() {
  254. $class = 'screen-reader-response';
  255. $role = '';
  256. $content = '';
  257. if ( $this->is_posted() ) { // Post response output for non-AJAX
  258. $role = 'alert';
  259. $result = self::get_submission_status( $this->id );
  260. if ( ! empty( $result['message'] ) ) {
  261. $content = esc_html( $result['message'] );
  262. if ( ! empty( $result['invalid_reasons'] ) ) {
  263. $content .= "\n" . '<ul>' . "\n";
  264. foreach ( (array) $result['invalid_reasons'] as $k => $v ) {
  265. if ( isset( $result['invalid_fields'][$k] )
  266. && wpcf7_is_name( $result['invalid_fields'][$k] ) ) {
  267. $link = sprintf( '<a href="#%1$s">%2$s</a>',
  268. $result['invalid_fields'][$k],
  269. esc_html( $v ) );
  270. $content .= sprintf( '<li>%s</li>', $link );
  271. } else {
  272. $content .= sprintf( '<li>%s</li>', esc_html( $v ) );
  273. }
  274. $content .= "\n";
  275. }
  276. $content .= '</ul>' . "\n";
  277. }
  278. }
  279. }
  280. $atts = array(
  281. 'class' => trim( $class ),
  282. 'role' => trim( $role ) );
  283. $atts = wpcf7_format_atts( $atts );
  284. $output = sprintf( '<div %1$s>%2$s</div>',
  285. $atts, $content );
  286. return $output;
  287. }
  288. public function validation_error( $name ) {
  289. if ( ! $this->is_posted() )
  290. return '';
  291. $result = self::get_submission_status( $this->id );
  292. if ( ! isset( $result['invalid_reasons'][$name] ) )
  293. return '';
  294. $ve = trim( $result['invalid_reasons'][$name] );
  295. if ( empty( $ve ) )
  296. return '';
  297. $ve = '<span role="alert" class="wpcf7-not-valid-tip">'
  298. . esc_html( $ve ) . '</span>';
  299. return apply_filters( 'wpcf7_validation_error', $ve, $name, $this );
  300. }
  301. /* Form Elements */
  302. public function form_do_shortcode() {
  303. $manager = WPCF7_ShortcodeManager::get_instance();
  304. $form = $this->form;
  305. if ( WPCF7_AUTOP ) {
  306. $form = $manager->normalize_shortcode( $form );
  307. $form = wpcf7_autop( $form );
  308. }
  309. $form = $manager->do_shortcode( $form );
  310. $this->scanned_form_tags = $manager->get_scanned_tags();
  311. return $form;
  312. }
  313. public function form_scan_shortcode( $cond = null ) {
  314. $manager = WPCF7_ShortcodeManager::get_instance();
  315. if ( ! empty( $this->scanned_form_tags ) ) {
  316. $scanned = $this->scanned_form_tags;
  317. } else {
  318. $scanned = $manager->scan_shortcode( $this->form );
  319. $this->scanned_form_tags = $scanned;
  320. }
  321. if ( empty( $scanned ) )
  322. return null;
  323. if ( ! is_array( $cond ) || empty( $cond ) )
  324. return $scanned;
  325. for ( $i = 0, $size = count( $scanned ); $i < $size; $i++ ) {
  326. if ( isset( $cond['type'] ) ) {
  327. if ( is_string( $cond['type'] ) && ! empty( $cond['type'] ) ) {
  328. if ( $scanned[$i]['type'] != $cond['type'] ) {
  329. unset( $scanned[$i] );
  330. continue;
  331. }
  332. } elseif ( is_array( $cond['type'] ) ) {
  333. if ( ! in_array( $scanned[$i]['type'], $cond['type'] ) ) {
  334. unset( $scanned[$i] );
  335. continue;
  336. }
  337. }
  338. }
  339. if ( isset( $cond['name'] ) ) {
  340. if ( is_string( $cond['name'] ) && ! empty( $cond['name'] ) ) {
  341. if ( $scanned[$i]['name'] != $cond['name'] ) {
  342. unset ( $scanned[$i] );
  343. continue;
  344. }
  345. } elseif ( is_array( $cond['name'] ) ) {
  346. if ( ! in_array( $scanned[$i]['name'], $cond['name'] ) ) {
  347. unset( $scanned[$i] );
  348. continue;
  349. }
  350. }
  351. }
  352. }
  353. return array_values( $scanned );
  354. }
  355. public function form_elements() {
  356. return apply_filters( 'wpcf7_form_elements', $this->form_do_shortcode() );
  357. }
  358. public function setup_posted_data() {
  359. $posted_data = (array) $_POST;
  360. $fes = $this->form_scan_shortcode();
  361. foreach ( $fes as $fe ) {
  362. if ( empty( $fe['name'] ) )
  363. continue;
  364. $name = $fe['name'];
  365. $value = '';
  366. if ( isset( $posted_data[$name] ) )
  367. $value = $posted_data[$name];
  368. $pipes = $fe['pipes'];
  369. if ( WPCF7_USE_PIPE && is_a( $pipes, 'WPCF7_Pipes' ) && ! $pipes->zero() ) {
  370. if ( is_array( $value) ) {
  371. $new_value = array();
  372. foreach ( $value as $v )
  373. $new_value[] = $pipes->do_pipe( wp_unslash( $v ) );
  374. $value = $new_value;
  375. } else {
  376. $value = $pipes->do_pipe( wp_unslash( $value ) );
  377. }
  378. }
  379. $posted_data[$name] = $value;
  380. }
  381. $this->posted_data = apply_filters( 'wpcf7_posted_data', $posted_data );
  382. return $this->posted_data;
  383. }
  384. public function submit( $ajax = false ) {
  385. $result = array(
  386. 'status' => 'init',
  387. 'valid' => true,
  388. 'invalid_reasons' => array(),
  389. 'invalid_fields' => array(),
  390. 'spam' => false,
  391. 'message' => '',
  392. 'mail_sent' => false,
  393. 'scripts_on_sent_ok' => null,
  394. 'scripts_on_submit' => null );
  395. $this->setup_posted_data();
  396. $validation = $this->validate();
  397. if ( ! $validation['valid'] ) { // Validation error occured
  398. $result['status'] = 'validation_failed';
  399. $result['valid'] = false;
  400. $result['invalid_reasons'] = $validation['reason'];
  401. $result['invalid_fields'] = $validation['idref'];
  402. $result['message'] = $this->message( 'validation_error' );
  403. } elseif ( ! $this->accepted() ) { // Not accepted terms
  404. $result['status'] = 'acceptance_missing';
  405. $result['message'] = $this->message( 'accept_terms' );
  406. } elseif ( $this->spam() ) { // Spam!
  407. $result['status'] = 'spam';
  408. $result['message'] = $this->message( 'spam' );
  409. $result['spam'] = true;
  410. } elseif ( $this->mail() ) {
  411. if ( $this->in_demo_mode() ) {
  412. $result['status'] = 'demo_mode';
  413. } else {
  414. $result['status'] = 'mail_sent';
  415. }
  416. $result['mail_sent'] = true;
  417. $result['message'] = $this->message( 'mail_sent_ok' );
  418. do_action_ref_array( 'wpcf7_mail_sent', array( &$this ) );
  419. if ( $ajax ) {
  420. $on_sent_ok = $this->additional_setting( 'on_sent_ok', false );
  421. if ( ! empty( $on_sent_ok ) )
  422. $result['scripts_on_sent_ok'] = array_map( 'wpcf7_strip_quote', $on_sent_ok );
  423. } else {
  424. $this->clear_post();
  425. }
  426. } else {
  427. $result['status'] = 'mail_failed';
  428. $result['message'] = $this->message( 'mail_sent_ng' );
  429. do_action_ref_array( 'wpcf7_mail_failed', array( &$this ) );
  430. }
  431. if ( $ajax ) {
  432. $on_submit = $this->additional_setting( 'on_submit', false );
  433. if ( ! empty( $on_submit ) )
  434. $result['scripts_on_submit'] = array_map( 'wpcf7_strip_quote', $on_submit );
  435. }
  436. // remove upload files
  437. foreach ( (array) $this->uploaded_files as $name => $path ) {
  438. @unlink( $path );
  439. }
  440. do_action_ref_array( 'wpcf7_submit', array( &$this, $result ) );
  441. self::add_submission_status( $this->id, $result );
  442. return $result;
  443. }
  444. /* Validate */
  445. public function validate() {
  446. $fes = $this->form_scan_shortcode();
  447. $result = array(
  448. 'valid' => true,
  449. 'reason' => array(),
  450. 'idref' => array() );
  451. foreach ( $fes as $fe ) {
  452. $result = apply_filters( 'wpcf7_validate_' . $fe['type'], $result, $fe );
  453. }
  454. $result = apply_filters( 'wpcf7_validate', $result );
  455. return $result;
  456. }
  457. public function accepted() {
  458. $accepted = true;
  459. return apply_filters( 'wpcf7_acceptance', $accepted );
  460. }
  461. public function spam() {
  462. $spam = false;
  463. if ( WPCF7_VERIFY_NONCE && ! $this->verify_nonce() )
  464. $spam = true;
  465. if ( $this->blacklist_check() )
  466. $spam = true;
  467. return apply_filters( 'wpcf7_spam', $spam );
  468. }
  469. public function verify_nonce() {
  470. return wpcf7_verify_nonce( $_POST['_wpnonce'], $this->id );
  471. }
  472. public function blacklist_check() {
  473. $target = wpcf7_array_flatten( $this->posted_data );
  474. $target[] = $_SERVER['REMOTE_ADDR'];
  475. $target[] = $_SERVER['HTTP_USER_AGENT'];
  476. $target = implode( "\n", $target );
  477. return wpcf7_blacklist_check( $target );
  478. }
  479. /* Mail */
  480. public function mail() {
  481. if ( $this->in_demo_mode() )
  482. $this->skip_mail = true;
  483. do_action_ref_array( 'wpcf7_before_send_mail', array( &$this ) );
  484. if ( $this->skip_mail )
  485. return true;
  486. $result = $this->compose_mail( $this->setup_mail_template( $this->mail, 'mail' ) );
  487. if ( $result ) {
  488. $additional_mail = array();
  489. if ( $this->mail_2['active'] )
  490. $additional_mail[] = $this->setup_mail_template( $this->mail_2, 'mail_2' );
  491. $additional_mail = apply_filters_ref_array( 'wpcf7_additional_mail',
  492. array( $additional_mail, &$this ) );
  493. foreach ( $additional_mail as $mail )
  494. $this->compose_mail( $mail );
  495. return true;
  496. }
  497. return false;
  498. }
  499. public function setup_mail_template( $mail_template, $name = '' ) {
  500. $defaults = array(
  501. 'subject' => '', 'sender' => '', 'body' => '',
  502. 'recipient' => '', 'additional_headers' => '',
  503. 'attachments' => '', 'use_html' => false );
  504. $mail_template = wp_parse_args( $mail_template, $defaults );
  505. $name = trim( $name );
  506. if ( ! empty( $name ) )
  507. $mail_template['name'] = $name;
  508. return $mail_template;
  509. }
  510. public function compose_mail( $mail_template, $send = true ) {
  511. $this->mail_template_in_process = $mail_template;
  512. $use_html = (bool) $mail_template['use_html'];
  513. $subject = $this->replace_mail_tags( $mail_template['subject'] );
  514. $sender = $this->replace_mail_tags( $mail_template['sender'] );
  515. $recipient = $this->replace_mail_tags( $mail_template['recipient'] );
  516. $additional_headers = $this->replace_mail_tags( $mail_template['additional_headers'] );
  517. if ( $use_html ) {
  518. $body = $this->replace_mail_tags( $mail_template['body'], true );
  519. $body = wpautop( $body );
  520. } else {
  521. $body = $this->replace_mail_tags( $mail_template['body'] );
  522. }
  523. $attachments = $this->mail_attachments( $mail_template['attachments'] );
  524. $components = compact(
  525. 'subject', 'sender', 'body', 'recipient', 'additional_headers', 'attachments' );
  526. $components = apply_filters_ref_array( 'wpcf7_mail_components',
  527. array( $components, &$this ) );
  528. extract( $components );
  529. $subject = wpcf7_strip_newline( $subject );
  530. $sender = wpcf7_strip_newline( $sender );
  531. $recipient = wpcf7_strip_newline( $recipient );
  532. $headers = "From: $sender\n";
  533. if ( $use_html )
  534. $headers .= "Content-Type: text/html\n";
  535. $additional_headers = trim( $additional_headers );
  536. if ( $additional_headers )
  537. $headers .= $additional_headers . "\n";
  538. if ( $send )
  539. return @wp_mail( $recipient, $subject, $body, $headers, $attachments );
  540. return compact( 'subject', 'sender', 'body', 'recipient', 'headers', 'attachments' );
  541. }
  542. public function replace_mail_tags( $content, $html = false ) {
  543. $regex = '/(\[?)\[[\t ]*'
  544. . '([a-zA-Z_][0-9a-zA-Z:._-]*)' // [2] = name
  545. . '((?:[\t ]+"[^"]*"|[\t ]+\'[^\']*\')*)' // [3] = values
  546. . '[\t ]*\](\]?)/';
  547. if ( $html )
  548. $callback = array( $this, 'mail_callback_html' );
  549. else
  550. $callback = array( $this, 'mail_callback' );
  551. return preg_replace_callback( $regex, $callback, $content );
  552. }
  553. private function mail_callback_html( $matches ) {
  554. return $this->mail_callback( $matches, true );
  555. }
  556. private function mail_callback( $matches, $html = false ) {
  557. // allow [[foo]] syntax for escaping a tag
  558. if ( $matches[1] == '[' && $matches[4] == ']' )
  559. return substr( $matches[0], 1, -1 );
  560. $tag = $matches[0];
  561. $tagname = $matches[2];
  562. $values = $matches[3];
  563. if ( ! empty( $values ) ) {
  564. preg_match_all( '/"[^"]*"|\'[^\']*\'/', $values, $matches );
  565. $values = wpcf7_strip_quote_deep( $matches[0] );
  566. }
  567. $do_not_heat = false;
  568. if ( preg_match( '/^_raw_(.+)$/', $tagname, $matches ) ) {
  569. $tagname = trim( $matches[1] );
  570. $do_not_heat = true;
  571. }
  572. $format = '';
  573. if ( preg_match( '/^_format_(.+)$/', $tagname, $matches ) ) {
  574. $tagname = trim( $matches[1] );
  575. $format = $values[0];
  576. }
  577. if ( isset( $this->posted_data[$tagname] ) ) {
  578. if ( $do_not_heat )
  579. $submitted = isset( $_POST[$tagname] ) ? $_POST[$tagname] : '';
  580. else
  581. $submitted = $this->posted_data[$tagname];
  582. $replaced = $submitted;
  583. if ( ! empty( $format ) ) {
  584. $replaced = $this->format( $replaced, $format );
  585. }
  586. $replaced = wpcf7_flat_join( $replaced );
  587. if ( $html ) {
  588. $replaced = esc_html( $replaced );
  589. $replaced = wptexturize( $replaced );
  590. }
  591. $replaced = apply_filters( 'wpcf7_mail_tag_replaced', $replaced,
  592. $submitted, $html );
  593. return wp_unslash( $replaced );
  594. }
  595. $special = apply_filters( 'wpcf7_special_mail_tags', '', $tagname, $html );
  596. if ( ! empty( $special ) )
  597. return $special;
  598. return $tag;
  599. }
  600. public function format( $original, $format ) {
  601. $original = (array) $original;
  602. foreach ( $original as $key => $value ) {
  603. if ( preg_match( '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $value ) ) {
  604. $original[$key] = mysql2date( $format, $value );
  605. }
  606. }
  607. return $original;
  608. }
  609. public function mail_attachments( $template ) {
  610. $attachments = array();
  611. foreach ( (array) $this->uploaded_files as $name => $path ) {
  612. if ( false !== strpos( $template, "[${name}]" ) && ! empty( $path ) )
  613. $attachments[] = $path;
  614. }
  615. foreach ( explode( "\n", $template ) as $line ) {
  616. $line = trim( $line );
  617. if ( '[' == substr( $line, 0, 1 ) )
  618. continue;
  619. $path = path_join( WP_CONTENT_DIR, $line );
  620. if ( @is_readable( $path ) && @is_file( $path ) )
  621. $attachments[] = $path;
  622. }
  623. return $attachments;
  624. }
  625. /* Message */
  626. public function message( $status ) {
  627. $messages = $this->messages;
  628. $message = isset( $messages[$status] ) ? $messages[$status] : '';
  629. $message = $this->replace_mail_tags( $message, true );
  630. return apply_filters( 'wpcf7_display_message', $message, $status );
  631. }
  632. /* Additional settings */
  633. public function additional_setting( $name, $max = 1 ) {
  634. $tmp_settings = (array) explode( "\n", $this->additional_settings );
  635. $count = 0;
  636. $values = array();
  637. foreach ( $tmp_settings as $setting ) {
  638. if ( preg_match('/^([a-zA-Z0-9_]+)[\t ]*:(.*)$/', $setting, $matches ) ) {
  639. if ( $matches[1] != $name )
  640. continue;
  641. if ( ! $max || $count < (int) $max ) {
  642. $values[] = trim( $matches[2] );
  643. $count += 1;
  644. }
  645. }
  646. }
  647. return $values;
  648. }
  649. public function is_true( $name ) {
  650. $settings = $this->additional_setting( $name, false );
  651. foreach ( $settings as $setting ) {
  652. if ( in_array( $setting, array( 'on', 'true', '1' ) ) )
  653. return true;
  654. }
  655. return false;
  656. }
  657. public function in_demo_mode() {
  658. return $this->is_true( 'demo_mode' );
  659. }
  660. /* Upgrade */
  661. public function upgrade() {
  662. if ( is_array( $this->mail ) ) {
  663. if ( ! isset( $this->mail['recipient'] ) )
  664. $this->mail['recipient'] = get_option( 'admin_email' );
  665. }
  666. if ( is_array( $this->messages ) ) {
  667. foreach ( wpcf7_messages() as $key => $arr ) {
  668. if ( ! isset( $this->messages[$key] ) )
  669. $this->messages[$key] = $arr['default'];
  670. }
  671. }
  672. }
  673. /* Save */
  674. public function save() {
  675. $props = $this->get_properties();
  676. $post_content = implode( "\n", wpcf7_array_flatten( $props ) );
  677. if ( $this->initial ) {
  678. $post_id = wp_insert_post( array(
  679. 'post_type' => self::post_type,
  680. 'post_status' => 'publish',
  681. 'post_title' => $this->title,
  682. 'post_content' => trim( $post_content ) ) );
  683. } else {
  684. $post_id = wp_update_post( array(
  685. 'ID' => (int) $this->id,
  686. 'post_status' => 'publish',
  687. 'post_title' => $this->title,
  688. 'post_content' => trim( $post_content ) ) );
  689. }
  690. if ( $post_id ) {
  691. foreach ( $props as $prop => $value )
  692. update_post_meta( $post_id, '_' . $prop, wpcf7_normalize_newline_deep( $value ) );
  693. if ( ! empty( $this->locale ) )
  694. update_post_meta( $post_id, '_locale', $this->locale );
  695. if ( $this->initial ) {
  696. $this->initial = false;
  697. $this->id = $post_id;
  698. do_action_ref_array( 'wpcf7_after_create', array( &$this ) );
  699. } else {
  700. do_action_ref_array( 'wpcf7_after_update', array( &$this ) );
  701. }
  702. do_action_ref_array( 'wpcf7_after_save', array( &$this ) );
  703. }
  704. return $post_id;
  705. }
  706. public function copy() {
  707. $new = new self;
  708. $new->initial = true;
  709. $new->title = $this->title . '_copy';
  710. $new->locale = $this->locale;
  711. $props = $this->get_properties();
  712. foreach ( $props as $prop => $value )
  713. $new->{$prop} = $value;
  714. $new = apply_filters_ref_array( 'wpcf7_copy', array( &$new, &$this ) );
  715. return $new;
  716. }
  717. public function delete() {
  718. if ( $this->initial )
  719. return;
  720. if ( wp_delete_post( $this->id, true ) ) {
  721. $this->initial = true;
  722. $this->id = null;
  723. return true;
  724. }
  725. return false;
  726. }
  727. }
  728. function wpcf7_contact_form( $id ) {
  729. $contact_form = new WPCF7_ContactForm( $id );
  730. if ( $contact_form->initial )
  731. return false;
  732. return $contact_form;
  733. }
  734. function wpcf7_get_contact_form_by_old_id( $old_id ) {
  735. global $wpdb;
  736. $q = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_old_cf7_unit_id'"
  737. . $wpdb->prepare( " AND meta_value = %d", $old_id );
  738. if ( $new_id = $wpdb->get_var( $q ) )
  739. return wpcf7_contact_form( $new_id );
  740. }
  741. function wpcf7_get_contact_form_by_title( $title ) {
  742. $page = get_page_by_title( $title, OBJECT, WPCF7_ContactForm::post_type );
  743. if ( $page )
  744. return wpcf7_contact_form( $page->ID );
  745. return null;
  746. }
  747. function wpcf7_get_contact_form_default_pack( $args = '' ) {
  748. $contact_form = WPCF7_ContactForm::generate_default_package( $args );
  749. return $contact_form;
  750. }
  751. function wpcf7_get_current_contact_form() {
  752. if ( $current = WPCF7_ContactForm::get_current() ) {
  753. return $current;
  754. }
  755. }
  756. function wpcf7_is_posted() {
  757. if ( ! $contact_form = wpcf7_get_current_contact_form() )
  758. return false;
  759. return $contact_form->is_posted();
  760. }
  761. function wpcf7_get_validation_error( $name ) {
  762. if ( ! $contact_form = wpcf7_get_current_contact_form() )
  763. return '';
  764. return $contact_form->validation_error( $name );
  765. }
  766. function wpcf7_get_message( $status ) {
  767. if ( ! $contact_form = wpcf7_get_current_contact_form() )
  768. return '';
  769. return $contact_form->message( $status );
  770. }
  771. function wpcf7_scan_shortcode( $cond = null ) {
  772. if ( ! $contact_form = wpcf7_get_current_contact_form() )
  773. return null;
  774. return $contact_form->form_scan_shortcode( $cond );
  775. }
  776. function wpcf7_form_controls_class( $type, $default = '' ) {
  777. $type = trim( $type );
  778. $default = array_filter( explode( ' ', $default ) );
  779. $classes = array_merge( array( 'wpcf7-form-control' ), $default );
  780. $typebase = rtrim( $type, '*' );
  781. $required = ( '*' == substr( $type, -1 ) );
  782. $classes[] = 'wpcf7-' . $typebase;
  783. if ( $required )
  784. $classes[] = 'wpcf7-validates-as-required';
  785. $classes = array_unique( $classes );
  786. return implode( ' ', $classes );
  787. }
  788. ?>