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: new option for minimum donation amount #1895

Merged
merged 7 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MoneyInput extends Component {
* Render.
*/
render() {
const { currencySymbol, label, value, onChange } = this.props;
const { currencySymbol, error, label, min, value, onChange } = this.props;

return (
<div className="newspack-donations-wizard__money-input-container">
Expand All @@ -30,13 +30,14 @@ class MoneyInput extends Component {
<div className="currency">{ currencySymbol }</div>
<TextControl
type="number"
min="0"
label={ label }
hideLabelFromVision
label={ label }
min={ min }
value={ value }
onChange={ onChange }
/>
</div>
{ error && <p className="newspack-donations-wizard__money-input-error">{ error }</p> }
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@
margin: 0 0 8px;
}
}

&-error {
color: wp-colors.$alert-red;
padding-top: 8px;
}
}
}
61 changes: 60 additions & 1 deletion assets/wizards/readerRevenue/views/donation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Notice,
SectionHeader,
SelectControl,
TextControl,
Wizard,
} from '../../../../components/src';
import { READER_REVENUE_WIZARD_SLUG } from '../../constants';
Expand Down Expand Up @@ -52,6 +53,7 @@ type WizardData = {
};
currencySymbol: string;
tiered: boolean;
minimumDonation: string;
};
donation_page: {
editUrl: string;
Expand All @@ -67,7 +69,8 @@ export const DonationAmounts = () => {
return null;
}

const { amounts, currencySymbol, tiered, disabledFrequencies } = wizardData.donation_data;
const { amounts, currencySymbol, tiered, disabledFrequencies, minimumDonation } =
wizardData.donation_data;

const changeHandler = path => value =>
updateWizardSettings( {
Expand All @@ -81,6 +84,9 @@ export const DonationAmounts = () => {
...FREQUENCIES[ slug ],
} ) );

// Minimum donation is returned by the REST API as a string.
const minimumDonationFloat = parseFloat( minimumDonation );

return (
<>
<Card headerActions noBorder>
Expand Down Expand Up @@ -129,19 +135,46 @@ export const DonationAmounts = () => {
<MoneyInput
currencySymbol={ currencySymbol }
label={ __( 'Low-tier' ) }
error={
amounts[ section.key ][ 0 ] < minimumDonationFloat
? __(
'Warning: suggested donations should be at least the minimum donation amount.',
'newspack'
)
: null
}
value={ amounts[ section.key ][ 0 ] }
min={ minimumDonationFloat }
onChange={ changeHandler( [ 'amounts', section.key, 0 ] ) }
/>
<MoneyInput
currencySymbol={ currencySymbol }
label={ __( 'Mid-tier' ) }
error={
amounts[ section.key ][ 1 ] < minimumDonationFloat
? __(
'Warning: suggested donations should be at least the minimum donation amount.',
'newspack'
)
: null
}
value={ amounts[ section.key ][ 1 ] }
min={ minimumDonationFloat }
onChange={ changeHandler( [ 'amounts', section.key, 1 ] ) }
/>
<MoneyInput
currencySymbol={ currencySymbol }
label={ __( 'High-tier' ) }
error={
amounts[ section.key ][ 2 ] < minimumDonationFloat
? __(
'Warning: suggested donations should be at least the minimum donation amount.',
'newspack'
)
: null
}
value={ amounts[ section.key ][ 2 ] }
min={ minimumDonationFloat }
onChange={ changeHandler( [ 'amounts', section.key, 2 ] ) }
/>
</Grid>
Expand All @@ -159,13 +192,39 @@ export const DonationAmounts = () => {
currencySymbol={ currencySymbol }
label={ section.staticLabel }
value={ amounts[ section.key ][ 3 ] }
min={ minimumDonationFloat }
error={
amounts[ section.key ][ 3 ] < minimumDonationFloat
? __(
'Warning: suggested donations should be at least the minimum donation amount.',
'newspack'
)
: null
}
adekbadek marked this conversation as resolved.
Show resolved Hide resolved
onChange={ changeHandler( [ 'amounts', section.key, 3 ] ) }
key={ section.key }
/>
) ) }
</Grid>
</Card>
) }
<Card headerActions noBorder>
<SectionHeader
title={ __( 'Minimum Donation', 'newspack' ) }
description={ __(
'Set minimum donation amount. Setting a reasonable minimum donation amount can help protect your site from bot attacks.',
'newspack'
) }
noMargin
/>
<TextControl
label={ __( 'Minimum donation', 'newspack' ) }
type="number"
min={ 1 }
value={ minimumDonationFloat }
onChange={ value => changeHandler( [ 'minimumDonation' ] )( value ) }
/>
</Card>
</>
);
};
Expand Down
10 changes: 8 additions & 2 deletions includes/class-donations.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ private static function get_donation_default_settings() {
'year' => false,
],
'platform' => self::get_platform_slug(),
'minimumDonation' => 5.0,
];
}

Expand Down Expand Up @@ -344,6 +345,11 @@ public static function get_donation_settings() {
$parsed_settings['amounts'][ $frequency ] = array_map( 'floatval', $amounts );
}

// Ensure a minimum donation amount is set.
if ( ! isset( $saved_settings['minimumDonation'] ) && self::is_platform_wc() ) {
self::update_donation_product( [ 'minimumDonation' => $settings['minimumDonation'] ] );
}

return $parsed_settings;
}

Expand Down Expand Up @@ -376,7 +382,7 @@ public static function set_donation_settings( $args ) {
*
* @param array $args Info that will be used to create the products.
*/
private static function update_donation_product( $args = [] ) {
public static function update_donation_product( $args = [] ) {
$defaults = self::get_donation_default_settings();
$configuration = wp_parse_args( $args, $defaults );

Expand Down Expand Up @@ -426,7 +432,7 @@ private static function update_donation_product( $args = [] ) {
$child_product->set_name( $product_name );
$child_product->set_regular_price( $price );
$child_product->update_meta_data( '_suggested_price', $price );
$child_product->update_meta_data( '_min_price', wc_format_decimal( 1.0 ) );
$child_product->update_meta_data( '_min_price', wc_format_decimal( $configuration['minimumDonation'] ) );
$child_product->update_meta_data( '_hide_nyp_minimum', 'yes' );
$child_product->update_meta_data( '_nyp', 'yes' );
$child_product->set_virtual( true );
Expand Down
6 changes: 6 additions & 0 deletions includes/wizards/class-reader-revenue-wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ public function api_update( $request ) {
}
update_option( NEWSPACK_NRH_CONFIG, $nrh_config );
}

// Ensure that any Reader Revenue settings changed while the platform wasn't WC are persisted to WC products.
if ( Donations::is_platform_wc() ) {
Donations::update_donation_product( Donations::get_donation_settings() );
}

return \rest_ensure_response( $this->fetch_all_data() );
}

Expand Down
1 change: 1 addition & 0 deletions tests/unit-tests/donations.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function test_donations_settings() {
'tiered',
'disabledFrequencies',
'platform',
'minimumDonation',
'currencySymbol',
],
array_keys( $donation_settings ),
Expand Down