| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Snapshot Store |
| 5 |
* |
| 6 |
* Static CRUD for chunked wp_options snapshot data. |
| 7 |
* All options use autoload=false to prevent loading on every page view. |
| 8 |
* |
| 9 |
* @package ThinkRank\Admin\Importers |
| 10 |
* @since 2.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
declare(strict_types=1); |
| 14 |
|
| 15 |
namespace ThinkRank\Admin\Importers; |
| 16 |
|
| 17 |
if (!defined('ABSPATH')) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Snapshot Store Class |
| 23 |
* |
| 24 |
* Manages chunked read/write of snapshot data in wp_options. |
| 25 |
* |
| 26 |
* @since 2.0.0 |
| 27 |
*/ |
| 28 |
class Snapshot_Store { |
| 29 |
|
| 30 |
/** |
| 31 |
* Option key prefix for all snapshot data |
| 32 |
*/ |
| 33 |
private const OPTION_PREFIX = 'thinkrank_snapshot_'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Write a chunk of snapshot data |
| 37 |
* |
| 38 |
* @param string $plugin Plugin slug (e.g., 'yoast') |
| 39 |
* @param string $type Data type (e.g., 'postmeta') |
| 40 |
* @param int $page Chunk/page number |
| 41 |
* @param array $records Array of normalized records |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public static function write_chunk(string $plugin, string $type, int $page, array $records): void { |
| 45 |
$option_key = self::OPTION_PREFIX . "{$plugin}_{$type}_chunk_{$page}"; |
| 46 |
update_option($option_key, $records, false); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Write or update the manifest for a plugin snapshot |
| 51 |
* |
| 52 |
* @param string $plugin Plugin slug |
| 53 |
* @param array $manifest Manifest data |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public static function write_manifest(string $plugin, array $manifest): void { |
| 57 |
$option_key = self::OPTION_PREFIX . "{$plugin}_manifest"; |
| 58 |
update_option($option_key, $manifest, false); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Read a chunk of snapshot data |
| 63 |
* |
| 64 |
* @param string $plugin Plugin slug |
| 65 |
* @param string $type Data type |
| 66 |
* @param int $page Chunk/page number |
| 67 |
* @return array|null Chunk data or null if not found |
| 68 |
*/ |
| 69 |
public static function read_chunk(string $plugin, string $type, int $page): ?array { |
| 70 |
$option_key = self::OPTION_PREFIX . "{$plugin}_{$type}_chunk_{$page}"; |
| 71 |
$data = get_option($option_key, null); |
| 72 |
return is_array($data) ? $data : null; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get the manifest for a plugin snapshot |
| 77 |
* |
| 78 |
* @param string $plugin Plugin slug |
| 79 |
* @return array|null Manifest data or null if not found |
| 80 |
*/ |
| 81 |
public static function get_manifest(string $plugin): ?array { |
| 82 |
$option_key = self::OPTION_PREFIX . "{$plugin}_manifest"; |
| 83 |
$data = get_option($option_key, null); |
| 84 |
return is_array($data) ? $data : null; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get all available snapshots with their manifests |
| 89 |
* |
| 90 |
* @return array Array of manifests keyed by plugin slug |
| 91 |
*/ |
| 92 |
public static function get_available_snapshots(): array { |
| 93 |
global $wpdb; |
| 94 |
|
| 95 |
$results = $wpdb->get_results( |
| 96 |
$wpdb->prepare( |
| 97 |
"SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name LIKE %s", |
| 98 |
$wpdb->esc_like(self::OPTION_PREFIX) . '%_manifest' |
| 99 |
), |
| 100 |
ARRAY_A |
| 101 |
); |
| 102 |
|
| 103 |
$snapshots = []; |
| 104 |
foreach ($results as $row) { |
| 105 |
// Extract plugin slug from option name: thinkrank_snapshot_{plugin}_manifest |
| 106 |
$option_name = $row['option_name']; |
| 107 |
$plugin = str_replace([self::OPTION_PREFIX, '_manifest'], '', $option_name); |
| 108 |
|
| 109 |
$manifest = Safe_Unserializer::unserialize($row['option_value']); |
| 110 |
if (is_array($manifest)) { |
| 111 |
$snapshots[$plugin] = $manifest; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return $snapshots; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Delete all snapshot data for a plugin |
| 120 |
* |
| 121 |
* @param string $plugin Plugin slug |
| 122 |
* @return int Number of options deleted |
| 123 |
*/ |
| 124 |
public static function delete_snapshot(string $plugin): int { |
| 125 |
global $wpdb; |
| 126 |
|
| 127 |
// Find all options for this plugin's snapshot |
| 128 |
$option_names = $wpdb->get_col( |
| 129 |
$wpdb->prepare( |
| 130 |
"SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s", |
| 131 |
$wpdb->esc_like(self::OPTION_PREFIX . $plugin . '_') . '%' |
| 132 |
) |
| 133 |
); |
| 134 |
|
| 135 |
$deleted = 0; |
| 136 |
foreach ($option_names as $option_name) { |
| 137 |
if (delete_option($option_name)) { |
| 138 |
$deleted++; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return $deleted; |
| 143 |
} |
| 144 |
} |
| 145 |
|