…, 'rule' => … )` * with the rule in the rule format. * * Reading is Free code, so shortcodes keep working when a Pro license lapses * (with Pro conditions skipped). Creating and editing saved rules is Pro. */ class SavedRules { const OPTION = 'yangra_conditional_saved_rules'; /** * Rule sanitizer. * * @var RuleSanitizer */ private $sanitizer; /** * Constructor. * * @param RuleSanitizer $sanitizer Rule sanitizer. */ public function __construct( RuleSanitizer $sanitizer ) { $this->sanitizer = $sanitizer; } /** * A saved rule, sanitized, or null when there's no saved rule with this ID. * * @param string $id Saved rule ID. * @return array{version: int, enabled: bool, action: string, groups: array}>}>}|null */ public function get( string $id ): ?array { $saved = get_option( self::OPTION, array() ); if ( '' === $id || ! is_array( $saved ) || ! isset( $saved[ $id ] ) || ! is_array( $saved[ $id ] ) || ! isset( $saved[ $id ]['rule'] ) ) { return null; } return $this->sanitizer->sanitize( $saved[ $id ]['rule'] ); } /** * Every saved rule with a valid ID, sanitized, sorted by name. * * @return array}>}>}}> */ public function all(): array { $saved = get_option( self::OPTION, array() ); if ( ! is_array( $saved ) ) { return array(); } $rules = array(); foreach ( $saved as $id => $item ) { if ( ! is_string( $id ) || 1 !== preg_match( '/^[a-z0-9]{12}$/', $id ) || ! is_array( $item ) || ! isset( $item['rule'] ) ) { continue; } $rules[] = array( 'id' => $id, 'name' => isset( $item['name'] ) && is_string( $item['name'] ) ? $item['name'] : '', 'rule' => $this->sanitizer->sanitize( $item['rule'] ), ); } usort( $rules, static function ( array $a, array $b ): int { return array( strtolower( $a['name'] ), $a['id'] ) <=> array( strtolower( $b['name'] ), $b['id'] ); } ); return $rules; } }