Skip to content

Commit 980ac04

Browse files
authored
fix(content-gates): resolve fatals and mismatched data types (#4338)
* fix(content-gates): resolve fatals and mismatched data types * fix: remove description field from update expected payload
1 parent 68bb544 commit 980ac04

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

includes/content-gate/class-content-restriction-control.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public static function get_post_gates( $post_id = null ) {
122122
}
123123

124124
foreach ( $content_rules as $content_rule ) {
125-
if ( $content_rule['slug'] === 'post_type' ) {
125+
if ( $content_rule['slug'] === 'post_types' ) {
126126
$post_type = get_post_type( $post_id );
127127
if ( ! in_array( $post_type, $content_rule['value'], true ) ) {
128128
continue 2;

includes/content-gate/content-gifting/class-content-gifting.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Content_Gifting {
4040
public static function init() {
4141
add_action( 'init', [ __CLASS__, 'hook_gift_button' ] );
4242
add_action( 'wp', [ __CLASS__, 'unrestrict_content' ], 5 );
43-
add_filter( 'newspack_content_gate_restrict_post', [ __CLASS__, 'restrict_post' ] );
43+
add_filter( 'newspack_content_gate_restrict_post', [ __CLASS__, 'restrict_post' ], 10, 2 );
4444
add_filter( 'newspack_content_gate_metering_short_circuit', [ __CLASS__, 'short_circuit_metering' ] );
4545
add_action( 'wp_enqueue_scripts', [ __CLASS__, 'enqueue_assets' ] );
4646
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'enqueue_assets' ] );

includes/wizards/audience/class-audience-content-gates.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,7 @@ public function register_api_endpoints() {
232232
*/
233233
public function sanitize_gate( $gate ) {
234234
return [
235-
'title' => sanitize_text_field( $gate['title'] ),
236-
'description' => sanitize_text_field( $gate['description'] ),
235+
'title' => isset( $gate['title'] ) ? sanitize_text_field( $gate['title'] ) : __( 'Untitled Content Gate', 'newspack-plugin' ),
237236
'metering' => $this->sanitize_metering( $gate['metering'] ),
238237
'access_rules' => $this->sanitize_rules( $gate['access_rules'] ),
239238
'content_rules' => $this->sanitize_rules( $gate['content_rules'], 'content' ),
@@ -249,6 +248,15 @@ public function sanitize_gate( $gate ) {
249248
* @return array The sanitized metering.
250249
*/
251250
public function sanitize_metering( $metering ) {
251+
$metering = wp_parse_args(
252+
$metering,
253+
[
254+
'enabled' => false,
255+
'anonymous_count' => 0,
256+
'registered_count' => 0,
257+
'period' => 'month',
258+
]
259+
);
252260
return [
253261
'enabled' => boolval( $metering['enabled'] ),
254262
'anonymous_count' => intval( $metering['anonymous_count'] ),
@@ -267,6 +275,9 @@ public function sanitize_metering( $metering ) {
267275
*/
268276
public function sanitize_rules( $rules, $type = 'access' ) {
269277
$sanitized_rules = [];
278+
if ( ! is_array( $rules ) ) {
279+
return $sanitized_rules;
280+
}
270281
foreach ( $rules as $rule ) {
271282
$sanitized = $type === 'access' ? $this->sanitize_access_rule( $rule ) : $this->sanitize_content_rule( $rule );
272283
if ( ! is_wp_error( $sanitized ) ) {
@@ -299,7 +310,16 @@ public function sanitize_access_rule( $access_rule ) {
299310
if ( ! is_array( $access_rule['value'] ) ) {
300311
return new \WP_Error( 'invalid_access_rule_value', __( 'Invalid access rule value.', 'newspack-plugin' ), [ 'status' => 400 ] );
301312
}
302-
$value = array_values( array_filter( array_map( 'sanitize_text_field', $access_rule['value'] ) ) );
313+
$value = array_values(
314+
array_filter(
315+
array_map(
316+
function( $value ) {
317+
return is_numeric( $value ) ? intval( $value ) : sanitize_text_field( $value );
318+
},
319+
$access_rule['value']
320+
)
321+
)
322+
);
303323
} else {
304324
$value = sanitize_text_field( $access_rule['value'] );
305325
}

src/wizards/audience/views/content-gates/access-rule-control.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/**
22
* WordPress dependencies.
33
*/
4-
import { CheckboxControl, SelectControl, TextControl } from '@wordpress/components';
4+
import { CheckboxControl, TextControl } from '@wordpress/components';
5+
6+
/**
7+
* Internal dependencies
8+
*/
9+
import { FormTokenField } from '../../../../../packages/components/src';
510

611
const noop = () => {};
712

@@ -16,12 +21,12 @@ export default function AccessRuleControl( { slug, value, onChange }: GateAccess
1621
}
1722
if ( rule.options && rule.options.length > 0 ) {
1823
return (
19-
<SelectControl
24+
<FormTokenField
2025
label={ rule.name }
21-
value={ value as string }
22-
onChange={ onChange }
23-
options={ rule.options.map( option => ( { value: option.value, label: option.label } ) ) }
24-
help={ rule.description }
26+
value={ rule.options.filter( o => value.includes( o.value ) ).map( o => o.label ) }
27+
onChange={ ( items: string[] ) => onChange( rule.options?.filter( o => items.includes( o.label ) ).map( o => o.value ) ?? [] ) }
28+
suggestions={ rule.options.map( o => o.label ) }
29+
__experimentalExpandOnFocus={ true }
2530
/>
2631
);
2732
}

0 commit comments

Comments
 (0)