Skip to content

Commit d4e9769

Browse files
committed
Add handling for multi-site refresh-token front-end requests
1 parent a07d53d commit d4e9769

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/elements/Form.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use verbb\formie\helpers\ArrayHelper;
1818
use verbb\formie\helpers\HandleHelper;
1919
use verbb\formie\helpers\Html;
20+
use verbb\formie\helpers\UrlHelper;
2021
use verbb\formie\models\FieldLayout;
2122
use verbb\formie\models\FieldLayoutPage;
2223
use verbb\formie\models\FormSettings;
@@ -45,7 +46,6 @@
4546
use craft\helpers\Json;
4647
use craft\helpers\Session;
4748
use craft\helpers\StringHelper;
48-
use craft\helpers\UrlHelper;
4949
use craft\models\FieldLayout as CraftFieldLayout;
5050
use craft\validators\HandleValidator;
5151
use craft\web\View;
@@ -1772,10 +1772,10 @@ public function getFrontEndJsVariables(): array
17721772
'enableBackSubmission' => $pluginSettings->enableBackSubmission,
17731773
'ajaxTimeout' => $pluginSettings->ajaxTimeout,
17741774
'outputConsoleMessages' => $pluginSettings->outputConsoleMessages,
1775-
'baseActionUrl' => rtrim(UrlHelper::actionUrl(''), '/'),
1775+
'baseActionUrl' => rtrim(UrlHelper::siteActionUrl(''), '/'),
17761776

17771777
// Generate the refresh token here to make use of `UrlHelper` generation
1778-
'refreshTokenUrl' => UrlHelper::actionUrl('formie/forms/refresh-tokens', ['form' => 'FORM_PLACEHOLDER']),
1778+
'refreshTokenUrl' => UrlHelper::siteActionUrl('formie/forms/refresh-tokens', ['form' => 'FORM_PLACEHOLDER']),
17791779
];
17801780

17811781
// Render options could contain settings for script tag attributes (CSP)

src/helpers/UrlHelper.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
namespace verbb\formie\helpers;
3+
4+
use verbb\formie\Formie;
5+
6+
use Craft;
7+
use craft\helpers\UrlHelper as CraftUrlHelper;
8+
9+
class UrlHelper extends CraftUrlHelper
10+
{
11+
// Static Methods
12+
// =========================================================================
13+
14+
public static function siteActionUrl(string $path = '', array|string|null $params = null, ?string $scheme = null, ?bool $showScriptName = null): string
15+
{
16+
// Swap the domain to resolve to the current site for front-end requests.
17+
// Note that this should only be done for other domains, hence the check for host changes.
18+
// Otherwise, sub-directory installs would be affected.
19+
// https://github.com/verbb/formie/issues/2479
20+
$url = static::actionUrl($path, $params, $scheme, $showScriptName);
21+
$baseSiteUrl = parse_url(static::baseSiteUrl())['host'] ?? '';
22+
$baseCpUrl = parse_url(static::baseCpUrl())['host'] ?? '';
23+
24+
return str_replace($baseCpUrl, $baseSiteUrl, $url);
25+
}
26+
27+
}

0 commit comments

Comments
 (0)