*/ class Darkify { /** * Plugin version * * @since 1.0.0 * @access protected * @var string */ protected $version; /** * Plugin slug * * @since 1.0.0 * @access protected * @var string */ protected $plugin_slug; /** * Main Loader. * * The loader that's responsible for maintaining and registering all hooks that empowers * the plugin. * * @since 1.0.0 * @access protected * @var object */ protected $loader; /** * Constructor for the Darkify class. * * Sets up all the appropriate hooks and actions within the plugin. */ public function __construct() { $this->version = DARKIFY_VERSION; $this->plugin_slug = 'darkify'; $this->load_dependencies(); $this->define_constants(); $this->define_admin_hooks(); $this->define_public_hooks(); $this->init_action(); add_action('plugin_loaded', array($this, 'init_plugin')); add_action('elementor/controls/register', [$this, 'register_controls']); add_action('elementor/widgets/register', array($this, 'register_darkify_widget')); add_action('init', [$this, 'create_darkify_block_init']); } /** * Run the loader to execute all of the hooks with WordPress. * * @since 1.0.0 */ public function run() { $this->loader->run(); } public function darkify_redirect_to($plugin) { if (DARKIFY_BASENAME === $plugin) { $redirect_url = esc_url(admin_url('admin.php?page=darkify')); exit(wp_kses_post(wp_safe_redirect($redirect_url))); } } /** * The slug of the plugin used to uniquely identify it within the context of * WordPress and to define internationalization functionality. * * @since 1.0.0 * @return string The name of the plugin. */ public function get_plugin_name() { return $this->plugin_slug; } /** * Retrieve the version number of the plugin. * * @since 1.0.0 * @return string The version number of the plugin. */ public function get_version() { return $this->version; } public function register_controls($controls_manager) { require_once DARKIFY_PATH . 'src/Admin/elementor/controls/class-elementor-control-switch.php'; $controls_manager->register_control( 'darkify_switch', new \Elementor_Darkify_Switcher() ); } public function register_darkify_widget($widgets_manager) { require_once DARKIFY_PATH . 'src/Admin/elementor/widgets/elementor-widget.php'; $widgets_manager->register(new \Elementor_Darkify()); } /** * Define the constants * * @return void */ public function define_constants() { define('DARKIFY_URL', plugins_url('', DARKIFY_FILE)); define('DARKIFY_ASSETS', DARKIFY_URL . '/src/assets/'); define('DARKIFY_ADMIN', DARKIFY_URL . '/src/Admin'); } /** * Load the plugin after all plugins are loaded. * * @return void */ public function init_plugin() { do_action('darkify_loaded'); load_plugin_textdomain('darkify', false, DARKIFY_DIR_NAME . "/languages"); } /** * Load the required dependencies for this plugin. * * Include the following files that make up the plugin: * * - Loader. Orchestrates the hooks of the plugin. * - Teamproi18n. Defines internationalization functionality. * - Admin. Defines all hooks for the admin area. * - Frontend. Defines all hooks for the public side of the site. * * Create an instance of the loader which will be used to register the hooks * with WordPress. * * @since 1.0.0 * @access private */ private function load_dependencies() { /** * The class responsible for orchestrating the actions and filters of the * core plugin. */ $this->loader = new Loader(); } /** * Register all of the hooks related to the admin dashboard functionality * of the plugin. * * @since 1.0.0 * @access private */ private function define_admin_hooks() { $plugin_admin = new Admin($this->get_plugin_name(), $this->get_version()); $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_styles'); $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts'); $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'admin_dequeue_for_specefic_pages', 999); } /** * Register all of the hooks related to the public-facing functionality * of the plugin. * * @since 1.0.0 * @access private */ private function define_public_hooks() { $plugin_public = new Frontend($this->get_plugin_name(), $this->get_version()); } public static function switcher($variant, $unique_id) { if ('classic' == $variant) { return ''; } else if ('expand' == $variant) { return ''; } else if ('inner-moon' === $variant) { return ''; } else if ('within' === $variant) { return ''; } elseif ('orbit' === $variant) { return ''; } } public static function switcher_wrapper($type, $unique_id, $variant, $switch_size, $position_class = '', $floating_switch_position = '', $tooltip_classes = '', $hide_dark_mode_on_mobile_screen = '', $floating_switch_width = '', $tooltip_on_floating_switch = '') { return '
' . self::switcher($variant, $unique_id) . '
' . ($type !== 'shortcode' && $floating_switch_width ? '' . esc_attr($tooltip_on_floating_switch) . '' : '') . '
'; } private function init_action() { add_action('activated_plugin', [$this, 'darkify_redirect_to']); } public function create_darkify_block_init() { register_block_type_from_metadata( DARKIFY_PATH . 'src/Admin/blocks/', array( 'render_callback' => [$this, 'render_darkify_block_callback'], ) ); wp_localize_script( 'darkify-switcher-editor-script', 'darkify', array( 'dir_url' => DARKIFY_DIR_URL, ) ); } /** * Render callback for the Darkify Pro block. */ public function render_darkify_block_callback($attributes) { $style = $attributes['style'] ?? 'classic'; $size = $attributes['size'] ?? '1.0'; $custom_size = $attributes['custom_size'] ?? 100; $bg_light = $attributes['switch_bg_light'] ?? '#121116'; $bg_dark = $attributes['switch_bg_dark'] ?? '#ffffff'; $light_icon_color = $attributes['light_icon_color'] ?? '#ffffff'; $dark_icon_color = $attributes['dark_icon_color'] ?? '#121116'; $light_text_color = $attributes['light_text_color'] ?? '#121116'; $dark_text_color = $attributes['dark_text_color'] ?? '#ffffff'; $border_width = $attributes['border_width'] ?? 0; $border_light_color = $attributes['border_light_color'] ?? '#121116'; $border_dark_color = $attributes['border_dark_color'] ?? '#ffffff'; $border_radius = $attributes['border_radius'] ?? 7; $alignment = $attributes['alignment'] ?? 'left'; // Size logic if ('custom' === $size) { $switcher_size = $custom_size; } else { $switcher_size = (float) $size * 100; } return '
' . do_shortcode(wp_sprintf( '[darkify switch="%s" icon_size="40px" light_mode_bg="%s" dark_mode_bg="%s" light_mode_color="%s" dark_mode_color="%s" border="%spx" border_light_color="%s" border_dark_color="%s" border_radius="%spx" text_light_mode_color="%s" text_dark_mode_color="%s" switch_size="%s"]', $style, $bg_light, $bg_dark, $light_icon_color, $dark_icon_color, $border_width, $border_light_color, $border_dark_color, $border_radius, $light_text_color, $dark_text_color, $switcher_size )) . '
'; } }