registry = $registry; } /** * The summary of a sanitized rule. * * @param array $rule Rule, as RuleSanitizer returns it. * @return string */ public function summarize( array $rule ): string { $groups = array(); foreach ( (array) ( $rule['groups'] ?? array() ) as $group ) { $conditions = array(); foreach ( (array) ( $group['conditions'] ?? array() ) as $condition ) { $conditions[] = $this->condition( array( 'type' => (string) ( $condition['type'] ?? '' ), 'operator' => (string) ( $condition['operator'] ?? 'is' ), 'values' => array_map( 'strval', (array) ( $condition['values'] ?? array() ) ), ) ); } if ( array() !== $conditions ) { $groups[] = $conditions; } } $parts = array(); foreach ( $groups as $conditions ) { $text = implode( __( ' and ', 'yangra-conditional' ), $conditions ); /* translators: %s: conditions joined with "and". */ $parts[] = count( $groups ) > 1 && count( $conditions ) > 1 ? sprintf( __( '(%s)', 'yangra-conditional' ), $text ) : $text; } $conditions = implode( __( ' or ', 'yangra-conditional' ), $parts ); if ( 'hide' === ( $rule['action'] ?? '' ) ) { /* translators: %s: the rule's conditions, e.g. "User state is Logged in". */ return sprintf( __( 'Hide when: %s', 'yangra-conditional' ), $conditions ); } /* translators: %s: the rule's conditions, e.g. "User state is Logged in". */ return sprintf( __( 'Show when: %s', 'yangra-conditional' ), $conditions ); } /** * One condition, e.g. "User role is not Editor, Author". * * @param array{type: string, operator: string, values: array} $condition Condition. * @return string */ private function condition( array $condition ): string { $registered = $this->registry->get( $condition['type'] ); if ( null === $registered ) { $name = $this->registry->is_pro_type( $condition['type'] ) ? $this->registry->pro_type_label( $condition['type'] ) : $condition['type']; /* translators: %s: condition name, e.g. "Visitor location". */ return sprintf( __( '%s (skipped)', 'yangra-conditional' ), $name ); } $labels = $registered->options(); if ( $registered instanceof SearchableCondition ) { $labels += $registered->labels( $condition['values'] ); } $values = array(); foreach ( $condition['values'] as $value ) { /* translators: %s: a value that no longer exists, e.g. a deleted role's slug. */ $values[] = isset( $labels[ $value ] ) ? $labels[ $value ] : sprintf( __( '%s (missing)', 'yangra-conditional' ), $value ); } $values = implode( __( ', ', 'yangra-conditional' ), $values ); if ( 'is_not' === $condition['operator'] ) { /* translators: 1: condition name, 2: values. */ return sprintf( __( '%1$s is not %2$s', 'yangra-conditional' ), $registered->label(), $values ); } /* translators: 1: condition name, 2: values. */ return sprintf( __( '%1$s is %2$s', 'yangra-conditional' ), $registered->label(), $values ); } }