| 1 |
<?php |
| 2 |
|
| 3 |
namespace ThemeAtelier\Darkify\Frontend; |
| 4 |
|
| 5 |
use ThemeAtelier\Darkify\Helpers\DarkifyExternalSupport; |
| 6 |
use ThemeAtelier\Darkify\Helpers\DarkifyUtils; |
| 7 |
|
| 8 |
/** |
| 9 |
* The public-facing functionality of the plugin. |
| 10 |
* |
| 11 |
* @link https://themeatelier.net |
| 12 |
* @since 1.0.0 |
| 13 |
* |
| 14 |
* @package Darkify |
| 15 |
* @subpackage Darkify/public |
| 16 |
*/ |
| 17 |
|
| 18 |
/** |
| 19 |
* The public-facing functionality of the plugin. |
| 20 |
* |
| 21 |
* Defines the plugin name, version, and two examples hooks for how to |
| 22 |
* enqueue the public-facing stylesheet and JavaScript. |
| 23 |
* |
| 24 |
* @package Darkify |
| 25 |
* @subpackage Darkify/public |
| 26 |
* @author ThemeAtelier <themeatelierbd@gmail.com> |
| 27 |
*/ |
| 28 |
class Frontend |
| 29 |
{ |
| 30 |
|
| 31 |
/** |
| 32 |
* The ID of this plugin. |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
* @access private |
| 36 |
* @var string $plugin_name The ID of this plugin. |
| 37 |
*/ |
| 38 |
private $plugin_name; |
| 39 |
|
| 40 |
/** |
| 41 |
* The version of this plugin. |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
* @access private |
| 45 |
* @var string $version The current version of this plugin. |
| 46 |
*/ |
| 47 |
private $version; |
| 48 |
|
| 49 |
public $utils; |
| 50 |
public $settings; |
| 51 |
public $external_support; |
| 52 |
public $unique_id; |
| 53 |
|
| 54 |
/** |
| 55 |
* Initialize the class and set its properties. |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
* @param string $plugin_name The name of the plugin. |
| 59 |
* @param string $version The version of this plugin. |
| 60 |
*/ |
| 61 |
public function __construct($plugin_name, $version) |
| 62 |
{ |
| 63 |
$this->plugin_name = $plugin_name; |
| 64 |
$this->version = $version; |
| 65 |
|
| 66 |
$this->utils = new DarkifyUtils($this); |
| 67 |
$this->external_support = new DarkifyExternalSupport($this); |
| 68 |
|
| 69 |
new DarkifyShortcode($this); |
| 70 |
|
| 71 |
if (function_exists('wp_rand')) { |
| 72 |
$this->unique_id = wp_rand(); |
| 73 |
} |
| 74 |
|
| 75 |
$options = get_option('darkify'); |
| 76 |
$show_in_menu = isset($options['show_in_menu']) ? $options['show_in_menu'] : ""; |
| 77 |
$enable_switcher_in_menu = isset($show_in_menu["enable_switch_in_menu"]) ? $show_in_menu["enable_switch_in_menu"] : false; |
| 78 |
|
| 79 |
if ($enable_switcher_in_menu) { |
| 80 |
add_filter('wp_nav_menu_items', array($this, 'darkify_switch_in_menu'), 10, 2); |
| 81 |
} |
| 82 |
|
| 83 |
add_action('wp_enqueue_scripts', array($this, 'darkify_client_enqueue'), 100); |
| 84 |
add_action('login_enqueue_scripts', array($this, 'darkify_client_enqueue')); |
| 85 |
add_action('register_enqueue_scripts', array($this, 'darkify_client_enqueue')); |
| 86 |
add_action('wp_head', array($this, 'darkify_client_header_script'), 1); |
| 87 |
add_action('login_head', array($this, 'darkify_client_header_script'), 1); |
| 88 |
add_action('register_head', array($this, 'darkify_client_header_script'), 1); |
| 89 |
add_action('wp_footer', array($this, 'darkify_client_footer_script')); |
| 90 |
add_action('login_footer', array($this, 'darkify_client_footer_script')); |
| 91 |
add_action('register_footer', array($this, 'darkify_client_footer_script')); |
| 92 |
|
| 93 |
add_filter('autoptimize_filter_js_exclude', array($this, 'darkify_exclude_js_from_cache_plugins')); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
public function darkify_exclude_js_from_cache_plugins($excludes) |
| 98 |
{ |
| 99 |
$excludes .= ',client_main.js,client_main.min.js'; |
| 100 |
return $excludes; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
public function darkify_client_enqueue() |
| 105 |
{ |
| 106 |
if ($this->darkify_is_dark_mode_allowed()) { |
| 107 |
$options = get_option('darkify'); |
| 108 |
$enable_dark_switcher = isset($options['enable_dark_switcher']) ? $options['enable_dark_switcher'] : 'classic'; |
| 109 |
|
| 110 |
$min = (apply_filters('darkify_dev_mode', false) || WP_DEBUG) ? '' : '.min'; |
| 111 |
wp_enqueue_style("theme-{$enable_dark_switcher}", DARKIFY_DIR_URL . 'src/assets/css/switcher/' . $enable_dark_switcher . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); |
| 112 |
|
| 113 |
wp_register_style('theme-around', DARKIFY_DIR_URL . 'src/assets/css/switcher/around' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); |
| 114 |
wp_register_style('theme-classic', DARKIFY_DIR_URL . 'src/assets/css/switcher/classic' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); |
| 115 |
wp_register_style('theme-dark-inner', DARKIFY_DIR_URL . 'src/assets/css/switcher/dark-inner' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); |
| 116 |
wp_register_style('theme-dark-side', DARKIFY_DIR_URL . 'src/assets/css/switcher/dark-side' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); |
| 117 |
wp_register_style('theme-eclipse', DARKIFY_DIR_URL . 'src/assets/css/switcher/eclipse' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); |
| 118 |
wp_register_style('theme-expand', DARKIFY_DIR_URL . 'src/assets/css/switcher/expand' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); |
| 119 |
wp_register_style('theme-half-sun', DARKIFY_DIR_URL . 'src/assets/css/switcher/half-sun' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); |
| 120 |
wp_register_style('theme-horizon', DARKIFY_DIR_URL . 'src/assets/css/switcher/horizon' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); |
| 121 |
wp_register_style('theme-inner-moon', DARKIFY_DIR_URL . 'src/assets/css/switcher/inner-moon' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); |
| 122 |
wp_register_style('theme-lightbulb', DARKIFY_DIR_URL . 'src/assets/css/switcher/lightbulb' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); |
| 123 |
wp_register_style('theme-simple', DARKIFY_DIR_URL . 'src/assets/css/switcher/simple' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); |
| 124 |
wp_register_style('theme-within', DARKIFY_DIR_URL . 'src/assets/css/switcher/within' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); |
| 125 |
wp_enqueue_style('darkify-client-main', DARKIFY_DIR_URL . 'src/assets/css/client_main' . $min . '.css', array(), DARKIFY_VERSION); |
| 126 |
wp_enqueue_script('darkify-client-main', DARKIFY_DIR_URL . 'src/assets/js/client_main' . $min . '.js', array('jquery'), DARKIFY_VERSION,false); |
| 127 |
} |
| 128 |
} |
| 129 |
public function darkify_is_dark_mode_allowed() |
| 130 |
{ |
| 131 |
$options = get_option('darkify'); |
| 132 |
/* Disable if Oxygen Builder is Opened */ |
| 133 |
if (isset($_GET['ct_builder'])) { |
| 134 |
if ($_GET['ct_builder'] == "true") { |
| 135 |
if (!isset($_GET['oxygen_iframe'])) { |
| 136 |
return False; |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return True; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
public function darkify_client_header_script() |
| 146 |
{ |
| 147 |
if ($this->darkify_is_dark_mode_allowed()) { |
| 148 |
include_once DarkifyUtils::darkify_locate_template('header_script.php'); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
public function darkify_client_footer_script() |
| 153 |
{ |
| 154 |
if ($this->darkify_is_dark_mode_allowed()) { |
| 155 |
include_once DarkifyUtils::darkify_locate_template('footer_script.php'); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
function darkify_switch_in_menu($items, $args) |
| 160 |
{ |
| 161 |
$options = get_option('darkify'); |
| 162 |
$show_in_menu = $options['show_in_menu']; |
| 163 |
$select_menus = isset($show_in_menu['select_menus']) ? $show_in_menu['select_menus'] : []; |
| 164 |
|
| 165 |
$last_shortcode = ''; |
| 166 |
|
| 167 |
if ($this->darkify_is_dark_mode_allowed() && isset($args->menu->term_id)) { |
| 168 |
foreach ($select_menus as $select_menu) { |
| 169 |
|
| 170 |
$switch_in_menu_location = $select_menu['switch_in_menu_location'] ?? ''; |
| 171 |
$darkify_menu_shortcode_helper = $select_menu['darkify_menu_shortcode_helper'] ?? ''; |
| 172 |
$menu_position = isset($select_menu['select_menu_position']) ? $select_menu['select_menu_position'] : 'after'; |
| 173 |
if ($args->menu->term_id == $switch_in_menu_location) { |
| 174 |
$last_shortcode = $darkify_menu_shortcode_helper; |
| 175 |
} |
| 176 |
} |
| 177 |
if ($last_shortcode) { |
| 178 |
$items = ($menu_position == 'before') ? '<li class="menu-item">' . do_shortcode($last_shortcode) . '</li>' . $items : $items . '<li class="menu-item">' . do_shortcode($last_shortcode) . '</li>'; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return $items; |
| 183 |
} |
| 184 |
} |
| 185 |
|