Skip to content

Commit

Permalink
Feature: add locale support for visual form builder (#7710)
Browse files Browse the repository at this point in the history
  • Loading branch information
glaubersilva authored Feb 18, 2025
1 parent bce52e6 commit cd521e1
Show file tree
Hide file tree
Showing 26 changed files with 282 additions and 30 deletions.
1 change: 1 addition & 0 deletions give.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ final class Give
Give\DonationSpam\ServiceProvider::class,
Give\Settings\ServiceProvider::class,
Give\FeatureFlags\OptionBasedFormEditor\ServiceProvider::class,
Give\ThirdPartySupport\ServiceProvider::class,
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
use Give\DonationForms\Models\DonationForm;
use Give\Framework\EnqueueScript;
use Give\Framework\Routes\RouteListener;
use Give\Helpers\Language;

class BlockRenderController
{
/**
* @unreleased Add locale support
* @since 3.2.0 include form url for new tab format.
* @since 3.0.0
*
Expand All @@ -38,6 +40,7 @@ public function render(array $attributes)

$embedId = $blockAttributes->blockId ?? '';

$locale = Language::getLocale();
$viewUrl = $this->getViewUrl($donationForm, $embedId);
$formUrl = esc_url(add_query_arg(['p' => $blockAttributes->formId], site_url('?post_type=give_forms')));
$formViewUrl = $this->getFormViewUrl($donationForm);
Expand All @@ -46,7 +49,7 @@ public function render(array $attributes)
* Note: iframe-resizer uses querySelectorAll so using a data attribute makes the most sense to target.
* It will also generate a dynamic ID - so when we have multiple embeds on a page there will be no conflict.
*/
return "<div class='root-data-givewp-embed' data-form-url='$formUrl' data-form-view-url='$formViewUrl' data-src='$viewUrl' data-givewp-embed-id='$embedId' data-form-format='$blockAttributes->formFormat' data-open-form-button='$blockAttributes->openFormButton'></div>";
return "<div class='root-data-givewp-embed' data-form-locale='$locale' data-form-url='$formUrl' data-form-view-url='$formViewUrl' data-src='$viewUrl' data-givewp-embed-id='$embedId' data-form-format='$blockAttributes->formFormat' data-open-form-button='$blockAttributes->openFormButton'></div>";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,19 @@ function DonationFormBlockApp({

const roots = document.querySelectorAll('.root-data-givewp-embed');

/**
* @unreleased Add locale support
*/
roots.forEach((root) => {
const dataSrc = root.getAttribute('data-src');
let dataSrcUrl = root.getAttribute('data-src');
const locale = root.getAttribute('data-form-locale');
if (locale) {
const url = new URL(dataSrcUrl);
url.searchParams.set('locale', locale);
dataSrcUrl = url.toString();
}

const dataSrc = dataSrcUrl;
const embedId = root.getAttribute('data-givewp-embed-id');
const formFormat = root.getAttribute('data-form-format');
const openFormButton = root.getAttribute('data-open-form-button');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class DonationConfirmationReceiptViewRouteData
public $receiptId;

/**
*
* @since 3.0.0
*/
public static function fromRequest(array $request): self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class DonationFormPreviewRouteData
/**
* Convert data from request into DTO
*
* @param array{form-id: string, form-settings: string, form-blocks: string} $request
*
* @since 3.0.0
*
* @param array{form-id: string, form-settings: string, form-blocks: string} $request
*/
public static function fromRequest(array $request): self
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class DonationFormViewRouteData
public $formId;

/**
*
* @since 3.0.0
*/
public static function fromRequest(array $request): self
Expand Down
5 changes: 4 additions & 1 deletion src/DonationForms/DonationFormsAdminPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Give\DonationForms;

use Give\Helpers\Language;

/**
* @unreleased Add locale support
* @since 3.15.0
*/
class DonationFormsAdminPage
Expand All @@ -15,6 +18,6 @@ public function addFormSubmenuLink()
remove_submenu_page('edit.php?post_type=give_forms', 'post-new.php?post_type=give_forms');
add_submenu_page('edit.php?post_type=give_forms', __('Add Form', 'give'), __('Add Form', 'give'),
'edit_give_forms',
'edit.php?post_type=give_forms&page=givewp-form-builder', '', 1);
'edit.php?post_type=give_forms&page=givewp-form-builder&locale=' . Language::getLocale(), '', 1);
}
}
18 changes: 17 additions & 1 deletion src/DonationForms/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Give\DonationForms\Actions\AddHoneyPotFieldToDonationForms;
use Give\DonationForms\Actions\DispatchDonateControllerDonationCreatedListeners;
use Give\DonationForms\Actions\DispatchDonateControllerSubscriptionCreatedListeners;
use Give\DonationForms\Actions\ReplaceGiveReceiptShortcodeViewWithDonationConfirmationIframe;
use Give\DonationForms\Actions\PrintFormMetaTags;
use Give\DonationForms\Actions\ReplaceGiveReceiptShortcodeViewWithDonationConfirmationIframe;
use Give\DonationForms\Actions\SanitizeDonationFormPreviewRequest;
use Give\DonationForms\Actions\StoreBackwardsCompatibleFormMeta;
use Give\DonationForms\AsyncData\Actions\GetAsyncFormDataForListView;
Expand Down Expand Up @@ -45,6 +45,7 @@
use Give\Framework\Migrations\MigrationsRegister;
use Give\Framework\Routes\Route;
use Give\Helpers\Hooks;
use Give\Helpers\Language;
use Give\Log\Log;
use Give\ServiceProviders\ServiceProvider as ServiceProviderInterface;

Expand Down Expand Up @@ -247,33 +248,48 @@ private function registerRoutes()
Route::post('authenticate', AuthenticationRoute::class);

/**
* @unreleased Add locale support
* @since 3.0.0
*/
Route::get('donation-form-view', static function (array $request) {
ini_set('display_errors', 0);
$routeData = DonationFormViewRouteData::fromRequest($request);

if ($locale = $request['locale'] ?? '') {
Language::switchToLocale($locale);
}

return give(DonationFormViewController::class)->show($routeData);
});

/**
* @unreleased Add locale support
* @since 3.0.0
*/
Route::get('donation-confirmation-receipt-view', static function (array $request) {
ini_set('display_errors', 0);
$routeData = DonationConfirmationReceiptViewRouteData::fromRequest($request);

if ($locale = $request['locale'] ?? '') {
Language::switchToLocale($locale);
}

return give(DonationConfirmationReceiptViewController::class)->show($routeData);
});

/**
* @unreleased Add locale support
* @since 3.0.0
*/
Route::post('donation-form-view-preview', static function () {
ini_set('display_errors', 0);
$requestData = (new SanitizeDonationFormPreviewRequest())($_REQUEST);
$routeData = DonationFormPreviewRouteData::fromRequest($requestData);

if ($locale = $requestData['locale'] ?? '') {
Language::switchToLocale($locale);
}

return give(DonationFormViewController::class)->preview($routeData);
});
}
Expand Down
4 changes: 4 additions & 0 deletions src/DonationForms/V2/DonationFormsAdminPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Give\DonationForms\V2\ListTable\DonationFormsListTable;
use Give\FeatureFlags\OptionBasedFormEditor\OptionBasedFormEditor;
use Give\Helpers\EnqueueScript;
use Give\Helpers\Language;
use WP_Post;
use WP_REST_Request;

Expand Down Expand Up @@ -90,6 +91,8 @@ public function highlightAllFormsMenuItem()

/**
* Load scripts
*
* @unreleased Add locale support
*/
public function loadScripts()
{
Expand All @@ -107,6 +110,7 @@ public function loadScripts()
'supportedAddons' => $this->getSupportedAddons(),
'supportedGateways' => $this->getSupportedGateways(),
'isOptionBasedFormEditorEnabled' => OptionBasedFormEditor::isEnabled(),
'locale' => Language::getLocale(),
];

EnqueueScript::make('give-admin-donation-forms', 'assets/dist/js/give-admin-donation-forms.js')
Expand Down
5 changes: 4 additions & 1 deletion src/DonationForms/V2/Endpoints/ListDonationForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Give\DonationForms\V2\ListTable\DonationFormsListTable;
use Give\Framework\Database\DB;
use Give\Framework\QueryBuilder\QueryBuilder;
use Give\Helpers\Language;
use WP_REST_Request;
use WP_REST_Response;

Expand Down Expand Up @@ -108,6 +109,7 @@ public function registerRoute()
}

/**
* @unreleased Add locale support
* @since 2.24.0 Change this to use the new ListTable class
*
* @param WP_REST_Request $request
Expand All @@ -131,7 +133,8 @@ public function handleRequest(WP_REST_Request $request): WP_REST_Response

foreach ($items as $i => &$item) {
$item['name'] = get_the_title($item['id']);
$item['edit'] = get_edit_post_link($item['id'], 'edit');
$item['edit'] = add_query_arg(['locale' => Language::getLocale()],
get_edit_post_link($item['id'], 'edit'));
$item['permalink'] = get_permalink($item['id']);
$item['v3form'] = (bool)give_get_meta($item['id'], 'formBuilderSettings');
$item['status_raw'] = $forms[$i]->status->getValue();
Expand Down
4 changes: 3 additions & 1 deletion src/DonationForms/V2/ListTable/Columns/TitleColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Give\DonationForms\V2\Models\DonationForm;
use Give\Framework\ListTable\ModelColumn;
use Give\Helpers\Language;

/**
* @since 2.24.0
Expand Down Expand Up @@ -38,6 +39,7 @@ public function getLabel(): string
}

/**
* @unreleased Add locale support
* @since 3.0.0 remove html tags from title
* @since 2.24.0
*
Expand All @@ -49,7 +51,7 @@ public function getCellValue($model): string
{
return sprintf(
'<a href="%s" class="giveDonationFormsLink">%s</a>',
get_edit_post_link( $model->id ),
add_query_arg(['locale' => Language::getLocale()], get_edit_post_link($model->id)),
wp_strip_all_tags($model->title)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ declare global {
supportedAddons: Array<string>;
supportedGateways: Array<string>;
isOptionBasedFormEditorEnabled: boolean;
locale: string;
};

GiveNextGen?: {
Expand Down Expand Up @@ -267,7 +268,10 @@ export default function DonationFormsListTable() {
</button>
)}
<a
href={'edit.php?post_type=give_forms&page=givewp-form-builder'}
href={
'edit.php?post_type=give_forms&page=givewp-form-builder&locale=' +
window.GiveDonationForms.locale
}
className={`button button-primary ${styles.button}`}
>
{__('Add Form', 'give')}
Expand Down
30 changes: 26 additions & 4 deletions src/DonationForms/resources/app/utilities/getCurrentFormUrlData.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/**
* @unreleased Add locale support
*/
export default function getCurrentFormUrlData() {
const originUrl = window.top.location.href;
const originUrl = window.top.location.href;

const isEmbed = window.frameElement !== null;
const isEmbed = window.frameElement !== null;

const getEmbedId = () => {
if (!isEmbed) {
Expand All @@ -15,9 +18,28 @@ export default function getCurrentFormUrlData() {
return window.frameElement.id;
};

const getLocale = () => {
if (!isEmbed) {
return null;
}

if (window.frameElement.hasAttribute('data-form-locale')) {
return window.frameElement.getAttribute('data-form-locale');
}

let locale = '';
if (window.frameElement.src) {
const url = new URL(window.frameElement.src);
locale = url.searchParams.get('locale') || '';
}

return locale;
};

return {
originUrl,
isEmbed,
embedId: getEmbedId(),
}
}
locale: getLocale(),
};
}
14 changes: 12 additions & 2 deletions src/DonationForms/resources/app/utilities/handleFormRedirect.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import isRouteInlineRedirect from "@givewp/forms/app/utilities/isRouteInlineRedirect";
import isRouteInlineRedirect from '@givewp/forms/app/utilities/isRouteInlineRedirect';
import getCurrentFormUrlData from '@givewp/forms/app/utilities/getCurrentFormUrlData';

/**
* @unreleased Add locale support
*/
export default async function handleRedirect(url: string, inlineRedirectRoutes: string[]) {
const redirectUrl = new URL(url);
const redirectUrlParams = new URLSearchParams(redirectUrl.search);
const shouldRedirectInline = isRouteInlineRedirect(redirectUrlParams, inlineRedirectRoutes);

const {locale} = getCurrentFormUrlData();

if (locale) {
redirectUrl.searchParams.set('locale', locale);
}

if (shouldRedirectInline) {
// redirect inside iframe
window.location.assign(redirectUrl);
} else {
// redirect outside iframe
window.top.location.assign(redirectUrl);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import getCurrentFormUrlData from '@givewp/forms/app/utilities/getCurrentFormUrl
import postFormData from '@givewp/forms/app/utilities/postFormData';
import convertValuesToFormData from '@givewp/forms/app/utilities/convertValuesToFormData';

/**
* @unreleased Add locale support
*/
export default async function handleSubmitRequest(
values,
setError,
Expand All @@ -31,13 +34,14 @@ export default async function handleSubmitRequest(
}

try {
const {originUrl, isEmbed, embedId} = getCurrentFormUrlData();
const {originUrl, isEmbed, embedId, locale} = getCurrentFormUrlData();

const formValues = {
...values,
originUrl,
isEmbed,
embedId,
locale,
};

const formData = convertValuesToFormData(formValues);
Expand Down Expand Up @@ -80,4 +84,4 @@ export default async function handleSubmitRequest(
message: error?.message ?? __('Something went wrong, please try again or contact support.', 'give'),
});
}
};
}
Loading

0 comments on commit cd521e1

Please sign in to comment.