Skip to content

Commit 61341a8

Browse files
committed
fix: infinite loop
1 parent 87437d1 commit 61341a8

3 files changed

Lines changed: 135 additions & 123 deletions

File tree

includes/reader-activation/integrations/class-integration.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -357,36 +357,25 @@ public function get_enabled_outgoing_fields_keys( $prefixed = false ) {
357357
* @return array Array of settings field declarations.
358358
*/
359359
public function get_metadata_fields() {
360-
$incoming_fields = $this->get_available_incoming_contact_fields( true );
361-
$outgoing_fields = Sync\Metadata::get_default_fields();
362-
363360
return [
364361
[
365362
'key' => 'metadata_prefix',
366363
'type' => 'text',
367364
'label' => __( 'Metadata field prefix', 'newspack-plugin' ),
368-
'description' => __( 'A string to prefix metadata fields attached to each contact synced to the integration. Required to ensure that metadata field names are unique. Default: NP_', 'newspack-plugin' ),
365+
'description' => __( 'A string to prefix metadata fields synced to the integration. Required to ensure that metadata field names are unique. Default: NP_', 'newspack-plugin' ),
369366
'default' => 'NP_',
370367
],
371368
[
372369
'key' => 'outgoing_metadata_fields',
373370
'type' => 'metadata',
374371
'label' => __( 'Outgoing metadata fields', 'newspack-plugin' ),
375-
'options' => $outgoing_fields,
372+
'default' => [],
376373
],
377374
[
378375
'key' => 'incoming_metadata_fields',
379376
'type' => 'metadata',
380377
'label' => __( 'Incoming metadata fields', 'newspack-plugin' ),
381378
'default' => [],
382-
'options' => array_values(
383-
array_map(
384-
function( $field ) {
385-
return $field->get_key();
386-
},
387-
is_wp_error( $incoming_fields ) ? [] : $incoming_fields
388-
)
389-
),
390379
],
391380
];
392381
}
@@ -516,7 +505,20 @@ public function get_settings_config() {
516505
$config = [];
517506
foreach ( $fields as $field ) {
518507
$field['value'] = $this->get_settings_field_value( $field['key'] );
519-
$config[] = $field;
508+
// Inject metadata options for metadata fields.
509+
if ( 'incoming_metadata_fields' === $field['key'] ) {
510+
$incoming_fields = $this->get_available_incoming_contact_fields( false );
511+
$field['options'] = array_map(
512+
function( $incoming_field ) {
513+
return $incoming_field->get_key();
514+
},
515+
is_wp_error( $incoming_fields ) ? [] : $incoming_fields
516+
);
517+
}
518+
if ( 'outgoing_metadata_fields' === $field['key'] ) {
519+
$field['options'] = Sync\Metadata::get_default_fields();
520+
}
521+
$config[] = $field;
520522
}
521523
return $config;
522524
}
@@ -553,8 +555,7 @@ private function sanitize_settings_field_value( $field, $value ) {
553555
case 'select':
554556
$valid_values = array_column( $field['options'] ?? [], 'value' );
555557
return in_array( $value, $valid_values, true ) ? $value : ( $field['default'] ?? '' );
556-
case 'outgoing_metadata':
557-
case 'incoming_metadata':
558+
case 'metadata':
558559
if ( ! is_array( $value ) ) {
559560
return $field['default'] ?? [];
560561
}

src/wizards/audience/views/integrations/index.js

Lines changed: 5 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -4,121 +4,19 @@
44
import { __ } from '@wordpress/i18n';
55
import apiFetch from '@wordpress/api-fetch';
66
import { forwardRef, useState, useEffect, useCallback } from '@wordpress/element';
7-
import { CheckboxControl, ExternalLink } from '@wordpress/components';
87

98
/**
109
* Internal dependencies.
1110
*/
12-
import { ActionCard, Button, Card, Grid, SelectControl, TextControl, Wizard, withWizard } from '../../../../../packages/components/src';
11+
import { ActionCard, Button, Card, Grid, Wizard, withWizard } from '../../../../../packages/components/src';
1312
import WizardsTab from '../../../wizards-tab';
1413
import WizardSection from '../../../wizards-section';
1514

16-
const API_PATH = '/newspack/v1/wizard/newspack-audience-integrations/settings';
15+
import { SettingsField } from './settings-field';
1716

18-
/**
19-
* Render a single settings field.
20-
*
21-
* @param {Object} props Component props.
22-
* @param {Object} props.field Field declaration.
23-
* @param {*} props.value Current value.
24-
* @param {Function} props.onChange Change handler.
25-
*/
26-
function SettingsField( { field, value, onChange } ) {
27-
const { key, type, label, description, placeholder, options, help_url: helpUrl } = field;
28-
const help = (
29-
<>
30-
{ description }
31-
{ helpUrl && (
32-
<>
33-
{ ' ' }
34-
<ExternalLink href={ helpUrl }>{ __( 'Learn more', 'newspack-plugin' ) }</ExternalLink>
35-
</>
36-
) }
37-
</>
38-
);
39-
40-
switch ( type ) {
41-
case 'metadata': {
42-
const selectedFields = Array.isArray( value ) ? value : [];
43-
return (
44-
<div key={ key }>
45-
<h3>{ label }</h3>
46-
<Grid columns={ 3 } rowGap={ 16 }>
47-
{ options.map( fieldName => (
48-
<CheckboxControl
49-
className="newspack-checkbox-control"
50-
key={ fieldName }
51-
label={ fieldName.replace( ': ', '' ) }
52-
checked={ selectedFields.includes( fieldName ) }
53-
onChange={ checked => {
54-
const newFields = checked ? [ ...selectedFields, fieldName ] : selectedFields.filter( f => f !== fieldName );
55-
onChange( newFields );
56-
} }
57-
/>
58-
) ) }
59-
</Grid>
60-
</div>
61-
);
62-
}
63-
case 'checkbox':
64-
return <CheckboxControl key={ key } label={ label } help={ help } checked={ !! value } onChange={ onChange } />;
65-
case 'select':
66-
return (
67-
<SelectControl
68-
key={ key }
69-
label={ label }
70-
help={ help }
71-
value={ value }
72-
options={ ( options || [] ).map( opt => ( {
73-
label: opt.label,
74-
value: opt.value,
75-
} ) ) }
76-
onChange={ onChange }
77-
/>
78-
);
79-
case 'textarea':
80-
return (
81-
<TextControl
82-
key={ key }
83-
label={ label }
84-
help={ help }
85-
value={ value || '' }
86-
placeholder={ placeholder }
87-
onChange={ onChange }
88-
isTextarea
89-
/>
90-
);
91-
case 'number':
92-
return (
93-
<TextControl
94-
key={ key }
95-
label={ label }
96-
help={ help }
97-
value={ value ?? '' }
98-
placeholder={ placeholder }
99-
onChange={ onChange }
100-
type="number"
101-
/>
102-
);
103-
case 'password':
104-
return (
105-
<TextControl
106-
key={ key }
107-
label={ label }
108-
help={ help }
109-
value={ value || '' }
110-
placeholder={ placeholder }
111-
onChange={ onChange }
112-
type="password"
113-
/>
114-
);
115-
case 'text':
116-
default:
117-
return <TextControl key={ key } label={ label } help={ help } value={ value || '' } placeholder={ placeholder } onChange={ onChange } />;
118-
}
119-
}
17+
const API_PATH = '/newspack/v1/wizard/newspack-audience-integrations/settings';
12018

121-
function AudienceIntegrations( props, ref ) {
19+
const AudienceIntegrations = ( props, ref ) => {
12220
const [ integrations, setIntegrations ] = useState( {} );
12321
const [ pendingChanges, setPendingChanges ] = useState( {} );
12422
const [ saving, setSaving ] = useState( {} );
@@ -265,6 +163,6 @@ function AudienceIntegrations( props, ref ) {
265163
ref={ ref }
266164
/>
267165
);
268-
}
166+
};
269167

270168
export default withWizard( forwardRef( AudienceIntegrations ) );
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* WordPress dependencies.
3+
*/
4+
import { __ } from '@wordpress/i18n';
5+
import { CheckboxControl, ExternalLink } from '@wordpress/components';
6+
7+
/**
8+
* Internal dependencies.
9+
*/
10+
import { Grid, SelectControl, TextControl } from '../../../../../packages/components/src';
11+
12+
/**
13+
* Render a single settings field.
14+
*
15+
* @param {Object} props Component props.
16+
* @param {Object} props.field Field declaration.
17+
* @param {*} props.value Current value.
18+
* @param {Function} props.onChange Change handler.
19+
*/
20+
export const SettingsField = ( { field, value, onChange } ) => {
21+
const { key, type, label, description, placeholder, options, help_url: helpUrl } = field;
22+
const help = (
23+
<>
24+
{ description }
25+
{ helpUrl && (
26+
<>
27+
{ ' ' }
28+
<ExternalLink href={ helpUrl }>{ __( 'Learn more', 'newspack-plugin' ) }</ExternalLink>
29+
</>
30+
) }
31+
</>
32+
);
33+
34+
switch ( type ) {
35+
case 'metadata': {
36+
const selectedFields = Array.isArray( value ) ? value : [];
37+
return (
38+
<div key={ key }>
39+
<h3>{ label }</h3>
40+
<Grid columns={ 3 } rowGap={ 16 }>
41+
{ options.map( fieldName => (
42+
<CheckboxControl
43+
className="newspack-checkbox-control"
44+
key={ fieldName }
45+
label={ fieldName.replace( ': ', '' ) }
46+
checked={ selectedFields.includes( fieldName ) }
47+
onChange={ checked => {
48+
const newFields = checked ? [ ...selectedFields, fieldName ] : selectedFields.filter( f => f !== fieldName );
49+
onChange( newFields );
50+
} }
51+
/>
52+
) ) }
53+
</Grid>
54+
</div>
55+
);
56+
}
57+
case 'checkbox':
58+
return <CheckboxControl key={ key } label={ label } help={ help } checked={ !! value } onChange={ onChange } />;
59+
case 'select':
60+
return (
61+
<SelectControl
62+
key={ key }
63+
label={ label }
64+
help={ help }
65+
value={ value }
66+
options={ ( options || [] ).map( opt => ( {
67+
label: opt.label,
68+
value: opt.value,
69+
} ) ) }
70+
onChange={ onChange }
71+
/>
72+
);
73+
case 'textarea':
74+
return (
75+
<TextControl
76+
key={ key }
77+
label={ label }
78+
help={ help }
79+
value={ value || '' }
80+
placeholder={ placeholder }
81+
onChange={ onChange }
82+
isTextarea
83+
/>
84+
);
85+
case 'number':
86+
return (
87+
<TextControl
88+
key={ key }
89+
label={ label }
90+
help={ help }
91+
value={ value ?? '' }
92+
placeholder={ placeholder }
93+
onChange={ onChange }
94+
type="number"
95+
/>
96+
);
97+
case 'password':
98+
return (
99+
<TextControl
100+
key={ key }
101+
label={ label }
102+
help={ help }
103+
value={ value || '' }
104+
placeholder={ placeholder }
105+
onChange={ onChange }
106+
type="password"
107+
/>
108+
);
109+
case 'text':
110+
default:
111+
return <TextControl key={ key } label={ label } help={ help } value={ value || '' } placeholder={ placeholder } onChange={ onChange } />;
112+
}
113+
};

0 commit comments

Comments
 (0)