PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/VTTE/sitios-vtte
PHP | 276 lines | 211 code | 65 blank | 0 comment | 34 complexity | 4ec7ede55cc404fe89b4759207713c80 MD5 | raw file
  1. <?php
  2. function wpcf7_contact_form( $id ) {
  3. return WPCF7_ContactForm::get_instance( $id );
  4. }
  5. function wpcf7_get_contact_form_by_old_id( $old_id ) {
  6. global $wpdb;
  7. $q = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_old_cf7_unit_id'"
  8. . $wpdb->prepare( " AND meta_value = %d", $old_id );
  9. if ( $new_id = $wpdb->get_var( $q ) ) {
  10. return wpcf7_contact_form( $new_id );
  11. }
  12. }
  13. function wpcf7_get_contact_form_by_title( $title ) {
  14. $page = get_page_by_title( $title, OBJECT, WPCF7_ContactForm::post_type );
  15. if ( $page ) {
  16. return wpcf7_contact_form( $page->ID );
  17. }
  18. return null;
  19. }
  20. function wpcf7_get_current_contact_form() {
  21. if ( $current = WPCF7_ContactForm::get_current() ) {
  22. return $current;
  23. }
  24. }
  25. function wpcf7_is_posted() {
  26. if ( ! $contact_form = wpcf7_get_current_contact_form() ) {
  27. return false;
  28. }
  29. return $contact_form->is_posted();
  30. }
  31. function wpcf7_get_hangover( $name, $default = null ) {
  32. if ( ! wpcf7_is_posted() ) {
  33. return $default;
  34. }
  35. $submission = WPCF7_Submission::get_instance();
  36. if ( ! $submission
  37. or $submission->is( 'mail_sent' ) ) {
  38. return $default;
  39. }
  40. return isset( $_POST[$name] ) ? wp_unslash( $_POST[$name] ) : $default;
  41. }
  42. function wpcf7_get_validation_error( $name ) {
  43. if ( ! $contact_form = wpcf7_get_current_contact_form() ) {
  44. return '';
  45. }
  46. return $contact_form->validation_error( $name );
  47. }
  48. function wpcf7_get_message( $status ) {
  49. if ( ! $contact_form = wpcf7_get_current_contact_form() ) {
  50. return '';
  51. }
  52. return $contact_form->message( $status );
  53. }
  54. function wpcf7_form_controls_class( $type, $default = '' ) {
  55. $type = trim( $type );
  56. $default = array_filter( explode( ' ', $default ) );
  57. $classes = array_merge( array( 'wpcf7-form-control' ), $default );
  58. $typebase = rtrim( $type, '*' );
  59. $required = ( '*' == substr( $type, -1 ) );
  60. $classes[] = 'wpcf7-' . $typebase;
  61. if ( $required ) {
  62. $classes[] = 'wpcf7-validates-as-required';
  63. }
  64. $classes = array_unique( $classes );
  65. return implode( ' ', $classes );
  66. }
  67. function wpcf7_contact_form_tag_func( $atts, $content = null, $code = '' ) {
  68. if ( is_feed() ) {
  69. return '[contact-form-7]';
  70. }
  71. if ( 'contact-form-7' == $code ) {
  72. $atts = shortcode_atts(
  73. array(
  74. 'id' => 0,
  75. 'title' => '',
  76. 'html_id' => '',
  77. 'html_name' => '',
  78. 'html_class' => '',
  79. 'output' => 'form',
  80. ),
  81. $atts, 'wpcf7'
  82. );
  83. $id = (int) $atts['id'];
  84. $title = trim( $atts['title'] );
  85. if ( ! $contact_form = wpcf7_contact_form( $id ) ) {
  86. $contact_form = wpcf7_get_contact_form_by_title( $title );
  87. }
  88. } else {
  89. if ( is_string( $atts ) ) {
  90. $atts = explode( ' ', $atts, 2 );
  91. }
  92. $id = (int) array_shift( $atts );
  93. $contact_form = wpcf7_get_contact_form_by_old_id( $id );
  94. }
  95. if ( ! $contact_form ) {
  96. return '[contact-form-7 404 "Not Found"]';
  97. }
  98. return $contact_form->form_html( $atts );
  99. }
  100. function wpcf7_save_contact_form( $args = '', $context = 'save' ) {
  101. $args = wp_parse_args( $args, array(
  102. 'id' => -1,
  103. 'title' => null,
  104. 'locale' => null,
  105. 'form' => null,
  106. 'mail' => null,
  107. 'mail_2' => null,
  108. 'messages' => null,
  109. 'additional_settings' => null,
  110. ) );
  111. $args = wp_unslash( $args );
  112. $args['id'] = (int) $args['id'];
  113. if ( -1 == $args['id'] ) {
  114. $contact_form = WPCF7_ContactForm::get_template();
  115. } else {
  116. $contact_form = wpcf7_contact_form( $args['id'] );
  117. }
  118. if ( empty( $contact_form ) ) {
  119. return false;
  120. }
  121. if ( null !== $args['title'] ) {
  122. $contact_form->set_title( $args['title'] );
  123. }
  124. if ( null !== $args['locale'] ) {
  125. $contact_form->set_locale( $args['locale'] );
  126. }
  127. $properties = array();
  128. if ( null !== $args['form'] ) {
  129. $properties['form'] = wpcf7_sanitize_form( $args['form'] );
  130. }
  131. if ( null !== $args['mail'] ) {
  132. $properties['mail'] = wpcf7_sanitize_mail( $args['mail'] );
  133. $properties['mail']['active'] = true;
  134. }
  135. if ( null !== $args['mail_2'] ) {
  136. $properties['mail_2'] = wpcf7_sanitize_mail( $args['mail_2'] );
  137. }
  138. if ( null !== $args['messages'] ) {
  139. $properties['messages'] = wpcf7_sanitize_messages( $args['messages'] );
  140. }
  141. if ( null !== $args['additional_settings'] ) {
  142. $properties['additional_settings'] = wpcf7_sanitize_additional_settings(
  143. $args['additional_settings']
  144. );
  145. }
  146. $contact_form->set_properties( $properties );
  147. do_action( 'wpcf7_save_contact_form', $contact_form, $args, $context );
  148. if ( 'save' == $context ) {
  149. $contact_form->save();
  150. }
  151. return $contact_form;
  152. }
  153. function wpcf7_sanitize_form( $input, $default = '' ) {
  154. if ( null === $input ) {
  155. return $default;
  156. }
  157. $output = trim( $input );
  158. return $output;
  159. }
  160. function wpcf7_sanitize_mail( $input, $defaults = array() ) {
  161. $input = wp_parse_args( $input, array(
  162. 'active' => false,
  163. 'subject' => '',
  164. 'sender' => '',
  165. 'recipient' => '',
  166. 'body' => '',
  167. 'additional_headers' => '',
  168. 'attachments' => '',
  169. 'use_html' => false,
  170. 'exclude_blank' => false,
  171. ) );
  172. $input = wp_parse_args( $input, $defaults );
  173. $output = array();
  174. $output['active'] = (bool) $input['active'];
  175. $output['subject'] = trim( $input['subject'] );
  176. $output['sender'] = trim( $input['sender'] );
  177. $output['recipient'] = trim( $input['recipient'] );
  178. $output['body'] = trim( $input['body'] );
  179. $output['additional_headers'] = '';
  180. $headers = str_replace( "\r\n", "\n", $input['additional_headers'] );
  181. $headers = explode( "\n", $headers );
  182. foreach ( $headers as $header ) {
  183. $header = trim( $header );
  184. if ( '' !== $header ) {
  185. $output['additional_headers'] .= $header . "\n";
  186. }
  187. }
  188. $output['additional_headers'] = trim( $output['additional_headers'] );
  189. $output['attachments'] = trim( $input['attachments'] );
  190. $output['use_html'] = (bool) $input['use_html'];
  191. $output['exclude_blank'] = (bool) $input['exclude_blank'];
  192. return $output;
  193. }
  194. function wpcf7_sanitize_messages( $input, $defaults = array() ) {
  195. $output = array();
  196. foreach ( wpcf7_messages() as $key => $val ) {
  197. if ( isset( $input[$key] ) ) {
  198. $output[$key] = trim( $input[$key] );
  199. } elseif ( isset( $defaults[$key] ) ) {
  200. $output[$key] = $defaults[$key];
  201. }
  202. }
  203. return $output;
  204. }
  205. function wpcf7_sanitize_additional_settings( $input, $default = '' ) {
  206. if ( null === $input ) {
  207. return $default;
  208. }
  209. $output = trim( $input );
  210. return $output;
  211. }