'. */ private const KEY_PATTERN = '/^(default|tags|exception:[1-9][0-9]*)$/'; /** * The current user's prefs for every generator that has any saved * (unknown/invalid generators in the stored blob, if any, are dropped). * * @param int $user_id WordPress user id. * @return array Generator => prefs; a generator with nothing saved is omitted. */ public function getAll( int $user_id ): array { $stored = $this->rawStored( $user_id ); $result = array(); foreach ( Generators::all() as $generator ) { if ( isset( $stored[ $generator ] ) && is_array( $stored[ $generator ] ) ) { $result[ $generator ] = $this->sanitizeGeneratorPrefs( $stored[ $generator ] ); } } return $result; } /** * @param int $user_id WordPress user id. * @param string $generator Generator key (already validated by the caller). * @return array{keep:bool,seen:array|null} {keep:false,seen:null} when nothing is saved yet. */ public function getForGenerator( int $user_id, string $generator ): array { $all = $this->getAll( $user_id ); return $all[ $generator ] ?? array( 'keep' => false, 'seen' => null ); } /** * Sanitize and persist one generator's prefs, merged into the user's * existing blob. * * @param int $user_id WordPress user id. * @param string $generator Generator key (already validated by the caller). * @param array $raw_prefs Raw {keep, seen} from the client. * @return array{ok:bool,prefs:array{keep:bool,seen:array|null}} `ok` false only when the sanitized blob would exceed the size cap (the write is skipped; `prefs` still reports what sanitize() computed, for the error response). */ public function save( int $user_id, string $generator, array $raw_prefs ): array { if ( ! Generators::isValid( $generator ) ) { return array( 'ok' => false, 'prefs' => array( 'keep' => false, 'seen' => null ) ); } $clean = $this->sanitizeGeneratorPrefs( $raw_prefs ); $stored = $this->rawStored( $user_id ); $stored[ $generator ] = $clean; $stored = array_intersect_key( $stored, array_flip( Generators::all() ) ); $encoded = wp_json_encode( $stored ); if ( ! is_string( $encoded ) || strlen( $encoded ) > self::MAX_BYTES ) { return array( 'ok' => false, 'prefs' => $clean ); } update_user_meta( $user_id, self::META_KEY, wp_slash( $encoded ) ); return array( 'ok' => true, 'prefs' => $clean ); } /** * @param int $user_id WordPress user id. * @return array Raw decoded blob, or empty array when missing/corrupted. */ private function rawStored( int $user_id ): array { $raw = get_user_meta( $user_id, self::META_KEY, true ); if ( ! is_string( $raw ) || '' === $raw ) { return array(); } $decoded = json_decode( $raw, true ); return is_array( $decoded ) ? $decoded : array(); } /** * @param array $raw Raw {keep, seen} for one generator. * @return array{keep:bool,seen:array{slots:array,order:string[]}|null} */ private function sanitizeGeneratorPrefs( array $raw ): array { $keep = ! empty( $raw['keep'] ); if ( ! $keep ) { return array( 'keep' => false, 'seen' => null ); } $seen = array_key_exists( 'seen', $raw ) ? $this->sanitizeSeen( $raw['seen'] ) : null; return array( 'keep' => true, 'seen' => $seen ); } /** * @param mixed $raw_seen Raw `seen` value. * @return array{slots:array,order:string[]}|null */ private function sanitizeSeen( $raw_seen ): ?array { if ( ! is_array( $raw_seen ) ) { return null; } $slots = array(); if ( isset( $raw_seen['slots'] ) && is_array( $raw_seen['slots'] ) ) { foreach ( $raw_seen['slots'] as $key => $version ) { if ( count( $slots ) >= self::MAX_SLOTS ) { break; } $key = (string) $key; if ( 1 !== preg_match( self::KEY_PATTERN, $key ) || ! is_array( $version ) ) { continue; } $blueprint_id = isset( $version['blueprintId'] ) && is_numeric( $version['blueprintId'] ) ? (int) $version['blueprintId'] : -1; $revision = isset( $version['revision'] ) && is_numeric( $version['revision'] ) ? (int) $version['revision'] : -1; if ( $blueprint_id < 0 || $revision < 0 ) { continue; } $slots[ $key ] = array( 'blueprintId' => $blueprint_id, 'revision' => $revision ); } } $order = array(); if ( isset( $raw_seen['order'] ) && is_array( $raw_seen['order'] ) ) { foreach ( $raw_seen['order'] as $key ) { if ( count( $order ) >= self::MAX_ORDER ) { break; } $key = is_string( $key ) ? $key : ''; if ( 1 !== preg_match( self::KEY_PATTERN, $key ) ) { continue; } $order[] = $key; } } return array( 'slots' => $slots, 'order' => $order ); } }