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

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

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