| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\DefaultStyles; |
| 4 |
|
| 5 |
use Elementor\Core\Kits\Concerns\Has_Kit_Dependency; |
| 6 |
use Elementor\Core\Kits\Documents\Kit; |
| 7 |
use Elementor\Modules\GlobalClasses\Utils\Kit_Utils; |
| 8 |
use WP_Post; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
class Default_Styles_Tag_Post_IDs { |
| 15 |
use Has_Kit_Dependency; |
| 16 |
|
| 17 |
const META_KEY = '_elementor_default_styles_post_ids'; |
| 18 |
|
| 19 |
private ?array $cache = null; |
| 20 |
|
| 21 |
public static function make( ?Kit $kit = null ): self { |
| 22 |
$instance = new self(); |
| 23 |
|
| 24 |
if ( null !== $kit ) { |
| 25 |
$instance->set_kit( $kit ); |
| 26 |
} |
| 27 |
|
| 28 |
return $instance; |
| 29 |
} |
| 30 |
|
| 31 |
public function register_hooks(): void { |
| 32 |
add_action( 'deleted_post', [ self::class, 'on_deleted_post' ], 10, 2 ); |
| 33 |
} |
| 34 |
|
| 35 |
public static function on_deleted_post( int $post_id, WP_Post $post ): void { |
| 36 |
if ( Default_Style_Post_Type::CPT !== $post->post_type ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
foreach ( Kit_Utils::get_all_kit_documents() as $kit ) { |
| 41 |
self::make( $kit )->remove_post_id( $post_id ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
public function get_post_id( string $tag ): ?int { |
| 46 |
$map = $this->read_map(); |
| 47 |
|
| 48 |
if ( ! isset( $map[ $tag ] ) ) { |
| 49 |
return null; |
| 50 |
} |
| 51 |
|
| 52 |
$post_id = (int) $map[ $tag ]; |
| 53 |
|
| 54 |
if ( get_post( $post_id ) ) { |
| 55 |
return $post_id; |
| 56 |
} |
| 57 |
|
| 58 |
$this->remove_post_id( $post_id ); |
| 59 |
|
| 60 |
return null; |
| 61 |
} |
| 62 |
|
| 63 |
public function get_all(): array { |
| 64 |
$map = $this->read_map(); |
| 65 |
$resolved = []; |
| 66 |
|
| 67 |
foreach ( $map as $tag => $post_id ) { |
| 68 |
$post_id = (int) $post_id; |
| 69 |
|
| 70 |
if ( get_post( $post_id ) ) { |
| 71 |
$resolved[ $tag ] = $post_id; |
| 72 |
} else { |
| 73 |
$this->remove_post_id( $post_id ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return $resolved; |
| 78 |
} |
| 79 |
|
| 80 |
public function set( string $tag, int $post_id ): void { |
| 81 |
$map = $this->read_map(); |
| 82 |
|
| 83 |
if ( $post_id <= 0 || '' === $tag ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
$map[ $tag ] = $post_id; |
| 88 |
$this->write_map( $map ); |
| 89 |
} |
| 90 |
|
| 91 |
public function remove_tag( string $tag ): void { |
| 92 |
$map = $this->read_map(); |
| 93 |
|
| 94 |
if ( ! isset( $map[ $tag ] ) ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
unset( $map[ $tag ] ); |
| 99 |
$this->write_map( $map ); |
| 100 |
} |
| 101 |
|
| 102 |
public function remove_post_id( int $post_id ): void { |
| 103 |
$map = $this->read_map(); |
| 104 |
$filtered = array_filter( $map, fn( $id ) => (int) $id !== $post_id ); |
| 105 |
|
| 106 |
if ( count( $filtered ) === count( $map ) ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
$this->write_map( $filtered ); |
| 111 |
} |
| 112 |
|
| 113 |
private function read_map(): array { |
| 114 |
if ( null !== $this->cache ) { |
| 115 |
return $this->cache; |
| 116 |
} |
| 117 |
|
| 118 |
$kit = $this->get_kit(); |
| 119 |
|
| 120 |
if ( ! $kit ) { |
| 121 |
$this->cache = []; |
| 122 |
|
| 123 |
return []; |
| 124 |
} |
| 125 |
|
| 126 |
$raw = $kit->get_meta( self::META_KEY ); |
| 127 |
$this->cache = is_array( $raw ) ? $raw : []; |
| 128 |
|
| 129 |
return $this->cache; |
| 130 |
} |
| 131 |
|
| 132 |
private function write_map( array $map ): bool { |
| 133 |
$kit = $this->get_kit(); |
| 134 |
|
| 135 |
if ( ! $kit ) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
$result = $kit->update_meta( self::META_KEY, $map ); |
| 140 |
$this->cache = $map; |
| 141 |
|
| 142 |
return false !== $result; |
| 143 |
} |
| 144 |
} |
| 145 |
|