diff --git a/assets/scripts/wplf-admin-form.js b/assets/scripts/wplf-admin-form.js index 5d399de..45a2edc 100644 --- a/assets/scripts/wplf-admin-form.js +++ b/assets/scripts/wplf-admin-form.js @@ -28,6 +28,13 @@ $(document).ready(function() { '

' + $(this).prop('name') + '

' ); + // check if we are in nojs fallback state and show warning if user uses input name reserved for WordPress core + if( wplf.nojs_fallback && $.inArray( $(this).prop('name'), wplf.wp_reserved_names ) > -1 ) { + $fieldbox.css('border-color', '#dc3232'); + $fieldbox.css('color', '#dc3232'); + $fieldbox.find('h4').append('

' + wplf.is_wp_reserved_name + ''); + } + if( $(this).prop('required') ) { // mark as required field for validation $fieldbox.addClass('required'); diff --git a/assets/scripts/wplf-form.js b/assets/scripts/wplf-form.js index be07f82..a62cbc1 100644 --- a/assets/scripts/wplf-form.js +++ b/assets/scripts/wplf-form.js @@ -9,6 +9,12 @@ !function(t){function e(){}function n(t,e){return function(){t.apply(e,arguments)}}function o(t){if("object"!=typeof this)throw new TypeError("Promises must be constructed via new");if("function"!=typeof t)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],s(t,this)}function r(t,e){for(;3===t._state;)t=t._value;return 0===t._state?void t._deferreds.push(e):(t._handled=!0,void a(function(){var n=1===t._state?e.onFulfilled:e.onRejected;if(null===n)return void(1===t._state?i:f)(e.promise,t._value);var o;try{o=n(t._value)}catch(r){return void f(e.promise,r)}i(e.promise,o)}))}function i(t,e){try{if(e===t)throw new TypeError("A promise cannot be resolved with itself.");if(e&&("object"==typeof e||"function"==typeof e)){var r=e.then;if(e instanceof o)return t._state=3,t._value=e,void u(t);if("function"==typeof r)return void s(n(r,e),t)}t._state=1,t._value=e,u(t)}catch(i){f(t,i)}}function f(t,e){t._state=2,t._value=e,u(t)}function u(t){2===t._state&&0===t._deferreds.length&&a(function(){t._handled||d(t._value)});for(var e=0,n=t._deferreds.length;n>e;e++)r(t,t._deferreds[e]);t._deferreds=null}function c(t,e,n){this.onFulfilled="function"==typeof t?t:null,this.onRejected="function"==typeof e?e:null,this.promise=n}function s(t,e){var n=!1;try{t(function(t){n||(n=!0,i(e,t))},function(t){n||(n=!0,f(e,t))})}catch(o){if(n)return;n=!0,f(e,o)}}var l=setTimeout,a="function"==typeof setImmediate&&setImmediate||function(t){l(t,0)},d=function(t){"undefined"!=typeof console&&console&&console.warn("Possible Unhandled Promise Rejection:",t)};o.prototype["catch"]=function(t){return this.then(null,t)},o.prototype.then=function(t,n){var o=new this.constructor(e);return r(this,new c(t,n,o)),o},o.all=function(t){var e=Array.prototype.slice.call(t);return new o(function(t,n){function o(i,f){try{if(f&&("object"==typeof f||"function"==typeof f)){var u=f.then;if("function"==typeof u)return void u.call(f,function(t){o(i,t)},n)}e[i]=f,0===--r&&t(e)}catch(c){n(c)}}if(0===e.length)return t([]);for(var r=e.length,i=0;io;o++)t[o].then(e,n)})},o._setImmediateFn=function(t){a=t},o._setUnhandledRejectionFn=function(t){d=t},"undefined"!=typeof module&&module.exports?module.exports=o:t.Promise||(t.Promise=o)}(this); (function() { + + // remove nojs fallback marker from forms + [].forEach.call(document.querySelectorAll(".libre-form .wplf-nojs"), function(element) { + element.parentNode.removeChild(element); + }); + window.wplf = { successCallbacks: [], errorCallbacks: [], diff --git a/classes/class-cpt-wplf-form.php b/classes/class-cpt-wplf-form.php index 097cd84..71b6349 100644 --- a/classes/class-cpt-wplf-form.php +++ b/classes/class-cpt-wplf-form.php @@ -42,6 +42,7 @@ private function __construct() { add_action( 'wp', array( $this, 'maybe_set_404_for_single_form' ) ); add_filter( 'the_content', array( $this, 'use_shortcode_for_preview' ), 0 ); add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_frontend_script' ) ); + add_action( 'init', array( $this, 'maybe_do_nojs_submit' ) ); // default filters for the_content, but we don't want to use actual the_content add_filter( 'wplf_form', 'convert_smilies' ); @@ -148,6 +149,11 @@ function admin_post_scripts_cpt( $hook ) { // enqueue the custom JS for this view wp_enqueue_script( 'wplf-form-edit-js', plugins_url( 'assets/scripts/wplf-admin-form.js', dirname( __FILE__ ) ) ); + wp_localize_script( 'wplf-form-edit-js', 'wplf', array( + 'nojs_fallback' => apply_filters( 'wplf_form_nojs_fallback', false ), + 'wp_reserved_names' => $this->get_wp_reserved_terms(), + 'is_wp_reserved_name' => __( 'Name of this field is reserved for WordPress and may conflict with core functionality when no-js fallback is used.', 'wp-libre-form' ) + ) ); } @@ -268,6 +274,18 @@ function add_meta_boxes_cpt() { 'high' ); + // Maybe WP reserved terms meta box + if ( apply_filters( 'wplf_form_nojs_fallback', false ) ) { + add_meta_box( + 'wplf-wp-reserved-names', + __( 'Field names reserved for WordPress', 'wp-libre-form' ), + array( $this, 'metabox_wp_reserved_terms' ), + 'wplf-form', + 'normal', + 'high' + ); + } + // Form Fields meta box add_meta_box( 'wplf-fields', @@ -328,6 +346,20 @@ function metabox_thank_you( $post ) { wp_nonce_field( 'wplf_form_meta', 'wplf_form_meta_nonce' ); } + /** + * Meta box callback for displaying reserved terms + */ + function metabox_wp_reserved_terms() { +?> +

