PluginProbe
Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) / 1.4.23
Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) v1.4.23
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.23, at src/Admin/Framework/Classes/Darkify.php

616 lines 19.8 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_shortcode_v2',
309 'switcher',
310 'section_tab',
311 'section_inner_tab',
312 'tabbed',
313 'ta_help',
314 'text',
315 'textarea',
316 'upload',
317 'wp_editor',
318 ));
319
320 if (! empty($fields)) {
321 foreach ($fields as $field) {
322 if (! class_exists('DarkifyField_' . $field) && class_exists('DarkifyFields')) {
323 self::include_plugin_file('fields/' . $field . '/' . $field . '.php');
324 }
325 }
326 }
327 }
328
329 // Set all of used fields
330 public static function set_used_fields($sections)
331 {
332
333 if (! empty($sections['fields'])) {
334
335 foreach ($sections['fields'] as $field) {
336
337 if (! empty($field['fields'])) {
338 self::set_used_fields($field);
339 }
340
341 if (! empty($field['tabs'])) {
342 self::set_used_fields(array('fields' => $field['tabs']));
343 }
344 if (! empty($field['tabs'])) {
345 self::set_used_fields(array('fields' => $field['tabs']));
346 }
347
348 if (! empty($field['accordions'])) {
349 self::set_used_fields(array('fields' => $field['accordions']));
350 }
351
352 if (! empty($field['elements'])) {
353 self::set_used_fields(array('fields' => $field['elements']));
354 }
355
356 if (! empty($field['type'])) {
357 self::$fields[$field['type']] = $field;
358 }
359 }
360 }
361 }
362
363 // Enqueue admin and fields styles and scripts
364 public static function add_admin_enqueue_scripts()
365 {
366
367 if (! self::$enqueue) {
368
369 // Loads scripts and styles only when needed
370 $wpscreen = get_current_screen();
371
372 if (! empty(self::$args['admin_options'])) {
373 foreach (self::$args['admin_options'] as $argument) {
374 if (substr($wpscreen->id, -strlen($argument['menu_slug'])) === $argument['menu_slug']) {
375 self::$enqueue = true;
376 }
377 }
378 }
379
380 if (! empty(self::$args['metabox_options'])) {
381 foreach (self::$args['metabox_options'] as $argument) {
382 if (in_array($wpscreen->post_type, (array) $argument['post_type'])) {
383 self::$enqueue = true;
384 }
385 }
386 }
387
388 if (! empty(self::$args['taxonomy_options'])) {
389 foreach (self::$args['taxonomy_options'] as $argument) {
390 if (in_array($wpscreen->taxonomy, (array) $argument['taxonomy'])) {
391 self::$enqueue = true;
392 }
393 }
394 }
395
396 if (! empty(self::$shortcode_instances)) {
397 foreach (self::$shortcode_instances as $argument) {
398 if (($argument['show_in_editor'] && $wpscreen->base === 'post') || $argument['show_in_custom']) {
399 self::$enqueue = true;
400 }
401 }
402 }
403
404 if (! empty(self::$args['widget_options']) && ($wpscreen->id === 'widgets' || $wpscreen->id === 'customize')) {
405 self::$enqueue = true;
406 }
407
408 if (! empty(self::$args['customize_options']) && $wpscreen->id === 'customize') {
409 self::$enqueue = true;
410 }
411
412 if (! empty(self::$args['nav_menu_options']) && $wpscreen->id === 'nav-menus') {
413 self::$enqueue = true;
414 }
415
416 if (! empty(self::$args['profile_options']) && ($wpscreen->id === 'profile' || $wpscreen->id === 'user-edit')) {
417 self::$enqueue = true;
418 }
419
420 if (! empty(self::$args['comment_options']) && $wpscreen->id === 'comment') {
421 self::$enqueue = true;
422 }
423
424 if ($wpscreen->id === 'tools_page_darkify-welcome') {
425 self::$enqueue = true;
426 }
427 }
428
429 if (! apply_filters('darkify_enqueue_assets', self::$enqueue)) {
430 return;
431 }
432
433 // Admin utilities
434 wp_enqueue_media();
435
436 // Wp color picker
437 wp_enqueue_style('wp-color-picker');
438 wp_enqueue_script('wp-color-picker');
439
440 // Check for developer mode
441 $min = (self::$premium && SCRIPT_DEBUG) ? '' : '.min';
442
443 // Main style
444 if (is_rtl()) {
445 wp_enqueue_style('darkify-rtl', self::include_plugin_url('assets/css/style-rtl' . $min . '.css'), array(), self::$version, 'all');
446 } else {
447 wp_enqueue_style('darkify', self::include_plugin_url('assets/css/style' . $min . '.css'), array(), self::$version, 'all');
448 }
449
450 // IcoFont Css
451 wp_enqueue_style('icofont', self::include_plugin_url('assets/css/icofont' . $min . '.css'), array(), self::$version, 'all');
452
453 // Main scripts
454 wp_enqueue_script('darkify-plugins', self::include_plugin_url('assets/js/plugins' . $min . '.js'), array(), self::$version, true);
455 wp_enqueue_script('darkify', self::include_plugin_url('assets/js/main' . $min . '.js'), array('darkify-plugins'), self::$version, true);
456
457 // Main variables
458 wp_localize_script('darkify', 'darkify_vars', array(
459 'color_palette' => apply_filters('darkify_color_palette', array()),
460 'i18n' => array(
461 'confirm' => esc_html__('Are you sure?', 'darkify'),
462
463 /* translators: %s: modify typing text. */
464 'typing_text' => esc_html__('Please enter %s or more characters', 'darkify'),
465 'searching_text' => esc_html__('Searching...', 'darkify'),
466 'no_results_text' => esc_html__('No results found.', 'darkify'),
467 ),
468 'darkify_ajaxurl' => admin_url('admin-ajax.php'),
469 'nonce' => wp_create_nonce('save_options'),
470 ));
471
472 // Enqueue fields scripts and styles
473 $enqueued = array();
474
475 if (! empty(self::$fields)) {
476 foreach (self::$fields as $field) {
477 if (! empty($field['type'])) {
478 $classname = 'DarkifyField_' . $field['type'];
479 if (class_exists($classname) && method_exists($classname, 'enqueue')) {
480 $instance = new $classname($field);
481 if (method_exists($classname, 'enqueue')) {
482 $instance->enqueue();
483 }
484 unset($instance);
485 }
486 }
487 }
488 }
489
490 do_action('darkify_enqueue');
491 }
492
493 // Add admin body class
494 public static function add_admin_body_class($classes)
495 {
496
497 if (apply_filters('darkify_fa4', false)) {
498 $classes .= 'darkify-fa5-shims';
499 }
500
501 return $classes;
502 }
503
504 // Add custom css to front page
505 public static function add_custom_css()
506 {
507
508 if (! empty(self::$css)) {
509 echo '<style type="text/css">' . esc_html(wp_strip_all_tags(self::$css)) . '</style>';
510 }
511 }
512
513 // Add a new framework field
514 public static function field($field = array(), $value = '', $unique = '', $where = '', $parent = '')
515 {
516
517 // Check for unallow fields
518 if (! empty($field['_notice'])) {
519
520 $field_type = $field['type'];
521
522 $field = array();
523 $field['content'] = esc_html__('Oops! Not allowed.', 'darkify') . ' <strong>(' . $field_type . ')</strong>';
524 $field['type'] = 'notice';
525 $field['style'] = 'danger';
526 }
527
528 $depend = '';
529 $visible = '';
530 $unique = (! empty($unique)) ? $unique : '';
531 $class = (! empty($field['class'])) ? ' ' . esc_attr($field['class']) : '';
532 $is_pseudo = (! empty($field['pseudo'])) ? ' darkify-pseudo-field' : '';
533 $field_type = (! empty($field['type'])) ? esc_attr($field['type']) : '';
534
535 if (! empty($field['dependency'])) {
536
537 $dependency = $field['dependency'];
538 $depend_visible = '';
539 $data_controller = '';
540 $data_condition = '';
541 $data_value = '';
542 $data_global = '';
543
544 if (is_array($dependency[0])) {
545 $data_controller = implode('|', array_column($dependency, 0));
546 $data_condition = implode('|', array_column($dependency, 1));
547 $data_value = implode('|', array_column($dependency, 2));
548 $data_global = implode('|', array_column($dependency, 3));
549 $depend_visible = implode('|', array_column($dependency, 4));
550 } else {
551 $data_controller = (! empty($dependency[0])) ? $dependency[0] : '';
552 $data_condition = (! empty($dependency[1])) ? $dependency[1] : '';
553 $data_value = (! empty($dependency[2])) ? $dependency[2] : '';
554 $data_global = (! empty($dependency[3])) ? $dependency[3] : '';
555 $depend_visible = (! empty($dependency[4])) ? $dependency[4] : '';
556 }
557
558 $depend .= ' data-controller="' . esc_attr($data_controller) . '"';
559 $depend .= ' data-condition="' . esc_attr($data_condition) . '"';
560 $depend .= ' data-value="' . esc_attr($data_value) . '"';
561 $depend .= (! empty($data_global)) ? ' data-depend-global="true"' : '';
562
563 $visible = (! empty($depend_visible)) ? ' darkify-depend-visible' : ' darkify-depend-hidden';
564 }
565
566 // These attributes has been sanitized above.
567 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) . '>';
568
569 if (! empty($field_type)) {
570
571 if (! empty($field['title'])) {
572 $title_help = '';
573 if (! empty($field['title_help']) || ! empty($field['title_video'])) {
574 $help_text = ! empty($field['title_help']) ? $field['title_help'] : '';
575 $icon_type = ! empty($field['title_video']) ? 'video_info.svg' : 'info.svg';
576 $video_help_attr = ! empty($field['title_video']) ? ' title-video-help' : '';
577 $title_help = sprintf('<span class="darkify-help darkify-title-help">
578 <span class="darkify-help-text' . esc_attr($video_help_attr) . '">%s</span>
579 <span class="tooltip-icon">
580 <img src="%s">
581 </span>
582 </span>', $help_text . (! empty($field['title_video']) ? $field['title_video'] : ''), self::include_plugin_url('assets/images/' . $icon_type));
583 }
584
585 echo '<div class="darkify-title">';
586 echo '<h4>' . wp_kses_post($field['title']) . $title_help . '</h4>'; // phpcs:ignore -- Attributes are escaped above.
587 echo (! empty($field['subtitle'])) ? '<div class="darkify-subtitle-text">' . wp_kses_post($field['subtitle']) . '</div>' : '';
588 echo '</div>';
589 }
590
591 echo (! empty($field['title'])) ? '<div class="darkify-fieldset">' : '';
592
593 $value = (! isset($value) && isset($field['default'])) ? $field['default'] : $value;
594 $value = (isset($field['value'])) ? $field['value'] : $value;
595
596 $classname = 'DarkifyField_' . $field_type;
597
598 if (class_exists($classname)) {
599 $instance = new $classname($field, $value, $unique, $where, $parent);
600 $instance->render();
601 } else {
602 echo '<p>' . esc_html__('Field not found!', 'darkify') . '</p>';
603 }
604 } else {
605 echo '<p>' . esc_html__('Field not found!', 'darkify') . '</p>';
606 }
607
608 echo (! empty($field['title'])) ? '</div>' : '';
609 echo '<div class="clear"></div>';
610 echo '</div>';
611 }
612 }
613 }
614
615 Darkify::init(__FILE__, true);
616