Skip to content

Commit c1bb46d

Browse files
authored
Merge pull request #2436 from Automattic/master
Alpha release May 03
2 parents cf56985 + 3aa6de7 commit c1bb46d

File tree

7 files changed

+372
-286
lines changed

7 files changed

+372
-286
lines changed

assets/wizards/engagement/components/prompt.tsx

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,9 @@ import {
4141
WebPreview,
4242
} from '../../../components/src';
4343

44-
const previewIcon = (
45-
<SVG xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24">
46-
<Path
47-
fillRule="evenodd"
48-
clipRule="evenodd"
49-
d="M4.5001 13C5.17092 13.3354 5.17078 13.3357 5.17066 13.3359L5.17346 13.3305C5.1767 13.3242 5.18233 13.3135 5.19036 13.2985C5.20643 13.2686 5.23209 13.2218 5.26744 13.1608C5.33819 13.0385 5.44741 12.8592 5.59589 12.6419C5.89361 12.2062 6.34485 11.624 6.95484 11.0431C8.17357 9.88241 9.99767 8.75 12.5001 8.75C15.0025 8.75 16.8266 9.88241 18.0454 11.0431C18.6554 11.624 19.1066 12.2062 19.4043 12.6419C19.5528 12.8592 19.662 13.0385 19.7328 13.1608C19.7681 13.2218 19.7938 13.2686 19.8098 13.2985C19.8179 13.3135 19.8235 13.3242 19.8267 13.3305L19.8295 13.3359C19.8294 13.3357 19.8293 13.3354 20.5001 13C21.1709 12.6646 21.1708 12.6643 21.1706 12.664L21.1702 12.6632L21.1693 12.6614L21.1667 12.6563L21.1588 12.6408C21.1522 12.6282 21.1431 12.6108 21.1315 12.5892C21.1083 12.5459 21.0749 12.4852 21.0311 12.4096C20.9437 12.2584 20.8146 12.0471 20.6428 11.7956C20.2999 11.2938 19.7823 10.626 19.0798 9.9569C17.6736 8.61759 15.4977 7.25 12.5001 7.25C9.50252 7.25 7.32663 8.61759 5.92036 9.9569C5.21785 10.626 4.70033 11.2938 4.35743 11.7956C4.1856 12.0471 4.05654 12.2584 3.96909 12.4096C3.92533 12.4852 3.89191 12.5459 3.86867 12.5892C3.85705 12.6108 3.84797 12.6282 3.84141 12.6408L3.83346 12.6563L3.8309 12.6614L3.82997 12.6632L3.82959 12.664C3.82943 12.6643 3.82928 12.6646 4.5001 13ZM12.5001 16C14.4331 16 16.0001 14.433 16.0001 12.5C16.0001 10.567 14.4331 9 12.5001 9C10.5671 9 9.0001 10.567 9.0001 12.5C9.0001 14.433 10.5671 16 12.5001 16Z"
50-
fill="#3366FF"
51-
/>
52-
</SVG>
53-
);
54-
5544
// Note: Schema and types for the `prompt` prop is defined in Newspack Campaigns: https://github.com/Automattic/newspack-popups/blob/master/includes/schemas/class-prompts.php
5645
export default function Prompt( { inFlight, prompt, setInFlight, setPrompts }: PromptProps ) {
5746
const [ values, setValues ] = useState< InputValues | Record< string, never > >( {} );
58-
const [ isDirty, setIsDirty ] = useState( false );
5947
const [ error, setError ] = useState< false | { message: string } >( false );
6048
const [ success, setSuccess ] = useState< false | string >( false );
6149
const [ image, setImage ] = useState< null | Attachment >( null );
@@ -64,14 +52,9 @@ export default function Prompt( { inFlight, prompt, setInFlight, setPrompts }: P
6452
useEffect( () => {
6553
if ( Array.isArray( prompt?.user_input_fields ) ) {
6654
const fields = { ...values };
67-
let _isDirty = true;
6855
prompt.user_input_fields.forEach( ( field: InputField ) => {
6956
fields[ field.name ] = field.value || field.default;
70-
if ( field.value ) {
71-
_isDirty = false; // Allow saving if all values are default.
72-
}
7357
} );
74-
setIsDirty( _isDirty );
7558
setValues( fields );
7659
}
7760

@@ -97,10 +80,21 @@ export default function Prompt( { inFlight, prompt, setInFlight, setPrompts }: P
9780
setTimeout( () => setSuccess( false ), 5000 );
9881
}, [ success ] );
9982

83+
const previewIcon = (
84+
<SVG xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24">
85+
<Path
86+
fillRule="evenodd"
87+
clipRule="evenodd"
88+
d="M4.5001 13C5.17092 13.3354 5.17078 13.3357 5.17066 13.3359L5.17346 13.3305C5.1767 13.3242 5.18233 13.3135 5.19036 13.2985C5.20643 13.2686 5.23209 13.2218 5.26744 13.1608C5.33819 13.0385 5.44741 12.8592 5.59589 12.6419C5.89361 12.2062 6.34485 11.624 6.95484 11.0431C8.17357 9.88241 9.99767 8.75 12.5001 8.75C15.0025 8.75 16.8266 9.88241 18.0454 11.0431C18.6554 11.624 19.1066 12.2062 19.4043 12.6419C19.5528 12.8592 19.662 13.0385 19.7328 13.1608C19.7681 13.2218 19.7938 13.2686 19.8098 13.2985C19.8179 13.3135 19.8235 13.3242 19.8267 13.3305L19.8295 13.3359C19.8294 13.3357 19.8293 13.3354 20.5001 13C21.1709 12.6646 21.1708 12.6643 21.1706 12.664L21.1702 12.6632L21.1693 12.6614L21.1667 12.6563L21.1588 12.6408C21.1522 12.6282 21.1431 12.6108 21.1315 12.5892C21.1083 12.5459 21.0749 12.4852 21.0311 12.4096C20.9437 12.2584 20.8146 12.0471 20.6428 11.7956C20.2999 11.2938 19.7823 10.626 19.0798 9.9569C17.6736 8.61759 15.4977 7.25 12.5001 7.25C9.50252 7.25 7.32663 8.61759 5.92036 9.9569C5.21785 10.626 4.70033 11.2938 4.35743 11.7956C4.1856 12.0471 4.05654 12.2584 3.96909 12.4096C3.92533 12.4852 3.89191 12.5459 3.86867 12.5892C3.85705 12.6108 3.84797 12.6282 3.84141 12.6408L3.83346 12.6563L3.8309 12.6614L3.82997 12.6632L3.82959 12.664C3.82943 12.6643 3.82928 12.6646 4.5001 13ZM12.5001 16C14.4331 16 16.0001 14.433 16.0001 12.5C16.0001 10.567 14.4331 9 12.5001 9C10.5671 9 9.0001 10.567 9.0001 12.5C9.0001 14.433 10.5671 16 12.5001 16Z"
89+
fill={ inFlight ? '#828282' : '#3366FF' }
90+
/>
91+
</SVG>
92+
);
93+
10094
const getPreviewUrl = ( { options, slug }: { options: PromptOptions; slug: string } ) => {
10195
const { placement, trigger_type: triggerType } = options;
10296
const previewQueryKeys = window.newspack_engagement_wizard.preview_query_keys;
103-
const abbreviatedKeys = { preset: slug };
97+
const abbreviatedKeys = { preset: slug, values };
10498
Object.keys( options ).forEach( ( key: string ) => {
10599
if ( previewQueryKeys.hasOwnProperty( key ) ) {
106100
abbreviatedKeys[ previewQueryKeys[ key ] ] = options[ key ];
@@ -137,7 +131,6 @@ export default function Prompt( { inFlight, prompt, setInFlight, setPrompts }: P
137131
.then( ( fetchedPrompts: Array< PromptType > ) => {
138132
setPrompts( fetchedPrompts );
139133
setSuccess( __( 'Prompt saved.', 'newspack' ) );
140-
setIsDirty( false );
141134
res();
142135
} )
143136
.catch( err => {
@@ -194,11 +187,9 @@ export default function Prompt( { inFlight, prompt, setInFlight, setPrompts }: P
194187
toUpdate[ field.name ].indexOf( option.id ),
195188
1
196189
);
197-
setIsDirty( true );
198190
}
199191
if ( value && toUpdate[ field.name ].indexOf( option.id ) === -1 ) {
200192
toUpdate[ field.name ].push( option.id );
201-
setIsDirty( true );
202193
}
203194
setValues( toUpdate );
204195
} }
@@ -221,7 +212,6 @@ export default function Prompt( { inFlight, prompt, setInFlight, setPrompts }: P
221212
const toUpdate = { ...values };
222213
toUpdate[ field.name ] = value;
223214
if ( JSON.stringify( toUpdate ) !== JSON.stringify( values ) ) {
224-
setIsDirty( true );
225215
}
226216
setValues( toUpdate );
227217
} }
@@ -243,7 +233,6 @@ export default function Prompt( { inFlight, prompt, setInFlight, setPrompts }: P
243233
const toUpdate = { ...values };
244234
toUpdate[ field.name ] = value;
245235
if ( JSON.stringify( toUpdate ) !== JSON.stringify( values ) ) {
246-
setIsDirty( true );
247236
}
248237
setValues( toUpdate );
249238
} }
@@ -264,7 +253,6 @@ export default function Prompt( { inFlight, prompt, setInFlight, setPrompts }: P
264253
const toUpdate = { ...values };
265254
toUpdate[ field.name ] = attachment?.id || 0;
266255
if ( toUpdate[ field.name ] !== values[ field.name ] ) {
267-
setIsDirty( true );
268256
}
269257
setValues( toUpdate );
270258
if ( attachment?.url ) {
@@ -309,13 +297,7 @@ export default function Prompt( { inFlight, prompt, setInFlight, setPrompts }: P
309297
disabled={ inFlight }
310298
icon={ previewIcon }
311299
isSecondary
312-
onClick={ async () => {
313-
if ( isDirty ) {
314-
setIsSavingFromPreview( true );
315-
await savePrompt( prompt.slug, values );
316-
}
317-
showPreview();
318-
} }
300+
onClick={ async () => showPreview() }
319301
>
320302
{ __( 'Preview prompt', 'newspack' ) }
321303
</Button>

assets/wizards/readerRevenue/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const ReaderRevenueWizard = () => {
6969
isHidden: usedPlatform !== NEWSPACK,
7070
},
7171
{
72-
label: __( 'NRH Settings', 'newspack' ),
72+
label: __( 'News Revenue Hub Settings', 'newspack' ),
7373
path: '/settings',
7474
render: Views.NRHSettings,
7575
isHidden: usedPlatform !== NRH,

assets/wizards/readerRevenue/views/nrh-settings/index.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
22
* WordPress dependencies
33
*/
4-
import { useDispatch } from '@wordpress/data';
54
import { __ } from '@wordpress/i18n';
5+
import { useDispatch } from '@wordpress/data';
66

77
/**
88
* Internal dependencies
99
*/
10-
import { Grid, TextControl, Wizard, Button } from '../../../../components/src';
10+
import { ActionCard, Button, Grid, TextControl, Wizard } from '../../../../components/src';
1111
import { READER_REVENUE_WIZARD_SLUG } from '../../constants';
1212

1313
const NRHSettings = () => {
@@ -27,25 +27,39 @@ const NRHSettings = () => {
2727
} );
2828

2929
return (
30-
<>
31-
<Grid>
30+
<ActionCard
31+
hasGreyHeader
32+
isMedium
33+
title={ __( 'News Revenue Hub Settings', 'newspack' ) }
34+
description={ __( 'Configure your site’s connection to News Revenue Hub.', 'newspack' ) }
35+
actionContent={
36+
<Button isPrimary onClick={ onSave }>
37+
{ __( 'Save Settings' ) }
38+
</Button>
39+
}
40+
>
41+
<Grid columns={ 3 }>
3242
<TextControl
33-
label={ __( 'NRH Organization ID (required)', 'newspack' ) }
43+
label={ __( 'Organization ID', 'newspack' ) }
44+
placeholder="exampleid"
3445
value={ wizardData.platform_data?.nrh_organization_id || '' }
3546
onChange={ changeHandler( 'nrh_organization_id' ) }
3647
/>
3748
<TextControl
38-
label={ __( 'NRH Salesforce Campaign ID', 'newspack' ) }
49+
label={ __( 'Custom domain (optional)', 'newspack' ) }
50+
help={ __( 'Enter the raw domain without protocol or slashes.' ) }
51+
placeholder="donate.example.com"
52+
value={ wizardData.platform_data?.nrh_custom_domain || '' }
53+
onChange={ changeHandler( 'nrh_custom_domain' ) }
54+
/>
55+
<TextControl
56+
label={ __( 'Salesforce Campaign ID (optional)', 'newspack' ) }
57+
placeholder="exampleid"
3958
value={ wizardData.platform_data?.nrh_salesforce_campaign_id || '' }
4059
onChange={ changeHandler( 'nrh_salesforce_campaign_id' ) }
4160
/>
4261
</Grid>
43-
<div className="newspack-buttons-card">
44-
<Button isPrimary onClick={ onSave }>
45-
{ __( 'Update' ) }
46-
</Button>
47-
</div>
48-
</>
62+
</ActionCard>
4963
);
5064
};
5165

includes/class-nrh.php

Lines changed: 99 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,90 @@
1010
defined( 'ABSPATH' ) || exit;
1111

1212
/**
13-
* Handles Salesforce functionality.
13+
* Handles News Revenue Hub functionality.
1414
*/
1515
class NRH {
16+
/**
17+
* Allowed config keys for NRH settings.
18+
*
19+
* @var array
20+
*/
21+
protected static $allowed_keys = [
22+
'nrh_organization_id',
23+
'nrh_custom_domain',
24+
'nrh_salesforce_campaign_id',
25+
];
26+
1627
/**
1728
* Add hooks.
1829
*/
1930
public static function init() {
20-
add_filter( 'newspack_donation_checkout_url', [ __CLASS__, 'redirect_to_nrh_checkout' ], 10, 3 );
21-
add_filter( 'newspack_blocks_donate_block_html', [ __CLASS__, 'handle_custom_campaign_id' ], 10, 2 );
22-
add_filter( 'allowed_redirect_hosts', [ __CLASS__, 'allow_redirect_to_nrh' ] );
31+
\add_filter( 'newspack_donation_checkout_url', [ __CLASS__, 'redirect_to_nrh_checkout' ], 10, 3 );
32+
\add_filter( 'newspack_blocks_donate_block_html', [ __CLASS__, 'handle_custom_campaign_id' ], 10, 2 );
33+
\add_filter( 'allowed_redirect_hosts', [ __CLASS__, 'allow_redirect_to_nrh' ] );
34+
}
35+
36+
/**
37+
* Get all News Revenue Hub settings.
38+
*
39+
* @return array Array of settings.
40+
*/
41+
private static function get_settings() {
42+
return \get_option( NEWSPACK_NRH_CONFIG, [] );
43+
}
44+
45+
/**
46+
* Get a specific News Revenue Hub setting by key name.
47+
* Validates given key against valid keys.
48+
*
49+
* @param string $key Key of setting to get.
50+
*
51+
* @return string|boolean Value of setting if found, false otherwise.
52+
*/
53+
private static function get_setting( $key ) {
54+
if ( ! in_array( $key, self::$allowed_keys, true ) ) {
55+
return false;
56+
}
57+
58+
$settings = self::get_settings();
59+
return isset( $settings[ $key ] ) ? \wp_strip_all_tags( $settings[ $key ] ) : false;
60+
}
61+
62+
/**
63+
* Update News Revenue Hub settings.
64+
* Validates given data against valid keys.
65+
*
66+
* @param array $data Array of settings to update.
67+
*
68+
* @return boolean True if settings were updated, false otherwise.
69+
*/
70+
public static function update_settings( $data ) {
71+
$settings = self::get_settings();
72+
73+
foreach ( $data as $key => $value ) {
74+
if ( in_array( $key, self::$allowed_keys, true ) ) {
75+
$settings[ $key ] = $value;
76+
}
77+
}
78+
79+
return \update_option( NEWSPACK_NRH_CONFIG, $settings );
80+
}
81+
82+
/**
83+
* Strips protocol from a domain, if it contains one.
84+
*
85+
* @param string|boolean $domain Domain to strip protocol from. Will accept falsy values as well.
86+
*
87+
* @return string|boolean Domain without protocol or false if the given domain is empty.
88+
*/
89+
public static function strip_protocol( $domain ) {
90+
if ( empty( $domain ) ) {
91+
return false;
92+
}
93+
94+
$domain_parts = explode( '//', $domain );
95+
96+
return \wp_unslash( end( $domain_parts ) );
2397
}
2498

2599
/**
@@ -38,16 +112,14 @@ public static function redirect_to_nrh_checkout( $checkout_url, $donation_value,
38112
'once' => 'once',
39113
];
40114

41-
$nrh_config = get_option( NEWSPACK_NRH_CONFIG );
115+
$nrh_config = self::get_settings();
42116

43-
$organization_id = wp_strip_all_tags( $nrh_config['nrh_organization_id'] );
44-
$salesforce_id = false;
45-
if ( isset( $nrh_config['nrh_salesforce_campaign_id'] ) ) {
46-
$salesforce_id = wp_strip_all_tags( $nrh_config['nrh_salesforce_campaign_id'] );
47-
}
117+
$organization_id = self::get_setting( 'nrh_organization_id' );
118+
$custom_domain = self::strip_protocol( self::get_setting( 'nrh_custom_domain' ) );
119+
$salesforce_id = self::get_setting( 'nrh_salesforce_campaign_id' );
48120
$custom_campaign = filter_input( INPUT_GET, 'campaign', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
49121
if ( $custom_campaign ) {
50-
$salesforce_id = wp_strip_all_tags( $custom_campaign );
122+
$salesforce_id = \wp_strip_all_tags( $custom_campaign );
51123
}
52124
$donation_value = floatval( $donation_value );
53125
$donation_frequency = isset( $donation_frequencies[ $donation_frequency ] ) ? $donation_frequencies[ $donation_frequency ] : false;
@@ -60,7 +132,8 @@ public static function redirect_to_nrh_checkout( $checkout_url, $donation_value,
60132
Donations::remove_donations_from_cart();
61133

62134
$url = sprintf(
63-
'https://checkout.fundjournalism.org/memberform?amount=%.1f&installmentPeriod=%s&org_id=%s',
135+
'https://%s/?amount=%.1f&frequency=%s&org_id=%s',
136+
! empty( $custom_domain ) ? $custom_domain : $organization_id . '.fundjournalism.org',
64137
$donation_value,
65138
$donation_frequency,
66139
$organization_id
@@ -97,13 +170,25 @@ public static function handle_custom_campaign_id( $html, $attributes ) {
97170
}
98171

99172
/**
100-
* Add the NRH checkout URL as an allowed redirect target.
173+
* Add the potential NRH checkout URLs as allowed redirect targets.
101174
*
102175
* @param array $hosts Array of allowed hosts.
103176
* @return array Modified $hosts.
104177
*/
105178
public static function allow_redirect_to_nrh( $hosts ) {
106-
$hosts[] = 'checkout.fundjournalism.org';
179+
$organization_id = self::get_setting( 'nrh_organization_id' );
180+
$custom_domain = self::strip_protocol( self::get_setting( 'nrh_custom_domain' ) );
181+
$allowed_urls = [
182+
'checkout.fundjournalism.org',
183+
$organization_id . '.fundjournalism.org',
184+
];
185+
186+
if ( ! empty( $custom_domain ) ) {
187+
$allowed_urls[] = $custom_domain;
188+
}
189+
190+
$hosts = array_merge( $hosts, $allowed_urls );
191+
107192
return $hosts;
108193
}
109194

includes/wizards/class-reader-revenue-wizard.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public function register_api_endpoints() {
9797
'sanitize_callback' => 'Newspack\newspack_clean',
9898
'validate_callback' => [ $this, 'api_validate_not_empty' ],
9999
],
100+
'nrh_custom_domain' => [
101+
'sanitize_callback' => 'Newspack\newspack_clean',
102+
],
100103
'nrh_salesforce_campaign_id' => [
101104
'sanitize_callback' => 'Newspack\newspack_clean',
102105
],
@@ -274,14 +277,10 @@ public function api_update( $request ) {
274277
$params = $request->get_params();
275278
$platform = $params['platform'];
276279
Donations::set_platform_slug( $platform );
277-
if ( 'nrh' === $platform && isset( $params['nrh_organization_id'] ) ) {
278-
$nrh_config = [
279-
'nrh_organization_id' => $params['nrh_organization_id'],
280-
];
281-
if ( isset( $params['nrh_salesforce_campaign_id'] ) ) {
282-
$nrh_config['nrh_salesforce_campaign_id'] = $params['nrh_salesforce_campaign_id'];
283-
}
284-
update_option( NEWSPACK_NRH_CONFIG, $nrh_config );
280+
281+
// Update NRH settings.
282+
if ( 'nrh' === $platform ) {
283+
NRH::update_settings( $params );
285284
}
286285

287286
// Ensure that any Reader Revenue settings changed while the platform wasn't WC are persisted to WC products.

0 commit comments

Comments
 (0)