PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 2.4.2
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v2.4.2
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.2, at admin/views/sp-framework/classes/setup.class.php

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