> List of stored views. */ public function get_views( 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 ); if ( ! is_array( $decoded ) ) { return array(); } $views = array(); foreach ( $decoded as $item ) { if ( is_array( $item ) && isset( $item['id'] ) ) { $views[] = $item; } } return $views; } /** * Create or update a saved view. * * A view without an `id` (or with an unknown id) is created — a fresh * `view_` id is generated when absent. An existing id updates in * place. Marking a view default clears the flag on all others. New views * beyond {@see self::MAX_VIEWS} are rejected without a write. * * @since 2.6.5 * * @param int $user_id WordPress user id. * @param array $view Raw view payload from the client. * @return array{success: bool, view: array|null, message: string|null} */ public function save_view( int $user_id, array $view ): array { $raw_name = is_string( $view['name'] ?? null ) ? trim( $view['name'] ) : ''; if ( '' === $raw_name ) { return $this->failure( __( 'View name is required.', 'ai-product-tools' ) ); } $name_length = mb_strlen( $raw_name, 'UTF-8' ); if ( $name_length > self::MAX_NAME_LENGTH ) { return $this->failure( __( 'View name must be 80 characters or fewer.', 'ai-product-tools' ) ); } $name = sanitize_text_field( $raw_name ); $views = $this->get_views( $user_id ); $id = ( isset( $view['id'] ) && is_string( $view['id'] ) && '' !== trim( $view['id'] ) ) ? trim( $view['id'] ) : ''; $index = -1; if ( '' !== $id ) { foreach ( $views as $i => $existing ) { if ( isset( $existing['id'] ) && $existing['id'] === $id ) { $index = $i; break; } } } $is_new = ( -1 === $index ); if ( $is_new && ! $this->is_valid_view_id( $id ) ) { $id = 'view_' . uniqid( '', true ); } if ( $is_new && count( $views ) >= self::MAX_VIEWS ) { return $this->failure( __( 'You can save up to 25 views.', 'ai-product-tools' ) ); } $clean = array( 'id' => $id, 'name' => $name, 'prefs' => PrefsService::sanitize_prefs( is_array( $view['prefs'] ?? null ) ? $view['prefs'] : array() ), 'filters' => PrefsService::sanitize_filters( is_array( $view['filters'] ?? null ) ? $view['filters'] : array() ), 'sorting' => PrefsService::sanitize_sorting( is_array( $view['sorting'] ?? null ) ? $view['sorting'] : array() ), 'isDefault' => ! empty( $view['isDefault'] ), ); if ( $clean['isDefault'] ) { foreach ( $views as $i => $existing ) { $views[ $i ]['isDefault'] = false; } } if ( $is_new ) { $views[] = $clean; } else { $views[ $index ] = $clean; } if ( ! $this->persist_views( $user_id, $views ) ) { return $this->failure( __( 'Failed to save view.', 'ai-product-tools' ) ); } return array( 'success' => true, 'view' => $clean, 'message' => null, ); } /** * Delete a saved view by id. * * @since 2.6.5 * * @param int $user_id WordPress user id. * @param string $view_id View id to remove. * @return bool True when a view was removed, false when the id was absent. */ public function delete_view( int $user_id, string $view_id ): bool { $views = $this->get_views( $user_id ); $remaining = array(); $found = false; foreach ( $views as $view ) { if ( isset( $view['id'] ) && $view['id'] === $view_id ) { $found = true; continue; } $remaining[] = $view; } if ( ! $found ) { return false; } return $this->persist_views( $user_id, $remaining ); } /** * Mark a single view as default, clearing the flag on all others. * * @since 2.6.5 * * @param int $user_id WordPress user id. * @param string $view_id View id to promote. * @return bool True when the id was found and persisted, false otherwise. */ public function set_default( int $user_id, string $view_id ): bool { $views = $this->get_views( $user_id ); $found = false; $changed = false; foreach ( $views as $i => $view ) { $should_be_default = ( isset( $view['id'] ) && $view['id'] === $view_id ); if ( $should_be_default ) { $found = true; } if ( ! empty( $view['isDefault'] ) !== $should_be_default ) { $changed = true; } $views[ $i ]['isDefault'] = $should_be_default; } if ( ! $found ) { return false; } if ( ! $changed ) { return true; } return $this->persist_views( $user_id, $views ); } /** * Whether a client-supplied id is a safe `view_`-prefixed token that may be * stored verbatim. Rejects empty or injection-style ids (spaces, slashes, * angle brackets); anything else is regenerated server-side by save_view(). * * @since 2.6.5 * * @param string $id Candidate view id. * @return bool */ private function is_valid_view_id( string $id ): bool { return 1 === preg_match( '/^view_[A-Za-z0-9._-]{1,64}$/', $id ); } /** * JSON-encode and write the (reindexed) views list to user_meta. * * @since 2.6.5 * * @param int $user_id WordPress user id. * @param array> $views Views to persist. * @return bool True on write, false when encoding fails. */ private function persist_views( int $user_id, array $views ): bool { $encoded = wp_json_encode( array_values( $views ) ); if ( ! is_string( $encoded ) ) { return false; } if ( false !== update_user_meta( $user_id, self::META_KEY, wp_slash( $encoded ) ) ) { return true; } return (string) get_user_meta( $user_id, self::META_KEY, true ) === $encoded; } /** * Build a uniform failure envelope. * * @since 2.6.5 * * @param string $message Human-readable reason. * @return array{success: bool, view: null, message: string} */ private function failure( string $message ): array { return array( 'success' => false, 'view' => null, 'message' => $message, ); } }