diff --git a/give.php b/give.php index 5faa31f229..4180202bd8 100644 --- a/give.php +++ b/give.php @@ -68,6 +68,7 @@ use Give\Framework\PaymentGateways\PaymentGatewayRegister; use Give\Framework\ValidationRules\ValidationRulesServiceProvider; use Give\Framework\WordPressShims\ServiceProvider as WordPressShimsServiceProvider; +use Give\Helpers\Language; use Give\LegacySubscriptions\ServiceProvider as LegacySubscriptionsServiceProvider; use Give\License\LicenseServiceProvider; use Give\Log\LogServiceProvider; @@ -361,6 +362,7 @@ private function setup_constants() /** * Loads the plugin language files. * + * @unreleased Use Language class * @since 1.0 * @access public * @@ -368,17 +370,7 @@ private function setup_constants() */ public function load_textdomain() { - // Set filter for Give's languages directory - $give_lang_dir = dirname(plugin_basename(GIVE_PLUGIN_FILE)) . '/languages/'; - $give_lang_dir = apply_filters('give_languages_directory', $give_lang_dir); - - // Traditional WordPress plugin locale filter. - $locale = is_admin() && function_exists('get_user_locale') ? get_user_locale() : get_locale(); - $locale = apply_filters('plugin_locale', $locale, 'give'); - - unload_textdomain('give'); - load_textdomain('give', WP_LANG_DIR . '/give/give-' . $locale . '.mo'); - load_plugin_textdomain('give', false, $give_lang_dir); + Language::load(); } /** diff --git a/globals.d.ts b/globals.d.ts index e1168d5a84..25867239f9 100644 --- a/globals.d.ts +++ b/globals.d.ts @@ -1,5 +1,5 @@ declare module '*.svg'; declare module '*.module.css'; declare module '*.module.scss'; - -declare module 'window.givewp.form.blocks'; +// this makes the wp or window.wp global variable available in the eyes of TypeScript +declare var wp; diff --git a/src/DonationForms/ViewModels/DonationConfirmationReceiptViewModel.php b/src/DonationForms/ViewModels/DonationConfirmationReceiptViewModel.php index 2da076e3c9..47c915c996 100644 --- a/src/DonationForms/ViewModels/DonationConfirmationReceiptViewModel.php +++ b/src/DonationForms/ViewModels/DonationConfirmationReceiptViewModel.php @@ -10,6 +10,7 @@ use Give\Framework\Receipts\DonationReceipt; use Give\Framework\Receipts\DonationReceiptBuilder; use Give\Framework\Support\Scripts\Concerns\HasScriptAssetFile; +use Give\Helpers\Language; /** * @since 3.0.0 @@ -92,7 +93,7 @@ public function render(): string $donationForm = !$donationFormRepository->isLegacyForm( $this->donation->formId ) ? $this->getDonationForm() : null; - + $formDesignId = $donationForm ? $donationForm->settings->designId : DeveloperFormDesign::id(); $customCss = $donationForm && $donationForm->settings->customCss ? $donationForm->settings->customCss : null; $primaryColor = $donationForm ? $donationForm->settings->primaryColor : '#69B868'; @@ -147,7 +148,7 @@ public function enqueueGlobalStyles(string $primaryColor, string $secondaryColor 'givewp-global-form-styles', ":root { --givewp-primary-color:{$primaryColor}; - --givewp-secondary-color:{$secondaryColor}; + --givewp-secondary-color:{$secondaryColor}; }" ); @@ -168,14 +169,17 @@ public function enqueueGlobalStyles(string $primaryColor, string $secondaryColor */ private function enqueueFormScripts(int $formId, string $formDesignId) { + $handle = 'givewp-donation-form-registrars'; wp_enqueue_script( - 'givewp-donation-form-registrars', + $handle, GIVE_PLUGIN_URL . 'build/donationFormRegistrars.js', $this->getScriptAssetDependencies(GIVE_PLUGIN_DIR . 'build/donationFormRegistrars.asset.php'), GIVE_VERSION, true ); + Language::setScriptTranslations($handle); + wp_add_inline_script( 'givewp-donation-form-registrars', 'window.givewpDonationFormExports = ' . wp_json_encode($this->formExports()) . ';', diff --git a/src/DonationForms/ViewModels/DonationFormViewModel.php b/src/DonationForms/ViewModels/DonationFormViewModel.php index 00b068f7b0..6261424b9d 100644 --- a/src/DonationForms/ViewModels/DonationFormViewModel.php +++ b/src/DonationForms/ViewModels/DonationFormViewModel.php @@ -15,6 +15,7 @@ use Give\Framework\FormDesigns\Registrars\FormDesignRegistrar; use Give\Framework\Support\Scripts\Concerns\HasScriptAssetFile; use Give\Helpers\Hooks; +use Give\Helpers\Language; /** * @since 3.0.0 @@ -270,14 +271,17 @@ private function enqueueRegistrars() GIVE_VERSION ); + $handle = 'givewp-donation-form-registrars'; wp_enqueue_script( - 'givewp-donation-form-registrars', + $handle, GIVE_PLUGIN_URL . 'build/donationFormRegistrars.js', $this->getScriptAssetDependencies(GIVE_PLUGIN_DIR . 'build/donationFormRegistrars.asset.php'), GIVE_VERSION, true ); + Language::setScriptTranslations($handle); + wp_add_inline_script( 'givewp-donation-form-registrars', 'window.givewpDonationFormExports = ' . wp_json_encode($this->exports()) . ';', @@ -304,6 +308,7 @@ private function enqueueGateways(int $formId) } /** + * @unreleased Set script translations * @since 3.0.0 */ private function enqueueDesign(string $formDesignId) @@ -317,8 +322,9 @@ private function enqueueDesign(string $formDesignId) } if ($design->js()) { + $handle = 'givewp-form-design-' . $design::id(); wp_enqueue_script( - 'givewp-form-design-' . $design::id(), + $handle, $design->js(), array_merge( $design->dependencies(), @@ -326,6 +332,8 @@ private function enqueueDesign(string $formDesignId) ), true ); + + Language::setScriptTranslations($handle); } } } diff --git a/src/DonationForms/resources/registrars/templates/fields/Gateways.tsx b/src/DonationForms/resources/registrars/templates/fields/Gateways.tsx index 65beb55a90..ef827f501d 100644 --- a/src/DonationForms/resources/registrars/templates/fields/Gateways.tsx +++ b/src/DonationForms/resources/registrars/templates/fields/Gateways.tsx @@ -1,7 +1,7 @@ import {ErrorMessage} from '@hookform/error-message'; import type {GatewayFieldProps, GatewayOptionProps} from '@givewp/forms/propTypes'; import {ErrorBoundary} from 'react-error-boundary'; -import {__} from '@wordpress/i18n'; +import {__, sprintf} from '@wordpress/i18n'; import {useEffect, useMemo} from 'react'; import {createInterpolateElement} from '@wordpress/element'; @@ -146,7 +146,7 @@ function GatewayOption({gateway, defaultChecked, inputProps}: GatewayOptionProps return (
  • - +
    { const { @@ -30,7 +30,7 @@ const Edit = ({attributes, setAttributes}) => { recurringOptInDefaultBillingPeriod, } = attributes; - const {gateways} = getFormBuilderData(); + const {gateways} = getFormBuilderWindowData(); const isRecurringSupported = gateways.some((gateway) => gateway.enabled && gateway.supportsSubscriptions); const isRecurring = isRecurringSupported && recurringEnabled; diff --git a/src/FormBuilder/resources/js/form-builder/src/blocks/fields/amount/inspector/index.tsx b/src/FormBuilder/resources/js/form-builder/src/blocks/fields/amount/inspector/index.tsx index 36f983b872..8031c748dd 100644 --- a/src/FormBuilder/resources/js/form-builder/src/blocks/fields/amount/inspector/index.tsx +++ b/src/FormBuilder/resources/js/form-builder/src/blocks/fields/amount/inspector/index.tsx @@ -12,7 +12,7 @@ import {InspectorControls} from '@wordpress/block-editor'; import {CurrencyControl, formatCurrencyAmount} from '@givewp/form-builder/common/currency'; import periodLookup from '../period-lookup'; import RecurringDonationsPromo from '@givewp/form-builder/promos/recurring-donations'; -import {getFormBuilderData} from '@givewp/form-builder/common/getWindowData'; +import {getFormBuilderWindowData} from '@givewp/form-builder/common/getWindowData'; import {useCallback, useState} from '@wordpress/element'; import Options from '@givewp/form-builder/components/OptionsPanel'; import {OptionProps} from '@givewp/form-builder/components/OptionsPanel/types'; @@ -71,7 +71,7 @@ const Inspector = ({attributes, setAttributes}) => { [recurringBillingPeriodOptions] ); - const {gateways, recurringAddonData, gatewaySettingsUrl} = getFormBuilderData(); + const {gateways, recurringAddonData, gatewaySettingsUrl} = getFormBuilderWindowData(); const enabledGateways = gateways.filter((gateway) => gateway.enabled); const recurringGateways = gateways.filter((gateway) => gateway.supportsSubscriptions); const isRecurringSupported = enabledGateways.some((gateway) => gateway.supportsSubscriptions); diff --git a/src/FormBuilder/resources/js/form-builder/src/blocks/fields/payment-gateways/Edit.tsx b/src/FormBuilder/resources/js/form-builder/src/blocks/fields/payment-gateways/Edit.tsx index 74dd314f87..383106e8a4 100644 --- a/src/FormBuilder/resources/js/form-builder/src/blocks/fields/payment-gateways/Edit.tsx +++ b/src/FormBuilder/resources/js/form-builder/src/blocks/fields/payment-gateways/Edit.tsx @@ -1,6 +1,6 @@ import {BlockEditProps} from '@wordpress/blocks'; import {ReactNode} from 'react'; -import {getFormBuilderData} from '@givewp/form-builder/common/getWindowData'; +import {getFormBuilderWindowData} from '@givewp/form-builder/common/getWindowData'; const GatewayItem = ({label, icon}: {label: string; icon: ReactNode}) => { return ( @@ -18,7 +18,7 @@ const GatewayItem = ({label, icon}: {label: string; icon: ReactNode}) => { }; export default function Edit(props: BlockEditProps) { - const {gateways} = getFormBuilderData(); + const {gateways} = getFormBuilderWindowData(); return (
    ) { const [showAgreementTextModal, setShowAgreementTextModal] = useState(false); - const globalSettings = getFormBuilderData().termsAndConditions; + const globalSettings = getFormBuilderWindowData().termsAndConditions; if (useGlobalSettings) { checkboxLabel = globalSettings.checkboxLabel; diff --git a/src/FormBuilder/resources/js/form-builder/src/common/currency.tsx b/src/FormBuilder/resources/js/form-builder/src/common/currency.tsx index 78e9fe3a78..08a6a75d06 100644 --- a/src/FormBuilder/resources/js/form-builder/src/common/currency.tsx +++ b/src/FormBuilder/resources/js/form-builder/src/common/currency.tsx @@ -1,9 +1,10 @@ import CurrencyInput, {CurrencyInputProps, formatValue} from 'react-currency-input-field'; import {BaseControl} from '@wordpress/components'; import {useInstanceId} from '@wordpress/compose'; +import {getFormBuilderWindowData} from '@givewp/form-builder/common/getWindowData'; const formatCurrencyAmount = (amount: string) => { - const {currency = 'USD'} = window?.storageData ?? {}; + const {currency = 'USD'} = getFormBuilderWindowData(); return formatValue({ value: amount, @@ -22,7 +23,7 @@ interface CurrencyControlProps extends CurrencyInputProps { } const CurrencyControl = ({label, help, hideLabelFromVision, ...rest}: CurrencyControlProps) => { - const {currency = 'USD'} = window?.storageData ?? {}; + const {currency = 'USD'} = getFormBuilderWindowData(); // simplified implementation of useBaseControlProps() const uniqueId = useInstanceId(BaseControl, 'wp-components-base-control'); diff --git a/src/FormBuilder/resources/js/form-builder/src/common/getWindowData.ts b/src/FormBuilder/resources/js/form-builder/src/common/getWindowData.ts index dba7309b55..25eb5df362 100644 --- a/src/FormBuilder/resources/js/form-builder/src/common/getWindowData.ts +++ b/src/FormBuilder/resources/js/form-builder/src/common/getWindowData.ts @@ -7,39 +7,64 @@ import type { TermsAndConditions, } from '@givewp/form-builder/types'; -declare global { - interface Window { - wp?: any; - storageData?: { - formId: number; - nonce: string; - formDesigns: FormDesign[]; - formPage: FormPageSettings; - currency: string; - gateways: Gateway[]; - recurringAddonData?: { - isInstalled: boolean; - }; - gatewaySettingsUrl: string; - emailPreviewURL: string; - emailTemplateTags: TemplateTag[]; - emailNotifications: EmailNotification[]; - emailDefaultAddress: string; - disallowedFieldNames: string[]; - donationConfirmationTemplateTags: TemplateTag[]; - termsAndConditions: TermsAndConditions; - }; - } +import BlockRegistrar from '@givewp/form-builder/registrars/blocks'; + +/** + * @since 3.0.0 + */ +interface FormBuilderWindowData { + formId: number; + nonce: string; + resourceURL: string; + previewURL: string; + blockData: string; + settings: string; + formDesigns: FormDesign[]; + formPage: FormPageSettings; + currency: string; + gateways: Gateway[]; + recurringAddonData?: { + isInstalled: boolean; + }; + gatewaySettingsUrl: string; + emailPreviewURL: string; + emailTemplateTags: TemplateTag[]; + emailNotifications: EmailNotification[]; + emailDefaultAddress: string; + disallowedFieldNames: string[]; + donationConfirmationTemplateTags: TemplateTag[]; + termsAndConditions: TermsAndConditions; } -export default function getWindowData() { +/** + * @since 3.0.0 + */ +declare const window: { + storageData: FormBuilderWindowData; + givewp: { + form: { + blocks: BlockRegistrar; + }; + }; +} & Window; + +/** + * @since 3.0.0 + */ +export default function getWindowData(): FormBuilderWindowData { return window.storageData; } -export function getStorageData() { +/** + * @since 3.0.0 + */ +export function getFormBuilderWindowData(): FormBuilderWindowData { return window.storageData; } -export function getFormBuilderData() { - return window.storageData; +/** + * @since 3.0.0 + */ +export function getBlockRegistrar(): BlockRegistrar { + return window.givewp.form.blocks; } diff --git a/src/FormBuilder/resources/js/form-builder/src/common/registerBlocks.ts b/src/FormBuilder/resources/js/form-builder/src/common/registerBlocks.ts index 6dd32c8a7e..bcfe10e41d 100644 --- a/src/FormBuilder/resources/js/form-builder/src/common/registerBlocks.ts +++ b/src/FormBuilder/resources/js/form-builder/src/common/registerBlocks.ts @@ -1,5 +1,6 @@ import {BlockSupports, registerBlockType, setUnregisteredTypeHandlerName} from '@wordpress/blocks'; import {__experimentalGetCoreBlocks} from '@wordpress/block-library'; +import {getBlockRegistrar} from '@givewp/form-builder/common/getWindowData'; /** * Registers the missing block from WordPress core. @@ -22,8 +23,7 @@ const registerMissingBlock = () => { } }; -// @ts-ignore -const blockRegistrar = window.givewp.form.blocks; +const blockRegistrar = getBlockRegistrar(); /** * @since 3.0.0 diff --git a/src/FormBuilder/resources/js/form-builder/src/components/sidebar/panels/FieldTypesList.tsx b/src/FormBuilder/resources/js/form-builder/src/components/sidebar/panels/FieldTypesList.tsx index b6ceafde1b..55df70afb2 100644 --- a/src/FormBuilder/resources/js/form-builder/src/components/sidebar/panels/FieldTypesList.tsx +++ b/src/FormBuilder/resources/js/form-builder/src/components/sidebar/panels/FieldTypesList.tsx @@ -7,9 +7,10 @@ import {Fragment, useState} from 'react'; import {BlockInstance} from '@wordpress/blocks'; import {FieldBlock} from '@givewp/form-builder/types'; import BlockTypesList from '@givewp/form-builder/components/forks/BlockTypesList'; +import {getBlockRegistrar} from '@givewp/form-builder/common/getWindowData'; // @ts-ignore -const blockRegistrar = window.givewp.form.blocks; +const blockRegistrar = getBlockRegistrar(); type SearchBlock = { id: string; diff --git a/src/FormBuilder/resources/js/form-builder/src/settings/donation-confirmation/index.tsx b/src/FormBuilder/resources/js/form-builder/src/settings/donation-confirmation/index.tsx index 1c72dd5cd2..ac6bb44ae8 100644 --- a/src/FormBuilder/resources/js/form-builder/src/settings/donation-confirmation/index.tsx +++ b/src/FormBuilder/resources/js/form-builder/src/settings/donation-confirmation/index.tsx @@ -4,9 +4,9 @@ import {setFormSettings, useFormState, useFormStateDispatch} from '@givewp/form- import PopoverContentWithTemplateTags from '@givewp/form-builder/components/settings/PopoverContentWithTemplateTags'; import usePopoverState from '@givewp/form-builder/hooks/usePopoverState'; import ControlForPopover from '@givewp/form-builder/components/settings/ControlForPopover'; -import {getFormBuilderData} from '@givewp/form-builder/common/getWindowData'; +import {getFormBuilderWindowData} from '@givewp/form-builder/common/getWindowData'; -const {donationConfirmationTemplateTags} = getFormBuilderData(); +const {donationConfirmationTemplateTags} = getFormBuilderWindowData(); const DonationConfirmation = () => { const { diff --git a/src/FormBuilder/resources/js/form-builder/src/settings/email/logo-upload/index.tsx b/src/FormBuilder/resources/js/form-builder/src/settings/email/logo-upload/index.tsx index d70a5e94ba..979efba43e 100644 --- a/src/FormBuilder/resources/js/form-builder/src/settings/email/logo-upload/index.tsx +++ b/src/FormBuilder/resources/js/form-builder/src/settings/email/logo-upload/index.tsx @@ -3,24 +3,23 @@ * @link https://wordpress.stackexchange.com/a/382291 */ -import React, { useState, useEffect } from 'react' -import _ from 'lodash' -import {Button, TextControl} from "@wordpress/components"; -import {upload} from "@wordpress/icons"; -import {__} from "@wordpress/i18n"; +import React from 'react'; +import _ from 'lodash'; +import {Button, TextControl} from '@wordpress/components'; +import {upload} from '@wordpress/icons'; +import {__} from '@wordpress/i18n'; export default ({value, onChange}) => { - // The media library uses Backbone.js, which can conflict with lodash. _.noConflict(); let frame; const openMediaLibrary = (event) => { - event.preventDefault() + event.preventDefault(); if (frame) { - frame.open() - return + frame.open(); + return; } frame = window.wp.media({ @@ -29,10 +28,9 @@ export default ({value, onChange}) => { text: __('Use this media', 'givewp'), }, multiple: false, // Set to true to allow multiple files to be selected - }) - - frame.on( 'select', function() { + }); + frame.on('select', function () { // Get media attachment details from the frame state var attachment = frame.state().get('selection').first().toJSON(); diff --git a/src/FormBuilder/resources/js/form-builder/src/settings/email/template-options/components/email-preview-content.tsx b/src/FormBuilder/resources/js/form-builder/src/settings/email/template-options/components/email-preview-content.tsx index a18bf2d727..ac3160ef03 100644 --- a/src/FormBuilder/resources/js/form-builder/src/settings/email/template-options/components/email-preview-content.tsx +++ b/src/FormBuilder/resources/js/form-builder/src/settings/email/template-options/components/email-preview-content.tsx @@ -1,18 +1,18 @@ -import {useEffect, useState} from "react"; -import {useFormState} from "@givewp/form-builder/stores/form-state"; -import {getStorageData} from "@givewp/form-builder/common/getWindowData"; -import {__} from "@wordpress/i18n"; +import {useEffect, useState} from 'react'; +import {useFormState} from '@givewp/form-builder/stores/form-state'; +import {getFormBuilderWindowData} from '@givewp/form-builder/common/getWindowData'; +import {__} from '@wordpress/i18n'; const EmailPreviewContent = ({emailType}) => { + const [previewHtml, setPreviewHtml] = useState(null); - const [ previewHtml, setPreviewHtml ] = useState(null); + const { + settings: {emailTemplateOptions, emailTemplate, emailLogo, emailFromName, emailFromEmail}, + } = useFormState(); - const {settings: {emailTemplateOptions, emailTemplate, emailLogo, emailFromName, emailFromEmail}} = useFormState(); - - const {formId, nonce, emailPreviewURL} = getStorageData() + const {formId, nonce, emailPreviewURL} = getFormBuilderWindowData(); useEffect(() => { - // @ts-ignore jQuery .post({ @@ -29,31 +29,32 @@ const EmailPreviewContent = ({emailType}) => { email_logo: emailLogo, email_from_name: emailFromName, email_from_email: emailFromEmail, - ...emailTemplateOptions[emailType] + ...emailTemplateOptions[emailType], }, }) .then((response) => { - setPreviewHtml(response) + setPreviewHtml(response); }) .fail((error) => { - setPreviewHtml('Error loading preview.') + setPreviewHtml('Error loading preview.'); }); }, []); - return previewHtml - ?