diff --git a/projects/packages/jitm/.phan/baseline.php b/projects/packages/jitm/.phan/baseline.php index a857c926630d5..4854cbea2fa00 100644 --- a/projects/packages/jitm/.phan/baseline.php +++ b/projects/packages/jitm/.phan/baseline.php @@ -12,10 +12,10 @@ // PhanTypeExpectedObjectPropAccess : 3 occurrences // PhanTypeMismatchArgumentProbablyReal : 2 occurrences // PhanTypeMismatchReturn : 2 occurrences + // PhanUndeclaredMethod : 2 occurrences // PhanPluginSimplifyExpressionBool : 1 occurrence // PhanTypeInvalidDimOffset : 1 occurrence // PhanTypeMismatchArgument : 1 occurrence - // PhanUndeclaredMethod : 1 occurrence // PhanUnreferencedUseNormal : 1 occurrence // Currently, file_suppressions and directory_suppressions are the only supported suppressions diff --git a/projects/packages/jitm/changelog/fix-donation-block-translations b/projects/packages/jitm/changelog/fix-donation-block-translations new file mode 100644 index 0000000000000..6f5229d5d7f8f --- /dev/null +++ b/projects/packages/jitm/changelog/fix-donation-block-translations @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: PHAN: update baseline + + diff --git a/projects/plugins/jetpack/.phan/baseline.php b/projects/plugins/jetpack/.phan/baseline.php index 992cb51900810..e054861a10ff0 100644 --- a/projects/plugins/jetpack/.phan/baseline.php +++ b/projects/plugins/jetpack/.phan/baseline.php @@ -119,7 +119,6 @@ // PhanTypeComparisonFromArray : 1 occurrence // PhanTypeInstantiateAbstract : 1 occurrence // PhanTypeInvalidCallableArraySize : 1 occurrence - // PhanTypeMismatchForeach : 1 occurrence // PhanTypeMismatchReturnSuperType : 1 occurrence // PhanTypeSuspiciousEcho : 1 occurrence // PhanTypeVoidArgument : 1 occurrence @@ -226,7 +225,7 @@ 'extensions/blocks/calendly/calendly.php' => ['PhanTypeMismatchArgumentProbablyReal', 'PhanTypeMismatchReturnProbablyReal'], 'extensions/blocks/contact-info/class-jetpack-contact-info-block.php' => ['PhanTypeMismatchReturn'], 'extensions/blocks/cookie-consent/cookie-consent.php' => ['PhanParamTooMany', 'PhanUndeclaredFunction'], - 'extensions/blocks/donations/donations.php' => ['PhanTypeMismatchArgument', 'PhanTypeMismatchForeach', 'PhanUndeclaredFunction'], + 'extensions/blocks/donations/donations.php' => ['PhanTypeMismatchArgument', 'PhanUndeclaredFunction'], 'extensions/blocks/gif/gif.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeMismatchReturnProbablyReal'], 'extensions/blocks/google-calendar/google-calendar.php' => ['PhanPluginDuplicateConditionalNullCoalescing'], 'extensions/blocks/google-docs-embed/google-docs-embed.php' => ['PhanRedundantCondition', 'PhanUndeclaredFunction'], diff --git a/projects/plugins/jetpack/changelog/fix-donation-block-translations b/projects/plugins/jetpack/changelog/fix-donation-block-translations new file mode 100644 index 0000000000000..73f45e7c5fefd --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-donation-block-translations @@ -0,0 +1,4 @@ +Significance: patch +Type: other + +Donations Block: fix non localized strings diff --git a/projects/plugins/jetpack/extensions/blocks/donations/block.json b/projects/plugins/jetpack/extensions/blocks/donations/block.json index fe0dc5294335e..9ebd3e1a15192 100644 --- a/projects/plugins/jetpack/extensions/blocks/donations/block.json +++ b/projects/plugins/jetpack/extensions/blocks/donations/block.json @@ -45,10 +45,7 @@ "default": { "show": true, "planId": null, - "amounts": [ 5, 15, 100 ], - "heading": "Make a one-time donation", - "extraText": "Your contribution is appreciated.", - "buttonText": "Donate" + "amounts": [ 5, 15, 100 ] } }, "monthlyDonation": { @@ -56,10 +53,7 @@ "default": { "show": true, "planId": null, - "amounts": [ 5, 15, 100 ], - "heading": "Make a monthly donation", - "extraText": "Your contribution is appreciated.", - "buttonText": "Donate monthly" + "amounts": [ 5, 15, 100 ] } }, "annualDonation": { @@ -67,10 +61,7 @@ "default": { "show": true, "planId": null, - "amounts": [ 5, 15, 100 ], - "heading": "Make a yearly donation", - "extraText": "Your contribution is appreciated.", - "buttonText": "Donate yearly" + "amounts": [ 5, 15, 100 ] } }, "showCustomAmount": { diff --git a/projects/plugins/jetpack/extensions/blocks/donations/constants.js b/projects/plugins/jetpack/extensions/blocks/donations/constants.js deleted file mode 100644 index 10c1bb690885f..0000000000000 --- a/projects/plugins/jetpack/extensions/blocks/donations/constants.js +++ /dev/null @@ -1,4 +0,0 @@ -import { __ } from '@wordpress/i18n'; - -export const DEFAULT_CHOOSE_AMOUNT_TEXT = __( 'Choose an amount', 'jetpack' ); -export const DEFAULT_CUSTOM_AMOUNT_TEXT = __( 'Or enter a custom amount', 'jetpack' ); diff --git a/projects/plugins/jetpack/extensions/blocks/donations/donations.php b/projects/plugins/jetpack/extensions/blocks/donations/donations.php index 6578c63260fac..4304688a99913 100644 --- a/projects/plugins/jetpack/extensions/blocks/donations/donations.php +++ b/projects/plugins/jetpack/extensions/blocks/donations/donations.php @@ -53,11 +53,15 @@ function render_block( $attr, $content ) { require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-currencies.php'; + $default_texts = get_default_texts(); + $donations = array( 'one-time' => array_merge( array( - 'title' => __( 'One-Time', 'jetpack' ), - 'class' => 'donations__one-time-item', + 'title' => __( 'One-Time', 'jetpack' ), + 'class' => 'donations__one-time-item', + 'heading' => $default_texts['oneTimeDonation']['heading'], + 'buttonText' => $default_texts['oneTimeDonation']['buttonText'], ), $attr['oneTimeDonation'] ), @@ -65,8 +69,10 @@ function render_block( $attr, $content ) { if ( $attr['monthlyDonation']['show'] ) { $donations['1 month'] = array_merge( array( - 'title' => __( 'Monthly', 'jetpack' ), - 'class' => 'donations__monthly-item', + 'title' => __( 'Monthly', 'jetpack' ), + 'class' => 'donations__monthly-item', + 'heading' => $default_texts['monthlyDonation']['heading'], + 'buttonText' => $default_texts['monthlyDonation']['buttonText'], ), $attr['monthlyDonation'] ); @@ -74,22 +80,23 @@ function render_block( $attr, $content ) { if ( $attr['annualDonation']['show'] ) { $donations['1 year'] = array_merge( array( - 'title' => __( 'Yearly', 'jetpack' ), - 'class' => 'donations__annual-item', + 'title' => __( 'Yearly', 'jetpack' ), + 'class' => 'donations__annual-item', + 'heading' => $default_texts['annualDonation']['heading'], + 'buttonText' => $default_texts['annualDonation']['buttonText'], ), $attr['annualDonation'] ); } - $choose_amount_text = isset( $attr['chooseAmountText'] ) && ! empty( $attr['chooseAmountText'] ) ? $attr['chooseAmountText'] : __( 'Choose an amount', 'jetpack' ); - $custom_amount_text = isset( $attr['customAmountText'] ) && ! empty( $attr['customAmountText'] ) ? $attr['customAmountText'] : __( 'Or enter a custom amount', 'jetpack' ); - - $currency = $attr['currency']; - $nav = ''; - $headings = ''; - $amounts = ''; - $extra_text = ''; - $buttons = ''; + $choose_amount_text = isset( $attr['chooseAmountText'] ) && ! empty( $attr['chooseAmountText'] ) ? $attr['chooseAmountText'] : $default_texts['chooseAmountText']; + $custom_amount_text = isset( $attr['customAmountText'] ) && ! empty( $attr['customAmountText'] ) ? $attr['customAmountText'] : $default_texts['customAmountText']; + $currency = $attr['currency']; + $nav = ''; + $headings = ''; + $amounts = ''; + $extra_text = ''; + $buttons = ''; foreach ( $donations as $interval => $donation ) { $plan_id = (int) $donation['planId']; $plan = get_post( $plan_id ); @@ -127,7 +134,7 @@ function render_block( $attr, $content ) { $extra_text .= sprintf( '

