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

feat(reader-revenue): make NYP and Stripe Gateway optional #1645

Merged
merged 18 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
24 changes: 11 additions & 13 deletions includes/class-modal-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,18 @@ function ( $item ) {
}

/** Apply NYP custom price */
if ( class_exists( 'WC_Name_Your_Price_Helpers' ) ) {
$is_product_nyp = \WC_Name_Your_Price_Helpers::is_nyp( $product_id );
$price = filter_input( INPUT_GET, 'price', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
if ( $is_product_nyp ) {
if ( empty( $price ) ) {
$price = \WC_Name_Your_Price_Helpers::get_suggested_price( $product_id );
}
$min_price = \WC_Name_Your_Price_Helpers::get_minimum_price( $product_id );
$max_price = \WC_Name_Your_Price_Helpers::get_maximum_price( $product_id );
$price = ! empty( $max_price ) ? min( $price, $max_price ) : $price;
$price = ! empty( $min_price ) ? max( $price, $min_price ) : $price;

$cart_item_data['nyp'] = (float) \WC_Name_Your_Price_Helpers::standardize_number( $price );
$is_product_nyp = \Newspack_Blocks::can_use_name_your_price() ? \WC_Name_Your_Price_Helpers::is_nyp( $product_id ) : false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: is it necessary to create a variable here considering this is only used in one place?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, not necessary—updated in 42e145b.

$price = filter_input( INPUT_GET, 'price', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
if ( $is_product_nyp ) {
if ( empty( $price ) ) {
$price = \WC_Name_Your_Price_Helpers::get_suggested_price( $product_id );
}
$min_price = \WC_Name_Your_Price_Helpers::get_minimum_price( $product_id );
$max_price = \WC_Name_Your_Price_Helpers::get_maximum_price( $product_id );
$price = ! empty( $max_price ) ? min( $price, $max_price ) : $price;
$price = ! empty( $min_price ) ? max( $price, $min_price ) : $price;

$cart_item_data['nyp'] = (float) \WC_Name_Your_Price_Helpers::standardize_number( $price );
}

/**
Expand Down
10 changes: 10 additions & 0 deletions includes/class-newspack-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ function( $tax ) {
return apply_filters( 'newspack_blocks_home_page_block_custom_taxonomies', $custom_taxonomies );
}

/**
* Check if the Name Your Price extension is available.
*
* @return bool True if available, false if not.
*/
public static function can_use_name_your_price() {
return class_exists( 'WC_Name_Your_Price_Helpers' );
}

/**
* Enqueue block scripts and styles for editor.
*/
Expand Down Expand Up @@ -257,6 +266,7 @@ public static function enqueue_block_editor_assets() {
'has_recaptcha' => class_exists( 'Newspack\Recaptcha' ) && \Newspack\Recaptcha::can_use_captcha(),
'recaptcha_url' => admin_url( 'admin.php?page=newspack-connections-wizard' ),
'custom_taxonomies' => self::get_custom_taxonomies(),
'can_use_name_your_price' => self::can_use_name_your_price(),
];

if ( class_exists( 'WP_REST_Newspack_Author_List_Controller' ) ) {
Expand Down
6 changes: 5 additions & 1 deletion src/blocks/checkout-button/edit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable @wordpress/no-unsafe-wp-apis */
/* globals newspack_blocks_data */

/**
* External dependencies
*/
Expand Down Expand Up @@ -47,7 +49,9 @@ function getVariationName( variation ) {

function getNYP( product ) {
return {
isNYP: product?.meta_data?.some( meta => meta.key === '_nyp' && meta.value === 'yes' ),
isNYP:
newspack_blocks_data?.can_use_name_your_price &&
product?.meta_data?.some( meta => meta.key === '_nyp' && meta.value === 'yes' ),
suggestedPrice: product?.meta_data?.find( meta => meta.key === '_suggested_price' )?.value,
minPrice: product?.meta_data?.find( meta => meta.key === '_min_price' )?.value,
maxPrice: product?.meta_data?.find( meta => meta.key === '_maximum_price' )?.value,
Expand Down
85 changes: 61 additions & 24 deletions src/blocks/donate/edit/FrequencyBasedLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const FrequencyBasedLayout = ( props: { isTiered: boolean } & ComponentProps ) =
}, [ attributes.defaultFrequency ] );

const [ selectedFrequency, setSelectedFrequency ] = useState( attributes.defaultFrequency );

const canUseNameYourPrice = window.newspack_blocks_data?.can_use_name_your_price;
const renderFrequencySelect = ( frequencySlug: DonationFrequencySlug ) => (
<>
<input
Expand Down Expand Up @@ -89,38 +89,75 @@ const FrequencyBasedLayout = ( props: { isTiered: boolean } & ComponentProps ) =
</button>
);

const getFrequencyLabel = ( frequencySlug: DonationFrequencySlug ) => {
switch ( frequencySlug ) {
case 'once':
return '';
case 'month':
return __( ' per month', 'newspack-blocks' );
case 'year':
return __( ' per year', 'newspack-blocks' );
}
};

// This code is fired on tab select and updates aria elements, tabindex states, and radio buttons
const displayAmount = ( amount: number ) => amount.toFixed( 2 ).replace( /\.?0*$/, '' );

const renderUntieredForm = () => (
<div className="wp-block-newspack-blocks-donate__options">
<div className="wp-block-newspack-blocks-donate__frequencies frequencies">
<div className="tab-container">{ availableFrequencies.map( renderTab ) }</div>
{ availableFrequencies.map( frequencySlug => (
<div
className="wp-block-newspack-blocks-donate__frequency frequency"
key={ frequencySlug }
>
{ renderFrequencySelect( frequencySlug ) }
<div className="input-container">
<label
className="donate-label"
htmlFor={ 'newspack-' + frequencySlug + '-' + uid + '-untiered-input' }
>
{ __( 'Donation amount', 'newspack-blocks' ) }
</label>
<div className="wp-block-newspack-blocks-donate__money-input money-input">
<span className="currency">{ settings.currencySymbol }</span>
<AmountValueInput
{ ...props }
frequencySlug={ frequencySlug }
tierIndex={ 3 }
id={ `newspack-${ frequencySlug }-${ uid }-untiered-input` }
/>
{ availableFrequencies.map( frequencySlug => {
const untieredAmount = amounts[ frequencySlug ][ 3 ];
return (
<div
className="wp-block-newspack-blocks-donate__frequency frequency"
key={ frequencySlug }
>
{ renderFrequencySelect( frequencySlug ) }
<div className="input-container">
{ canUseNameYourPrice ? (
<>
<label
className="donate-label"
htmlFor={ 'newspack-' + frequencySlug + '-' + uid + '-untiered-input' }
>
{ __( 'Donation amount', 'newspack-blocks' ) }
</label>
<div className="wp-block-newspack-blocks-donate__money-input money-input">
<span className="currency">{ settings.currencySymbol }</span>
<AmountValueInput
{ ...props }
frequencySlug={ frequencySlug }
tierIndex={ 3 }
id={ `newspack-${ frequencySlug }-${ uid }-untiered-input` }
/>
</div>
</>
) : (
<>
<input
type="radio"
value={ untieredAmount }
className={ 'frequency-input' }
id={ `newspack-${ frequencySlug }-${ uid }-untiered-input` }
name={ `donation_value_${ frequencySlug }` }
defaultChecked={ true }
/>
<label
className="tier-select-label tier-label"
htmlFor={ `newspack-${ frequencySlug }-${ uid }-untiered-input` }
>
{ settings.currencySymbol +
untieredAmount.toFixed( 2 ).replace( /\.?0*$/, '' ) +
getFrequencyLabel( frequencySlug ) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this might cause an issue for languages where the amount should not precede the per month/year text? Might be a better idea to have the getFrequencyLabel method accept the amount and return a translated string which includes the amount in it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, although I had some trouble conceptualizing how we'd translate such a string when it has to include HTML tags for styling, too. 3701068 is my best attempt—how's it look?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dkoo! I left some additional review comments for this 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestions here! Thank you for the guidance on this part—admittedly our work around making sure things are translatable has been lacking. 😅

</label>
</>
) }
</div>
</div>
</div>
) ) }
);
} ) }
</div>
</div>
);
Expand Down
Loading