Features_Badge.php
302 lines
| 1 | <?php |
| 2 | namespace Elementor; |
| 3 | namespace SPEL\includes\Admin\extension; |
| 4 | |
| 5 | use Elementor\Controls_Manager; |
| 6 | use Elementor\Core\Files\CSS\Post; |
| 7 | use Elementor\Element_Base; |
| 8 | use Elementor\Group_Control_Background; |
| 9 | |
| 10 | if (!defined('ABSPATH')) { |
| 11 | exit; // Exit if accessed directly |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * Register Advance Features Control Tab |
| 16 | * Container & Section Features |
| 17 | */ |
| 18 | class Features_Badge { |
| 19 | |
| 20 | public function __construct () |
| 21 | { |
| 22 | |
| 23 | // Section Elements Fields |
| 24 | add_action('elementor/element/section/section_advanced/after_section_end', [ $this, 'register_section_controls' ]); |
| 25 | add_action('elementor/element/column/section_advanced/after_section_end', [ $this, 'register_section_controls' ]); |
| 26 | add_action('elementor/element/container/section_layout/after_section_end', [ $this, 'register_section_controls' ]); |
| 27 | |
| 28 | // Add the 'before_render' action hook to the widget |
| 29 | add_action('elementor/editor/before_render', [ $this, 'callback_render_display_content' ], 10, 1); |
| 30 | add_action('elementor/frontend/before_render', [ $this, 'callback_render_display_content' ], 10, 1); |
| 31 | |
| 32 | //Frontend Before View |
| 33 | add_action('elementor/frontend/container/before_render', [ $this, 'frontend_render_before' ]); |
| 34 | add_action('elementor/frontend/column/before_render', [ $this, 'frontend_render_before' ]); |
| 35 | add_action('elementor/frontend/section/before_render', [ $this, 'frontend_render_before' ]); |
| 36 | |
| 37 | //Frontend After View |
| 38 | add_action('elementor/frontend/container/after_render', [ $this, 'frontend_render_after' ]); |
| 39 | add_action('elementor/frontend/column/after_render', [ $this, 'frontend_render_after' ]); |
| 40 | add_action('elementor/frontend/section/after_render', [ $this, 'frontend_render_after' ]); |
| 41 | |
| 42 | //Editor View |
| 43 | add_action('elementor/section/print_template', [ $this, 'render_display_on_editor' ], 10, 2); |
| 44 | add_action('elementor/column/print_template', [ $this, 'render_display_on_editor' ], 10, 2); |
| 45 | add_action('elementor/container/print_template', [ $this, 'render_display_on_editor' ], 10, 2); |
| 46 | |
| 47 | // Add custom CSS rules to a post's CSS file. |
| 48 | add_action('elementor/element/parse_css', [ $this, 'add_custom_rules_to_css_file' ], 10, 2); |
| 49 | |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * Section Controls |
| 55 | */ |
| 56 | public function register_section_controls (Element_Base $element): void |
| 57 | { |
| 58 | |
| 59 | //=============== Start Features Box ===============// |
| 60 | $element->start_controls_section( |
| 61 | 'spel_features_badge_sec', [ |
| 62 | 'label' => esc_html__('Feature Badge', 'spider-elements'), |
| 63 | 'tab' => Controls_Manager::TAB_LAYOUT, |
| 64 | ] |
| 65 | ); |
| 66 | |
| 67 | //Badge Label Settings |
| 68 | $element->add_control( |
| 69 | 'spe_fb_badge_enable', [ |
| 70 | 'type' => \Elementor\Controls_Manager::SWITCHER, |
| 71 | 'label' => esc_html__('Enable Badge', 'spider-elements-pro'), |
| 72 | 'frontend_available' => true, |
| 73 | 'label_on' => esc_html__('Yes', 'spider-elements-pro'), |
| 74 | 'label_off' => esc_html__('No', 'spider-elements-pro'), |
| 75 | 'return_value' => 'yes', |
| 76 | 'default' => 'no', |
| 77 | 'separator' => 'before', |
| 78 | ] |
| 79 | ); |
| 80 | |
| 81 | $element->add_control( |
| 82 | 'spe_fb_badge_text', |
| 83 | [ |
| 84 | 'label' => esc_html__('Badge Label', 'spider-elements-pro'), |
| 85 | 'type' => Controls_Manager::TEXT, |
| 86 | 'dynamic' => [ |
| 87 | 'active' => true, |
| 88 | ], |
| 89 | 'condition' => [ |
| 90 | 'spe_fb_badge_enable' => 'yes', |
| 91 | ], |
| 92 | ] |
| 93 | ); |
| 94 | |
| 95 | // Badge Text Color Settings |
| 96 | $element->add_control( |
| 97 | 'spe_fb_badge_color', |
| 98 | [ |
| 99 | 'label' => esc_html__('Text Color', 'spider-elements-pro'), |
| 100 | 'type' => Controls_Manager::COLOR, |
| 101 | 'condition' => [ |
| 102 | 'spe_fb_badge_enable' => 'yes', |
| 103 | ], |
| 104 | 'separator' => 'before', |
| 105 | ] |
| 106 | ); |
| 107 | |
| 108 | // Badge Background Color Settings |
| 109 | $element->add_control( |
| 110 | 'spe_fb_badge_bg_color', |
| 111 | [ |
| 112 | 'label' => esc_html__('Background Color', 'spider-elements-pro'), |
| 113 | 'type' => Controls_Manager::COLOR, |
| 114 | 'condition' => [ |
| 115 | 'spe_fb_badge_enable' => 'yes', |
| 116 | ], |
| 117 | ] |
| 118 | ); |
| 119 | |
| 120 | // Badge Text Typography Settings |
| 121 | $element->add_responsive_control( |
| 122 | 'spe_fb_badge_position_top', [ |
| 123 | 'label' => esc_html__('Position Top', 'spider-elements-pro'), |
| 124 | 'type' => \Elementor\Controls_Manager::SLIDER, |
| 125 | 'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ], |
| 126 | 'range' => [ |
| 127 | 'px' => [ |
| 128 | 'min' => 0, |
| 129 | 'max' => 100, |
| 130 | ], |
| 131 | '%' => [ |
| 132 | 'min' => 0, |
| 133 | 'max' => 100, |
| 134 | ], |
| 135 | ], |
| 136 | 'default' => [ |
| 137 | 'unit' => 'px', |
| 138 | 'size' => '', |
| 139 | ], |
| 140 | 'selectors' => [ |
| 141 | '{{WRAPPER}} .wrapper_badge_text .badge_text' => 'top: {{SIZE}}{{UNIT}};', |
| 142 | ], |
| 143 | 'condition' => [ |
| 144 | 'spe_fb_badge_enable' => 'yes', |
| 145 | ], |
| 146 | ] |
| 147 | ); |
| 148 | |
| 149 | $element->end_controls_section(); // End Section |
| 150 | |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /** |
| 155 | * @param Element_Base $element |
| 156 | * |
| 157 | * @return void |
| 158 | */ |
| 159 | public function callback_render_display_content (Element_Base $element): void |
| 160 | { |
| 161 | |
| 162 | $feature_icon = $element->get_settings_for_display('spe_fb_icon'); |
| 163 | |
| 164 | // It's render elementor wrapper div - Icon |
| 165 | if ($feature_icon && !empty($feature_icon[ 'value' ])) { |
| 166 | |
| 167 | //It's render elementor wrapper div |
| 168 | $element->add_render_attribute( |
| 169 | '_wrapper', [ |
| 170 | 'class' => 'spe-features-box-enable', |
| 171 | 'data-spe-element-icon' => $feature_icon[ 'value' ], |
| 172 | ] |
| 173 | ); |
| 174 | |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /** |
| 181 | * Renders content before a section, column, or container in the frontend. |
| 182 | * |
| 183 | * @param Element_Base $section The section, column, or container element. |
| 184 | */ |
| 185 | public function frontend_render_before (Element_Base $section): void |
| 186 | { |
| 187 | |
| 188 | if ($section->get_name() === 'container' || $section->get_name() === 'column' || $section->get_name() === 'section') { |
| 189 | $settings = $section->get_settings_for_display(); |
| 190 | |
| 191 | if (isset($settings[ 'spe_fb_badge_enable' ]) && !empty($settings[ 'spe_fb_badge_enable' ] === 'yes')) { |
| 192 | $badge_text = !empty($settings[ 'spe_fb_badge_text' ]) ? $settings[ 'spe_fb_badge_text' ] : ''; |
| 193 | ?> |
| 194 | <div class="wrapper_badge_text position-relative"> |
| 195 | <span class="badge_text"> |
| 196 | <span class="badge-element before"></span> |
| 197 | <?php echo esc_html($badge_text) ?> |
| 198 | <span class="badge-element after"></span> |
| 199 | </span> |
| 200 | <?php |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | } |
| 205 | |
| 206 | |
| 207 | /** |
| 208 | * Renders content after a section, column, or container in the frontend. |
| 209 | * |
| 210 | * @param Element_Base $section The section, column, or container element. |
| 211 | */ |
| 212 | public function frontend_render_after (Element_Base $section): void |
| 213 | { |
| 214 | |
| 215 | if ($section->get_name() === 'container' || $section->get_name() === 'column' || $section->get_name() === 'section') { |
| 216 | $settings = $section->get_settings_for_display(); |
| 217 | |
| 218 | if (isset($settings[ 'spe_fb_badge_enable' ]) && ($settings[ 'spe_fb_badge_enable' ] === 'yes')) { |
| 219 | ?> |
| 220 | </div> |
| 221 | <?php |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Inject Image Editor |
| 228 | * |
| 229 | * @param $template |
| 230 | * @return string |
| 231 | */ |
| 232 | public function render_display_on_editor ($template): string |
| 233 | { |
| 234 | |
| 235 | ob_start(); |
| 236 | $old_template = $template; |
| 237 | ?> |
| 238 | <# if ( settings.spe_fb_badge_enable==='yes' ) { #> |
| 239 | <# let badge_text=settings.spe_fb_badge_text; let badge_text_color=settings.spe_fb_badge_color ? |
| 240 | settings.spe_fb_badge_color : '#ffffff' ; let badge_bg_color=settings.spe_fb_badge_bg_color ? |
| 241 | settings.spe_fb_badge_bg_color : '#31795A' ; let span_style='color: ' + badge_text_color + ';' ; let |
| 242 | span_style_before='background: ' + badge_bg_color + ';' ; let span_style_after='background: ' + badge_bg_color |
| 243 | + ';' ; #> |
| 244 | |
| 245 | <div class="wrapper_badge_text"> |
| 246 | <span class="badge_text" style="{{span_style}}"> |
| 247 | <span class="badge-element before" style="{{span_style_before}}"></span> |
| 248 | {{badge_text}} |
| 249 | <span class="badge-element after" style="{{span_style_after}}"></span> |
| 250 | </span> |
| 251 | </div> |
| 252 | <# } #> |
| 253 | <?php |
| 254 | $content = ob_get_contents(); |
| 255 | ob_end_clean(); |
| 256 | return $content . $old_template; |
| 257 | |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Add custom CSS rules to a post's CSS file. |
| 262 | * |
| 263 | * @param Post $post_css_file The post's CSS file. |
| 264 | * @param Element_Base $element The Elementor element. |
| 265 | * |
| 266 | * @return void |
| 267 | */ |
| 268 | public function add_custom_rules_to_css_file (Post $post_css_file, Element_Base $element): void |
| 269 | { |
| 270 | |
| 271 | // Get the display settings for the element. |
| 272 | $settings = $element->get_settings_for_display(); |
| 273 | |
| 274 | // Check if the badge is enabled |
| 275 | if (isset($settings[ 'spe_fb_badge_enable' ]) && !empty($settings[ 'spe_fb_badge_enable' ] === 'yes')) { |
| 276 | |
| 277 | // Add the custom CSS rules to the post's CSS file. |
| 278 | $text_color = !empty($settings[ 'spe_fb_badge_color' ]) ? $settings[ 'spe_fb_badge_color' ] : ''; |
| 279 | $position_top = !empty($settings[ 'spe_fb_badge_position_top' ]) ? $settings[ 'spe_fb_badge_position_top' ][ 'size' ] . $settings[ 'spe_fb_badge_position_top' ][ 'unit' ] : ''; |
| 280 | |
| 281 | $post_css_file->get_stylesheet()->add_rules( |
| 282 | '.wrapper_badge_text .badge_text', |
| 283 | [ |
| 284 | 'color' => $text_color, |
| 285 | 'top' => $position_top, |
| 286 | ], |
| 287 | ); |
| 288 | |
| 289 | if (!empty($settings[ 'spe_fb_badge_bg_color' ])) { |
| 290 | $post_css_file->get_stylesheet()->add_rules( |
| 291 | '.wrapper_badge_text .badge_text .badge-element', |
| 292 | [ |
| 293 | 'background' => $settings[ 'spe_fb_badge_bg_color' ] . ' !important', |
| 294 | ], |
| 295 | ); |
| 296 | } |
| 297 | |
| 298 | } |
| 299 | |
| 300 | } |
| 301 | |
| 302 | } |