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

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