| 1 |
<?php |
| 2 |
|
| 3 |
namespace HelloPlus\Classes; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
use Elementor\{ |
| 10 |
Controls_Manager, |
| 11 |
Widget_Base |
| 12 |
}; |
| 13 |
|
| 14 |
class Ehp_Shapes { |
| 15 |
private $context = []; |
| 16 |
private ?Widget_Base $widget = null; |
| 17 |
|
| 18 |
private $widget_settings = []; |
| 19 |
private $control_prefix = ''; |
| 20 |
private $prefix_attr = ''; |
| 21 |
private $key_attr = ''; |
| 22 |
|
| 23 |
public function set_context( array $context ) { |
| 24 |
$this->context = $context; |
| 25 |
} |
| 26 |
|
| 27 |
private function get_options() { |
| 28 |
$options_names = [ |
| 29 |
'default' => esc_html__( 'Default', 'hello-plus' ), |
| 30 |
'sharp' => esc_html__( 'Sharp', 'hello-plus' ), |
| 31 |
'rounded' => esc_html__( 'Rounded', 'hello-plus' ), |
| 32 |
'round' => esc_html__( 'Round', 'hello-plus' ), |
| 33 |
'oval' => esc_html__( 'Oval', 'hello-plus' ), |
| 34 |
'custom' => esc_html__( 'Custom', 'hello-plus' ), |
| 35 |
]; |
| 36 |
|
| 37 |
$options = [ |
| 38 |
'button' => [ 'default', 'sharp', 'rounded', 'round', 'oval', 'custom' ], |
| 39 |
'submenu' => [ 'default', 'sharp', 'rounded', 'round', 'oval', 'custom' ], |
| 40 |
'box' => [ 'sharp', 'rounded', 'custom' ], |
| 41 |
'image' => [ 'sharp', 'rounded', 'round', 'oval', 'custom' ], |
| 42 |
'map' => [ 'sharp', 'rounded', 'round', 'oval', 'custom' ], |
| 43 |
'float' => [ 'default', 'sharp', 'round', 'rounded', 'custom' ], |
| 44 |
]; |
| 45 |
|
| 46 |
return array_map( function ( $keys ) use ( $options_names ) { |
| 47 |
return array_intersect_key( $options_names, array_flip( $keys ) ); |
| 48 |
}, $options ); |
| 49 |
} |
| 50 |
|
| 51 |
public function get_shape_classnames() { |
| 52 |
$this->widget_settings = $this->widget->get_settings_for_display(); |
| 53 |
$container_prefix = $this->context['container_prefix']; |
| 54 |
$is_responsive = $this->context['is_responsive'] ?? true; |
| 55 |
|
| 56 |
$shape = $this->widget_settings[ $this->control_prefix . $container_prefix . '_shape' ] ?? ''; |
| 57 |
|
| 58 |
$shape_classnames = []; |
| 59 |
|
| 60 |
if ( ! empty( $shape ) ) { |
| 61 |
$shape_classnames[] = 'has-shape-' . $shape; |
| 62 |
$shape_classnames[] = 'shape-type-' . $container_prefix; |
| 63 |
|
| 64 |
if ( $is_responsive ) { |
| 65 |
$shape_mobile = $this->widget_settings[ $this->control_prefix . $container_prefix . '_shape_mobile' ]; |
| 66 |
$shape_tablet = $this->widget_settings[ $this->control_prefix . $container_prefix . '_shape_tablet' ]; |
| 67 |
|
| 68 |
if ( ! empty( $shape_mobile ) ) { |
| 69 |
$shape_classnames[] = 'has-shape-sm-' . $shape_mobile; |
| 70 |
} |
| 71 |
|
| 72 |
if ( ! empty( $shape_tablet ) ) { |
| 73 |
$shape_classnames[] = 'has-shape-md-' . $shape_tablet; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return $shape_classnames; |
| 79 |
} |
| 80 |
|
| 81 |
public function add_shape_attributes() { |
| 82 |
$this->widget_settings = $this->widget->get_settings_for_display(); |
| 83 |
$shape = $this->widget_settings[ $this->control_prefix . $this->context['container_prefix'] . '_shape' ] ?? ''; |
| 84 |
|
| 85 |
if ( ! empty( $shape ) ) { |
| 86 |
$shape_classnames = $this->get_shape_classnames(); |
| 87 |
|
| 88 |
$this->widget->add_render_attribute( $this->context['render_attribute'] . $this->key_attr, [ |
| 89 |
'class' => $shape_classnames, |
| 90 |
] ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
public function add_style_controls() { |
| 95 |
$widget_name = $this->context['widget_name']; |
| 96 |
$container_prefix = $this->context['container_prefix']; |
| 97 |
$condition = $this->context['condition'] ?? []; |
| 98 |
$is_responsive = $this->context['is_responsive'] ?? true; |
| 99 |
|
| 100 |
$defaults = [ |
| 101 |
'box' => 'sharp', |
| 102 |
'image' => 'sharp', |
| 103 |
'map' => 'sharp', |
| 104 |
]; |
| 105 |
|
| 106 |
if ( $is_responsive ) { |
| 107 |
$this->widget->add_responsive_control( |
| 108 |
$this->control_prefix . $container_prefix . '_shape', |
| 109 |
[ |
| 110 |
'label' => esc_html__( 'Shape', 'hello-plus' ), |
| 111 |
'type' => Controls_Manager::SELECT, |
| 112 |
'default' => $defaults[ $container_prefix ] ?? 'default', |
| 113 |
'options' => $this->get_options()[ $container_prefix ], |
| 114 |
'frontend_available' => true, |
| 115 |
'condition' => $condition, |
| 116 |
] |
| 117 |
); |
| 118 |
} else { |
| 119 |
$this->widget->add_control( |
| 120 |
$this->control_prefix . $container_prefix . '_shape', |
| 121 |
[ |
| 122 |
'label' => esc_html__( 'Shape', 'hello-plus' ), |
| 123 |
'type' => Controls_Manager::SELECT, |
| 124 |
'default' => $defaults[ $container_prefix ] ?? 'default', |
| 125 |
'options' => $this->get_options()[ $container_prefix ], |
| 126 |
'frontend_available' => true, |
| 127 |
'condition' => $condition, |
| 128 |
] |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
$prefix_property = "--{$widget_name}-{$container_prefix}{$this->prefix_attr}-border-radius"; |
| 133 |
|
| 134 |
if ( $is_responsive ) { |
| 135 |
$this->widget->add_responsive_control( |
| 136 |
$this->control_prefix . $container_prefix . '_shape_custom', |
| 137 |
[ |
| 138 |
'label' => esc_html__( 'Border Radius', 'hello-plus' ), |
| 139 |
'type' => Controls_Manager::DIMENSIONS, |
| 140 |
'size_units' => [ 'px', '%', 'em', 'rem' ], |
| 141 |
'selectors' => [ |
| 142 |
'{{WRAPPER}} .ehp-' . $widget_name => "{$prefix_property}-block-end: {{BOTTOM}}{{UNIT}}; {$prefix_property}-block-start: {{TOP}}{{UNIT}}; {$prefix_property}-inline-end: {{RIGHT}}{{UNIT}}; {$prefix_property}-inline-start: {{LEFT}}{{UNIT}};", |
| 143 |
], |
| 144 |
'condition' => array_merge( $condition, [ |
| 145 |
$this->control_prefix . $container_prefix . '_shape' => 'custom', |
| 146 |
] ), |
| 147 |
] |
| 148 |
); |
| 149 |
} else { |
| 150 |
$this->widget->add_control( |
| 151 |
$this->control_prefix . $container_prefix . '_shape_custom', |
| 152 |
[ |
| 153 |
'label' => esc_html__( 'Border Radius', 'hello-plus' ), |
| 154 |
'type' => Controls_Manager::DIMENSIONS, |
| 155 |
'size_units' => [ 'px', '%', 'em', 'rem' ], |
| 156 |
'selectors' => [ |
| 157 |
'{{WRAPPER}} .ehp-' . $widget_name => "{$prefix_property}-block-end: {{BOTTOM}}{{UNIT}}; {$prefix_property}-block-start: {{TOP}}{{UNIT}}; {$prefix_property}-inline-end: {{RIGHT}}{{UNIT}}; {$prefix_property}-inline-start: {{LEFT}}{{UNIT}};", |
| 158 |
], |
| 159 |
'condition' => array_merge( $condition, [ |
| 160 |
$this->control_prefix . $container_prefix . '_shape' => 'custom', |
| 161 |
] ), |
| 162 |
] |
| 163 |
); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
public function __construct( Widget_Base $widget, $context = [] ) { |
| 168 |
$this->widget = $widget; |
| 169 |
$this->context = $context; |
| 170 |
|
| 171 |
$control_prefix = ( $this->context['control_prefix'] ?? $this->context['type_prefix'] ?? '' ); |
| 172 |
$this->control_prefix = '' !== $control_prefix ? $control_prefix . '_' : ''; |
| 173 |
|
| 174 |
$this->prefix_attr = ! empty( $this->context['type_prefix'] ) ? '-' . $this->context['type_prefix'] : ''; |
| 175 |
$this->key_attr = ! empty( $this->context['key'] ) ? '-' . $this->context['key'] : ''; |
| 176 |
} |
| 177 |
} |
| 178 |
|