isset($record['namespace']) ? (string) $record['namespace'] : '', 'id' => $id > 0 ? $id : null, 'mode' => (isset($record['mode']) && 'entire_site' === $record['mode']) ? 'entire_site' : 'selected', 'priority' => isset($record['priority']) ? (int) $record['priority'] : 0, 'show' => self::normalize_rules(isset($record['show']) ? $record['show'] : []), 'hide' => self::normalize_rules(isset($record['hide']) ? $record['hide'] : []), ]; } public static function get(string $namespace): ?array { $record = get_option(self::option_name($namespace), null); if (!is_array($record) || !isset($record['namespace']) || (string) $record['namespace'] !== $namespace) { return null; } return self::normalize($record); } /** * Saves one agent's placement. A new agent goes last in the order, so it * wins over every agent placed before it; a saved agent keeps its place. * * @return array The record as stored. */ public static function save(array $record): array { $record = self::normalize($record); $index = self::read_index_from_db(); $existing = self::get($record['namespace']); if (null !== $existing && in_array($record['namespace'], $index, true)) { $record['priority'] = $existing['priority']; } else { $highest = 0; foreach ($index as $namespace) { $other = self::get($namespace); if (null !== $other && $other['priority'] > $highest) { $highest = $other['priority']; } } $record['priority'] = $highest + 1; $index = array_values(array_diff($index, [$record['namespace']])); $index[] = $record['namespace']; } self::write_record($record); self::write_index($index); return $record; } /** * Writes one record without touching the index. For the migration, which * writes every record first and the index after. */ public static function write_record(array $record): array { $record = self::normalize($record); update_option(self::option_name($record['namespace']), $record, false); return $record; } public static function delete(string $namespace): void { delete_option(self::option_name($namespace)); $index = self::read_index_from_db(); if (in_array($namespace, $index, true)) { self::write_index(array_values(array_diff($index, [$namespace]))); } } /** * The priority order through WordPress's cached copy. For front-end reads. * * @return string[] */ public static function index(): array { return self::clean_index(get_option(self::INDEX_OPTION, [])); } /** * The priority order straight from the database, bypassing the cached * autoloaded copy, so a write never drops an entry another request added. * * @return string[] */ public static function read_index_from_db(): array { global $wpdb; $value = $wpdb->get_var($wpdb->prepare( "SELECT option_value FROM `{$wpdb->options}` WHERE option_name = %s LIMIT 1", self::INDEX_OPTION )); return null === $value ? [] : self::clean_index(maybe_unserialize($value)); } public static function write_index(array $namespaces): void { update_option(self::INDEX_OPTION, self::clean_index($namespaces), true); } /** * Every placed agent's record in priority order, earliest first. An index * entry with no record is skipped. * * @return array[] */ public static function all(): array { $records = []; foreach (self::index() as $namespace) { $record = self::get($namespace); if (null !== $record) { $records[] = $record; } } return $records; } /** * The option names of every stored record, read from the database. * * @return string[] */ public static function record_option_names(): array { global $wpdb; $names = $wpdb->get_col($wpdb->prepare( "SELECT option_name FROM `{$wpdb->options}` WHERE option_name LIKE %s", $wpdb->esc_like(self::RECORD_PREFIX) . '%' )); $pattern = '/^' . preg_quote(self::RECORD_PREFIX, '/') . '[0-9a-f]{32}$/'; return array_values(array_filter((array) $names, function ($name) use ($pattern) { return 1 === preg_match($pattern, (string) $name); })); } /** * Rebuilds the index from the stored records when the two disagree. * Records are ordered by their last known priority. * * @return bool Whether the index changed. */ public static function repair(): bool { $records = []; foreach (self::record_option_names() as $option_name) { $record = get_option($option_name, null); if (!is_array($record) || !isset($record['namespace'])) { continue; } $record = self::normalize($record); if (self::option_name($record['namespace']) === $option_name) { $records[] = $record; } } usort($records, function ($a, $b) { if ($a['priority'] === $b['priority']) { return strcmp($a['namespace'], $b['namespace']); } return $a['priority'] < $b['priority'] ? -1 : 1; }); $repaired = array_column($records, 'namespace'); $current = self::read_index_from_db(); $same_set = count($repaired) === count($current) && !array_diff($repaired, $current) && !array_diff($current, $repaired); if ($same_set) { return false; } self::write_index($repaired); return true; } private static function normalize_rules($rules): array { $rules = is_array($rules) ? $rules : []; $post_types = []; foreach (isset($rules['post_types']) && is_array($rules['post_types']) ? $rules['post_types'] : [] as $post_type) { if (is_scalar($post_type) && '' !== (string) $post_type) { $post_types[] = (string) $post_type; } } $items = []; foreach (isset($rules['items']) && is_array($rules['items']) ? $rules['items'] : [] as $item) { if (is_numeric($item) && (int) $item > 0) { $items[] = (int) $item; } } return [ 'home' => !empty($rules['home']), 'post_types' => array_values(array_unique($post_types)), 'items' => array_values(array_unique($items)), ]; } /** * @return string[] */ private static function clean_index($index): array { if (!is_array($index)) { return []; } $clean = []; foreach ($index as $namespace) { if (is_scalar($namespace) && '' !== (string) $namespace) { $clean[] = (string) $namespace; } } return array_values(array_unique($clean)); } }