PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.2
Elementor Website Builder – more than just a page builder v4.2.2
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 / favorites / module.php

module.php in Elementor Website Builder – more than just a page builder 4.2.2, at modules/favorites/module.php

248 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\Favorites;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 use Elementor\Core\Base\Module as BaseModule;
9 use Elementor\Core\Experiments\Manager;
10 use Elementor\Modules\Favorites\Types\Widgets;
11 use Elementor\Plugin;
12 use http\Exception\InvalidArgumentException;
13 use WP_Error;
14
15 class Module extends BaseModule {
16
17 /**
18 * List of registered favorites type.
19 *
20 * @var Favorites_Type[]
21 */
22 protected $types = [];
23
24 const OPTION_NAME = 'elementor_editor_user_favorites';
25
26 /**
27 * The name of the merge action.
28 *
29 * @var string
30 */
31 const ACTION_MERGE = 'merge';
32
33 /**
34 * The name of the delete action.
35 *
36 * @var string
37 */
38 const ACTION_DELETE = 'delete';
39
40 /**
41 * Favorites module constructor.
42 */
43 public function __construct() {
44 // Register default types
45 $this->register( Widgets::class );
46
47 $this->populate();
48
49 Plugin::instance()->data_manager_v2->register_controller( new Controller() );
50
51 add_filter( 'elementor/tracker/send_tracking_data_params', [ $this, 'add_tracking_data' ] );
52 }
53
54 /**
55 * Add usage data related to favorites.
56 *
57 * @param $params
58 *
59 * @return array
60 */
61 public function add_tracking_data( $params ) {
62 $params['usages']['favorites'] = $this->get();
63
64 return $params;
65 }
66
67 public function get_name() {
68 return 'favorites';
69 }
70
71 /**
72 * Get user favorites by type.
73 *
74 * @param string[]|string $type
75 *
76 * @return array
77 */
78 public function get( $type = null ) {
79 if ( null === $type ) {
80 $type = array_keys( $this->types );
81 }
82
83 if ( is_array( $type ) ) {
84 return array_intersect_key(
85 $this->combined(),
86 array_flip( (array) $type )
87 );
88 }
89
90 return $this->type_instance( $type )
91 ->values();
92 }
93
94 /**
95 * Merge new user favorites to a type.
96 *
97 * @param string $type
98 * @param array|string $favorites
99 * @param bool $store
100 *
101 * @return array|bool
102 */
103 public function merge( $type, $favorites, $store = true ) {
104 return $this->update( $type, $favorites, static::ACTION_MERGE, $store );
105 }
106
107 /**
108 * Delete existing favorites from a type.
109 *
110 * @param string $type
111 * @param array|string $favorites
112 * @param bool $store
113 *
114 * @return array|int
115 */
116 public function delete( $type, $favorites, $store = true ) {
117 return $this->update( $type, $favorites, static::ACTION_DELETE, $store );
118 }
119
120 /**
121 * Update favorites on a type by merging or deleting from it.
122 *
123 * @param $type
124 * @param $favorites
125 * @param $action
126 * @param bool $store
127 *
128 * @return array|boolean
129 */
130 public function update( $type, $favorites, $action, $store = true ) {
131 $type_instance = $this->type_instance( $type );
132 $favorites = $type_instance->prepare( $favorites );
133
134 switch ( $action ) {
135 case static::ACTION_MERGE:
136 $type_instance->merge( $favorites );
137 break;
138 case static::ACTION_DELETE:
139 $type_instance->filter(
140 function( $value ) use ( $favorites ) {
141 return ! in_array( $value, $favorites, true );
142 }
143 );
144 break;
145 default:
146 $this->action_doesnt_exists( $action );
147 }
148
149 if ( $store && ! $this->store() ) {
150 return false;
151 }
152
153 return $type_instance->values();
154 }
155
156 /**
157 * Get registered favorites type instance.
158 *
159 * @param string $type
160 *
161 * @return Favorites_Type
162 */
163 public function type_instance( $type ) {
164 return $this->types[ $type ];
165 }
166
167 /**
168 * Register a new type class.
169 *
170 * @param string $class_name
171 */
172 public function register( $class_name ) {
173 $type_instance = new $class_name();
174
175 $this->types[ $type_instance->get_name() ] = $type_instance;
176 }
177
178 /**
179 * Returns all available types keys.
180 *
181 * @return string[]
182 */
183 public function available() {
184 return array_keys( $this->types );
185 }
186
187 /**
188 * Combine favorites from all types into a single array.
189 *
190 * @return array
191 */
192 protected function combined() {
193 $all = [];
194
195 foreach ( $this->types as $type ) {
196 $favorites = $type->values();
197
198 if ( ! empty( $favorites ) ) {
199 $all[ $type->get_name() ] = $favorites;
200 }
201 }
202
203 return $all;
204 }
205
206 /**
207 * Populate all type classes with the stored data.
208 */
209 protected function populate() {
210 $combined = $this->retrieve();
211
212 foreach ( $this->types as $key => $type ) {
213 if ( isset( $combined[ $key ] ) ) {
214 $type->merge( $combined[ $key ] );
215 }
216 }
217 }
218
219 /**
220 * Retrieve stored user favorites types.
221 *
222 * @return mixed|false
223 */
224 protected function retrieve() {
225 return get_user_option( static::OPTION_NAME );
226 }
227
228 /**
229 * Update all changes to user favorites type.
230 *
231 * @return int|bool
232 */
233 protected function store() {
234 return update_user_option( get_current_user_id(), static::OPTION_NAME, $this->combined() );
235 }
236
237 /**
238 * Throw action doesn't exist exception.
239 *
240 * @param string $action
241 *
242 * @throws \InvalidArgumentException If favorite action fails or validation errors occur.
243 */
244 public function action_doesnt_exists( $action ) {
245 throw new \InvalidArgumentException( sprintf( "Action '%s' to apply on favorites doesn't exists", esc_html( $action ) ) );
246 }
247 }
248