| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WindPress\WindPress\Core\Scanner; |
| 5 |
|
| 6 |
use RuntimeException; |
| 7 |
/** |
| 8 |
* Detect WordPress content mutations while a browser builds its CSS. |
| 9 |
*/ |
| 10 |
class SourceRevision |
| 11 |
{ |
| 12 |
private const OPTION = 'windpress_source_revision'; |
| 13 |
public static function register_hooks(): void |
| 14 |
{ |
| 15 |
foreach (['save_post', 'deleted_post', 'created_term', 'edited_term', 'delete_term', 'set_object_terms', 'deleted_term_relationships', 'added_term_meta', 'updated_term_meta', 'deleted_term_meta', 'profile_update', 'deleted_user', 'added_user_meta', 'updated_user_meta', 'deleted_user_meta', 'switch_theme', 'activated_plugin', 'deactivated_plugin', 'upgrader_process_complete'] as $hook) { |
| 16 |
add_action($hook, [self::class, 'invalidate'], 10, 0); |
| 17 |
} |
| 18 |
foreach (['added_post_meta', 'updated_post_meta', 'deleted_post_meta'] as $hook) { |
| 19 |
add_action($hook, [self::class, 'invalidate_post_meta'], 10, 3); |
| 20 |
} |
| 21 |
foreach (['added_option', 'updated_option', 'deleted_option'] as $hook) { |
| 22 |
add_action($hook, [self::class, 'invalidate_option']); |
| 23 |
} |
| 24 |
} |
| 25 |
public static function get(): string |
| 26 |
{ |
| 27 |
wp_cache_delete(self::OPTION, 'options'); |
| 28 |
$revision = get_option(self::OPTION, \false); |
| 29 |
if (!is_string($revision) || $revision === '') { |
| 30 |
add_option(self::OPTION, wp_generate_uuid4(), '', \false); |
| 31 |
wp_cache_delete(self::OPTION, 'options'); |
| 32 |
$revision = get_option(self::OPTION, \false); |
| 33 |
if (!is_string($revision) || $revision === '') { |
| 34 |
throw new RuntimeException(__('Unable to initialize the source revision.', 'windpress')); |
| 35 |
} |
| 36 |
} |
| 37 |
return $revision; |
| 38 |
} |
| 39 |
public static function matches(string $revision): bool |
| 40 |
{ |
| 41 |
return hash_equals(self::get(), $revision); |
| 42 |
} |
| 43 |
public static function invalidate(): void |
| 44 |
{ |
| 45 |
update_option(self::OPTION, wp_generate_uuid4(), \false); |
| 46 |
} |
| 47 |
public static function invalidate_post_meta($meta_id, int $post_id, string $meta_key): void |
| 48 |
{ |
| 49 |
// Editor heartbeat refreshes this lock while source content stays unchanged. |
| 50 |
if ($meta_key !== '_edit_lock' && apply_filters('f!windpress/core/scanner/source_revision:track_post_meta', \true, $meta_key, $post_id)) { |
| 51 |
self::invalidate(); |
| 52 |
} |
| 53 |
} |
| 54 |
public static function invalidate_option(string $option): void |
| 55 |
{ |
| 56 |
// Scan checkpoints and routine runtime caches must not invalidate their own build. |
| 57 |
if ($option === self::OPTION || strpos($option, 'windpress_scan_') === 0 || strpos($option, '_transient_') === 0 || strpos($option, '_site_transient_') === 0 || in_array($option, ['cron', 'rewrite_rules', 'recently_activated', 'auto_updater.lock', 'core_updater.lock'], \true)) { |
| 58 |
return; |
| 59 |
} |
| 60 |
if (apply_filters('f!windpress/core/scanner/source_revision:track_option', \true, $option)) { |
| 61 |
self::invalidate(); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|