-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass-elementor-extra-widgets.php
376 lines (320 loc) · 11.2 KB
/
class-elementor-extra-widgets.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/**
* Class ThemeIsle\ElementorExtraWidgets
*
* @package ThemeIsle\ElementorExtraWidgets
* @copyright Copyright (c) 2017, Andrei Lupu
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 1.0.0
*/
namespace ThemeIsle;
use Elementor\Plugin;
use ThemeIsle\ElementorExtraWidgets\Traits\Sanitization;
if ( ! class_exists( '\ThemeIsle\ElementorExtraWidgets' ) ) {
class ElementorExtraWidgets {
use Sanitization;
/**
* @var ElementorExtraWidgets
*/
public static $instance = null;
/**
* The version of this library
* @var string
*/
public static $version = '1.0.6';
/**
* Defines the library behaviour
*/
protected function init() {
add_action( 'elementor/frontend/after_enqueue_styles', array( $this, 'register_styles' ) );
add_action( 'elementor/frontend/after_register_scripts', [ $this, 'register_scripts' ] );
add_action( 'elementor/elements/categories_registered', array( $this, 'add_elementor_category' ) );
add_action( 'widgets_init', array( $this, 'register_woo_widgets' ) );
add_action( 'widgets_init', array( $this, 'register_posts_widgets' ) );
add_action( 'elementor/widgets/widgets_registered', array( $this, 'add_elementor_widgets' ) );
if ( ! defined( 'EAW_PRO_VERSION' ) ) {
add_action( 'elementor/editor/after_enqueue_scripts', array( $this, 'enqueue_sidebar_css' ) );
}
// Ensure Font Awesome is always enqeued
add_action(
'elementor/editor/after_enqueue_styles',
function() {
wp_enqueue_style( 'font-awesome' );
}
);
// Before Elementor Widget Settings Save
add_filter( 'elementor/document/save/data', array( $this, 'before_settings_save' ), 10, 2 );
}
/**
* Sanititze title tags.
*
* @param string $tag The tag to sanitize.
* @param string $default The default tag.
*
* @return string
*/
private function sanitize_title_attributes( $tag, $default = 'h3' ) {
$allowed_tags = [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'span', 'div', 'p' ];
if ( ! in_array( $tag, $allowed_tags ) ) {
return $default;
}
return $tag;
}
/**
* Sanitize grid column values.
*
* @param array $settings The settings to sanitize.
*/
private function sanitize_grid_columns( &$settings ) {
if ( isset( $settings['grid_columns'] ) ) {
$settings['grid_columns'] = $this->sanitize_numeric( $settings['grid_columns'], 3 );
}
if ( isset( $settings['grid_columns_tablet'] ) ) {
$settings['grid_columns_tablet'] = $this->sanitize_numeric( $settings['grid_columns_tablet'], 2 );
}
if ( isset( $settings['grid_columns_mobile'] ) ) {
$settings['grid_columns_mobile'] = $this->sanitize_numeric( $settings['grid_columns_mobile'], 1 );
}
}
/**
* Recursively search and sanitize element fields data.
*
* @param array $elements_data The elements data.
*/
private function search_and_modify_widget_settings( &$elements_data ) {
foreach ( $elements_data as &$element ) {
if ( isset( $element['elType'] ) && $element['elType'] === 'widget' ) {
// Check if the widget is of the desired type
if ( isset( $element['widgetType'] ) && in_array( $element['widgetType'], [ 'obfx-pricing-table', 'obfx-posts-grid', 'obfx-services' ] ) ) {
// Modify the settings of the widget
$settings = $element['settings'];
// Sanitize the grid columns passing the settings by reference
$this->sanitize_grid_columns( $settings );
if ( isset( $settings['title_tag'] ) ) {
$settings['title_tag'] = $this->sanitize_title_attributes( $settings['title_tag'], 'h3' );
}
if ( isset( $settings['subtitle_tag'] ) ) {
$settings['subtitle_tag'] = $this->sanitize_title_attributes( $settings['subtitle_tag'], 'p' );
}
if ( isset( $settings['grid_title_tag'] ) ) {
$settings['grid_title_tag'] = $this->sanitize_title_attributes( $settings['grid_title_tag'], 'h3' );
}
$element['settings'] = $settings;
}
}
if ( isset( $element['elements'] ) && is_array( $element['elements'] ) ) {
// If the element has nested elements (e.g., section or column), recursively call the function
$this->search_and_modify_widget_settings( $element['elements'] );
}
}
}
/**
* Filter the document data and sanitize the form parameters.
*
* @param array $data The document data.
* @param @param \Elementor\Core\Base\Document $document The document instance.
*
* @return mixed
*/
public function before_settings_save( $data, $document ) {
if ( ! isset( $data['elements'] ) ) {
return $data;
}
$this->search_and_modify_widget_settings( $data['elements'] );
return $data;
}
/**
* Add the Category for Orbit Fox Widgets.
*
* @param \Elementor\Elements_Manager $elements_manager Elementor elements manager.
*/
public function add_elementor_category( $elements_manager ) {
$category_args = apply_filters( 'elementor_extra_widgets_category_args', array(
'slug' => 'obfx-elementor-widgets',
'title' => __( 'Orbit Fox Addons', 'textdomain' ),
'icon' => 'fa fa-plug',
) );
// add a separate category for the premium widgets
$elements_manager->add_category(
$category_args['slug'] . '-pro',
array(
'title' => 'Neve PRO Addon Widgets',
'icon' => $category_args['slug'],
)
);
$elements_manager->add_category(
$category_args['slug'],
array(
'title' => $category_args['title'],
'icon' => $category_args['slug'],
)
);
}
/**
* Register style.
*/
public function register_styles() {
wp_register_style( 'eaw-elementor', plugins_url( '/css/public.css', __FILE__ ), array(), $this::$version );
wp_register_style( 'font-awesome-5', ELEMENTOR_ASSETS_URL . 'lib/font-awesome/css/all.min.css', false, $this::$version );
}
/**
* Register js scripts.
*/
public function register_scripts() {
wp_register_script( 'obfx-grid-js', plugins_url( '/js/obfx-grid.js', __FILE__ ), array( 'jquery' ), $this::$version, true );
}
public function enqueue_sidebar_css() {
wp_enqueue_style( 'eaw-elementor-admin', plugins_url( '/css/admin.css', __FILE__ ), array(), $this::$version );
}
/**
* Require and instantiate Elementor Widgets and Premium Placeholders.
*
* @param $widgets_manager
*/
public function add_elementor_widgets( $widgets_manager ) {
$elementor_widgets = $this->get_dir_files( __DIR__ . '/widgets/elementor' );
foreach ( $elementor_widgets as $widget ) {
require_once $widget;
$widget = basename( $widget, ".php" );
if ( $widget === 'premium-placeholder' ) {// avoid instantiate an abstract class
continue;
}
$classname = $this->convert_filename_to_classname( $widget );
if ( class_exists( $classname ) ) {
$widget_object = new $classname();
$widgets_manager->register_widget_type( $widget_object );
}
}
if( class_exists( 'Elementor_Widgets_OBFX_Module', false ) && \Elementor_Widgets_OBFX_Module::should_add_placeholders() ){
$placeholders = $this->get_dir_files( __DIR__ . '/widgets/elementor/placeholders' );
foreach ( $placeholders as $widget ) {
require_once $widget;
}
do_action( 'eaw_before_pro_widgets', $placeholders, $widgets_manager );
foreach ( $placeholders as $widget ) {
$widget = basename( $widget, ".php" );
$classname = $this->convert_filename_to_classname( $widget );
// Maybe Premium Elements
if ( ! class_exists( $classname ) ) {
$classname = $classname . '_Placeholder';
}
if ( class_exists( $classname ) ) {
$widget_object = new $classname();
$widgets_manager->register_widget_type( $widget_object );
}
}
}
}
/**
* WooCommerce Widget section
*
* @since 1.0.0
* @return void
*/
public function register_woo_widgets() {
if ( ! class_exists( 'woocommerce' ) ) { // Lets not do anything unless WooCommerce is active!
return null;
}
include_once( plugin_dir_path( __FILE__ ) . 'widgets/woo/class-eaw-wp-widget.php' );
include_once( plugin_dir_path( __FILE__ ) . 'widgets/woo/products-categories.php' );
include_once( plugin_dir_path( __FILE__ ) . 'widgets/woo/recent-products.php' );
include_once( plugin_dir_path( __FILE__ ) . 'widgets/woo/featured-products.php' );
include_once( plugin_dir_path( __FILE__ ) . 'widgets/woo/popular-products.php' );
include_once( plugin_dir_path( __FILE__ ) . 'widgets/woo/sale-products.php' );
include_once( plugin_dir_path( __FILE__ ) . 'widgets/woo/best-products.php' );
register_widget( 'Woo_Product_Categories' );
register_widget( 'Woo_Recent_Products' );
register_widget( 'Woo_Featured_Products' );
register_widget( 'Woo_Popular_Products' );
register_widget( 'Woo_Sale_Products' );
register_widget( 'Woo_Best_Products' );
}
/**
* Posts Widget section
*
* @since 1.0.0
* @return void
*/
public function register_posts_widgets() {
include_once( plugin_dir_path( __FILE__ ) . 'widgets/wp/eaw-posts-widget.php' );
register_widget( 'EAW_Recent_Posts' );
include_once( plugin_dir_path( __FILE__ ) . 'widgets/wp/eaw-posts-widget-plus.php' );
register_widget( 'EAW_Recent_Posts_Plus' );
}
/**
* Returns an array of all PHP files in the specified absolute path.
* Inspired from jetpack's glob_php
*
* @param string $absolute_path The absolute path of the directory to search.
*
* @return array Array of absolute paths to the PHP files.
*/
protected function get_dir_files( $absolute_path ) {
if ( function_exists( 'glob' ) ) {
return glob( "$absolute_path/*.php" );
}
$absolute_path = untrailingslashit( $absolute_path );
$files = array();
if ( ! $dir = @opendir( $absolute_path ) ) {
return $files;
}
while ( false !== $file = readdir( $dir ) ) {
if ( '.' == substr( $file, 0, 1 ) || '.php' != substr( $file, - 4 ) ) {
continue;
}
$file = "$absolute_path/$file";
if ( ! is_file( $file ) ) {
continue;
}
$files[] = $file;
}
closedir( $dir );
return $files;
}
protected function convert_filename_to_classname( $widget ) {
$classname = ucwords( $widget, "-" );
$classname = str_replace( '-', '_', $classname );
$classname = '\\ThemeIsle\\ElementorExtraWidgets\\' . $classname;
return $classname;
}
/**
*
* @static
* @since 1.0.0
* @access public
* @return ElementorExtraWidgets
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
self::$instance->init();
}
return self::$instance;
}
/**
* Throw error on object clone
*
* The whole idea of the singleton design pattern is that there is a single
* object therefore, we don't want the object to be cloned.
*
* @access public
* @since 1.0.0
* @return void
*/
public function __clone() {
// Cloning instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'textdomain' ), '1.0.0' );
}
/**
* Disable unserializing of the class
*
* @access public
* @since 1.0.0
* @return void
*/
public function __wakeup() {
// Unserializing instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ huh?', 'textdomain' ), '1.0.0' );
}
}
}