-
-
Notifications
You must be signed in to change notification settings - Fork 825
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
Temporary fix for frontend AfformTokens being generated incorrectly #31801
base: master
Are you sure you want to change the base?
Conversation
🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷 Introduction for new contributors...
Quick links for reviewers...
|
Oy. My head does spin a little bit with WP URLs. Just to cover background... make sure I'm following...
Dumb question: In these examples, none of the paths show (If it's literal -- how does the second one work? Is something in Civi/Afform hotwired? Or is it also a WP-page with embed?)
Just to make sure, "the tokens code detects..." is referring to this stuff in i.e. We are sending a well-formed request to The configuration is a little elaborate. I was trying to reproduce in somewhat thinner environment:
For me, on Which isn't super-helpful for me wanting to reproduce. But if you try the same, it might be an interesting comparison. Like:
|
@totten Thanks for spending some time on this. Sorry if my initial explanation was not clear enough. I've clarified below in response to your testing and identified the difference between your "test environment" and my "live environment". ie. I can reproduce with a modified version of your WP hello-world plugin.
Right, sorry! The second URL actually looks like https://mysite.com/civicrm/mysecondform/?_aff=Bearer+secretbearertokenhere#?Case1=123
Correct.
It's basically this code in CRM_Utils_System_WordPress::url() that breaks the Afform MessageTokens URL:
because it finds a valid
Right, the difference between my more complex setup and your nice example is that for me We can simulate that in your example hello-world WP plugin by adding
right after I can't quite see where that is meant to get set but it is set if you access any CiviCRM content via the CiviCRM basepage or embedded via shortcode. With |
Brilliant. So with that, we don't need anything in CiviRules or Afform. The problem can be reframed even more tightly. FWIW, I think that https://lab.civicrm.org/dev/wordpress/-/issues/149 was initially pointing to the same thing. That was closed. But I think it's worth pursuing. Re-filed as: https://lab.civicrm.org/dev/wordpress/-/issues/152 |
It looks like the ordinary path to getting activated is through |
@mattwire Is this correct? The |
@totten Try putting a genuine CiviCRM Shortcode into the page before your EDIT: The code that @mattwire posted also triggers "shortcode mode". |
@@ -100,6 +100,7 @@ public static function evaluateTokens(\Civi\Token\Event\TokenValueEvent $e) { | |||
if (empty($row->context['contactId'])) { | |||
continue; | |||
} | |||
\Civi::$statics['afformtokenscontext'] = TRUE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mattwire Rather than using a global to modify what you're doing, you could replicate what CiviCRM-WordPress does to ensure that a Base Page URL is generated. Something like:
if (function_exists('add_filter')) {
// For WordPress: add modifying callbacks prior to multi-lingual compat.
add_filter('civicrm/basepage/match', [civi_wp()->basepage, 'ensure_match'], 9);
add_filter('civicrm/core/url/base', [civi_wp()->basepage, 'ensure_url'], 9, 2);
}
$url = self::createUrl($afform, $row->context['contactId']);
if (function_exists('add_filter')) {
// For WordPress: remove callbacks.
remove_filter('civicrm/basepage/match', [civi_wp()->basepage, 'ensure_match'], 9);
remove_filter('civicrm/core/url/base', [civi_wp()->basepage, 'ensure_url'], 9);
}
This should make modifying CRM_Utils_System_WordPress::url()
unnecessary.
Overview
On WordPress when you use the Afform "MessageTokens" it doesn't always generate the correct URLs.
Before
URLs generated incorrectly depending on context
After
URLs generated correctly.
Technical Details
Example:
https://mysite.com/myfirstform
.https://mysite.com/mysecondform
.It looks like:
https://mysite.com/myfirstform?civiwp=mysite.com/mysecondform&token=...
But should look like:
https://mysite.com/mysecondform?token=...
This is because the tokens code detects the referrer form and uses that as the frontend_url which we don't want in this case because the forms are completely separate.
Comments
Adding this as draft because it's a hacky proof of concept using \Civi::$statics to pass context through. But we should do it a better way.