-
Notifications
You must be signed in to change notification settings - Fork 798
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
Donations block: fix non lolcalized strings #36951
Changes from 2 commits
c8bba58
c8ec2ea
44b1657
cc06f5e
06e5c34
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: other | ||
|
||
Donations Block: fix non localized strings |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,43 +53,50 @@ 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'] | ||
), | ||
); | ||
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'] | ||
); | ||
} | ||
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( | ||
'<p class="%1$s">%2$s</p>', | ||
esc_attr( $donation['class'] ), | ||
wp_kses_post( $donation['extraText'] ) | ||
wp_kses_post( $donation['extraText'] ?? $default_texts['extraText'] ) | ||
); | ||
$buttons .= sprintf( | ||
'<a class="wp-block-button__link donations__donate-button %1$s" href="%2$s">%3$s</a>', | ||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make texts available to the editor. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're going to do it this way rather than using |
||
'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. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
attributes
are not localized. Let's define these strings indonations.php
instead.