css
13 years ago
js
12 years ago
capabilities.php
13 years ago
classes.php
13 years ago
controller.php
12 years ago
deprecated.php
14 years ago
formatting.php
13 years ago
functions.php
13 years ago
pipe.php
14 years ago
shortcodes.php
13 years ago
controller.php
278 lines
| 1 | <?php |
| 2 | |
| 3 | add_action( 'init', 'wpcf7_control_init', 11 ); |
| 4 | |
| 5 | function wpcf7_control_init() { |
| 6 | wpcf7_ajax_onload(); |
| 7 | wpcf7_ajax_json_echo(); |
| 8 | wpcf7_submit_nonajax(); |
| 9 | } |
| 10 | |
| 11 | function wpcf7_ajax_onload() { |
| 12 | global $wpcf7_contact_form; |
| 13 | |
| 14 | if ( 'GET' != $_SERVER['REQUEST_METHOD'] || ! isset( $_GET['_wpcf7_is_ajax_call'] ) ) |
| 15 | return; |
| 16 | |
| 17 | $echo = ''; |
| 18 | |
| 19 | if ( isset( $_GET['_wpcf7'] ) ) { |
| 20 | $id = (int) $_GET['_wpcf7']; |
| 21 | |
| 22 | if ( $wpcf7_contact_form = wpcf7_contact_form( $id ) ) { |
| 23 | $items = apply_filters( 'wpcf7_ajax_onload', array() ); |
| 24 | $wpcf7_contact_form = null; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | $echo = json_encode( $items ); |
| 29 | |
| 30 | if ( wpcf7_is_xhr() ) { |
| 31 | @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) ); |
| 32 | echo $echo; |
| 33 | } |
| 34 | |
| 35 | exit(); |
| 36 | } |
| 37 | |
| 38 | function wpcf7_ajax_json_echo() { |
| 39 | global $wpcf7_contact_form; |
| 40 | |
| 41 | if ( 'POST' != $_SERVER['REQUEST_METHOD'] || ! isset( $_POST['_wpcf7_is_ajax_call'] ) ) |
| 42 | return; |
| 43 | |
| 44 | $echo = ''; |
| 45 | |
| 46 | if ( isset( $_POST['_wpcf7'] ) ) { |
| 47 | $id = (int) $_POST['_wpcf7']; |
| 48 | $unit_tag = wpcf7_sanitize_unit_tag( $_POST['_wpcf7_unit_tag'] ); |
| 49 | |
| 50 | if ( $wpcf7_contact_form = wpcf7_contact_form( $id ) ) { |
| 51 | |
| 52 | $items = array( |
| 53 | 'mailSent' => false, |
| 54 | 'into' => '#' . $unit_tag, |
| 55 | 'captcha' => null ); |
| 56 | |
| 57 | $result = $wpcf7_contact_form->submit( true ); |
| 58 | |
| 59 | if ( ! empty( $result['message'] ) ) |
| 60 | $items['message'] = $result['message']; |
| 61 | |
| 62 | if ( $result['mail_sent'] ) |
| 63 | $items['mailSent'] = true; |
| 64 | |
| 65 | if ( ! $result['valid'] ) { |
| 66 | $invalids = array(); |
| 67 | |
| 68 | foreach ( $result['invalid_reasons'] as $name => $reason ) { |
| 69 | $invalids[] = array( |
| 70 | 'into' => 'span.wpcf7-form-control-wrap.' . $name, |
| 71 | 'message' => $reason ); |
| 72 | } |
| 73 | |
| 74 | $items['invalids'] = $invalids; |
| 75 | } |
| 76 | |
| 77 | if ( $result['spam'] ) |
| 78 | $items['spam'] = true; |
| 79 | |
| 80 | if ( ! empty( $result['scripts_on_sent_ok'] ) ) |
| 81 | $items['onSentOk'] = $result['scripts_on_sent_ok']; |
| 82 | |
| 83 | if ( ! empty( $result['scripts_on_submit'] ) ) |
| 84 | $items['onSubmit'] = $result['scripts_on_submit']; |
| 85 | |
| 86 | $items = apply_filters( 'wpcf7_ajax_json_echo', $items, $result ); |
| 87 | |
| 88 | $wpcf7_contact_form = null; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | $echo = json_encode( $items ); |
| 93 | |
| 94 | if ( wpcf7_is_xhr() ) { |
| 95 | @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) ); |
| 96 | echo $echo; |
| 97 | } else { |
| 98 | @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
| 99 | echo '<textarea>' . $echo . '</textarea>'; |
| 100 | } |
| 101 | |
| 102 | exit(); |
| 103 | } |
| 104 | |
| 105 | function wpcf7_is_xhr() { |
| 106 | if ( ! isset( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ) |
| 107 | return false; |
| 108 | |
| 109 | return $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'; |
| 110 | } |
| 111 | |
| 112 | function wpcf7_submit_nonajax() { |
| 113 | global $wpcf7, $wpcf7_contact_form; |
| 114 | |
| 115 | if ( ! isset( $_POST['_wpcf7'] ) ) |
| 116 | return; |
| 117 | |
| 118 | $id = (int) $_POST['_wpcf7']; |
| 119 | |
| 120 | if ( $wpcf7_contact_form = wpcf7_contact_form( $id ) ) |
| 121 | $wpcf7->result = $wpcf7_contact_form->submit(); |
| 122 | |
| 123 | $wpcf7_contact_form = null; |
| 124 | } |
| 125 | |
| 126 | add_action( 'the_post', 'wpcf7_the_post' ); |
| 127 | |
| 128 | function wpcf7_the_post() { |
| 129 | global $wpcf7; |
| 130 | |
| 131 | $wpcf7->processing_within = 'p' . get_the_ID(); |
| 132 | $wpcf7->unit_count = 0; |
| 133 | } |
| 134 | |
| 135 | add_action( 'loop_end', 'wpcf7_loop_end' ); |
| 136 | |
| 137 | function wpcf7_loop_end() { |
| 138 | global $wpcf7; |
| 139 | |
| 140 | $wpcf7->processing_within = ''; |
| 141 | } |
| 142 | |
| 143 | add_filter( 'widget_text', 'wpcf7_widget_text_filter', 9 ); |
| 144 | |
| 145 | function wpcf7_widget_text_filter( $content ) { |
| 146 | global $wpcf7; |
| 147 | |
| 148 | if ( ! preg_match( '/\[[\r\n\t ]*contact-form(-7)?[\r\n\t ].*?\]/', $content ) ) |
| 149 | return $content; |
| 150 | |
| 151 | $wpcf7->widget_count += 1; |
| 152 | $wpcf7->processing_within = 'w' . $wpcf7->widget_count; |
| 153 | $wpcf7->unit_count = 0; |
| 154 | |
| 155 | $content = do_shortcode( $content ); |
| 156 | |
| 157 | $wpcf7->processing_within = ''; |
| 158 | |
| 159 | return $content; |
| 160 | } |
| 161 | |
| 162 | /* Shortcodes */ |
| 163 | |
| 164 | add_action( 'plugins_loaded', 'wpcf7_add_shortcodes', 1 ); |
| 165 | |
| 166 | function wpcf7_add_shortcodes() { |
| 167 | add_shortcode( 'contact-form-7', 'wpcf7_contact_form_tag_func' ); |
| 168 | add_shortcode( 'contact-form', 'wpcf7_contact_form_tag_func' ); |
| 169 | } |
| 170 | |
| 171 | function wpcf7_contact_form_tag_func( $atts, $content = null, $code = '' ) { |
| 172 | global $wpcf7, $wpcf7_contact_form; |
| 173 | |
| 174 | if ( is_feed() ) |
| 175 | return '[contact-form-7]'; |
| 176 | |
| 177 | if ( 'contact-form-7' == $code ) { |
| 178 | $atts = shortcode_atts( array( 'id' => 0, 'title' => '' ), $atts ); |
| 179 | |
| 180 | $id = (int) $atts['id']; |
| 181 | $title = trim( $atts['title'] ); |
| 182 | |
| 183 | if ( ! $wpcf7_contact_form = wpcf7_contact_form( $id ) ) |
| 184 | $wpcf7_contact_form = wpcf7_get_contact_form_by_title( $title ); |
| 185 | |
| 186 | } else { |
| 187 | if ( is_string( $atts ) ) |
| 188 | $atts = explode( ' ', $atts, 2 ); |
| 189 | |
| 190 | $id = (int) array_shift( $atts ); |
| 191 | $wpcf7_contact_form = wpcf7_get_contact_form_by_old_id( $id ); |
| 192 | } |
| 193 | |
| 194 | if ( ! $wpcf7_contact_form ) |
| 195 | return '[contact-form-7 404 "Not Found"]'; |
| 196 | |
| 197 | if ( $wpcf7->processing_within ) { // Inside post content or text widget |
| 198 | $wpcf7->unit_count += 1; |
| 199 | $unit_count = $wpcf7->unit_count; |
| 200 | $processing_within = $wpcf7->processing_within; |
| 201 | |
| 202 | } else { // Inside template |
| 203 | |
| 204 | if ( ! isset( $wpcf7->global_unit_count ) ) |
| 205 | $wpcf7->global_unit_count = 0; |
| 206 | |
| 207 | $wpcf7->global_unit_count += 1; |
| 208 | $unit_count = 1; |
| 209 | $processing_within = 't' . $wpcf7->global_unit_count; |
| 210 | } |
| 211 | |
| 212 | $unit_tag = 'wpcf7-f' . $wpcf7_contact_form->id . '-' . $processing_within . '-o' . $unit_count; |
| 213 | $wpcf7_contact_form->unit_tag = $unit_tag; |
| 214 | |
| 215 | $form = $wpcf7_contact_form->form_html(); |
| 216 | |
| 217 | $wpcf7_contact_form = null; |
| 218 | |
| 219 | return $form; |
| 220 | } |
| 221 | |
| 222 | if ( WPCF7_LOAD_JS ) |
| 223 | add_action( 'wp_enqueue_scripts', 'wpcf7_enqueue_scripts' ); |
| 224 | |
| 225 | function wpcf7_enqueue_scripts() { |
| 226 | // jquery.form.js originally bundled with WordPress is out of date and deprecated |
| 227 | // so we need to deregister it and re-register the latest one |
| 228 | wp_deregister_script( 'jquery-form' ); |
| 229 | wp_register_script( 'jquery-form', |
| 230 | wpcf7_plugin_url( 'includes/js/jquery.form.min.js' ), |
| 231 | array( 'jquery' ), '3.36.0-2013.06.16', true ); |
| 232 | |
| 233 | $in_footer = true; |
| 234 | if ( 'header' === WPCF7_LOAD_JS ) |
| 235 | $in_footer = false; |
| 236 | |
| 237 | wp_enqueue_script( 'contact-form-7', |
| 238 | wpcf7_plugin_url( 'includes/js/scripts.js' ), |
| 239 | array( 'jquery', 'jquery-form' ), WPCF7_VERSION, $in_footer ); |
| 240 | |
| 241 | $_wpcf7 = array( |
| 242 | 'loaderUrl' => wpcf7_ajax_loader(), |
| 243 | 'sending' => __( 'Sending ...', 'wpcf7' ) ); |
| 244 | |
| 245 | if ( defined( 'WP_CACHE' ) && WP_CACHE ) |
| 246 | $_wpcf7['cached'] = 1; |
| 247 | |
| 248 | wp_localize_script( 'contact-form-7', '_wpcf7', $_wpcf7 ); |
| 249 | |
| 250 | do_action( 'wpcf7_enqueue_scripts' ); |
| 251 | } |
| 252 | |
| 253 | function wpcf7_script_is() { |
| 254 | return wp_script_is( 'contact-form-7' ); |
| 255 | } |
| 256 | |
| 257 | if ( WPCF7_LOAD_CSS ) |
| 258 | add_action( 'wp_enqueue_scripts', 'wpcf7_enqueue_styles' ); |
| 259 | |
| 260 | function wpcf7_enqueue_styles() { |
| 261 | wp_enqueue_style( 'contact-form-7', |
| 262 | wpcf7_plugin_url( 'includes/css/styles.css' ), |
| 263 | array(), WPCF7_VERSION, 'all' ); |
| 264 | |
| 265 | if ( wpcf7_is_rtl() ) { |
| 266 | wp_enqueue_style( 'contact-form-7-rtl', |
| 267 | wpcf7_plugin_url( 'includes/css/styles-rtl.css' ), |
| 268 | array(), WPCF7_VERSION, 'all' ); |
| 269 | } |
| 270 | |
| 271 | do_action( 'wpcf7_enqueue_styles' ); |
| 272 | } |
| 273 | |
| 274 | function wpcf7_style_is() { |
| 275 | return wp_style_is( 'contact-form-7' ); |
| 276 | } |
| 277 | |
| 278 | ?> |