Port AJAX form submission from premium to core#836
Conversation
|
@faisalahammad Nice, will look in more detail once I get back from my vacation! One thought that occurred to me is that we should probably move the JS inside the core forms JavaScript instead of an additional script. In a way that does not break AJAX forms for users on older versions of Premium. Agree? |
|
@dannyvankooten I've gone ahead and pushed these changes:
Enjoy the rest of your vacation! |
7871033 to
401fb25
Compare
dannyvankooten
left a comment
There was a problem hiding this comment.
@faisalahammad Two minor things. Also, with these changes, what does the upgrade path for existing users of MC4WP Premium look like? We should plan for them to be on this same logic so that we can remove MC4WP_AJAX_Forms from the premium plugin, but does this re-use any existing setting keys or do we need additional logic?
What I want is:
- Users on older Premium plugin with latest Mailchimp for WordPress: AJAX forms via older Premium plugin
- Users on latest Premium plugin with latest Mailchimp for WordPress: AJAX forms via free plugin
What we should be absolutely certain of:
- AJAX forms not breaking under Premium users after they update either plugin.
401fb25 to
d027643
Compare
- Migrates AJAX form submission from premium to core via REST API. - Replaces static 'Loading button text' with animated dots. - Merges AJAX JS logic directly into core forms.js bundle. - Includes backwards compatibility checks to prevent conflicts with older premium versions.
d027643 to
5473b6c
Compare
|
Hi @faisalahammad, Can you clarify what happens in the following scenario's and confirm that everything keeps working as expected:
Do we need to take any special steps to ensure Premium users are automatically on the code path from the free plugin once we remove |
- JS: bail early if window.mc4wp_ajax_vars.inited is set (Premium's own 'already running' flag), preventing any edge-case double-initialization if plugin load order ever shifts - PHP: expand comment on class_exists guard to document the migration contract — when Premium drops MC4WP_AJAX_Forms it must declare Requires Plugins: mailchimp-for-wp (>=4.13.0) to avoid version-gap breakage for users who update Premium but not the free plugin
|
Hi @dannyvankooten, I checked the three scenarios. Everything looks safe. Here is the summary. For scenario 1 and 2, the old premium is active. The code checks if the class For scenario 3, when you remove the class from premium, the free plugin will see it and start doing the AJAX automatically via REST API. No extra work needed here. The only risk is if user updates premium but keeps very old free plugin. Then AJAX will break. To prevent this, the new premium must check for free plugin version 4.13 or higher. The current header only checks if the plugin is active, not the version. This is something we need to do in the premium plugin later. I also added a comment in |
Summary
Ports the AJAX form submission feature from
mc4wp-premiuminto the free plugin, using the existing REST API endpoint instead ofadmin-ajax.php. AJAX is enabled by default with a per-form opt-out toggle.Fixes #835
Problem
AJAX form submission is only available as a premium add-on. The free plugin always does a full page reload on submit.
Solution
Reused the existing
mc4wp/v1/formREST endpoint (already handles form processing) and enriched its JSON response with the data needed for client-side updates. The JS loader and submission handler are ported from premium with minimal changes — same event structure, same loading indicator, same CSS class hook (mc4wp-ajax).When the premium AJAX module is active, the free plugin's AJAX settings and scripts are automatically suppressed via
class_exists('MC4WP_AJAX_Forms')guards to avoid duplicates.Changes
REST endpoint —
class-form-manager.phpBefore: Returns
{ "success": true/false }After:
Why: The JS handler needs event names, messages, and redirect URLs to replicate server-side behavior client-side. This matches the response shape from the premium's
respond_to_request().Asset management —
class-asset-manager.phpRegisters
mc4wp-ajax-formsscript, localizes it with REST URL/nonce and loading character, and enqueues it when an AJAX-enabled form is on the page. Guarded with! class_exists('MC4WP_AJAX_Forms')so it won't conflict with premium.Uses
•as the default loading character with the same animated dot pattern as premium (• → • • → • • •). Filterable viamc4wp_forms_ajax_loading_character.Form element —
class-form-element.phpAdds
mc4wp-ajaxCSS class when the form'sajaxsetting is enabled — same hook the JS uses to identify AJAX-enabled forms.Admin UI —
form-settings.phpAdds "Enable AJAX form submission?" radio toggle below the redirect URL field (same position as premium). Wrapped in
! class_exists('MC4WP_AJAX_Forms')so it doesn't duplicate when premium is active.JavaScript —
ajax-forms.js+ajax-form-loader.jsPorted from premium. Intercepts form submit, POSTs to REST endpoint, handles JSON response (success/error messages, hide fields, redirect, events). The loader shows an animated bullet indicator matching premium's UX. Override via
data-loading-textattribute on the submit button.Config —
default-form-settings.php+webpack.config.jsAdded
'ajax' => 1default. Addedajax-formswebpack entry point.Testing
AJAX submit (default):
Error handling:
Opt-out:
Premium compatibility:
Plugin Build
mailchimp-for-wp-fix-835.zip