/includes/contact-form.php

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