PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 2.4.26
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v2.4.26
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / admin / views / sp-framework / classes / setup.class.php

setup.class.php in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 2.4.26, at admin/views/sp-framework/classes/setup.class.php

541 lines 14.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The setup class of the plugin.
4 *
5 * @package Smart_Post_Show
6 * @subpackage Smart_Post_Show/views/sp-framework/classes
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 die; } // Cannot a cess directly.
11
12 if ( ! class_exists( 'SP_PC' ) ) {
13
14 /**
15 *
16 * Setup Class
17 *
18 * @since 1.0.0
19 * @version 1.0.0
20 */
21 class SP_PC {
22
23 /**
24 * Constants.
25 *
26 * @var string
27 */
28 public static $version = '2.0.6';
29
30 /**
31 * Premium or not.
32 *
33 * @var boolean
34 */
35 public static $premium = true;
36
37 /**
38 * Directory.
39 *
40 * @var string
41 */
42 public static $dir = null;
43
44 /**
45 * URL
46 *
47 * @var string
48 */
49 public static $url = null;
50
51 /**
52 * Initiated.
53 *
54 * @var array
55 */
56 public static $inited = array();
57
58 /**
59 * The fields array.
60 *
61 * @var array
62 */
63 public static $fields = array();
64
65 /**
66 * The arguments.
67 *
68 * @var array
69 */
70 public static $args = array(
71 'options' => array(),
72 'customize_options' => array(),
73 'metaboxes' => array(),
74 'shortcoders' => array(),
75 'taxonomy_options' => array(),
76 'widgets' => array(),
77 );
78
79 /**
80 * Shortcode instances.
81 *
82 * @var array
83 */
84 public static $shortcode_instances = array();
85
86 /**
87 * Init.
88 *
89 * @return void
90 */
91 public static function init() {
92
93 // init action.
94 do_action( 'spf_init' );
95
96 // set constants.
97 self::constants();
98
99 // include files.
100 self::includes();
101
102 add_action( 'after_setup_theme', array( 'SP_PC', 'setup' ) );
103 add_action( 'init', array( 'SP_PC', 'setup' ) );
104 add_action( 'switch_theme', array( 'SP_PC', 'setup' ) );
105 add_action( 'admin_enqueue_scripts', array( 'SP_PC', 'add_admin_enqueue_scripts' ), 20 );
106
107 }
108
109 /**
110 * Setup.
111 *
112 * @return void
113 */
114 public static function setup() {
115
116 // setup options.
117 $params = array();
118 if ( ! empty( self::$args['options'] ) ) {
119 foreach ( self::$args['options'] as $key => $value ) {
120 if ( ! empty( self::$args['sections'][ $key ] ) && ! isset( self::$inited[ $key ] ) ) {
121
122 $params['args'] = $value;
123 $params['sections'] = self::$args['sections'][ $key ];
124 self::$inited[ $key ] = true;
125
126 SP_PC_Options::instance( $key, $params );
127
128 if ( ! empty( $value['show_in_customizer'] ) ) {
129 self::$args['customize_options'][ $key ] = ( is_array( $value['show_in_customizer'] ) ) ? $value['show_in_customizer'] : $value;
130 }
131 }
132 }
133 }
134
135 // setup metaboxes.
136 $params = array();
137 if ( ! empty( self::$args['metaboxes'] ) ) {
138 foreach ( self::$args['metaboxes'] as $key => $value ) {
139 if ( ! empty( self::$args['sections'][ $key ] ) && ! isset( self::$inited[ $key ] ) ) {
140
141 $params['args'] = $value;
142 $params['sections'] = self::$args['sections'][ $key ];
143 self::$inited[ $key ] = true;
144
145 SP_PC_Metabox::instance( $key, $params );
146
147 }
148 }
149 }
150
151 // create widgets.
152 if ( ! empty( self::$args['widgets'] ) && class_exists( 'WP_Widget_Factory' ) ) {
153
154 $wp_widget_factory = new WP_Widget_Factory();
155
156 foreach ( self::$args['widgets'] as $key => $value ) {
157 if ( ! isset( self::$inited[ $key ] ) ) {
158 self::$inited[ $key ] = true;
159 $wp_widget_factory->register( SP_PC_Widget::instance( $key, $value ) );
160 }
161 }
162 }
163
164 do_action( 'spf_loaded' );
165
166 }
167
168 /**
169 * Create options.
170 *
171 * @param string $id The option ID.
172 * @param array $args The arguments array.
173 * @return void
174 */
175 public static function createOptions( $id, $args = array() ) {
176 self::$args['options'][ $id ] = $args;
177 }
178
179 /**
180 * Create metabox options.
181 *
182 * @param string $id The option ID.
183 * @param array $args The arguments array.
184 * @return void
185 */
186 public static function createMetabox( $id, $args = array() ) {
187 self::$args['metaboxes'][ $id ] = $args;
188 }
189
190 /**
191 * Create widget.
192 *
193 * @param string $id The option ID.
194 * @param array $args The arguments array.
195 * @return void
196 */
197 public static function createWidget( $id, $args = array() ) {
198 self::$args['widgets'][ $id ] = $args;
199 self::set_used_fields( $args );
200 }
201
202 /**
203 * Create sections.
204 *
205 * @param string $id The option ID.
206 * @param array $sections The sections array.
207 * @return void
208 */
209 public static function createSection( $id, $sections ) {
210 self::$args['sections'][ $id ][] = $sections;
211 self::set_used_fields( $sections );
212 }
213
214 /**
215 * Constants.
216 *
217 * @return void
218 */
219 public static function constants() {
220
221 // we need this path-finder code for set URL of framework.
222 $dirname = wp_normalize_path( dirname( dirname( __FILE__ ) ) );
223 $theme_dir = wp_normalize_path( get_parent_theme_file_path() );
224 $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
225 $located_plugin = ( preg_match( '#' . self::sanitize_dirname( $plugin_dir ) . '#', self::sanitize_dirname( $dirname ) ) ) ? true : false;
226 $directory = ( $located_plugin ) ? $plugin_dir : $theme_dir;
227 $directory_uri = ( $located_plugin ) ? SP_PC_URL : get_parent_theme_file_uri();
228 $foldername = str_replace( $directory, '', $dirname );
229 $protocol_uri = ( is_ssl() ) ? 'https' : 'http';
230 $directory_uri = set_url_scheme( $directory_uri, $protocol_uri );
231
232 self::$dir = $dirname;
233 self::$url = $directory_uri . $foldername;
234
235 }
236
237 /**
238 * Include plugin files.
239 *
240 * @param string $file Plugin files.
241 * @param boolean $load Load files.
242 *
243 * @return mixed
244 */
245 public static function include_plugin_file( $file, $load = true ) {
246
247 $path = '';
248 $file = ltrim( $file, '/' );
249 $override = apply_filters( 'spf_override', 'spf-override' );
250
251 if ( file_exists( get_parent_theme_file_path( $override . '/' . $file ) ) ) {
252 $path = get_parent_theme_file_path( $override . '/' . $file );
253 } elseif ( file_exists( get_theme_file_path( $override . '/' . $file ) ) ) {
254 $path = get_theme_file_path( $override . '/' . $file );
255 } elseif ( file_exists( self::$dir . '/' . $override . '/' . $file ) ) {
256 $path = self::$dir . '/' . $override . '/' . $file;
257 } elseif ( file_exists( self::$dir . '/' . $file ) ) {
258 $path = self::$dir . '/' . $file;
259 }
260
261 if ( ! empty( $path ) && ! empty( $file ) && $load ) {
262
263 global $wp_query;
264
265 if ( is_object( $wp_query ) && function_exists( 'load_template' ) ) {
266
267 load_template( $path, true );
268
269 } else {
270
271 require_once $path;
272
273 }
274 } else {
275
276 return self::$dir . '/' . $file;
277
278 }
279
280 }
281
282 /**
283 * Is plugin active.
284 *
285 * @param string $file The plugin file.
286 * @return boolean
287 */
288 public static function is_active_plugin( $file = '' ) {
289 return in_array( $file, (array) get_option( 'active_plugins', array() ) );
290 }
291
292 /**
293 * Sanitize dirname.
294 *
295 * @param string $dirname The directory name.
296 * @return string
297 */
298 public static function sanitize_dirname( $dirname ) {
299 return preg_replace( '/[^A-Za-z]/', '', $dirname );
300 }
301
302 /**
303 * General includes.
304 *
305 * @return void
306 */
307 public static function includes() {
308
309 // includes helpers.
310 self::include_plugin_file( 'functions/actions.php' );
311 self::include_plugin_file( 'functions/helpers.php' );
312 self::include_plugin_file( 'functions/sanitize.php' );
313 self::include_plugin_file( 'functions/validate.php' );
314
315 // includes free version classes.
316 self::include_plugin_file( 'classes/abstract.class.php' );
317 self::include_plugin_file( 'classes/fields.class.php' );
318 self::include_plugin_file( 'classes/options.class.php' );
319
320 // includes premium version classes.
321 if ( self::$premium ) {
322 self::include_plugin_file( 'classes/metabox.class.php' );
323
324 self::include_plugin_file( 'classes/widgets.class.php' );
325 }
326
327 }
328
329 /**
330 * Include field.
331 *
332 * @param string $type File type.
333 * @return void
334 */
335 public static function maybe_include_field( $type = '' ) {
336 if ( ! class_exists( 'SP_PC_Field_' . $type ) && class_exists( 'SP_PC_Fields' ) ) {
337 self::include_plugin_file( 'fields/' . $type . '/' . $type . '.php' );
338 }
339 }
340
341 /**
342 * Get all of fields.
343 *
344 * @param array $sections All the sections used in the plugin.
345 * @return void
346 */
347 public static function set_used_fields( $sections ) {
348
349 if ( ! empty( $sections['fields'] ) ) {
350
351 foreach ( $sections['fields'] as $field ) {
352
353 if ( ! empty( $field['fields'] ) ) {
354 self::set_used_fields( $field );
355 }
356
357 if ( ! empty( $field['tabs'] ) ) {
358 self::set_used_fields( array( 'fields' => $field['tabs'] ) );
359 }
360
361 if ( ! empty( $field['accordions'] ) ) {
362 self::set_used_fields( array( 'fields' => $field['accordions'] ) );
363 }
364
365 if ( ! empty( $field['type'] ) ) {
366 self::$fields[ $field['type'] ] = $field;
367 }
368 }
369 }
370
371 }
372
373 /**
374 * Enqueue admin and fields styles and scripts.
375 *
376 * @return void
377 */
378 public static function add_admin_enqueue_scripts() {
379 $current_screen = get_current_screen();
380 $the_current_post_type = $current_screen->post_type;
381 if ( 'sp_post_carousel' === $the_current_post_type ) {
382 // check for developer mode.
383 $min = ( apply_filters( 'spf_dev_mode', false ) || WP_DEBUG ) ? '' : '.min';
384 // admin utilities.
385 wp_enqueue_media();
386 // wp color picker.
387 wp_enqueue_style( 'wp-color-picker' );
388 wp_enqueue_script( 'wp-color-picker' );
389 // framework core styles.
390 wp_enqueue_style( 'spf', SP_PC_URL . 'admin/views/sp-framework/assets/css/spf.css', array(), SP_PC_VERSION, 'all' );
391 // rtl styles.
392 if ( is_rtl() ) {
393 wp_enqueue_style( 'spf-rtl', SP_PC_URL . 'admin/views/sp-framework/assets/css/spf-rtl' . $min . '.css', array(), SP_PC_VERSION, 'all' );
394 }
395
396 // framework core scripts.
397 wp_enqueue_script( 'spf-plugins', SP_PC_URL . 'admin/views/sp-framework/assets/js/spf-plugins' . $min . '.js', array(), SP_PC_VERSION, true );
398 wp_enqueue_script( 'spf', SP_PC_URL . 'admin/views/sp-framework/assets/js/spf' . $min . '.js', array( 'spf-plugins' ), SP_PC_VERSION, true );
399
400 wp_localize_script(
401 'spf',
402 'spf_vars',
403 array(
404 'previewJS' => esc_url( SP_PC_URL . 'public/assets/js/scripts' . $min . '.js' ),
405 'color_palette' => apply_filters( 'spf_color_palette', array() ),
406 'i18n' => array(
407 // global localize.
408 'confirm' => esc_html__( 'Are you sure?', 'post-carousel' ),
409 'reset_notification' => esc_html__( 'Restoring options.', 'post-carousel' ),
410 'import_notification' => esc_html__( 'Importing options.', 'post-carousel' ),
411
412 // chosen localize.
413 'typing_text' => esc_html__( 'Please enter %s or more characters', 'post-carousel' ),
414 'searching_text' => esc_html__( 'Searching...', 'post-carousel' ),
415 'no_results_text' => esc_html__( 'No results match', 'post-carousel' ),
416 ),
417 )
418 );
419
420 // load admin enqueue scripts and styles.
421 $enqueued = array();
422
423 if ( ! empty( self::$fields ) ) {
424 foreach ( self::$fields as $field ) {
425 if ( ! empty( $field['type'] ) ) {
426 $classname = 'SP_PC_Field_' . $field['type'];
427 self::maybe_include_field( $field['type'] );
428 if ( class_exists( $classname ) && method_exists( $classname, 'enqueue' ) ) {
429 $instance = new $classname( $field );
430 if ( method_exists( $classname, 'enqueue' ) ) {
431 $instance->enqueue();
432 }
433 unset( $instance );
434 }
435 }
436 }
437 }
438
439 do_action( 'spf_enqueue' );
440 } // Check screen ID.
441
442 }
443
444 /**
445 * Add a new framework field.
446 *
447 * @param array $field The fields array.
448 * @param string $value The field value.
449 * @param string $unique The unique string.
450 * @param string $where The position to show the fields.
451 * @param string $parent If the fields has parent.
452 * @return void
453 */
454 public static function field( $field = array(), $value = '', $unique = '', $where = '', $parent = '' ) {
455
456 // Check for unallow fields.
457 if ( ! empty( $field['_notice'] ) ) {
458
459 $field_type = $field['type'];
460
461 $field = array();
462 $field['content'] = sprintf( esc_html__( 'Ooops! This field type (%s) can not be used here, yet.', 'post-carousel' ), '<strong>' . $field_type . '</strong>' );
463 $field['type'] = 'notice';
464 $field['style'] = 'danger';
465
466 }
467
468 $depend = '';
469 $hidden = '';
470 $unique = ( ! empty( $unique ) ) ? $unique : '';
471 $class = ( ! empty( $field['class'] ) ) ? ' ' . $field['class'] : '';
472 $is_pseudo = ( ! empty( $field['pseudo'] ) ) ? ' spf-pseudo-field' : '';
473 $field_type = ( ! empty( $field['type'] ) ) ? $field['type'] : '';
474
475 if ( ! empty( $field['dependency'] ) ) {
476
477 $dependency = $field['dependency'];
478 $hidden = ' hidden';
479 $data_controller = '';
480 $data_condition = '';
481 $data_value = '';
482 $data_global = '';
483
484 if ( is_array( $dependency[0] ) ) {
485 $data_controller = implode( '|', array_column( $dependency, 0 ) );
486 $data_condition = implode( '|', array_column( $dependency, 1 ) );
487 $data_value = implode( '|', array_column( $dependency, 2 ) );
488 $data_global = implode( '|', array_column( $dependency, 3 ) );
489 } else {
490 $data_controller = ( ! empty( $dependency[0] ) ) ? $dependency[0] : '';
491 $data_condition = ( ! empty( $dependency[1] ) ) ? $dependency[1] : '';
492 $data_value = ( ! empty( $dependency[2] ) ) ? $dependency[2] : '';
493 $data_global = ( ! empty( $dependency[3] ) ) ? $dependency[3] : '';
494 }
495
496 $depend .= ' data-controller="' . $data_controller . '"';
497 $depend .= ' data-condition="' . $data_condition . '"';
498 $depend .= ' data-value="' . $data_value . '"';
499 $depend .= ( ! empty( $data_global ) ) ? ' data-depend-global="true"' : '';
500
501 }
502
503 if ( ! empty( $field_type ) ) {
504
505 echo '<div class="spf-field spf-field-' . esc_attr( $field_type ) . esc_attr( $is_pseudo ) . esc_attr( $class ) . esc_attr( $hidden ) . '"' . wp_kses_post( $depend ) . '>';
506
507 if ( ! empty( $field['title'] ) ) {
508 $subtitle = ( ! empty( $field['subtitle'] ) ) ? '<p class="spf-text-subtitle">' . $field['subtitle'] . '</p>' : '';
509 echo '<div class="spf-title"><h4>' . esc_html( $field['title'] ) . '</h4>' . wp_kses_post( $subtitle ) . '</div>';
510 }
511
512 echo ( ! empty( $field['title'] ) ) ? '<div class="spf-fieldset">' : '';
513
514 $value = ( ! isset( $value ) && isset( $field['default'] ) ) ? $field['default'] : $value;
515 $value = ( isset( $field['value'] ) ) ? $field['value'] : $value;
516
517 self::maybe_include_field( $field_type );
518
519 $classname = 'SP_PC_Field_' . $field_type;
520
521 if ( class_exists( $classname ) ) {
522 $instance = new $classname( $field, $value, $unique, $where, $parent );
523 $instance->render();
524 } else {
525 echo '<p>' . esc_html__( 'This field class is not available!', 'post-carousel' ) . '</p>';
526 }
527 } else {
528 echo '<p>' . esc_html__( 'This type is not found!', 'post-carousel' ) . '</p>';
529 }
530
531 echo ( ! empty( $field['title'] ) ) ? '</div>' : '';
532 echo '<div class="clear"></div>';
533 echo '</div>';
534
535 }
536
537 }
538
539 SP_PC::init();
540 }
541