Skip to content
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

Feature: add locale support for visual form builder #7710

Merged
merged 21 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b0c1fe3
feature: add locale support for visual form builder
glaubersilva Feb 5, 2025
687e541
Merge branch 'develop' into feature/vfb-locale-support-GIVE-2124
glaubersilva Feb 5, 2025
46b2fc4
feature: respect language selected on the WPML in the admin area when…
glaubersilva Feb 6, 2025
cb35c91
refactor: replace function to switch locale
glaubersilva Feb 6, 2025
8b66eb7
refactor: replace get_locale function
glaubersilva Feb 6, 2025
3707519
feature: add locale support to form preview mode
glaubersilva Feb 6, 2025
5b98d5b
feature: add Polylang support
glaubersilva Feb 6, 2025
4927d32
refactor: replace get_locale function
glaubersilva Feb 7, 2025
27e45f6
refactor: remove unnecessary method and property
glaubersilva Feb 7, 2025
57a859e
refactor: check if PPL function exist
glaubersilva Feb 7, 2025
a32a034
doc: add unreleased tag
glaubersilva Feb 7, 2025
156fc41
Merge branch 'develop' into feature/vfb-locale-support-GIVE-2124
glaubersilva Feb 7, 2025
88698fa
feature: add givewp_locale filter
glaubersilva Feb 10, 2025
a467763
Merge branch 'feature/vfb-locale-support-GIVE-2124' of github.com:imp…
glaubersilva Feb 10, 2025
f338156
refactor: move logic to switch locale from controllers and views to t…
glaubersilva Feb 10, 2025
5d6d324
Merge branch 'develop' into feature/vfb-locale-support-GIVE-2124
glaubersilva Feb 10, 2025
1b59a47
chore: remove spaces
glaubersilva Feb 10, 2025
4c5d3a5
refactor: remove locale from DTOs
glaubersilva Feb 12, 2025
666a510
feature: add locale support to edit form links
glaubersilva Feb 12, 2025
23e8b2a
doc: remove unreleased tag
glaubersilva Feb 12, 2025
920f146
fix: edit link
glaubersilva Feb 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading