| 1 |
<?php |
| 2 |
// Exit if accessed directly. |
| 3 |
if (! defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Elementor Darkify widget. |
| 9 |
* |
| 10 |
* This class handles the creation of a custom Elementor widget |
| 11 |
* for displaying Switcher in the Darkify Pro plugin. |
| 12 |
* |
| 13 |
* @since 1.0 |
| 14 |
*/ |
| 15 |
class Elementor_Darkify extends \Elementor\Widget_Base |
| 16 |
{ |
| 17 |
|
| 18 |
/** |
| 19 |
* Get widget name. |
| 20 |
* |
| 21 |
* Returns the widget name for use in Elementor. |
| 22 |
* |
| 23 |
* @since 1.0 |
| 24 |
* @return string Widget name. |
| 25 |
*/ |
| 26 |
public function get_name() |
| 27 |
{ |
| 28 |
return 'darkify'; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get widget title. |
| 33 |
* |
| 34 |
* Returns the display title of the widget in Elementor. |
| 35 |
* |
| 36 |
* @since 1.0 |
| 37 |
* @return string Widget title. |
| 38 |
*/ |
| 39 |
public function get_title() |
| 40 |
{ |
| 41 |
return esc_html__('Dark Mode Switcher', 'darkify'); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get widget icon. |
| 46 |
* |
| 47 |
* Returns the icon for the widget that will appear in the Elementor editor. |
| 48 |
* |
| 49 |
* @since 1.0 |
| 50 |
* @return string Widget icon class. |
| 51 |
*/ |
| 52 |
public function get_icon() |
| 53 |
{ |
| 54 |
return 'eicon-adjust'; |
| 55 |
} |
| 56 |
|
| 57 |
public function get_style_depends() |
| 58 |
{ |
| 59 |
return ['theme-classic', 'theme-expand', 'theme-inner-moon', 'theme-within', 'theme-orbit']; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get widget categories. |
| 64 |
* |
| 65 |
* Defines the category or categories the widget belongs to in Elementor. |
| 66 |
* |
| 67 |
* @since 1.0 |
| 68 |
* @return array Widget categories. |
| 69 |
*/ |
| 70 |
public function get_categories() |
| 71 |
{ |
| 72 |
return ['basic']; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Every switch style, slug => display name, in the order the picker shows |
| 77 |
* them. The names are what the cards are labelled with: the picker used to |
| 78 |
* show artwork alone, which left "Dark Side", "Dark Inner" and "Half Sun" |
| 79 |
* to be told apart by eye. |
| 80 |
*/ |
| 81 |
private function darkify_style_labels() |
| 82 |
{ |
| 83 |
return [ |
| 84 |
'classic' => __('Classic', 'darkify'), |
| 85 |
'expand' => __('Expand', 'darkify'), |
| 86 |
'inner-moon' => __('Inner Moon', 'darkify'), |
| 87 |
'within' => __('Within', 'darkify'), |
| 88 |
'orbit' => __('Orbit', 'darkify'), |
| 89 |
'around' => __('Around', 'darkify'), |
| 90 |
'dark-side' => __('Dark Side', 'darkify'), |
| 91 |
'horizon' => __('Horizon', 'darkify'), |
| 92 |
'eclipse' => __('Eclipse', 'darkify'), |
| 93 |
'lightbulb' => __('Lightbulb', 'darkify'), |
| 94 |
'dark-inner' => __('Dark Inner', 'darkify'), |
| 95 |
'half-sun' => __('Half Sun', 'darkify'), |
| 96 |
'simple' => __('Simple', 'darkify'), |
| 97 |
'duality' => __('Duality', 'darkify'), |
| 98 |
'dual' => __('Dual', 'darkify'), |
| 99 |
'shift' => __('Shift', 'darkify'), |
| 100 |
]; |
| 101 |
} |
| 102 |
|
| 103 |
protected function _register_controls() |
| 104 |
{ |
| 105 |
$style_labels = $this->darkify_style_labels(); |
| 106 |
|
| 107 |
// ---------------------------------------- Switch Style ------------------------------ |
| 108 |
$this->start_controls_section( |
| 109 |
'darkify__settings', |
| 110 |
array( |
| 111 |
'label' => esc_html__('Switch Style', 'darkify'), |
| 112 |
'tab' => \Elementor\Controls_Manager::TAB_CONTENT, |
| 113 |
) |
| 114 |
); |
| 115 |
$this->add_control( |
| 116 |
'style', |
| 117 |
[ |
| 118 |
'type' => 'darkify_switch', |
| 119 |
'options' => array_keys($style_labels), |
| 120 |
'style_labels' => $style_labels, |
| 121 |
/* |
| 122 |
* How many of the styles above may be chosen, counted from the |
| 123 |
* start of the list; 0 would mean no limit. The control renders |
| 124 |
* the rest as locked Pro previews and marks their inputs |
| 125 |
* `disabled`. |
| 126 |
* |
| 127 |
* Sent from here rather than decided in the control's template |
| 128 |
* or its stylesheet: the old test was `switchId > 3` against a |
| 129 |
* slug, which is a NaN comparison and so never true, leaving a |
| 130 |
* `pointer-events: none` CSS rule as the only guard — and that |
| 131 |
* stops a mouse but not the keyboard, since the inputs are only |
| 132 |
* clip-hidden and stay focusable. |
| 133 |
*/ |
| 134 |
'unlocked' => 5, |
| 135 |
/* |
| 136 |
* Was `1`, which matches no option, so a fresh widget showed |
| 137 |
* nothing selected while still rendering Classic (the shortcode |
| 138 |
* maps the legacy `1` to it). The slug shows the selection and |
| 139 |
* renders exactly the same switch; widgets that already stored |
| 140 |
* `1` keep working through that same mapping. |
| 141 |
*/ |
| 142 |
'default' => 'classic', |
| 143 |
'id' => 'darkify-switch', |
| 144 |
'classes' => 'darkify-switch-style-elementor', |
| 145 |
'assets_url' => DARKIFY_DIR_URL . 'src/Admin/assets/images/', |
| 146 |
] |
| 147 |
); |
| 148 |
$this->end_controls_section(); |
| 149 |
|
| 150 |
// ---------------------------------------- Size & Shape ------------------------------ |
| 151 |
$this->start_controls_section( |
| 152 |
'darkify__size', |
| 153 |
array( |
| 154 |
'label' => esc_html__('Size & Shape', 'darkify'), |
| 155 |
'tab' => \Elementor\Controls_Manager::TAB_CONTENT, |
| 156 |
) |
| 157 |
); |
| 158 |
$this->add_control( |
| 159 |
'size', |
| 160 |
[ |
| 161 |
'label' => __('Switch Size', 'darkify'), |
| 162 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 163 |
'options' => [ |
| 164 |
'0.6' => __('XS', 'darkify'), |
| 165 |
'0.8' => __('S', 'darkify'), |
| 166 |
'1.0' => __('M', 'darkify'), |
| 167 |
'1.2' => __('L', 'darkify'), |
| 168 |
'1.4' => __('XL', 'darkify'), |
| 169 |
'1.6' => __('XXL', 'darkify'), |
| 170 |
'custom' => __('Custom', 'darkify'), |
| 171 |
], |
| 172 |
'default' => '1.0', |
| 173 |
] |
| 174 |
); |
| 175 |
$this->add_control( |
| 176 |
'custom_size', |
| 177 |
[ |
| 178 |
'label' => __('Custom Switch Size', 'darkify'), |
| 179 |
'type' => \Elementor\Controls_Manager::NUMBER, |
| 180 |
'min' => 40, |
| 181 |
'max' => 5000, |
| 182 |
'step' => 10, |
| 183 |
'default' => 100, |
| 184 |
// The value is divided by 100 into `--darkify-switch-scale`, so |
| 185 |
// 100 is the switch's natural size rather than a pixel measure. |
| 186 |
'description' => __('Percent of the default size.', 'darkify'), |
| 187 |
'condition' => array( |
| 188 |
'size' => 'custom', |
| 189 |
), |
| 190 |
] |
| 191 |
); |
| 192 |
$this->add_control( |
| 193 |
'switch_border_width', |
| 194 |
[ |
| 195 |
'label' => __('Border Width', 'darkify'), |
| 196 |
'type' => \Elementor\Controls_Manager::SLIDER, |
| 197 |
'size_units' => ['px'], |
| 198 |
'range' => [ |
| 199 |
'px' => [ |
| 200 |
'min' => 0, |
| 201 |
'max' => 10, |
| 202 |
], |
| 203 |
], |
| 204 |
'default' => [ |
| 205 |
'size' => 0, |
| 206 |
], |
| 207 |
'separator' => 'before', |
| 208 |
'selectors' => [ |
| 209 |
'{{WRAPPER}} .darkify_switch_style' => 'border-width: {{SIZE}}{{UNIT}};', |
| 210 |
], |
| 211 |
] |
| 212 |
); |
| 213 |
$this->add_control( |
| 214 |
'switch_border_radius', |
| 215 |
[ |
| 216 |
'label' => __('Border Radius', 'darkify'), |
| 217 |
'type' => \Elementor\Controls_Manager::SLIDER, |
| 218 |
'size_units' => ['px'], |
| 219 |
'range' => [ |
| 220 |
'px' => [ |
| 221 |
'min' => 0, |
| 222 |
'max' => 100, |
| 223 |
], |
| 224 |
], |
| 225 |
'default' => [ |
| 226 |
'size' => 7, |
| 227 |
], |
| 228 |
'selectors' => [ |
| 229 |
'{{WRAPPER}} .darkify_switch_style' => 'border-radius: {{SIZE}}{{UNIT}};', |
| 230 |
], |
| 231 |
] |
| 232 |
); |
| 233 |
$this->add_responsive_control( |
| 234 |
'align', |
| 235 |
[ |
| 236 |
'label' => __('Alignment', 'darkify'), |
| 237 |
'type' => \Elementor\Controls_Manager::CHOOSE, |
| 238 |
'options' => [ |
| 239 |
'left' => [ |
| 240 |
'title' => __('Left', 'darkify'), |
| 241 |
'icon' => 'eicon-text-align-left', |
| 242 |
], |
| 243 |
'center' => [ |
| 244 |
'title' => __('Center', 'darkify'), |
| 245 |
'icon' => 'eicon-text-align-center', |
| 246 |
], |
| 247 |
'right' => [ |
| 248 |
'title' => __('Right', 'darkify'), |
| 249 |
'icon' => 'eicon-text-align-right', |
| 250 |
], |
| 251 |
], |
| 252 |
'toggle' => true, |
| 253 |
'default' => 'left', |
| 254 |
'separator' => 'before', |
| 255 |
'selectors' => [ |
| 256 |
'{{WRAPPER}}' => 'display: flex; justify-content: {{VALUE}};', |
| 257 |
], |
| 258 |
] |
| 259 |
); |
| 260 |
$this->end_controls_section(); |
| 261 |
|
| 262 |
// ---------------------------------------- Colors ------------------------------ |
| 263 |
/* |
| 264 |
* The colours come in light/dark pairs — background, icon and border — |
| 265 |
* and used to be six controls in a flat stack, each labelled "Light |
| 266 |
* Mode" or "Dark Mode" under its own heading, so the two halves of one |
| 267 |
* part sat apart while unrelated parts sat together. Elementor's own |
| 268 |
* tabs put one mode on screen at a time and let each control be named |
| 269 |
* for the part it colours, which is the same grouping the block editor |
| 270 |
* panel uses. Control names, defaults, conditions and selectors are |
| 271 |
* unchanged, so saved widgets and the CSS they emit are untouched. |
| 272 |
*/ |
| 273 |
$this->start_controls_section( |
| 274 |
'darkify__colors', |
| 275 |
array( |
| 276 |
'label' => esc_html__('Colors', 'darkify'), |
| 277 |
'tab' => \Elementor\Controls_Manager::TAB_CONTENT, |
| 278 |
) |
| 279 |
); |
| 280 |
$this->start_controls_tabs('darkify_color_modes'); |
| 281 |
|
| 282 |
$this->start_controls_tab( |
| 283 |
'darkify_colors_light', |
| 284 |
['label' => esc_html__('Light Mode', 'darkify')] |
| 285 |
); |
| 286 |
$this->add_control( |
| 287 |
'switch_bg_light', |
| 288 |
[ |
| 289 |
'label' => __('Background', 'darkify'), |
| 290 |
'type' => \Elementor\Controls_Manager::COLOR, |
| 291 |
'default' => '#121116', |
| 292 |
'selectors' => [ |
| 293 |
'{{WRAPPER}} .darkify_switch_style, {{WRAPPER}} .switch-shift .theme-toggle .toggle-thumb' => 'background-color: {{VALUE}};', |
| 294 |
] |
| 295 |
] |
| 296 |
); |
| 297 |
$this->add_control( |
| 298 |
'switch_icon_light', |
| 299 |
[ |
| 300 |
'label' => __('Icon', 'darkify'), |
| 301 |
'type' => \Elementor\Controls_Manager::COLOR, |
| 302 |
'default' => '#ffffff', |
| 303 |
'condition' => array( |
| 304 |
'style!' => ['duality', 'shift', 'dual'], |
| 305 |
), |
| 306 |
'selectors' => [ |
| 307 |
'{{WRAPPER}} .darkify_switch_style svg' => 'color: {{VALUE}}; stroke: {{VALUE}};', |
| 308 |
'{{WRAPPER}} .darkify_switch_style.switch-orbit .theme-toggle svg' => 'fill: {{VALUE}};', |
| 309 |
], |
| 310 |
] |
| 311 |
); |
| 312 |
$this->add_control( |
| 313 |
'switch_border_light', |
| 314 |
[ |
| 315 |
'label' => __('Border', 'darkify'), |
| 316 |
'type' => \Elementor\Controls_Manager::COLOR, |
| 317 |
'default' => '#121116', |
| 318 |
'selectors' => [ |
| 319 |
'{{WRAPPER}} .darkify_switch_style' => 'border-color: {{VALUE}};', |
| 320 |
], |
| 321 |
] |
| 322 |
); |
| 323 |
$this->end_controls_tab(); |
| 324 |
|
| 325 |
$this->start_controls_tab( |
| 326 |
'darkify_colors_dark', |
| 327 |
['label' => esc_html__('Dark Mode', 'darkify')] |
| 328 |
); |
| 329 |
$this->add_control( |
| 330 |
'switch_bg_dark', |
| 331 |
[ |
| 332 |
'label' => __('Background', 'darkify'), |
| 333 |
'type' => \Elementor\Controls_Manager::COLOR, |
| 334 |
'default' => '#ffffff', |
| 335 |
'selectors' => [ |
| 336 |
'.darkify_dark_mode_enabled {{WRAPPER}} .darkify_switch_style, .darkify_dark_mode_enabled {{WRAPPER}} .switch-shift .theme-toggle .toggle-thumb' => 'background-color: {{VALUE}};', |
| 337 |
], |
| 338 |
] |
| 339 |
); |
| 340 |
$this->add_control( |
| 341 |
'switch_icon_dark', |
| 342 |
[ |
| 343 |
'label' => __('Icon', 'darkify'), |
| 344 |
'type' => \Elementor\Controls_Manager::COLOR, |
| 345 |
'default' => '#121116', |
| 346 |
'condition' => array( |
| 347 |
'style!' => ['duality', 'shift', 'dual'], |
| 348 |
), |
| 349 |
'selectors' => [ |
| 350 |
'.darkify_dark_mode_enabled {{WRAPPER}} .darkify_switch_style svg' => 'color: {{VALUE}}; stroke: {{VALUE}};', |
| 351 |
'{{WRAPPER}} .darkify_switch_style.switch-orbit .theme-toggle .toggle-sun svg' => 'fill: {{VALUE}};', |
| 352 |
], |
| 353 |
] |
| 354 |
); |
| 355 |
$this->add_control( |
| 356 |
'switch_border_dark', |
| 357 |
[ |
| 358 |
'label' => __('Border', 'darkify'), |
| 359 |
'type' => \Elementor\Controls_Manager::COLOR, |
| 360 |
'default' => '#ffffff', |
| 361 |
'selectors' => [ |
| 362 |
'.darkify_dark_mode_enabled {{WRAPPER}} .darkify_switch_style' => 'border-color: {{VALUE}};', |
| 363 |
], |
| 364 |
] |
| 365 |
); |
| 366 |
$this->end_controls_tab(); |
| 367 |
|
| 368 |
$this->end_controls_tabs(); |
| 369 |
$this->end_controls_section(); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
protected function render() |
| 374 |
{ |
| 375 |
$settings = $this->get_settings_for_display(); |
| 376 |
|
| 377 |
$style = isset($settings['style']) ? $settings['style'] : 1; |
| 378 |
$size = isset($settings['size']) ? $settings['size'] : 1; |
| 379 |
$custom_size = isset($settings['custom_size']) ? $settings['custom_size'] : 100; |
| 380 |
if ('custom' === $size) { |
| 381 |
$switcher_size = $custom_size; |
| 382 |
} else { |
| 383 |
$switcher_size = $size * 100; |
| 384 |
} |
| 385 |
|
| 386 |
echo do_shortcode(wp_sprintf('[darkify switch="%s" icon_size="40px" light_mode_bg="#121116" dark_mode_bg="#ffffff" light_mode_color="#ffffff" dark_mode_color="#121116" border="0px" border_light_color="#121119" border_dark_color="#ffffff" border_radius="7px" switch_size="%s"]', esc_attr($style), esc_attr($switcher_size))); |
| 387 |
} |
| 388 |
} |
| 389 |
|