| @@ -28,10 +28,11 @@ | ||
| 28 | 28 | |
| 29 | 29 | public function hook() { |
| 30 | 30 | add_action( 'init', array( $this, 'register' ) ); |
| 31 | 31 | add_action( 'init', array( $this, 'listen_for_submit' ) ); |
| 32 | + add_action( 'init', array( $this, 'register_assets' ) ); | |
| 33 | + add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); | |
| 32 | 34 | add_action( 'parse_request', array( $this, 'listen_for_preview' ) ); |
| 33 | - add_action( 'wp_enqueue_scripts', array( $this, 'assets' ) ); | |
| 34 | 35 | add_filter( 'hf_form_markup', 'hf_template' ); |
| 35 | 36 | } |
| 36 | 37 | |
| 37 | 38 | public function register() { |
| @@ -59,27 +60,41 @@ | ||
| 59 | 60 | |
| 60 | 61 | add_shortcode( 'hf_form', array( $this, 'shortcode' ) ); |
| 61 | 62 | } |
| 62 | 63 | |
| 63 | - public function assets() { | |
| 64 | - $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; | |
| 64 | + public function register_assets() { | |
| 65 | 65 | $assets_url = plugins_url( 'assets/', $this->plugin_file ); |
| 66 | 66 | |
| 67 | - wp_register_script( 'html-forms', $assets_url . "js/public{$suffix}.js", array(), HTML_FORMS_VERSION, true ); | |
| 67 | + wp_register_script( 'html-forms', $assets_url . 'js/public.js', array(), HTML_FORMS_VERSION, true ); | |
| 68 | 68 | wp_localize_script( |
| 69 | 69 | 'html-forms', |
| 70 | 70 | 'hf_js_vars', |
| 71 | 71 | array( |
| 72 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), | |
| 72 | + 'ajax_url' => admin_url( 'admin-ajax.php?action=hf_form_submit' ), | |
| 73 | 73 | ) |
| 74 | 74 | ); |
| 75 | 75 | |
| 76 | + wp_register_style( 'html-forms', $assets_url . 'css/forms.css', array(), HTML_FORMS_VERSION ); | |
| 77 | + add_filter( 'script_loader_tag', array( $this, 'add_defer_attribute' ), 10, 2 ); | |
| 78 | + } | |
| 79 | + | |
| 80 | + public function enqueue_assets() { | |
| 76 | 81 | if ( $this->settings['load_stylesheet'] ) { |
| 77 | - wp_enqueue_style( 'html-forms', $assets_url . "css/forms{$suffix}.css", array(), HTML_FORMS_VERSION ); | |
| 82 | + wp_enqueue_style( 'html-forms' ); | |
| 78 | 83 | } |
| 79 | 84 | } |
| 80 | 85 | |
| 81 | 86 | /** |
| 87 | + * Adds defer attribute to our <script> element | |
| 88 | + */ | |
| 89 | + public function add_defer_attribute( $tag, $handle ) { | |
| 90 | + if ( $handle !== 'html-forms' ) { | |
| 91 | + return $tag; | |
| 92 | + } | |
| 93 | + | |
| 94 | + return str_replace( ' src=', ' defer src=', $tag ); | |
| 95 | + } | |
| 96 | + /** | |
| 82 | 97 | * @param Form $form |
| 83 | 98 | * @param array $data |
| 84 | 99 | * @return string |
| 85 | 100 | */ |
| @@ -145,13 +160,13 @@ | ||
| 145 | 160 | return ''; |
| 146 | 161 | } |
| 147 | 162 | |
| 148 | 163 | /** |
| 149 | - * Sanitize array with values before saving. Can be called recursively. | |
| 150 | - * | |
| 151 | - * @param mixed $value | |
| 152 | - * @return mixed | |
| 153 | - */ | |
| 164 | + * Sanitize array with values before saving. Can be called recursively. | |
| 165 | + * | |
| 166 | + * @param mixed $value | |
| 167 | + * @return mixed | |
| 168 | + */ | |
| 154 | 169 | public function sanitize( $value ) { |
| 155 | 170 | if ( is_string( $value ) ) { |
| 156 | 171 | // do nothing if empty string |
| 157 | 172 | if ( $value === '' ) { |
| @@ -189,15 +204,14 @@ | ||
| 189 | 204 | return $value; |
| 190 | 205 | } |
| 191 | 206 | |
| 192 | 207 | /** |
| 193 | - * @return array | |
| 194 | - */ | |
| 208 | + * @return array | |
| 209 | + */ | |
| 195 | 210 | public function get_request_data() { |
| 196 | 211 | $data = $_POST; |
| 197 | 212 | |
| 198 | 213 | if ( ! empty( $_FILES ) ) { |
| 199 | - | |
| 200 | 214 | foreach ( $_FILES as $field_name => $file ) { |
| 201 | 215 | // only add non-empty files so that required field validation works as expected |
| 202 | 216 | // upload could still have errored at this point |
| 203 | 217 | if ( $file['error'] !== UPLOAD_ERR_NO_FILE ) { |
| @@ -209,9 +223,8 @@ | ||
| 209 | 223 | return $data; |
| 210 | 224 | } |
| 211 | 225 | |
| 212 | 226 | public function listen_for_submit() { |
| 213 | - | |
| 214 | 227 | // only respond to AJAX requests with _hf_form_id set. |
| 215 | 228 | if ( empty( $_POST['_hf_form_id'] ) |
| 216 | 229 | || empty( $_SERVER['HTTP_X_REQUESTED_WITH'] ) |
| 217 | 230 | || strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) !== strtolower( 'XMLHttpRequest' ) ) { |
| @@ -223,15 +236,14 @@ | ||
| 223 | 236 | $form = hf_get_form( $form_id ); |
| 224 | 237 | $error_code = $this->validate_form( $form, $data ); |
| 225 | 238 | |
| 226 | 239 | if ( empty( $error_code ) ) { |
| 227 | - | |
| 228 | 240 | /** |
| 229 | - * Filters the field names that should be ignored on the Submission object. | |
| 230 | - * Fields starting with an underscore (_) are ignored by default. | |
| 231 | - * | |
| 232 | - * @param array $names | |
| 233 | - */ | |
| 241 | + * Filters the field names that should be ignored on the Submission object. | |
| 242 | + * Fields starting with an underscore (_) are ignored by default. | |
| 243 | + * | |
| 244 | + * @param array $names | |
| 245 | + */ | |
| 234 | 246 | $ignored_field_names = apply_filters( 'hf_ignored_field_names', array() ); |
| 235 | 247 | |
| 236 | 248 | // filter out ignored field names |
| 237 | 249 | foreach ( $data as $key => $value ) { |
| @@ -261,19 +273,19 @@ | ||
| 261 | 273 | $submission->submitted_at = gmdate( 'Y-m-d H:i:s' ); |
| 262 | 274 | |
| 263 | 275 | // save submission object so that other form processor have an insert ID to work with (eg file upload) |
| 264 | 276 | if ( $form->settings['save_submissions'] ) { |
| 265 | - $submission->save(); | |
| 277 | + $submission->save(); | |
| 266 | 278 | } |
| 267 | 279 | |
| 268 | 280 | /** |
| 269 | - * General purpose hook that runs before all form actions, so we can still modify the submission object that is passed to actions. | |
| 270 | - */ | |
| 281 | + * General purpose hook that runs before all form actions, so we can still modify the submission object that is passed to actions. | |
| 282 | + */ | |
| 271 | 283 | do_action( 'hf_process_form', $form, $submission ); |
| 272 | 284 | |
| 273 | 285 | // re-save submission object for convenience in form processors hooked into hf_process_form |
| 274 | 286 | if ( $form->settings['save_submissions'] ) { |
| 275 | - $submission->save(); | |
| 287 | + $submission->save(); | |
| 276 | 288 | } |
| 277 | 289 | |
| 278 | 290 | // process form actions |
| 279 | 291 | if ( isset( $form->settings['actions'] ) ) { |