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