PluginProbe
Elementor Website Builder – more than just a page builder / 3.7.0
Elementor Website Builder – more than just a page builder v3.7.0
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 3.7.0, at modules/favorites/module.php

261 lines 5.3 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 public static function get_experimental_data() {
55 return [
56 'name' => 'favorite-widgets',
57 'title' => esc_html__( 'Favorite Widgets', 'elementor' ),
58 'description' => esc_html__( 'Mark widgets as favorites by right clicking them. Favorite widgets will always appear at the top of the editor panel for easy access.', 'elementor' ),
59 'release_status' => Manager::RELEASE_STATUS_STABLE,
60 'new_site' => [
61 'default_active' => true,
62 ],
63 ];
64 }
65
66 /**
67 * Add usage data related to favorites.
68 *
69 * @param $params
70 *
71 * @return array
72 */
73 public function add_tracking_data( $params ) {
74 $params['usages']['favorites'] = $this->get();
75
76 return $params;
77 }
78
79 public function get_name() {
80 return 'favorites';
81 }
82
83 /**
84 * Get user favorites by type.
85 *
86 * @param string[]|string $type
87 *
88 * @return array
89 */
90 public function get( $type = null ) {
91 if ( null === $type ) {
92 $type = array_keys( $this->types );
93 }
94
95 if ( is_array( $type ) ) {
96 return array_intersect_key(
97 $this->combined(),
98 array_flip( (array) $type )
99 );
100 }
101
102 return $this->type_instance( $type )
103 ->values();
104 }
105
106 /**
107 * Merge new user favorites to a type.
108 *
109 * @param string $type
110 * @param array|string $favorites
111 * @param bool $store
112 *
113 * @return array|bool
114 */
115 public function merge( $type, $favorites, $store = true ) {
116 return $this->update( $type, $favorites, static::ACTION_MERGE, $store );
117 }
118
119 /**
120 * Delete existing favorites from a type.
121 *
122 * @param string $type
123 * @param array|string $favorites
124 * @param bool $store
125 *
126 * @return array|int
127 */
128 public function delete( $type, $favorites, $store = true ) {
129 return $this->update( $type, $favorites, static::ACTION_DELETE, $store );
130 }
131
132 /**
133 * Update favorites on a type by merging or deleting from it.
134 *
135 * @param $type
136 * @param $favorites
137 * @param $action
138 * @param bool $store
139 *
140 * @return array|boolean
141 */
142 public function update( $type, $favorites, $action, $store = true ) {
143 $type_instance = $this->type_instance( $type );
144 $favorites = $type_instance->prepare( $favorites );
145
146 switch ( $action ) {
147 case static::ACTION_MERGE:
148 $type_instance->merge( $favorites );
149 break;
150 case static::ACTION_DELETE:
151 $type_instance->filter(
152 function( $value ) use ( $favorites ) {
153 return ! in_array( $value, $favorites, true );
154 }
155 );
156 break;
157 default:
158 $this->action_doesnt_exists( $action );
159 }
160
161 if ( $store && ! $this->store() ) {
162 return false;
163 }
164
165 return $type_instance->values();
166 }
167
168 /**
169 * Get registered favorites type instance.
170 *
171 * @param string $type
172 *
173 * @return Favorites_Type
174 */
175 public function type_instance( $type ) {
176 return $this->types[ $type ];
177 }
178
179 /**
180 * Register a new type class.
181 *
182 * @param string $class
183 */
184 public function register( $class ) {
185 $type_instance = new $class();
186
187 $this->types[ $type_instance->get_name() ] = $type_instance;
188 }
189
190 /**
191 * Returns all available types keys.
192 *
193 * @return string[]
194 */
195 public function available() {
196 return array_keys( $this->types );
197 }
198
199 /**
200 * Combine favorites from all types into a single array.
201 *
202 * @return array
203 */
204 protected function combined() {
205 $all = [];
206
207 foreach ( $this->types as $type ) {
208 $favorites = $type->values();
209
210 if ( ! empty( $favorites ) ) {
211 $all[ $type->get_name() ] = $favorites;
212 }
213 }
214
215 return $all;
216 }
217
218 /**
219 * Populate all type classes with the stored data.
220 */
221 protected function populate() {
222 $combined = $this->retrieve();
223
224 foreach ( $this->types as $key => $type ) {
225 if ( isset( $combined[ $key ] ) ) {
226 $type->merge( $combined[ $key ] );
227 }
228 }
229 }
230
231 /**
232 * Retrieve stored user favorites types.
233 *
234 * @return mixed|false
235 */
236 protected function retrieve() {
237 return get_user_option( static::OPTION_NAME );
238 }
239
240 /**
241 * Update all changes to user favorites type.
242 *
243 * @return int|bool
244 */
245 protected function store() {
246 return update_user_option( get_current_user_id(), static::OPTION_NAME, $this->combined() );
247 }
248
249 /**
250 * Throw action doesn't exist exception.
251 *
252 * @param string $action
253 */
254 public function action_doesnt_exists( $action ) {
255 throw new \InvalidArgumentException( sprintf(
256 "Action '%s' to apply on favorites doesn't exists",
257 $action
258 ) );
259 }
260 }
261