@@ -369,10 +369,13 @@ public function register_api_endpoints() {
369369 'access_rules ' => [
370370 'type ' => 'array ' ,
371371 'items ' => [
372- 'type ' => 'object ' ,
373- 'properties ' => [
374- 'slug ' => [ 'type ' => 'string ' ],
375- 'value ' => [ 'type ' => 'mixed ' ],
372+ 'type ' => 'array ' ,
373+ 'items ' => [
374+ 'type ' => 'object ' ,
375+ 'properties ' => [
376+ 'slug ' => [ 'type ' => 'string ' ],
377+ 'value ' => [ 'type ' => 'mixed ' ],
378+ ],
376379 ],
377380 ],
378381 ],
@@ -472,22 +475,81 @@ public function sanitize_metering( $metering ) {
472475 * @param array $rules The rules.
473476 * @param string $type The type of rules to sanitize.
474477 *
475- * @return array The sanitized access rules.
478+ * @return array The sanitized rules.
476479 */
477480 public function sanitize_rules ( $ rules , $ type = 'access ' ) {
478- $ sanitized_rules = [];
479481 if ( ! is_array ( $ rules ) ) {
480- return $ sanitized_rules ;
482+ return [];
483+ }
484+
485+ // For access rules, handle grouped format.
486+ if ( 'access ' === $ type ) {
487+ return $ this ->sanitize_access_rules_grouped ( $ rules );
481488 }
489+
490+ // For content rules, use flat format.
491+ $ sanitized_rules = [];
482492 foreach ( $ rules as $ rule ) {
483- $ sanitized = $ type === ' access ' ? $ this -> sanitize_access_rule ( $ rule ) : $ this ->sanitize_content_rule ( $ rule );
493+ $ sanitized = $ this ->sanitize_content_rule ( $ rule );
484494 if ( ! is_wp_error ( $ sanitized ) ) {
485495 $ sanitized_rules [] = $ sanitized ;
486496 }
487497 }
488498 return $ sanitized_rules ;
489499 }
490500
501+ /**
502+ * Sanitize access rules in grouped format.
503+ *
504+ * Accepts both flat format [ rule1, rule2 ] and grouped format [ [ rule1, rule2 ], [ rule3 ] ].
505+ * Always returns grouped format [ [ rule1, rule2 ], [ rule3 ] ].
506+ *
507+ * @param array $rules The access rules.
508+ *
509+ * @return array The sanitized access rules in grouped format.
510+ */
511+ public function sanitize_access_rules_grouped ( $ rules ) {
512+ if ( empty ( $ rules ) ) {
513+ return [];
514+ }
515+
516+ // Normalize rules (flat or grouped) to a consistent grouped format.
517+ $ rules = Access_Rules::normalize_rules ( $ rules );
518+
519+ // Sanitize each group.
520+ $ sanitized_groups = [];
521+ foreach ( $ rules as $ group ) {
522+ $ sanitized_group = $ this ->sanitize_access_rules_group ( $ group );
523+ if ( ! empty ( $ sanitized_group ) ) {
524+ $ sanitized_groups [] = $ sanitized_group ;
525+ }
526+ }
527+
528+ return $ sanitized_groups ;
529+ }
530+
531+ /**
532+ * Sanitize a single group of access rules.
533+ *
534+ * @param array $group The group of access rules.
535+ *
536+ * @return array The sanitized group.
537+ */
538+ public function sanitize_access_rules_group ( $ group ) {
539+ if ( ! is_array ( $ group ) ) {
540+ return [];
541+ }
542+
543+ $ sanitized_group = [];
544+ foreach ( $ group as $ rule ) {
545+ $ sanitized = $ this ->sanitize_access_rule ( $ rule );
546+ if ( ! is_wp_error ( $ sanitized ) ) {
547+ $ sanitized_group [] = $ sanitized ;
548+ }
549+ }
550+ return $ sanitized_group ;
551+ }
552+
491553 /**
492554 * Sanitize access rule.
493555 *
0 commit comments