Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/elementor/modules/library/user-favorites.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
namespace Elementor\Modules\Library;
3
+
4
+
if ( ! defined( 'ABSPATH' ) ) {
5
+
exit; // Exit if accessed directly.
6
+
}
7
+
8
+
class User_Favorites {
9
+
const USER_META_KEY = 'elementor_library_favorites';
10
+
11
+
/**
12
+
* @var int
13
+
*/
14
+
private $user_id;
15
+
16
+
/**
17
+
* @var array|null
18
+
*/
19
+
private $cache;
20
+
21
+
/**
22
+
* User_Favorites constructor.
23
+
*
24
+
* @param $user_id
25
+
*/
26
+
public function __construct( $user_id ) {
27
+
$this->user_id = $user_id;
28
+
}
29
+
30
+
/**
31
+
* @param null $vendor
32
+
* @param null $resource_name
33
+
* @param false $ignore_cache
34
+
*
35
+
* @return array
36
+
*/
37
+
public function get( $vendor = null, $resource_name = null, $ignore_cache = false ) {
38
+
if ( $ignore_cache || empty( $this->cache ) ) {
39
+
$this->cache = get_user_meta( $this->user_id, self::USER_META_KEY, true );
40
+
}
41
+
42
+
if ( ! $this->cache || ! is_array( $this->cache ) ) {
43
+
return [];
44
+
}
45
+
46
+
if ( $vendor && $resource_name ) {
47
+
$key = $this->get_key( $vendor, $resource_name );
48
+
49
+
return isset( $this->cache[ $key ] ) ? $this->cache[ $key ] : [];
50
+
}
51
+
52
+
return $this->cache;
53
+
}
54
+
55
+
/**
56
+
* @param $vendor
57
+
* @param $resource_name
58
+
* @param $id
59
+
*
60
+
* @return bool
61
+
*/
62
+
public function exists( $vendor, $resource_name, $id ) {
63
+
return in_array( $id, $this->get( $vendor, $resource_name ), true );
64
+
}
65
+
66
+
/**
67
+
* @param $vendor
68
+
* @param $resource_name
69
+
* @param array $value
70
+
*
71
+
* @return $this
72
+
* @throws \Exception If the favorites cannot be saved.
73
+
*/
74
+
public function save( $vendor, $resource_name, $value = [] ) {
75
+
$all_favorites = $this->get();
76
+
77
+
$all_favorites[ $this->get_key( $vendor, $resource_name ) ] = $value;
78
+
79
+
$result = update_user_meta( $this->user_id, self::USER_META_KEY, $all_favorites );
80
+
81
+
if ( false === $result ) {
82
+
throw new \Exception( 'Failed to save user favorites.' );
83
+
}
84
+
85
+
$this->cache = $all_favorites;
86
+
87
+
return $this;
88
+
}
89
+
90
+
/**
91
+
* @param $vendor
92
+
* @param $resource_name
93
+
* @param $id
94
+
*
95
+
* @return $this
96
+
* @throws \Exception If the favorites cannot be added.
97
+
*/
98
+
public function add( $vendor, $resource_name, $id ) {
99
+
$favorites = $this->get( $vendor, $resource_name );
100
+
101
+
if ( in_array( $id, $favorites, true ) ) {
102
+
return $this;
103
+
}
104
+
105
+
$favorites[] = $id;
106
+
107
+
$this->save( $vendor, $resource_name, $favorites );
108
+
109
+
return $this;
110
+
}
111
+
112
+
/**
113
+
* @param $vendor
114
+
* @param $resource_name
115
+
* @param $id
116
+
*
117
+
* @return $this
118
+
* @throws \Exception If the favorites cannot be removed.
119
+
*/
120
+
public function remove( $vendor, $resource_name, $id ) {
121
+
$favorites = $this->get( $vendor, $resource_name );
122
+
123
+
if ( ! in_array( $id, $favorites, true ) ) {
124
+
return $this;
125
+
}
126
+
127
+
$favorites = array_filter( $favorites, function ( $item ) use ( $id ) {
128
+
return $item !== $id;
129
+
} );
130
+
131
+
$this->save( $vendor, $resource_name, $favorites );
132
+
133
+
return $this;
134
+
}
135
+
136
+
/**
137
+
* @param $vendor
138
+
* @param $resource_name
139
+
*
140
+
* @return string
141
+
*/
142
+
private function get_key( $vendor, $resource_name ) {
143
+
return "{$vendor}/{$resource_name}";
144
+
}
145
+
}
146
+