| 1 |
<?php |
| 2 |
namespace Elementor\Modules\GlobalClasses; |
| 3 |
|
| 4 |
use Elementor\Core\Kits\Documents\Kit; |
| 5 |
use Elementor\Modules\AtomicWidgets\Styles\Utils as Atomic_Styles_Utils; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
class Repository { |
| 12 |
const META_KEY = '_elementor_global_classes'; |
| 13 |
|
| 14 |
private Kit $kit; |
| 15 |
|
| 16 |
public function __construct( Kit $kit ) { |
| 17 |
$this->kit = $kit; |
| 18 |
} |
| 19 |
|
| 20 |
public function all() { |
| 21 |
$all = $this->kit->get_json_meta( self::META_KEY ); |
| 22 |
|
| 23 |
return Global_Classes::make( $all['items'] ?? [], $all['order'] ?? [] ); |
| 24 |
} |
| 25 |
|
| 26 |
public function get( string $id ) { |
| 27 |
return $this->all()->get_items()->get( $id ); |
| 28 |
} |
| 29 |
|
| 30 |
public function delete( string $id ) { |
| 31 |
$all = $this->all(); |
| 32 |
|
| 33 |
if ( ! isset( $all->get_items()[ $id ] ) ) { |
| 34 |
throw new \Exception( "Global class with id ${id} not found" ); |
| 35 |
} |
| 36 |
|
| 37 |
$this->kit->update_json_meta( self::META_KEY, [ |
| 38 |
'items' => $all->get_items()->except( [ $id ] )->all(), |
| 39 |
'order' => $all->get_order()->filter( fn( $item ) => $item !== $id )->all(), |
| 40 |
] ); |
| 41 |
} |
| 42 |
|
| 43 |
public function patch( string $id, array $value ) { |
| 44 |
$all = $this->all(); |
| 45 |
|
| 46 |
unset( $value['id'] ); |
| 47 |
|
| 48 |
if ( ! isset( $all->get_items()[ $id ] ) ) { |
| 49 |
throw new \Exception( "Global class with id ${id} not found" ); |
| 50 |
} |
| 51 |
|
| 52 |
$value = $this->kit->update_json_meta( self::META_KEY, [ |
| 53 |
'items' => $all->get_items()->merge( [ $id => $value ] )->all(), |
| 54 |
'order' => $all->get_order()->all(), |
| 55 |
] ); |
| 56 |
|
| 57 |
if ( ! $value ) { |
| 58 |
throw new \Exception( 'Failed to update global class' ); |
| 59 |
} |
| 60 |
|
| 61 |
return $this->get( $id ); |
| 62 |
} |
| 63 |
|
| 64 |
public function create( array $value ) { |
| 65 |
$all = $this->all(); |
| 66 |
$id = $this->generate_global_class_id(); |
| 67 |
|
| 68 |
$value['id'] = $id; |
| 69 |
|
| 70 |
$updated = $this->kit->update_json_meta( self::META_KEY, [ |
| 71 |
'items' => $all->get_items()->merge( [ $id => $value ] )->all(), |
| 72 |
'order' => $all->get_order()->push( $id )->all(), |
| 73 |
] ); |
| 74 |
|
| 75 |
if ( ! $updated ) { |
| 76 |
throw new \Exception( 'Failed to create global class' ); |
| 77 |
} |
| 78 |
|
| 79 |
return $this->get( $id ); |
| 80 |
} |
| 81 |
|
| 82 |
public function arrange( array $value ) { |
| 83 |
$all = $this->all(); |
| 84 |
|
| 85 |
$updated = $this->kit->update_json_meta( self::META_KEY, [ |
| 86 |
'items' => $all->get_items()->all(), |
| 87 |
'order' => $value, |
| 88 |
] ); |
| 89 |
|
| 90 |
if ( ! $updated ) { |
| 91 |
throw new \Exception( 'Failed to arrange global classes' ); |
| 92 |
} |
| 93 |
|
| 94 |
return $this->all()->get_order()->all(); |
| 95 |
} |
| 96 |
|
| 97 |
private function generate_global_class_id() { |
| 98 |
$existing_ids = $this->all()->get_items()->keys(); |
| 99 |
$kit_id = $this->kit->get_id(); |
| 100 |
|
| 101 |
return Atomic_Styles_Utils::generate_id( 'g-' . $kit_id . '-', $existing_ids ); |
| 102 |
} |
| 103 |
} |
| 104 |
|