Skip to content

Commit

Permalink
Register block attributes in the server and format amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
mmtr committed Oct 19, 2020
1 parent 81b988d commit 14d3f50
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 227 deletions.
175 changes: 175 additions & 0 deletions class-jetpack-currencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php
/**
* Jetpack_Currencies: Utils for displaying and managing currencies.
*
* @package Jetpack
* @since 9.1.0
*/

/**
* General Gutenberg editor specific functionality
*/
class Jetpack_Currencies {
/**
* Currencies definition
*/
const CURRENCIES = array(
'USD' => array(
'format' => '%1$s%2$s', // 1: Symbol 2: currency value
'symbol' => '$',
'decimal' => 2,
),
'GBP' => array(
'format' => '%1$s%2$s', // 1: Symbol 2: currency value
'symbol' => '&#163;',
'decimal' => 2,
),
'JPY' => array(
'format' => '%1$s%2$s', // 1: Symbol 2: currency value
'symbol' => '&#165;',
'decimal' => 0,
),
'BRL' => array(
'format' => '%1$s%2$s', // 1: Symbol 2: currency value
'symbol' => 'R$',
'decimal' => 2,
),
'EUR' => array(
'format' => '%1$s%2$s', // 1: Symbol 2: currency value
'symbol' => '&#8364;',
'decimal' => 2,
),
'NZD' => array(
'format' => '%1$s%2$s', // 1: Symbol 2: currency value
'symbol' => 'NZ$',
'decimal' => 2,
),
'AUD' => array(
'format' => '%1$s%2$s', // 1: Symbol 2: currency value
'symbol' => 'A$',
'decimal' => 2,
),
'CAD' => array(
'format' => '%1$s%2$s', // 1: Symbol 2: currency value
'symbol' => 'C$',
'decimal' => 2,
),
'ILS' => array(
'format' => '%2$s %1$s', // 1: Symbol 2: currency value
'symbol' => '',
'decimal' => 2,
),
'RUB' => array(
'format' => '%2$s %1$s', // 1: Symbol 2: currency value
'symbol' => '',
'decimal' => 2,
),
'MXN' => array(
'format' => '%1$s%2$s', // 1: Symbol 2: currency value
'symbol' => 'MX$',
'decimal' => 2,
),
'MYR' => array(
'format' => '%2$s%1$s', // 1: Symbol 2: currency value
'symbol' => 'RM',
'decimal' => 2,
),
'SEK' => array(
'format' => '%2$s %1$s', // 1: Symbol 2: currency value
'symbol' => 'Skr',
'decimal' => 2,
),
'HUF' => array(
'format' => '%2$s %1$s', // 1: Symbol 2: currency value
'symbol' => 'Ft',
'decimal' => 0, // Decimals are supported by Stripe but not by PayPal.
),
'CHF' => array(
'format' => '%2$s %1$s', // 1: Symbol 2: currency value
'symbol' => 'CHF',
'decimal' => 2,
),
'CZK' => array(
'format' => '%2$s %1$s', // 1: Symbol 2: currency value
'symbol' => '',
'decimal' => 2,
),
'DKK' => array(
'format' => '%2$s %1$s', // 1: Symbol 2: currency value
'symbol' => 'Dkr',
'decimal' => 2,
),
'HKD' => array(
'format' => '%2$s %1$s', // 1: Symbol 2: currency value
'symbol' => 'HK$',
'decimal' => 2,
),
'NOK' => array(
'format' => '%2$s %1$s', // 1: Symbol 2: currency value
'symbol' => 'Kr',
'decimal' => 2,
),
'PHP' => array(
'format' => '%2$s %1$s', // 1: Symbol 2: currency value
'symbol' => '',
'decimal' => 2,
),
'PLN' => array(
'format' => '%2$s %1$s', // 1: Symbol 2: currency value
'symbol' => 'PLN',
'decimal' => 2,
),
'SGD' => array(
'format' => '%1$s%2$s', // 1: Symbol 2: currency value
'symbol' => 'S$',
'decimal' => 2,
),
'TWD' => array(
'format' => '%1$s%2$s', // 1: Symbol 2: currency value
'symbol' => 'NT$',
'decimal' => 0, // Decimals are supported by Stripe but not by PayPal.
),
'THB' => array(
'format' => '%2$s%1$s', // 1: Symbol 2: currency value
'symbol' => '฿',
'decimal' => 2,
),
);

/**
* Format a price with currency.
*
* Uses currency-aware formatting to output a formatted price with a simple fallback.
*
* Largely inspired by WordPress.com's Store_Price::display_currency
*
* @param string $price Price.
* @param string $currency Currency.
* @param string $symbol (Optional) If provided, overrides the default currency symbol.
* @return string Formatted price.
*/
public static function format_price( $price, $currency, $symbol = null ) {
$currency_details = self::CURRENCIES[ $currency ];

if ( $currency_details ) {
// Ensure USD displays as 1234.56 even in non-US locales.
$amount = 'USD' === $currency
? number_format( $price, $currency_details['decimal'], '.', ',' )
: number_format_i18n( $price, $currency_details['decimal'] );

return sprintf(
$currency_details['format'],
null !== $symbol ? $symbol : $currency_details['symbol'],
$amount
);
}

// Fall back to unspecified currency symbol like `¤1,234.05`.
// @link https://en.wikipedia.org/wiki/Currency_sign_(typography).
if ( ! $currency ) {
return '¤' . number_format_i18n( $price, 2 );
}

return number_format_i18n( $price, 2 ) . ' ' . $currency;
}
}
62 changes: 0 additions & 62 deletions extensions/blocks/donations/attributes.js