+ +" || false !== strpos( $content, 'type=file' ) ) : ?> enctype="multipart/form-data" - - element foreach ( $attributes as $attr_name => $attr_value ) { echo esc_attr( $attr_name ) . '="' . esc_attr( $attr_value ) . "\"\n"; } + + if ( apply_filters( 'wplf_form_nojs_fallback', false ) ) : ?> + method="post" + > @@ -556,8 +596,14 @@ class="libre-form libre-form-" + ok = 1; @@ -88,7 +88,20 @@ function wplf_ajax_submit_handler() { do_action( "wplf_{$form->ID}_post_validate_submission", $return ); } - // respond with json - wp_send_json( $return ); - wp_die(); + // Respond with json if was a js call + if ( ! $is_nojs_fallback_call ) { + wp_send_json( $return ); + wp_die(); + } else { + if( $return->ok ) { + $nojs_redirect = get_permalink( $_POST['_referrer_id'] ) . '?wplf_success=true'; + $nojs_redirect = apply_filters( 'wplf_nojs_success_redirect', $nojs_redirect ); + $nojs_redirect = apply_filters( "wplf_$form->post_name}_nojs_success_redirect", $nojs_redirect ); + $nojs_redirect = apply_filters( "wplf_{$form->ID}_nojs_success_redirect", $nojs_redirect ); + } else { + $nojs_redirect = get_permalink( $_POST['_referrer_id'] ) . '?wplf_error=' . urlencode( $return->error ); + } + + wp_safe_redirect( $nojs_redirect ); + } }