contact-form.php 22 KB

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