PluginProbe
Elementor Website Builder – more than just a page builder / 3.31.1
Elementor Website Builder – more than just a page builder v3.31.1
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / library / user-favorites.php

user-favorites.php in Elementor Website Builder – more than just a page builder 3.31.1, at modules/library/user-favorites.php

146 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
33 * @param false $ignore_cache
34 *
35 * @return array
36 */
37 public function get( $vendor = null, $resource = 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 ) {
47 $key = $this->get_key( $vendor, $resource );
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
58 * @param $id
59 *
60 * @return bool
61 */
62 public function exists( $vendor, $resource, $id ) {
63 return in_array( $id, $this->get( $vendor, $resource ), true );
64 }
65
66 /**
67 * @param $vendor
68 * @param $resource
69 * @param array $value
70 *
71 * @return $this
72 * @throws \Exception If fails to save.
73 */
74 public function save( $vendor, $resource, $value = [] ) {
75 $all_favorites = $this->get();
76
77 $all_favorites[ $this->get_key( $vendor, $resource ) ] = $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
93 * @param $id
94 *
95 * @return $this
96 * @throws \Exception If fails to add.
97 */
98 public function add( $vendor, $resource, $id ) {
99 $favorites = $this->get( $vendor, $resource );
100
101 if ( in_array( $id, $favorites, true ) ) {
102 return $this;
103 }
104
105 $favorites[] = $id;
106
107 $this->save( $vendor, $resource, $favorites );
108
109 return $this;
110 }
111
112 /**
113 * @param $vendor
114 * @param $resource
115 * @param $id
116 *
117 * @return $this
118 * @throws \Exception If fails to save.
119 */
120 public function remove( $vendor, $resource, $id ) {
121 $favorites = $this->get( $vendor, $resource );
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, $favorites );
132
133 return $this;
134 }
135
136 /**
137 * @param $vendor
138 * @param $resource
139 *
140 * @return string
141 */
142 private function get_key( $vendor, $resource ) {
143 return "{$vendor}/{$resource}";
144 }
145 }
146