| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\AtomicWidgets; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
const CACHE_KEY_PREFIX = 'elementor_atomic_cache_validity-'; |
| 10 |
|
| 11 |
class Cache_Validity { |
| 12 |
|
| 13 |
public function is_valid( array $keys ): bool { |
| 14 |
$root = array_shift( $keys ); |
| 15 |
|
| 16 |
$state_item = get_option( CACHE_KEY_PREFIX . $root, null ); |
| 17 |
|
| 18 |
if ( ! empty( $keys ) ) { |
| 19 |
if ( ! $state_item ) { |
| 20 |
return false; |
| 21 |
} |
| 22 |
|
| 23 |
$state_item = $this->get_nested_item( $state_item, $keys ); |
| 24 |
} |
| 25 |
|
| 26 |
return $state_item ? $state_item['state'] : false; |
| 27 |
} |
| 28 |
|
| 29 |
public function invalidate( array $keys ): void { |
| 30 |
$root = array_shift( $keys ); |
| 31 |
|
| 32 |
$state_item = get_option( CACHE_KEY_PREFIX . $root, [ |
| 33 |
'state' => false, |
| 34 |
'children' => [], |
| 35 |
] ); |
| 36 |
|
| 37 |
$current_item = &$state_item; |
| 38 |
|
| 39 |
if ( ! empty( $keys ) ) { |
| 40 |
$current_item = &$this->get_nested_item( $current_item, $keys ); |
| 41 |
} |
| 42 |
|
| 43 |
$current_item['state'] = false; |
| 44 |
|
| 45 |
$this->invalidate_nested_items( $current_item ); |
| 46 |
|
| 47 |
update_option( CACHE_KEY_PREFIX . $root, $state_item ); |
| 48 |
} |
| 49 |
|
| 50 |
public function validate( array $keys ): void { |
| 51 |
$root = array_shift( $keys ); |
| 52 |
|
| 53 |
$state_item = get_option( CACHE_KEY_PREFIX . $root, [ |
| 54 |
'state' => false, |
| 55 |
'children' => [], |
| 56 |
] ); |
| 57 |
|
| 58 |
$current_item = &$state_item; |
| 59 |
|
| 60 |
if ( ! empty( $keys ) ) { |
| 61 |
$current_item = &$this->get_nested_item( $current_item, $keys ); |
| 62 |
} |
| 63 |
|
| 64 |
$current_item['state'] = true; |
| 65 |
|
| 66 |
update_option( CACHE_KEY_PREFIX . $root, $state_item ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* @param array{state: boolean, children: array<string, self>} $root_item |
| 72 |
* @param array<string> $keys |
| 73 |
* @return array{state: boolean, children: array<string, self>} |
| 74 |
*/ |
| 75 |
private function &get_nested_item( array &$root_item, array $keys ): array { |
| 76 |
$current_item = &$root_item; |
| 77 |
|
| 78 |
while ( ! empty( $keys ) ) { |
| 79 |
$key = array_shift( $keys ); |
| 80 |
|
| 81 |
if ( ! isset( $current_item['children'][ $key ] ) ) { |
| 82 |
$current_item['children'][ $key ] = [ |
| 83 |
'state' => false, |
| 84 |
'children' => [], |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
$current_item = &$current_item['children'][ $key ]; |
| 89 |
} |
| 90 |
|
| 91 |
return $current_item; |
| 92 |
} |
| 93 |
|
| 94 |
private function invalidate_nested_items( array &$root_item ): void { |
| 95 |
foreach ( $root_item['children'] as &$child_item ) { |
| 96 |
$child_item['state'] = false; |
| 97 |
|
| 98 |
$this->invalidate_nested_items( $child_item ); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|