| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Elementor Coupon Block |
| 5 |
* |
| 6 |
* @package Copy the Code |
| 7 |
* @since 3.1.0 |
| 8 |
*/ |
| 9 |
namespace CopyTheCode\Elementor\Block; |
| 10 |
|
| 11 |
use Elementor\Utils; |
| 12 |
use CopyTheCode\Helpers; |
| 13 |
use Elementor\Widget_Base; |
| 14 |
use Elementor\Controls_Manager; |
| 15 |
/** |
| 16 |
* Coupon Block |
| 17 |
* |
| 18 |
* @since 3.1.0 |
| 19 |
*/ |
| 20 |
class Coupon extends Widget_Base { |
| 21 |
/** |
| 22 |
* Constructor |
| 23 |
* |
| 24 |
* @param array $data |
| 25 |
* @param array $args |
| 26 |
* |
| 27 |
* @since 3.1.0 |
| 28 |
*/ |
| 29 |
public function __construct( $data = [], $args = null ) { |
| 30 |
parent::__construct( $data, $args ); |
| 31 |
// Block. |
| 32 |
wp_enqueue_style( |
| 33 |
'ctc-el-coupon', |
| 34 |
COPY_THE_CODE_URI . 'classes/elementor/widgets/coupon/style.css', |
| 35 |
[], |
| 36 |
COPY_THE_CODE_VER, |
| 37 |
'all' |
| 38 |
); |
| 39 |
wp_enqueue_script( |
| 40 |
'ctc-el-coupon', |
| 41 |
COPY_THE_CODE_URI . 'classes/elementor/widgets/coupon/script.js', |
| 42 |
[], |
| 43 |
COPY_THE_CODE_VER, |
| 44 |
true |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get style dependencies |
| 50 |
*/ |
| 51 |
public function get_style_depends() { |
| 52 |
return ['ctc-el-coupon']; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get script dependencies |
| 57 |
*/ |
| 58 |
public function get_script_depends() { |
| 59 |
return ['ctc-el-coupon']; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get name |
| 64 |
*/ |
| 65 |
public function get_name() { |
| 66 |
return 'ctc_coupon'; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get title |
| 71 |
*/ |
| 72 |
public function get_title() { |
| 73 |
return esc_html__( 'Coupon Code', 'copy-the-code' ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get icon |
| 78 |
*/ |
| 79 |
public function get_icon() { |
| 80 |
return 'eicon-cart-solid'; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get categories |
| 85 |
*/ |
| 86 |
public function get_categories() { |
| 87 |
return Helpers::get_categories(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get keywords |
| 92 |
*/ |
| 93 |
public function get_keywords() { |
| 94 |
return Helpers::get_keywords( [ |
| 95 |
'coupon', |
| 96 |
'offer', |
| 97 |
'coupon', |
| 98 |
'sale' |
| 99 |
] ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Render |
| 104 |
*/ |
| 105 |
public function render() { |
| 106 |
$image = $this->get_settings_for_display( 'image' ); |
| 107 |
$heading = $this->get_settings_for_display( 'heading' ); |
| 108 |
$description = $this->get_settings_for_display( 'description' ); |
| 109 |
$slide_button_text = $this->get_settings_for_display( 'slide_button_text' ); |
| 110 |
$coupon_code = $this->get_settings_for_display( 'coupon_code' ); |
| 111 |
$button_link = $this->get_settings_for_display( 'button_link' ); |
| 112 |
$link_target = $this->get_settings_for_display( 'link_target' ); |
| 113 |
$details = $this->get_settings_for_display( 'details' ); |
| 114 |
$toggle_details = $this->get_settings_for_display( 'toggle_details' ); |
| 115 |
$button_text = $this->get_settings_for_display( 'button_text' ); |
| 116 |
$button_text_copied = $this->get_settings_for_display( 'button_text_copied' ); |
| 117 |
$show_icon = $this->get_settings_for_display( 'show_icon' ); |
| 118 |
$icon_direction = $this->get_settings_for_display( 'icon_direction' ); |
| 119 |
$display_slide_button = $this->get_settings_for_display( 'display_slide_button' ); |
| 120 |
$clicked = ( 'yes' === $display_slide_button ? '' : ' ctc-coupon-clicked' ); |
| 121 |
?> |
| 122 |
<div class="ctc-block ctc-coupon <?php |
| 123 |
echo $clicked; |
| 124 |
?>"> |
| 125 |
<div class="ctc-coupon-header"> |
| 126 |
<?php |
| 127 |
if ( $image['url'] ) { |
| 128 |
?> |
| 129 |
<div class="ctc-coupon-image"> |
| 130 |
<img src="<?php |
| 131 |
echo esc_url( $image['url'] ); |
| 132 |
?>" alt="<?php |
| 133 |
echo esc_attr( $heading ); |
| 134 |
?>"> |
| 135 |
</div> |
| 136 |
<?php |
| 137 |
} |
| 138 |
?> |
| 139 |
<div class="ctc-coupon-headings"> |
| 140 |
<h3 class="ctc-coupon-heading"><?php |
| 141 |
echo esc_html( $heading ); |
| 142 |
?></h3> |
| 143 |
|
| 144 |
<?php |
| 145 |
if ( $description ) { |
| 146 |
?> |
| 147 |
<div class="ctc-coupon-description"><?php |
| 148 |
echo wp_kses_post( wpautop( $description ) ); |
| 149 |
?></div> |
| 150 |
<?php |
| 151 |
} |
| 152 |
?> |
| 153 |
|
| 154 |
</div> |
| 155 |
<div class="ctc-coupon-cta"> |
| 156 |
<span class="ctc-coupon-code"><?php |
| 157 |
echo esc_html( $coupon_code ); |
| 158 |
?></span> |
| 159 |
|
| 160 |
<?php |
| 161 |
if ( 'yes' === $display_slide_button ) { |
| 162 |
?> |
| 163 |
<a href="<?php |
| 164 |
echo esc_url( $button_link['url'] ); |
| 165 |
?>" target="<?php |
| 166 |
echo esc_attr( $link_target ); |
| 167 |
?>" class="ctc-coupon-link"><?php |
| 168 |
echo esc_html( $slide_button_text ); |
| 169 |
?></a> |
| 170 |
<?php |
| 171 |
} |
| 172 |
?> |
| 173 |
|
| 174 |
<?php |
| 175 |
echo Helpers::get_copy_button( [ |
| 176 |
'as_raw' => 'no', |
| 177 |
'copy_button_text' => $button_text, |
| 178 |
'copy_button_text_copied' => $button_text_copied, |
| 179 |
'icon_direction' => $icon_direction, |
| 180 |
'show_icon' => $show_icon, |
| 181 |
] ); |
| 182 |
?> |
| 183 |
<textarea class="ctc-copy-content" style="display: none;"><?php |
| 184 |
echo esc_html( $coupon_code ); |
| 185 |
?></textarea> |
| 186 |
</div> |
| 187 |
</div> |
| 188 |
<?php |
| 189 |
if ( $details ) { |
| 190 |
?> |
| 191 |
<div class="ctc-coupon-details"> |
| 192 |
<?php |
| 193 |
$toggle_details_class = ( 'yes' === $toggle_details ? 'ctc-toggle-details' : '' ); |
| 194 |
if ( 'yes' === $toggle_details ) { |
| 195 |
echo '<a href="#" class="ctc-coupon-toggle-link">Show Details</a>'; |
| 196 |
} |
| 197 |
echo '<div class="ctc-details-content ' . $toggle_details_class . '">' . wp_kses_post( wpautop( $details ) ) . '</div>'; |
| 198 |
?> |
| 199 |
</div> |
| 200 |
<?php |
| 201 |
} |
| 202 |
?> |
| 203 |
</div> |
| 204 |
<?php |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Register controls |
| 209 |
*/ |
| 210 |
protected function _register_controls() { |
| 211 |
$this->start_controls_section( 'image_section', [ |
| 212 |
'label' => esc_html__( 'Coupon Image', 'copy-the-code' ), |
| 213 |
] ); |
| 214 |
$this->add_control( 'image', [ |
| 215 |
'label' => esc_html__( 'Coupon Image', 'copy-the-code' ), |
| 216 |
'type' => Controls_Manager::MEDIA, |
| 217 |
'default' => [ |
| 218 |
'url' => Utils::get_placeholder_image_src(), |
| 219 |
], |
| 220 |
] ); |
| 221 |
$this->end_controls_section(); |
| 222 |
$this->start_controls_section( 'heading_section', [ |
| 223 |
'label' => esc_html__( 'Heading', 'copy-the-code' ), |
| 224 |
] ); |
| 225 |
$this->add_control( 'heading', [ |
| 226 |
'label' => esc_html__( 'Heading', 'copy-the-code' ), |
| 227 |
'type' => Controls_Manager::TEXT, |
| 228 |
'default' => esc_html__( 'Up to 50% off', 'copy-the-code' ), |
| 229 |
] ); |
| 230 |
$this->end_controls_section(); |
| 231 |
$this->start_controls_section( 'description_section', [ |
| 232 |
'label' => esc_html__( 'Short Description', 'copy-the-code' ), |
| 233 |
] ); |
| 234 |
$this->add_control( 'description', [ |
| 235 |
'label' => esc_html__( 'Short Description', 'copy-the-code' ), |
| 236 |
'type' => Controls_Manager::WYSIWYG, |
| 237 |
'default' => 'Get up to 50% off on all products. Offer valid till <b>31st December 2020</b>.', |
| 238 |
] ); |
| 239 |
$this->end_controls_section(); |
| 240 |
$this->start_controls_section( 'coupon_button_section', [ |
| 241 |
'label' => esc_html__( 'Coupon Code', 'copy-the-code' ), |
| 242 |
] ); |
| 243 |
$this->add_control( 'coupon_code', [ |
| 244 |
'label' => esc_html__( 'Coupon Code', 'copy-the-code' ), |
| 245 |
'type' => Controls_Manager::TEXT, |
| 246 |
'default' => esc_html__( 'CTC30', 'copy-the-code' ), |
| 247 |
] ); |
| 248 |
$this->add_control( 'display_slide_button', [ |
| 249 |
'label' => esc_html__( 'Display Slide Button', 'copy-the-code' ), |
| 250 |
'type' => Controls_Manager::SWITCHER, |
| 251 |
'default' => 'yes', |
| 252 |
] ); |
| 253 |
// Button Text. |
| 254 |
$this->add_control( 'slide_button_text', [ |
| 255 |
'label' => esc_html__( 'Slide Button Text', 'copy-the-code' ), |
| 256 |
'type' => Controls_Manager::TEXT, |
| 257 |
'default' => esc_html__( 'Show Coupon Code', 'copy-the-code' ), |
| 258 |
'condition' => [ |
| 259 |
'display_slide_button' => 'yes', |
| 260 |
], |
| 261 |
] ); |
| 262 |
// Button Link. |
| 263 |
$this->add_control( 'button_link', [ |
| 264 |
'label' => esc_html__( 'Button Link', 'copy-the-code' ), |
| 265 |
'type' => Controls_Manager::URL, |
| 266 |
'default' => [ |
| 267 |
'url' => '#', |
| 268 |
], |
| 269 |
'condition' => [ |
| 270 |
'display_slide_button' => 'yes', |
| 271 |
], |
| 272 |
] ); |
| 273 |
// Link target. |
| 274 |
$this->add_control( 'link_target', [ |
| 275 |
'label' => esc_html__( 'Link Target', 'copy-the-code' ), |
| 276 |
'type' => Controls_Manager::SELECT, |
| 277 |
'default' => '_blank', |
| 278 |
'options' => [ |
| 279 |
'_self' => esc_html__( 'Self', 'copy-the-code' ), |
| 280 |
'_blank' => esc_html__( 'Blank', 'copy-the-code' ), |
| 281 |
], |
| 282 |
'condition' => [ |
| 283 |
'display_slide_button' => 'yes', |
| 284 |
], |
| 285 |
] ); |
| 286 |
$this->end_controls_section(); |
| 287 |
$this->start_controls_section( 'details_section', [ |
| 288 |
'label' => esc_html__( 'Details', 'copy-the-code' ), |
| 289 |
] ); |
| 290 |
// Enable Toggle Details. |
| 291 |
$this->add_control( 'toggle_details', [ |
| 292 |
'label' => esc_html__( 'Enable Toggle Details', 'copy-the-code' ), |
| 293 |
'type' => Controls_Manager::SWITCHER, |
| 294 |
'default' => 'yes', |
| 295 |
] ); |
| 296 |
// Details. |
| 297 |
$this->add_control( 'details', [ |
| 298 |
'label' => esc_html__( 'Details', 'copy-the-code' ), |
| 299 |
'type' => Controls_Manager::WYSIWYG, |
| 300 |
'default' => '<ul><li>Get upto 50% off on all products.</li><li> |
| 301 |
Offer valid till <b>31st December 2020</b>.</li><li>No coupon code required.</li></ul>', |
| 302 |
] ); |
| 303 |
$this->end_controls_section(); |
| 304 |
/** |
| 305 |
* Group - Copy Button |
| 306 |
*/ |
| 307 |
$this->start_controls_section( 'button_section', [ |
| 308 |
'label' => esc_html__( 'Copy Button', 'copy-the-code' ), |
| 309 |
] ); |
| 310 |
$this->add_control( 'button_text', [ |
| 311 |
'label' => esc_html__( 'Copy Button Text', 'copy-the-code' ), |
| 312 |
'type' => Controls_Manager::TEXT, |
| 313 |
'default' => esc_html__( 'Copy Code', 'copy-the-code' ), |
| 314 |
] ); |
| 315 |
$this->add_control( 'button_text_copied', [ |
| 316 |
'label' => esc_html__( 'After Copy Button Text', 'copy-the-code' ), |
| 317 |
'type' => Controls_Manager::TEXT, |
| 318 |
'default' => esc_html__( 'Code Copied', 'copy-the-code' ), |
| 319 |
] ); |
| 320 |
// Show Icon. |
| 321 |
$this->add_control( 'show_icon', [ |
| 322 |
'label' => esc_html__( 'Show Icon', 'copy-the-code' ), |
| 323 |
'type' => Controls_Manager::SWITCHER, |
| 324 |
'label_on' => esc_html__( 'Show', 'copy-the-code' ), |
| 325 |
'label_off' => esc_html__( 'Hide', 'copy-the-code' ), |
| 326 |
'return_value' => 'yes', |
| 327 |
'default' => 'yes', |
| 328 |
] ); |
| 329 |
$this->add_control( 'icon_direction', [ |
| 330 |
'label' => esc_html__( 'Icon Direction', 'copy-the-code' ), |
| 331 |
'type' => Controls_Manager::SELECT, |
| 332 |
'default' => 'before', |
| 333 |
'options' => [ |
| 334 |
'before' => esc_html__( 'Before', 'copy-the-code' ), |
| 335 |
'after' => esc_html__( 'After', 'copy-the-code' ), |
| 336 |
], |
| 337 |
'condition' => [ |
| 338 |
'show_icon' => 'yes', |
| 339 |
], |
| 340 |
] ); |
| 341 |
$this->add_responsive_control( 'icon_text_gap', [ |
| 342 |
'label' => esc_html__( 'Icon and Text Gap', 'copy-the-code' ), |
| 343 |
'type' => Controls_Manager::SLIDER, |
| 344 |
'size_units' => ['px', 'em'], |
| 345 |
'range' => [ |
| 346 |
'px' => [ |
| 347 |
'min' => 0, |
| 348 |
'max' => 100, |
| 349 |
], |
| 350 |
'em' => [ |
| 351 |
'min' => 0, |
| 352 |
'max' => 10, |
| 353 |
], |
| 354 |
], |
| 355 |
'selectors' => [ |
| 356 |
'{{WRAPPER}} .ctc-with-icon' => 'gap: {{SIZE}}{{UNIT}};', |
| 357 |
], |
| 358 |
'condition' => [ |
| 359 |
'show_icon' => 'yes', |
| 360 |
], |
| 361 |
] ); |
| 362 |
$this->end_controls_section(); |
| 363 |
Helpers::register_pro_sections( $this, [ |
| 364 |
'Header', |
| 365 |
'Heading', |
| 366 |
'Description', |
| 367 |
'Button', |
| 368 |
'Coupon Code', |
| 369 |
'Details', |
| 370 |
'Details > Link', |
| 371 |
'Details > Content' |
| 372 |
] ); |
| 373 |
} |
| 374 |
|
| 375 |
} |
| 376 |
|