%2$s

', esc_attr( $donation['class'] ), - wp_kses_post( $donation['extraText'] ) + wp_kses_post( $donation['extraText'] ?? $default_texts['extraText'] ) ); $buttons .= sprintf( '%3$s', @@ -188,6 +195,52 @@ function render_block( $attr, $content ) { ); } +/** + * Get the default texts for the block. + * + * @return array + */ +function get_default_texts() { + return array( + 'chooseAmountText' => __( 'Choose an amount', 'jetpack' ), + 'customAmountText' => __( 'Or enter a custom amount', 'jetpack' ), + 'extraText' => __( 'Your contribution is appreciated.', 'jetpack' ), + 'oneTimeDonation' => array( + 'heading' => __( 'Make a one-time donation', 'jetpack' ), + 'buttonText' => __( 'Donate', 'jetpack' ), + ), + 'monthlyDonation' => array( + 'heading' => __( 'Make a monthly donation', 'jetpack' ), + 'buttonText' => __( 'Donate monthly', 'jetpack' ), + ), + 'annualDonation' => array( + 'heading' => __( 'Make a yearly donation', 'jetpack' ), + 'buttonText' => __( 'Donate yearly', 'jetpack' ), + ), + ); +} + +/** + * Make default texts available to the editor. + */ +function load_editor_scripts() { + // Only relevant to the editor right now. + if ( ! is_admin() ) { + return; + } + + $data = array( + 'defaultTexts' => get_default_texts(), + ); + + wp_add_inline_script( + 'jetpack-blocks-editor', + 'var Jetpack_DonationsBlock = ' . wp_json_encode( $data, JSON_HEX_TAG | JSON_HEX_AMP ) . ';', + 'before' + ); +} +add_action( 'enqueue_block_assets', __NAMESPACE__ . '\load_editor_scripts' ); + /** * Determine if AMP should be disabled on posts having Donations blocks. * diff --git a/projects/plugins/jetpack/extensions/blocks/donations/save.js b/projects/plugins/jetpack/extensions/blocks/donations/save.js index 4d3559d8e1f9e..149f50aa6b79d 100644 --- a/projects/plugins/jetpack/extensions/blocks/donations/save.js +++ b/projects/plugins/jetpack/extensions/blocks/donations/save.js @@ -1,4 +1,7 @@ import { RichText, useBlockProps } from '@wordpress/block-editor'; +import { getDefaultTexts } from './utils'; + +const DEFAULT_TEXTS = getDefaultTexts(); const Save = ( { attributes } ) => { const blockProps = useBlockProps.save(); @@ -15,43 +18,52 @@ const Save = ( { attributes } ) => { return (
- - + + { monthlyDonation.show && ( <>
- - + + ) } { annualDonation.show && ( <>
- - + + ) } diff --git a/projects/plugins/jetpack/extensions/blocks/donations/tab.js b/projects/plugins/jetpack/extensions/blocks/donations/tab.js index 65a463ded1504..d70e018d10e55 100644 --- a/projects/plugins/jetpack/extensions/blocks/donations/tab.js +++ b/projects/plugins/jetpack/extensions/blocks/donations/tab.js @@ -6,7 +6,9 @@ import { minimumTransactionAmountForCurrency, } from '../../shared/currencies'; import Amount from './amount'; -import { DEFAULT_CHOOSE_AMOUNT_TEXT, DEFAULT_CUSTOM_AMOUNT_TEXT } from './constants'; +import { getDefaultTexts } from './utils'; + +const DEFAULT_TEXTS = getDefaultTexts(); const Tab = ( { activeTab, attributes, setAttributes } ) => { const { @@ -15,8 +17,8 @@ const Tab = ( { activeTab, attributes, setAttributes } ) => { monthlyDonation, annualDonation, showCustomAmount, - chooseAmountText = DEFAULT_CHOOSE_AMOUNT_TEXT, - customAmountText = DEFAULT_CUSTOM_AMOUNT_TEXT, + chooseAmountText = DEFAULT_TEXTS.chooseAmountText, + customAmountText = DEFAULT_TEXTS.customAmountText, } = attributes; const donationAttributes = { @@ -25,9 +27,9 @@ const Tab = ( { activeTab, attributes, setAttributes } ) => { '1 year': 'annualDonation', }; - const getDonationValue = key => attributes[ donationAttributes[ activeTab ] ][ key ]; + const donationAttribute = donationAttributes[ activeTab ]; + const getDonationValue = key => attributes[ donationAttribute ][ key ]; const setDonationValue = ( key, value ) => { - const donationAttribute = donationAttributes[ activeTab ]; const donation = attributes[ donationAttribute ]; setAttributes( { [ donationAttribute ]: { @@ -66,7 +68,7 @@ const Tab = ( { activeTab, attributes, setAttributes } ) => { setDonationValue( 'heading', value ) } /> { setDonationValue( 'extraText', value ) } />
setButtonText( value ) } allowedFormats={ allowedFormatsForButton } /> diff --git a/projects/plugins/jetpack/extensions/blocks/donations/utils.js b/projects/plugins/jetpack/extensions/blocks/donations/utils.js new file mode 100644 index 0000000000000..2fae1ed7d8d92 --- /dev/null +++ b/projects/plugins/jetpack/extensions/blocks/donations/utils.js @@ -0,0 +1,19 @@ +/** + * Return the default texts defined in `donations.php` and injected client side by assigning them + * to the `Jetpack_DonationsBlock` attribute of the window object. + * + * @returns {object} Defaut texts for the block. + */ +export function getDefaultTexts() { + if ( 'undefined' === typeof window ) { + return {}; + } + + const texts = window.Jetpack_DonationsBlock?.defaultTexts; + + if ( 'object' !== typeof texts ) { + return {}; + } + + return texts; +}