EAEL_Background.php
1 year ago
EAEL_Choose.php
8 months ago
EAEL_Gradient_Text.php
6 days ago
Select2.php
4 months ago
index.php
3 years ago
EAEL_Gradient_Text.php
301 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Essential_Addons_Elementor\Controls; |
| 4 | |
| 5 | use Elementor\Controls_Manager; |
| 6 | use Elementor\Group_Control_Base; |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * EAEL Gradient Text group control. |
| 14 | * |
| 15 | * A self-contained group control that provides both a classic single-color |
| 16 | * picker and a full gradient builder for text, selected via an internal |
| 17 | * `color_type` CHOOSE field. |
| 18 | * |
| 19 | * Classic mode → outputs `color: {{VALUE}}` only. |
| 20 | * Gradient mode → outputs `background-image`, `-webkit-background-clip`, |
| 21 | * `background-clip: text`, and `color: transparent`. |
| 22 | * The first color (`color`) also emits `color: {{VALUE}}` |
| 23 | * as the automatic browser fallback (no separate field needed). |
| 24 | * |
| 25 | * Default behaviour (nothing passed in `fields_options`): |
| 26 | * classic mode, single color picker only. |
| 27 | * |
| 28 | * To default to gradient mode pass via `fields_options`: |
| 29 | * 'color_type' => [ 'default' => 'gradient' ] |
| 30 | * |
| 31 | * Usage: |
| 32 | * |
| 33 | * // Classic default (single color) |
| 34 | * $this->add_group_control( |
| 35 | * \Essential_Addons_Elementor\Controls\EAEL_Gradient_Text::get_type(), |
| 36 | * [ |
| 37 | * 'name' => 'my_text', |
| 38 | * 'selector' => '{{WRAPPER}} .my-el', |
| 39 | * 'fields_options' => [ |
| 40 | * 'color' => [ 'default' => '#4d4d4d' ], |
| 41 | * ], |
| 42 | * ] |
| 43 | * ); |
| 44 | * |
| 45 | * // Gradient default |
| 46 | * $this->add_group_control( |
| 47 | * \Essential_Addons_Elementor\Controls\EAEL_Gradient_Text::get_type(), |
| 48 | * [ |
| 49 | * 'name' => 'my_text', |
| 50 | * 'selector' => '{{WRAPPER}} .my-el', |
| 51 | * 'fields_options' => [ |
| 52 | * 'color_type' => [ 'default' => 'gradient' ], |
| 53 | * 'color' => [ 'default' => '#4d4d4d' ], |
| 54 | * 'color_b' => [ 'default' => '#4d4d4d' ], |
| 55 | * ], |
| 56 | * ] |
| 57 | * ); |
| 58 | * |
| 59 | * @since 6.6.0 |
| 60 | */ |
| 61 | class EAEL_Gradient_Text extends Group_Control_Base { |
| 62 | |
| 63 | /** |
| 64 | * Holds the control fields. |
| 65 | * |
| 66 | * @since 6.6.0 |
| 67 | * @access protected |
| 68 | * @static |
| 69 | * |
| 70 | * @var array |
| 71 | */ |
| 72 | protected static $fields; |
| 73 | |
| 74 | /** |
| 75 | * Return the unique type key for this group control. |
| 76 | * |
| 77 | * @since 6.6.0 |
| 78 | * @access public |
| 79 | * @static |
| 80 | * |
| 81 | * @return string |
| 82 | */ |
| 83 | public static function get_type() { |
| 84 | return 'eael-gradient-text'; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Initialize the control fields. |
| 89 | * |
| 90 | * @since 6.6.0 |
| 91 | * @access public |
| 92 | * |
| 93 | * @return array |
| 94 | */ |
| 95 | public function init_fields() { |
| 96 | $fields = []; |
| 97 | |
| 98 | // ── Color type picker ───────────────────────────────────────────────── |
| 99 | // Drives classic/gradient visibility. Default is 'classic' so the control |
| 100 | // behaves as a plain color picker unless opted into gradient mode. |
| 101 | $fields['color_type'] = [ |
| 102 | 'label' => esc_html__( 'Color Type', 'essential-addons-for-elementor-lite' ), |
| 103 | 'type' => Controls_Manager::CHOOSE, |
| 104 | 'options' => [ |
| 105 | 'classic' => [ |
| 106 | 'title' => esc_html__( 'Classic', 'essential-addons-for-elementor-lite' ), |
| 107 | 'icon' => 'eicon-paint-brush', |
| 108 | ], |
| 109 | 'gradient' => [ |
| 110 | 'title' => esc_html__( 'Gradient', 'essential-addons-for-elementor-lite' ), |
| 111 | 'icon' => 'eicon-barcode', |
| 112 | ], |
| 113 | ], |
| 114 | 'default' => 'classic', |
| 115 | 'render_type' => 'ui', |
| 116 | ]; |
| 117 | |
| 118 | // ── First / only color ──────────────────────────────────────────────── |
| 119 | // Shown in both modes. |
| 120 | // - Classic: its `color: {{VALUE}}` selector is the sole CSS output. |
| 121 | // - Gradient: acts as the first gradient stop AND as the automatic |
| 122 | // browser fallback (overridden by `color: transparent` in modern |
| 123 | // browsers that support background-clip: text). |
| 124 | $fields['color'] = [ |
| 125 | 'label' => esc_html__( 'Color', 'essential-addons-for-elementor-lite' ), |
| 126 | 'type' => Controls_Manager::COLOR, |
| 127 | 'default' => '', |
| 128 | 'selectors' => [ |
| 129 | '{{SELECTOR}}' => 'color: {{VALUE}};', |
| 130 | ], |
| 131 | ]; |
| 132 | |
| 133 | // ── Gradient-only fields ────────────────────────────────────────────── |
| 134 | |
| 135 | $fields['color_stop'] = [ |
| 136 | 'label' => esc_html__( 'Location', 'essential-addons-for-elementor-lite' ), |
| 137 | 'type' => Controls_Manager::SLIDER, |
| 138 | 'size_units' => [ '%', 'custom' ], |
| 139 | 'default' => [ |
| 140 | 'unit' => '%', |
| 141 | 'size' => 0, |
| 142 | ], |
| 143 | 'render_type' => 'ui', |
| 144 | 'condition' => [ |
| 145 | 'color_type' => 'gradient', |
| 146 | ], |
| 147 | ]; |
| 148 | |
| 149 | $fields['color_b'] = [ |
| 150 | 'label' => esc_html__( 'Second Color', 'essential-addons-for-elementor-lite' ), |
| 151 | 'type' => Controls_Manager::COLOR, |
| 152 | 'default' => '#f2295b', |
| 153 | 'render_type' => 'ui', |
| 154 | 'condition' => [ |
| 155 | 'color_type' => 'gradient', |
| 156 | ], |
| 157 | ]; |
| 158 | |
| 159 | $fields['color_b_stop'] = [ |
| 160 | 'label' => esc_html__( 'Location', 'essential-addons-for-elementor-lite' ), |
| 161 | 'type' => Controls_Manager::SLIDER, |
| 162 | 'size_units' => [ '%', 'custom' ], |
| 163 | 'default' => [ |
| 164 | 'unit' => '%', |
| 165 | 'size' => 100, |
| 166 | ], |
| 167 | 'render_type' => 'ui', |
| 168 | 'condition' => [ |
| 169 | 'color_type' => 'gradient', |
| 170 | ], |
| 171 | ]; |
| 172 | |
| 173 | $fields['gradient_type'] = [ |
| 174 | 'label' => esc_html__( 'Type', 'essential-addons-for-elementor-lite' ), |
| 175 | 'type' => Controls_Manager::SELECT, |
| 176 | 'options' => [ |
| 177 | 'linear' => esc_html__( 'Linear', 'essential-addons-for-elementor-lite' ), |
| 178 | 'radial' => esc_html__( 'Radial', 'essential-addons-for-elementor-lite' ), |
| 179 | ], |
| 180 | 'default' => 'linear', |
| 181 | 'render_type' => 'ui', |
| 182 | 'condition' => [ |
| 183 | 'color_type' => 'gradient', |
| 184 | ], |
| 185 | ]; |
| 186 | |
| 187 | // Linear gradient — outputs full text-clip CSS. |
| 188 | $fields['gradient_angle'] = [ |
| 189 | 'label' => esc_html__( 'Angle', 'essential-addons-for-elementor-lite' ), |
| 190 | 'type' => Controls_Manager::SLIDER, |
| 191 | 'size_units' => [ 'deg', 'grad', 'rad', 'turn', 'custom' ], |
| 192 | 'default' => [ |
| 193 | 'unit' => 'deg', |
| 194 | 'size' => 135, |
| 195 | ], |
| 196 | 'selectors' => [ |
| 197 | '{{SELECTOR}}' => implode( '; ', [ |
| 198 | 'background-image: linear-gradient({{SIZE}}{{UNIT}}, {{color.VALUE}} {{color_stop.SIZE}}{{color_stop.UNIT}}, {{color_b.VALUE}} {{color_b_stop.SIZE}}{{color_b_stop.UNIT}})', |
| 199 | '-webkit-background-clip: text', |
| 200 | 'background-clip: text', |
| 201 | 'color: transparent', |
| 202 | ] ), |
| 203 | ], |
| 204 | 'condition' => [ |
| 205 | 'color_type' => 'gradient', |
| 206 | 'gradient_type' => 'linear', |
| 207 | ], |
| 208 | ]; |
| 209 | |
| 210 | // Radial gradient — outputs full text-clip CSS. |
| 211 | $fields['gradient_position'] = [ |
| 212 | 'label' => esc_html__( 'Position', 'essential-addons-for-elementor-lite' ), |
| 213 | 'type' => Controls_Manager::SELECT, |
| 214 | 'options' => [ |
| 215 | 'center center' => esc_html__( 'Center Center', 'essential-addons-for-elementor-lite' ), |
| 216 | 'center left' => esc_html__( 'Center Left', 'essential-addons-for-elementor-lite' ), |
| 217 | 'center right' => esc_html__( 'Center Right', 'essential-addons-for-elementor-lite' ), |
| 218 | 'top center' => esc_html__( 'Top Center', 'essential-addons-for-elementor-lite' ), |
| 219 | 'top left' => esc_html__( 'Top Left', 'essential-addons-for-elementor-lite' ), |
| 220 | 'top right' => esc_html__( 'Top Right', 'essential-addons-for-elementor-lite' ), |
| 221 | 'bottom center' => esc_html__( 'Bottom Center', 'essential-addons-for-elementor-lite' ), |
| 222 | 'bottom left' => esc_html__( 'Bottom Left', 'essential-addons-for-elementor-lite' ), |
| 223 | 'bottom right' => esc_html__( 'Bottom Right', 'essential-addons-for-elementor-lite' ), |
| 224 | ], |
| 225 | 'default' => 'center center', |
| 226 | 'selectors' => [ |
| 227 | '{{SELECTOR}}' => implode( '; ', [ |
| 228 | 'background-image: radial-gradient(at {{VALUE}}, {{color.VALUE}} {{color_stop.SIZE}}{{color_stop.UNIT}}, {{color_b.VALUE}} {{color_b_stop.SIZE}}{{color_b_stop.UNIT}})', |
| 229 | '-webkit-background-clip: text', |
| 230 | 'background-clip: text', |
| 231 | 'color: transparent', |
| 232 | ] ), |
| 233 | ], |
| 234 | 'condition' => [ |
| 235 | 'color_type' => 'gradient', |
| 236 | 'gradient_type' => 'radial', |
| 237 | ], |
| 238 | ]; |
| 239 | |
| 240 | return $fields; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Apply defaults from the `default` arg passed to add_group_control(). |
| 245 | * |
| 246 | * - string → classic mode; value becomes color default. |
| 247 | * - array → gradient mode; keys 'color' and 'color_b' set respective defaults. |
| 248 | * - omitted → no change (controller field defaults apply as-is). |
| 249 | * |
| 250 | * @since 6.6.0 |
| 251 | * @access protected |
| 252 | * |
| 253 | * @param array $fields Prepared fields array. |
| 254 | * @return array |
| 255 | */ |
| 256 | protected function prepare_fields( $fields ) { |
| 257 | $args = $this->get_args(); |
| 258 | $default = isset( $args['default'] ) ? $args['default'] : null; |
| 259 | |
| 260 | if ( \is_string( $default ) && '' !== $default ) { |
| 261 | // Classic mode — single color default. |
| 262 | $fields['color']['default'] = $default; |
| 263 | } elseif ( \is_array( $default ) ) { |
| 264 | // Gradient mode. |
| 265 | $fields['color_type']['default'] = 'gradient'; |
| 266 | |
| 267 | if ( isset( $default['color'] ) ) { |
| 268 | $fields['color']['default'] = $default['color']; |
| 269 | } |
| 270 | if ( isset( $default['color_b'] ) ) { |
| 271 | $fields['color_b']['default'] = $default['color_b']; |
| 272 | } |
| 273 | if ( isset( $default['color_stop'] ) ) { |
| 274 | $fields['color_stop']['default'] = $default['color_stop']; |
| 275 | } |
| 276 | if ( isset( $default['color_b_stop'] ) ) { |
| 277 | $fields['color_b_stop']['default'] = $default['color_b_stop']; |
| 278 | } |
| 279 | |
| 280 | } |
| 281 | |
| 282 | return parent::prepare_fields( $fields ); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Default options for this group control. |
| 287 | * |
| 288 | * Renders fields inline in the panel (no popover toggle button). |
| 289 | * |
| 290 | * @since 6.6.0 |
| 291 | * @access protected |
| 292 | * |
| 293 | * @return array |
| 294 | */ |
| 295 | protected function get_default_options() { |
| 296 | return [ |
| 297 | 'popover' => false, |
| 298 | ]; |
| 299 | } |
| 300 | } |
| 301 |