This file was deleted.

89 changes: 74 additions & 15 deletions extensions/blocks/donations/donations.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,63 @@ function register_block() {
array(
'render_callback' => __NAMESPACE__ . '\render_block',
'plan_check' => true,
'attributes' => array(
'currency' => array(
'type' => 'string',
'default' => 'USD',
),
'oneTimeDonation' => array(
'type' => 'object',
'default' => array(
'show' => true,
'planId' => null,
'amounts' => array( 5, 15, 100 ),
'heading' => __( 'Make a one-time donation', 'jetpack' ),
'extraText' => __( 'Your contribution is appreciated.', 'jetpack' ),
'buttonText' => __( 'Donate', 'jetpack' ),
),
),
'monthlyDonation' => array(
'type' => 'object',
'default' => array(
'show' => true,
'planId' => null,
'amounts' => array( 5, 15, 100 ),
'heading' => __( 'Make a monthly donation', 'jetpack' ),
'extraText' => __( 'Your contribution is appreciated.', 'jetpack' ),
'buttonText' => __( 'Donate monthly', 'jetpack' ),
),
),
'annualDonation' => array(
'type' => 'object',
'default' => array(
'show' => true,
'planId' => null,
'amounts' => array( 5, 15, 100 ),
'heading' => __( 'Make a yearly donation', 'jetpack' ),
'extraText' => __( 'Your contribution is appreciated.', 'jetpack' ),
'buttonText' => __( 'Donate yearly', 'jetpack' ),
),
),
'showCustomAmount' => array(
'type' => 'boolean',
'default' => true,
),
'chooseAmountText' => array(
'type' => 'string',
'default' => __( 'Choose an amount', 'jetpack' ),
),
'customAmountText' => array(
'type' => 'string',
'default' => __( 'Or enter a custom amount', 'jetpack' ),
),
'fallbackLinkUrl' => array(
'type' => 'string',
'source' => 'attribute',
'selector' => '.jetpack-donations-fallback-link',
'attribute' => 'href',
),
),
)
);
}
Expand All @@ -47,7 +104,9 @@ function render_block( $attr, $content ) {

Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME, array( 'thickbox' ) );

require_once JETPACK__PLUGIN_DIR . '/modules/memberships/class-jetpack-memberships.php';
require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php';
require_once JETPACK__PLUGIN_DIR . 'class-jetpack-currencies.php';

add_thickbox();

$donations = array(
Expand Down Expand Up @@ -91,7 +150,7 @@ function render_block( $attr, $content ) {
$extra_text = '';
$buttons = '';
foreach ( $donations as $interval => $donation ) {
$plan_id = intval( $donation['planId'] );
$plan_id = (int) $donation['planId'];
$plan = get_post( $plan_id );
if ( ! $plan || is_wp_error( $plan ) ) {
continue;
Expand All @@ -118,11 +177,9 @@ function render_block( $attr, $content ) {
);
foreach ( $donation['amounts'] as $amount ) {
$amounts .= sprintf(
'<div class="donations__amount" data-amount="%1$s" data-currency="%2$s">%3$s%4$s</div>',
'<div class="donations__amount" data-amount="%1$s">%2$s</div>',
esc_attr( $amount ),
esc_attr( $currency ),
$currency,
$amount
\Jetpack_Currencies::format_price( $amount, $currency )
);
}
$amounts .= '</div>';
Expand All @@ -144,32 +201,34 @@ function render_block( $attr, $content ) {

$custom_amount = '';
if ( $attr['showCustomAmount'] ) {
$custom_amount .= sprintf(
'<p>%s</div>',
$custom_amount .= sprintf(
'<p>%s</p>',
$attr['customAmountText']
);
$custom_amount .= sprintf(
$default_custom_amount = \Jetpack_Memberships::SUPPORTED_CURRENCIES[ $currency ] * 100;
$custom_amount .= sprintf(
'<div class="donations__amount donations__custom-amount">
%1$s
<div class="donations__amount-value" data-currency="%2$s"></div>
<div class="donations__amount-value" data-currency="%2$s" data-empty-text="%3$s"></div>
</div>',
\Jetpack_Currencies::CURRENCIES[ $attr['currency'] ]['symbol'],
$attr['currency'],
$attr['currency']
\Jetpack_Currencies::format_price( $default_custom_amount, $currency, '' )
);
}

return sprintf(
'
<div class="%1$s">
<div className="donations__container">
<div class="donations__container">
%2$s
<div className="donations__content">
<div className="donations__tab">
<div class="donations__content">
<div class="donations__tab">
%3$s
<p>%4$s</p>
%5$s
%6$s
<hr className="donations__separator">
<hr class="donations__separator">
%7$s
%8$s
</div>
Expand Down
2 changes: 0 additions & 2 deletions extensions/blocks/donations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import attributes from './attributes';
import edit from './edit';
import save from './save';
import GridiconHeart from 'gridicons/dist/heart-outline';
Expand Down Expand Up @@ -34,6 +33,5 @@ export const settings = {
},
edit,
save,
attributes,
example: {},
};
Loading

0 comments on commit 14d3f50

Please sign in to comment.