-
-
Notifications
You must be signed in to change notification settings - Fork 824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow for custom base page in AJAX requests #23420
Conversation
(Standard links)
|
…sed on WordPress frontend)
@christianwach @kcristiano input? |
@eileenmcnaughton The fix makes sense and I do want to test in a number of situations. |
Thanks, I have tested and the fix works for me. |
@eileenmcnaughton I'm happy with this. Great catch @mattwire :) |
This seems to bork drupal 9 (e.g. the menu disappears). I'm not clear on how this code works so I'm not sure what to suggest yet. When I do the console test |
@demeritcowboy I wonder if this line (https://github.com/civicrm/civicrm-core/pull/23420/files#diff-ca1e48f494632f29fb7b6867e7767b4507d345e1f468e9671f3bc9a102c8b71eR32) is generating a path starting with |
Checking... Before the patch that line comes out as
After:
The difference seems to be the |
@demeritcowboy What happens if you don't apply the second commit - ie. "Use a more unique placeholder url path for CiviCRM AJAX requests" That commit is not required for the fix but seemed sensible. |
Cool the menu comes back. |
It seems that any relative url that looks like If I use, say, diff --git a/js/crm.ajax.js b/js/crm.ajax.js
index 39f7c85ef9..4cb20cd03e 100644
--- a/js/crm.ajax.js
+++ b/js/crm.ajax.js
@@ -28,11 +28,13 @@
path = path.split('#')[0];
}
frag = path.split('?');
+ // Remove basepage as it can be changed on some CMS eg. WordPress frontend.
+ frag[0] = frag[0].replace('civicrm/', '/');
// Encode url path only if slashes in placeholder were also encoded
- if (tplURL[mode].indexOf('civicrm/placeholder-url-path') >= 0) {
- url = tplURL[mode].replace('civicrm/placeholder-url-path', frag[0]);
+ if (tplURL[mode].indexOf('/crmajax-placeholder-url-path') >= 0) {
+ url = tplURL[mode].replace('/crmajax-placeholder-url-path', frag[0]);
} else {
- url = tplURL[mode].replace('civicrm%2Fplaceholder-url-path', encodeURIComponent(frag[0]));
+ url = tplURL[mode].replace('%2Fcrmajax-placeholder-url-path', encodeURIComponent(frag[0]));
}
if (_.isEmpty(query)) {
diff --git a/templates/CRM/common/l10n.js.tpl b/templates/CRM/common/l10n.js.tpl
index 7e804f5568..67f69ada05 100644
--- a/templates/CRM/common/l10n.js.tpl
+++ b/templates/CRM/common/l10n.js.tpl
@@ -29,7 +29,7 @@
CRM.config.entityRef = $.extend({ldelim}{rdelim}, {$entityRef|@json_encode}, CRM.config.entityRef || {ldelim}{rdelim});
// Initialize CRM.url and CRM.formatMoney
- CRM.url({ldelim}back: '{crmURL p="civicrm/placeholder-url-path" q="civicrm-placeholder-url-query=1" h=0 fb=1}', front: '{crmURL p="civicrm/placeholder-url-path" q="civicrm-placeholder-url-query=1" h=0 fe=1}'{rdelim});
+ CRM.url({ldelim}back: '{crmURL p="civicrm/crmajax-placeholder-url-path" q="civicrm-placeholder-url-query=1" h=0 fb=1}', front: '{crmURL p="civicrm/crmajax-placeholder-url-path" q="civicrm-placeholder-url-query=1" h=0 fe=1}'{rdelim});
CRM.formatMoney('init', false, {$moneyFormat|@json_encode});
// Localize select2 |
@demeritcowboy crmajax sounds sensible. I've updated the PR |
Thanks. Works for me. |
Overview
See #20702. This is a simpler, safer fix for the same issue.
Variants of this issue come up a lot with anything CiviCRM that uses AJAX/API requests on the frontend with a custom base page on WordPress. Eg. Stripe - https://lab.civicrm.org/extensions/stripe/-/issues/370
Before
Custom base page does not work for AJAX requests from "frontend".
After
AJAX requests can be used with any "base page".
Technical Details
On WordPress it is possible to define a custom "base page" for frontend CiviCRM URLs. Eg. if you specify custom base page as
crm
then the URLcivicrm/ajax/rest
would need to be rewritten ascrm/ajax/rest
.CiviCRM uses a placeholder URL
civicrm/placeholder-url-path
in crm.ajax.js and l10n.js scripts which is replaced with the actual URL using a match/replace. But this fails if you have the custom basepage because the placeholder URL gets set to eg.crm/placeholder-url-path
in l10n.js and then does not match the find/replace in crm.ajax.js.We fix that by matching on the part after
civicrm
so we are not dependent on the part that can change. The second commit in this PR changescivicrm/placeholder-url-path
tocivicrm/civicrm-placeholder-url-path
so that the second part is more unique (in case we had another plugin/software on the frontend that also decided to useplaceholder-url-path
).Comments
You can test this by eg:
CRM.url('civicrm/ajax/rest')
on the browser console and seeing what it returns.Be careful that you clear CiviCRM caches between tests (l10n.js is processed using assetbuilder and is only updated when caches are cleared).