WPCF7_ContactForm

The Contact Form 7 WPCF7 ContactForm class.

Defined (1)

The class is defined in the following location(s).

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