PluginProbe
Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) / 1.4.9
Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) v1.4.9
2.1.3 2.1.2 2.1.1 2.1.0 2.0.4 2.0.3 2.0.2 2.0.1 2.0.0 1.5.5 1.5.4 1.5.3 1.5.2 1.5.1 1.5.0 trunk 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 All 57 releases
darkify / src / Admin / Framework / Classes / Darkify.php

Darkify.php in Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) 1.4.9, at src/Admin/Framework/Classes/Darkify.php

613 lines 19.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ThemeAtelier\Darkify\Admin\Framework\Classes;
4
5 if (! defined('ABSPATH')) {
6 die;
7 } // Cannot access directly.
8 /**
9 *
10 * Setup Class
11 *
12 * @since 1.0.0
13 * @version 1.0.0
14 *
15 */
16 if (! class_exists('Darkify')) {
17 class Darkify
18 {
19
20 // Default constants
21 public static $premium = true;
22 public static $version = '2.3.0';
23 public static $dir = '';
24 public static $url = '';
25 public static $css = '';
26 public static $file = '';
27 public static $enqueue = false;
28 public static $webfonts = array();
29 public static $subsets = array();
30 public static $inited = array();
31 public static $fields = array();
32 public static $args = array(
33 'admin_options' => array(),
34 'customize_options' => array(),
35 'metabox_options' => array(),
36 'nav_menu_options' => array(),
37 'profile_options' => array(),
38 'taxonomy_options' => array(),
39 'widget_options' => array(),
40 'comment_options' => array(),
41 'shortcode_options' => array(),
42 );
43
44 // Shortcode instances
45 public static $shortcode_instances = array();
46
47 private static $instance = null;
48
49 public static function init($file = __FILE__, $premium = true)
50 {
51
52 // Set file constant
53 self::$file = $file;
54
55 // Set file constant
56 self::$premium = $premium;
57
58 // Set constants
59 self::constants();
60
61 // Include files
62 self::includes();
63
64 if (is_null(self::$instance)) {
65 self::$instance = new self();
66 }
67
68 return self::$instance;
69 }
70
71 // Initalize
72 public function __construct()
73 {
74
75 // Init action
76 do_action('darkify_init');
77
78 add_action('after_setup_theme', array('ThemeAtelier\Darkify\Admin\Framework\Classes\Darkify', 'setup'));
79 add_action('init', array('ThemeAtelier\Darkify\Admin\Framework\Classes\Darkify', 'setup'));
80 add_action('switch_theme', array('ThemeAtelier\Darkify\Admin\Framework\Classes\Darkify', 'setup'));
81 add_action('admin_enqueue_scripts', array('ThemeAtelier\Darkify\Admin\Framework\Classes\Darkify', 'add_admin_enqueue_scripts'));
82 add_action('wp_head', array('ThemeAtelier\Darkify\Admin\Framework\Classes\Darkify', 'add_custom_css'), 80);
83 add_filter('admin_body_class', array('ThemeAtelier\Darkify\Admin\Framework\Classes\Darkify', 'add_admin_body_class'));
84 }
85
86 // Setup frameworks
87 public static function setup()
88 {
89
90 // Welcome
91 self::include_plugin_file('views/welcome.php');
92
93 // Setup admin option framework
94 $params = array();
95 if (class_exists('DarkifyOptions') && ! empty(self::$args['admin_options'])) {
96 foreach (self::$args['admin_options'] as $key => $value) {
97 if (! empty(self::$args['sections'][$key]) && ! isset(self::$inited[$key])) {
98
99 $params['args'] = $value;
100 $params['sections'] = self::$args['sections'][$key];
101 self::$inited[$key] = true;
102
103 \DarkifyOptions::instance($key, $params);
104
105 if (! empty($value['show_in_customizer'])) {
106 $value['output_css'] = false;
107 $value['enqueue_webfont'] = false;
108 self::$args['customize_options'][$key] = $value;
109 self::$inited[$key] = null;
110 }
111 }
112 }
113 }
114 }
115
116 // Create options
117 public static function createOptions($id, $args = array())
118 {
119 self::$args['admin_options'][$id] = $args;
120 }
121
122 // Create customize options
123 public static function createCustomizeOptions($id, $args = array())
124 {
125 self::$args['customize_options'][$id] = $args;
126 }
127
128 // Create metabox options
129 public static function createMetabox($id, $args = array())
130 {
131 self::$args['metabox_options'][$id] = $args;
132 }
133
134 // Create menu options
135 public static function createNavMenuOptions($id, $args = array())
136 {
137 self::$args['nav_menu_options'][$id] = $args;
138 }
139
140 // Create shortcoder options
141 public static function createShortcoder($id, $args = array())
142 {
143 self::$args['shortcode_options'][$id] = $args;
144 }
145
146 // Create taxonomy options
147 public static function createTaxonomyOptions($id, $args = array())
148 {
149 self::$args['taxonomy_options'][$id] = $args;
150 }
151
152 // Create profile options
153 public static function createProfileOptions($id, $args = array())
154 {
155 self::$args['profile_options'][$id] = $args;
156 }
157
158 // Create widget
159 public static function createWidget($id, $args = array())
160 {
161 self::$args['widget_options'][$id] = $args;
162 self::set_used_fields($args);
163 }
164
165 // Create comment metabox
166 public static function createCommentMetabox($id, $args = array())
167 {
168 self::$args['comment_options'][$id] = $args;
169 }
170
171 // Create section
172 public static function createSection($id, $sections)
173 {
174 self::$args['sections'][$id][] = $sections;
175 self::set_used_fields($sections);
176 }
177
178 // Set directory constants
179 public static function constants()
180 {
181
182 // We need this path-finder code for set URL of framework
183 $dirname = str_replace('//', '/', wp_normalize_path(dirname(dirname(self::$file))));
184 $theme_dir = str_replace('//', '/', wp_normalize_path(get_parent_theme_file_path()));
185 $plugin_dir = str_replace('//', '/', wp_normalize_path(WP_PLUGIN_DIR));
186 $plugin_dir = str_replace('/opt/bitnami', '/bitnami', $plugin_dir);
187 $located_plugin = (preg_match('#' . self::sanitize_dirname($plugin_dir) . '#', self::sanitize_dirname($dirname))) ? true : false;
188 $directory = ($located_plugin) ? $plugin_dir : $theme_dir;
189 $directory_uri = ($located_plugin) ? WP_PLUGIN_URL : get_parent_theme_file_uri();
190 $foldername = str_replace($directory, '', $dirname);
191 $protocol_uri = (is_ssl()) ? 'https' : 'http';
192 $directory_uri = set_url_scheme($directory_uri, $protocol_uri);
193
194 self::$dir = $dirname;
195 self::$url = $directory_uri . $foldername;
196 }
197
198 // Include file helper
199 public static function include_plugin_file($file, $load = true)
200 {
201
202 $path = '';
203 $file = ltrim($file, '/');
204 $override = apply_filters('darkify_override', 'darkify-override');
205
206 if (file_exists(get_parent_theme_file_path($override . '/' . $file))) {
207 $path = get_parent_theme_file_path($override . '/' . $file);
208 } elseif (file_exists(get_theme_file_path($override . '/' . $file))) {
209 $path = get_theme_file_path($override . '/' . $file);
210 } elseif (file_exists(self::$dir . '/' . $override . '/' . $file)) {
211 $path = self::$dir . '/' . $override . '/' . $file;
212 } elseif (file_exists(self::$dir . '/' . $file)) {
213 $path = self::$dir . '/' . $file;
214 }
215
216 if (! empty($path) && ! empty($file) && $load) {
217
218 global $wp_query;
219
220 if (is_object($wp_query) && function_exists('load_template')) {
221
222 load_template($path, true);
223 } else {
224
225 require_once($path);
226 }
227 } else {
228
229 return self::$dir . '/' . $file;
230 }
231 }
232
233 // Is active plugin helper
234 public static function is_active_plugin($file = '')
235 {
236 return in_array($file, (array) get_option('active_plugins', array()));
237 }
238
239 // Sanitize dirname
240 public static function sanitize_dirname($dirname)
241 {
242 return preg_replace('/[^A-Za-z]/', '', $dirname);
243 }
244
245 // Set url constant
246 public static function include_plugin_url($file)
247 {
248 return esc_url(self::$url) . '/' . ltrim($file, '/');
249 }
250
251 // Include files
252 public static function includes()
253 {
254
255 // Include common functions
256 self::include_plugin_file('functions/actions.php');
257 self::include_plugin_file('functions/helpers.php');
258 self::include_plugin_file('functions/sanitize.php');
259 self::include_plugin_file('functions/validate.php');
260
261 // Include free version Classes
262 self::include_plugin_file('Classes/abstract.class.php');
263 self::include_plugin_file('Classes/fields.class.php');
264 self::include_plugin_file('Classes/DarkifyOptions.php');
265 self::include_plugin_file('Classes/DarkifyMetabox.php');
266
267
268 // Include all framework fields
269 $fields = apply_filters('darkify_fields', array(
270 'accordion',
271 'background',
272 'backup',
273 'border',
274 'button_set',
275 'callback',
276 'checkbox',
277 'code_editor',
278 'color',
279 'color_group',
280 'content',
281 'date',
282 'datetime',
283 'dimensions',
284 'fieldset',
285 'gallery',
286 'group',
287 'heading',
288 'icon',
289 'image_select',
290 'link',
291 'link_color',
292 'license',
293 'media',
294 'notice',
295 'number',
296 'palette',
297 'radio',
298 'repeater',
299 'select',
300 'slider',
301 'sortable',
302 'sorter',
303 'spacing',
304 'spinner',
305 'subheading',
306 'submessage',
307 'switcher_shortcode',
308 'switcher',
309 'section_tab',
310 'section_inner_tab',
311 'tabbed',
312 'ta_help',
313 'text',
314 'textarea',
315 'upload',
316 'wp_editor',
317 ));
318
319 if (! empty($fields)) {
320 foreach ($fields as $field) {
321 if (! class_exists('DarkifyField_' . $field) && class_exists('DarkifyFields')) {
322 self::include_plugin_file('fields/' . $field . '/' . $field . '.php');
323 }
324 }
325 }
326 }
327
328 // Set all of used fields
329 public static function set_used_fields($sections)
330 {
331
332 if (! empty($sections['fields'])) {
333
334 foreach ($sections['fields'] as $field) {
335
336 if (! empty($field['fields'])) {
337 self::set_used_fields($field);
338 }
339
340 if (! empty($field['tabs'])) {
341 self::set_used_fields(array('fields' => $field['tabs']));
342 }
343 if (! empty($field['tabs'])) {
344 self::set_used_fields(array('fields' => $field['tabs']));
345 }
346
347 if (! empty($field['accordions'])) {
348 self::set_used_fields(array('fields' => $field['accordions']));
349 }
350
351 if (! empty($field['elements'])) {
352 self::set_used_fields(array('fields' => $field['elements']));
353 }
354
355 if (! empty($field['type'])) {
356 self::$fields[$field['type']] = $field;
357 }
358 }
359 }
360 }
361
362 // Enqueue admin and fields styles and scripts
363 public static function add_admin_enqueue_scripts()
364 {
365
366 if (! self::$enqueue) {
367
368 // Loads scripts and styles only when needed
369 $wpscreen = get_current_screen();
370
371 if (! empty(self::$args['admin_options'])) {
372 foreach (self::$args['admin_options'] as $argument) {
373 if (substr($wpscreen->id, -strlen($argument['menu_slug'])) === $argument['menu_slug']) {
374 self::$enqueue = true;
375 }
376 }
377 }
378
379 if (! empty(self::$args['metabox_options'])) {
380 foreach (self::$args['metabox_options'] as $argument) {
381 if (in_array($wpscreen->post_type, (array) $argument['post_type'])) {
382 self::$enqueue = true;
383 }
384 }
385 }
386
387 if (! empty(self::$args['taxonomy_options'])) {
388 foreach (self::$args['taxonomy_options'] as $argument) {
389 if (in_array($wpscreen->taxonomy, (array) $argument['taxonomy'])) {
390 self::$enqueue = true;
391 }
392 }
393 }
394
395 if (! empty(self::$shortcode_instances)) {
396 foreach (self::$shortcode_instances as $argument) {
397 if (($argument['show_in_editor'] && $wpscreen->base === 'post') || $argument['show_in_custom']) {
398 self::$enqueue = true;
399 }
400 }
401 }
402
403 if (! empty(self::$args['widget_options']) && ($wpscreen->id === 'widgets' || $wpscreen->id === 'customize')) {
404 self::$enqueue = true;
405 }
406
407 if (! empty(self::$args['customize_options']) && $wpscreen->id === 'customize') {
408 self::$enqueue = true;
409 }
410
411 if (! empty(self::$args['nav_menu_options']) && $wpscreen->id === 'nav-menus') {
412 self::$enqueue = true;
413 }
414
415 if (! empty(self::$args['profile_options']) && ($wpscreen->id === 'profile' || $wpscreen->id === 'user-edit')) {
416 self::$enqueue = true;
417 }
418
419 if (! empty(self::$args['comment_options']) && $wpscreen->id === 'comment') {
420 self::$enqueue = true;
421 }
422
423 if ($wpscreen->id === 'tools_page_darkify-welcome') {
424 self::$enqueue = true;
425 }
426 }
427
428 if (! apply_filters('darkify_enqueue_assets', self::$enqueue)) {
429 return;
430 }
431
432 // Admin utilities
433 wp_enqueue_media();
434
435 // Wp color picker
436 wp_enqueue_style('wp-color-picker');
437 wp_enqueue_script('wp-color-picker');
438
439 // Check for developer mode
440 $min = (self::$premium && SCRIPT_DEBUG) ? '' : '.min';
441
442 // Main style
443 if (is_rtl()) {
444 wp_enqueue_style('darkify-rtl', self::include_plugin_url('assets/css/style-rtl' . $min . '.css'), array(), self::$version, 'all');
445 } else {
446 wp_enqueue_style('darkify', self::include_plugin_url('assets/css/style' . $min . '.css'), array(), self::$version, 'all');
447 }
448
449 // IcoFont Css
450 wp_enqueue_style('icofont', self::include_plugin_url('assets/css/icofont' . $min . '.css'), array(), self::$version, 'all');
451
452 // Main scripts
453 wp_enqueue_script('darkify-plugins', self::include_plugin_url('assets/js/plugins' . $min . '.js'), array(), self::$version, true);
454 wp_enqueue_script('darkify', self::include_plugin_url('assets/js/main' . $min . '.js'), array('darkify-plugins'), self::$version, true);
455
456 // Main variables
457 wp_localize_script('darkify', 'darkify_vars', array(
458 'color_palette' => apply_filters('darkify_color_palette', array()),
459 'i18n' => array(
460 'confirm' => esc_html__('Are you sure?', 'darkify'),
461 'typing_text' => esc_html__('Please enter %s or more characters', 'darkify'),
462 'searching_text' => esc_html__('Searching...', 'darkify'),
463 'no_results_text' => esc_html__('No results found.', 'darkify'),
464 ),
465 'darkify_ajaxurl' => admin_url('admin-ajax.php'),
466 'nonce' => wp_create_nonce('save_options'),
467 ));
468
469 // Enqueue fields scripts and styles
470 $enqueued = array();
471
472 if (! empty(self::$fields)) {
473 foreach (self::$fields as $field) {
474 if (! empty($field['type'])) {
475 $classname = 'DarkifyField_' . $field['type'];
476 if (class_exists($classname) && method_exists($classname, 'enqueue')) {
477 $instance = new $classname($field);
478 if (method_exists($classname, 'enqueue')) {
479 $instance->enqueue();
480 }
481 unset($instance);
482 }
483 }
484 }
485 }
486
487 do_action('darkify_enqueue');
488 }
489
490 // Add admin body class
491 public static function add_admin_body_class($classes)
492 {
493
494 if (apply_filters('darkify_fa4', false)) {
495 $classes .= 'darkify-fa5-shims';
496 }
497
498 return $classes;
499 }
500
501 // Add custom css to front page
502 public static function add_custom_css()
503 {
504
505 if (! empty(self::$css)) {
506 echo '<style type="text/css">' . esc_html(wp_strip_all_tags(self::$css)) . '</style>';
507 }
508 }
509
510 // Add a new framework field
511 public static function field($field = array(), $value = '', $unique = '', $where = '', $parent = '')
512 {
513
514 // Check for unallow fields
515 if (! empty($field['_notice'])) {
516
517 $field_type = $field['type'];
518
519 $field = array();
520 $field['content'] = esc_html__('Oops! Not allowed.', 'darkify') . ' <strong>(' . $field_type . ')</strong>';
521 $field['type'] = 'notice';
522 $field['style'] = 'danger';
523 }
524
525 $depend = '';
526 $visible = '';
527 $unique = (! empty($unique)) ? $unique : '';
528 $class = (! empty($field['class'])) ? ' ' . esc_attr($field['class']) : '';
529 $is_pseudo = (! empty($field['pseudo'])) ? ' darkify-pseudo-field' : '';
530 $field_type = (! empty($field['type'])) ? esc_attr($field['type']) : '';
531
532 if (! empty($field['dependency'])) {
533
534 $dependency = $field['dependency'];
535 $depend_visible = '';
536 $data_controller = '';
537 $data_condition = '';
538 $data_value = '';
539 $data_global = '';
540
541 if (is_array($dependency[0])) {
542 $data_controller = implode('|', array_column($dependency, 0));
543 $data_condition = implode('|', array_column($dependency, 1));
544 $data_value = implode('|', array_column($dependency, 2));
545 $data_global = implode('|', array_column($dependency, 3));
546 $depend_visible = implode('|', array_column($dependency, 4));
547 } else {
548 $data_controller = (! empty($dependency[0])) ? $dependency[0] : '';
549 $data_condition = (! empty($dependency[1])) ? $dependency[1] : '';
550 $data_value = (! empty($dependency[2])) ? $dependency[2] : '';
551 $data_global = (! empty($dependency[3])) ? $dependency[3] : '';
552 $depend_visible = (! empty($dependency[4])) ? $dependency[4] : '';
553 }
554
555 $depend .= ' data-controller="' . esc_attr($data_controller) . '"';
556 $depend .= ' data-condition="' . esc_attr($data_condition) . '"';
557 $depend .= ' data-value="' . esc_attr($data_value) . '"';
558 $depend .= (! empty($data_global)) ? ' data-depend-global="true"' : '';
559
560 $visible = (! empty($depend_visible)) ? ' darkify-depend-visible' : ' darkify-depend-hidden';
561 }
562
563 // These attributes has been sanitized above.
564 echo '<div class="darkify-field darkify-field-' . esc_attr($field_type) . esc_attr($is_pseudo) . esc_attr($class) . esc_attr($visible) . '"' . wp_kses_post($depend) . '>';
565
566 if (! empty($field_type)) {
567
568 if (! empty($field['title'])) {
569 $title_help = '';
570 if (! empty($field['title_help']) || ! empty($field['title_video'])) {
571 $help_text = ! empty($field['title_help']) ? $field['title_help'] : '';
572 $icon_type = ! empty($field['title_video']) ? 'video_info.svg' : 'info.svg';
573 $video_help_attr = ! empty($field['title_video']) ? ' title-video-help' : '';
574 $title_help = sprintf('<span class="darkify-help darkify-title-help">
575 <span class="darkify-help-text' . esc_attr($video_help_attr) . '">%s</span>
576 <span class="tooltip-icon">
577 <img src="%s">
578 </span>
579 </span>', $help_text . (! empty($field['title_video']) ? $field['title_video'] : ''), self::include_plugin_url('assets/images/' . $icon_type));
580 }
581
582 echo '<div class="darkify-title">';
583 echo '<h4>' . wp_kses_post($field['title']) . $title_help . '</h4>'; // phpcs:ignore -- Attributes are escaped above.
584 echo (! empty($field['subtitle'])) ? '<div class="darkify-subtitle-text">' . wp_kses_post($field['subtitle']) . '</div>' : '';
585 echo '</div>';
586 }
587
588 echo (! empty($field['title'])) ? '<div class="darkify-fieldset">' : '';
589
590 $value = (! isset($value) && isset($field['default'])) ? $field['default'] : $value;
591 $value = (isset($field['value'])) ? $field['value'] : $value;
592
593 $classname = 'DarkifyField_' . $field_type;
594
595 if (class_exists($classname)) {
596 $instance = new $classname($field, $value, $unique, $where, $parent);
597 $instance->render();
598 } else {
599 echo '<p>' . esc_html__('Field not found!', 'darkify') . '</p>';
600 }
601 } else {
602 echo '<p>' . esc_html__('Field not found!', 'darkify') . '</p>';
603 }
604
605 echo (! empty($field['title'])) ? '</div>' : '';
606 echo '<div class="clear"></div>';
607 echo '</div>';
608 }
609 }
610 }
611
612 Darkify::init(__FILE__, true);
613