| 1 |
<?php |
| 2 |
/** |
| 3 |
* Helpers |
| 4 |
* |
| 5 |
* @package Copy the Code |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace CopyTheCode; |
| 10 |
|
| 11 |
use Elementor\Controls_Manager; |
| 12 |
use Elementor\Group_Control_Background; |
| 13 |
use Elementor\Group_Control_Typography; |
| 14 |
use Elementor\Group_Control_Border; |
| 15 |
use Elementor\Group_Control_Box_Shadow; |
| 16 |
|
| 17 |
/** |
| 18 |
* Helpers |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
class Helpers { |
| 23 |
public static function get_svg_copy_icon() { |
| 24 |
return '<svg aria-hidden="true" focusable="false" role="img" class="copy-icon" viewBox="0 0 16 16" width="16" height="16" fill="currentColor"><path d="M0 6.75C0 5.784.784 5 1.75 5h1.5a.75.75 0 0 1 0 1.5h-1.5a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-1.5a.75.75 0 0 1 1.5 0v1.5A1.75 1.75 0 0 1 9.25 16h-7.5A1.75 1.75 0 0 1 0 14.25Z"></path><path d="M5 1.75C5 .784 5.784 0 6.75 0h7.5C15.216 0 16 .784 16 1.75v7.5A1.75 1.75 0 0 1 14.25 11h-7.5A1.75 1.75 0 0 1 5 9.25Zm1.75-.25a.25.25 0 0 0-.25.25v7.5c0 .138.112.25.25.25h7.5a.25.25 0 0 0 .25-.25v-7.5a.25.25 0 0 0-.25-.25Z"></path></svg>'; |
| 25 |
} |
| 26 |
|
| 27 |
public static function get_svg_checked_icon() { |
| 28 |
return '<svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="check-icon" fill="currentColor"><path d="M13.78 4.22a.75.75 0 0 1 0 1.06l-7.25 7.25a.75.75 0 0 1-1.06 0L2.22 9.28a.751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018L6 10.94l6.72-6.72a.75.75 0 0 1 1.06 0Z"></path></svg>'; |
| 29 |
} |
| 30 |
|
| 31 |
public static function get_copy_button( $args = [] ) { |
| 32 |
$show_icon = isset( $args['show_icon'] ) ? $args['show_icon'] : 'yes'; |
| 33 |
$with_icon = 'yes' === $show_icon ? 'with-icon' : 'without-icon'; |
| 34 |
$as_raw = isset( $args['as_raw'] ) ? 'yes' : ''; |
| 35 |
$button_text = isset( $args['button_text'] ) ? $args['button_text'] : esc_html__( 'Copy', 'copy-the-code' ); |
| 36 |
$button_class = isset( $args['button_class'] ) ? $args['button_class'] : ''; |
| 37 |
$button_text_copied = isset( $args['button_text_copied'] ) ? $args['button_text_copied'] : esc_html__( 'Copied!', 'copy-the-code' ); |
| 38 |
$icon_direction = isset( $args['icon_direction'] ) ? $args['icon_direction'] : 'before'; |
| 39 |
|
| 40 |
ob_start(); |
| 41 |
?> |
| 42 |
<button class="ctc-block-copy ctc-<?php echo esc_attr( $with_icon ); ?>" copy-as-raw='<?php echo esc_html( $as_raw ); ?>' data-copied="<?php echo esc_html( $button_text_copied ); ?>"> |
| 43 |
<?php |
| 44 |
if ( 'before' === $icon_direction && 'yes' === $show_icon ) { |
| 45 |
echo self::get_svg_copy_icon(); |
| 46 |
echo self::get_svg_checked_icon(); |
| 47 |
} |
| 48 |
echo '<span class="ctc-button-text">' . esc_html( $button_text ) . '</span>'; |
| 49 |
if ( 'after' === $icon_direction && 'yes' === $show_icon ) { |
| 50 |
echo self::get_svg_copy_icon(); |
| 51 |
echo self::get_svg_checked_icon(); |
| 52 |
} |
| 53 |
?> |
| 54 |
</button> |
| 55 |
<?php |
| 56 |
return ob_get_clean(); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Is shortcode used |
| 61 |
* |
| 62 |
* @param string $shortcode |
| 63 |
* @since 3.1.0 |
| 64 |
*/ |
| 65 |
public static function is_shortcode_used( $shortcode = '' ) { |
| 66 |
global $post; |
| 67 |
if ( ! $post ) { |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
$found = false; |
| 72 |
if ( has_shortcode( $post->post_content, $shortcode ) ) { |
| 73 |
$found = true; |
| 74 |
} |
| 75 |
|
| 76 |
return $found; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get categories |
| 81 |
* |
| 82 |
* @since 3.2.0 |
| 83 |
*/ |
| 84 |
public static function get_categories() { |
| 85 |
return [ 'copy-the-code', 'basic' ]; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get common keywords |
| 90 |
* |
| 91 |
* @param array $keywords New keywords. |
| 92 |
* @since 3.2.0 |
| 93 |
*/ |
| 94 |
public static function get_keywords( $keywords = [] ) { |
| 95 |
$default = [ 'copy', 'paste', 'clipboard', 'copy clipboard', 'copy to clipboard', 'copy anything to clipboard' ]; |
| 96 |
|
| 97 |
return array_merge( $default, $keywords ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Register "Copy Content" Section |
| 102 |
* |
| 103 |
* @param object $self |
| 104 |
*/ |
| 105 |
public static function register_copy_content_section( $self, $args = [] ) { |
| 106 |
$self->start_controls_section( |
| 107 |
'copy_content_section', |
| 108 |
[ |
| 109 |
'label' => esc_html__( 'Copy Content', 'copy-the-code' ), |
| 110 |
] |
| 111 |
); |
| 112 |
|
| 113 |
$self->add_control( |
| 114 |
'copy_content', |
| 115 |
[ |
| 116 |
'label' => esc_html__( 'Enter Text that Copy to Clipboard', 'copy-the-code' ), |
| 117 |
'type' => Controls_Manager::TEXTAREA, |
| 118 |
'default' => isset( $args[ 'default' ] ) ? $args[ 'default' ] : '', |
| 119 |
'rows' => 10, |
| 120 |
] |
| 121 |
); |
| 122 |
|
| 123 |
$self->end_controls_section(); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Register "Copy Button" Section |
| 128 |
* |
| 129 |
* @param array $args |
| 130 |
* @param object $self |
| 131 |
*/ |
| 132 |
public static function register_copy_button_section( $self, $args = [] ) { |
| 133 |
$default = [ |
| 134 |
'button_text' => isset( $args[ 'button_text' ] ) ? $args[ 'button_text' ] : esc_html__( 'Copy to Clipboard', 'copy-the-code' ), |
| 135 |
'button_text_copied' => isset( $args[ 'button_text_copied' ] ) ? $args[ 'button_text_copied' ] : esc_html__( 'Copied!', 'copy-the-code' ), |
| 136 |
'show_icon' => 'yes', |
| 137 |
'icon_direction' => 'before', |
| 138 |
]; |
| 139 |
|
| 140 |
$self->start_controls_section( |
| 141 |
'copy_button_section', |
| 142 |
[ |
| 143 |
'label' => esc_html__( 'Copy Button', 'copy-the-code' ), |
| 144 |
] |
| 145 |
); |
| 146 |
|
| 147 |
$self->add_control( |
| 148 |
'copy_button_text', |
| 149 |
[ |
| 150 |
'label' => esc_html__( 'Button Text', 'copy-the-code' ), |
| 151 |
'type' => Controls_Manager::TEXT, |
| 152 |
'default' => $default[ 'button_text' ], |
| 153 |
] |
| 154 |
); |
| 155 |
|
| 156 |
$self->add_control( |
| 157 |
'copy_button_text_copied', |
| 158 |
[ |
| 159 |
'label' => esc_html__( 'After Copy Text', 'copy-the-code' ), |
| 160 |
'type' => Controls_Manager::TEXT, |
| 161 |
'default' => $default[ 'button_text_copied' ], |
| 162 |
] |
| 163 |
); |
| 164 |
|
| 165 |
// Show Icon. |
| 166 |
$self->add_control( |
| 167 |
'copy_show_icon', |
| 168 |
[ |
| 169 |
'label' => esc_html__( 'Show Icon', 'copy-the-code' ), |
| 170 |
'type' => Controls_Manager::SWITCHER, |
| 171 |
'label_on' => esc_html__( 'Show', 'copy-the-code' ), |
| 172 |
'label_off' => esc_html__( 'Hide', 'copy-the-code' ), |
| 173 |
'return_value' => 'yes', |
| 174 |
'default' => 'yes', |
| 175 |
] |
| 176 |
); |
| 177 |
|
| 178 |
$self->add_control( |
| 179 |
'copy_icon_direction', |
| 180 |
[ |
| 181 |
'label' => esc_html__( 'Icon Direction', 'copy-the-code' ), |
| 182 |
'type' => Controls_Manager::SELECT, |
| 183 |
'default' => 'before', |
| 184 |
'options' => [ |
| 185 |
'before' => esc_html__( 'Before', 'copy-the-code' ), |
| 186 |
'after' => esc_html__( 'After', 'copy-the-code' ), |
| 187 |
], |
| 188 |
'condition' => [ |
| 189 |
'copy_show_icon' => 'yes', |
| 190 |
], |
| 191 |
] |
| 192 |
); |
| 193 |
|
| 194 |
$self->add_responsive_control( |
| 195 |
'copy_icon_text_gap', |
| 196 |
[ |
| 197 |
'label' => esc_html__( 'Icon and Text Gap', 'copy-the-code' ), |
| 198 |
'type' => Controls_Manager::SLIDER, |
| 199 |
'size_units' => [ 'px', 'em' ], |
| 200 |
'range' => [ |
| 201 |
'px' => [ |
| 202 |
'min' => 0, |
| 203 |
'max' => 100, |
| 204 |
], |
| 205 |
'em' => [ |
| 206 |
'min' => 0, |
| 207 |
'max' => 10, |
| 208 |
], |
| 209 |
], |
| 210 |
'selectors' => [ |
| 211 |
'{{WRAPPER}} .ctc-with-icon' => 'gap: {{SIZE}}{{UNIT}};', |
| 212 |
], |
| 213 |
'condition' => [ |
| 214 |
'copy_show_icon' => 'yes', |
| 215 |
], |
| 216 |
] |
| 217 |
); |
| 218 |
|
| 219 |
$self->end_controls_section(); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Register "Copy Button Style" Section |
| 224 |
* |
| 225 |
* @param object $self |
| 226 |
*/ |
| 227 |
public static function register_copy_button_style_section( $self ) { |
| 228 |
$self->start_controls_section( |
| 229 |
'copy_button_style_section', |
| 230 |
[ |
| 231 |
'label' => esc_html__( 'Copy Button', 'copy-the-code' ), |
| 232 |
'tab' => Controls_Manager::TAB_STYLE, |
| 233 |
] |
| 234 |
); |
| 235 |
|
| 236 |
$self->add_control( |
| 237 |
'copy_button_align', |
| 238 |
[ |
| 239 |
'label' => esc_html__( 'Alignment', 'copy-the-code' ), |
| 240 |
'type' => Controls_Manager::SELECT, |
| 241 |
'default' => 'left', |
| 242 |
'options' => [ |
| 243 |
'' => esc_html__( 'Default', 'copy-the-code' ), |
| 244 |
'left' => esc_html__( 'Left', 'copy-the-code' ), |
| 245 |
'center' => esc_html__( 'Center', 'copy-the-code' ), |
| 246 |
'right' => esc_html__( 'Right', 'copy-the-code' ), |
| 247 |
], |
| 248 |
'selectors' => [ |
| 249 |
'{{WRAPPER}} .ctc-block-actions' => 'text-align: {{VALUE}};', |
| 250 |
], |
| 251 |
] |
| 252 |
); |
| 253 |
|
| 254 |
// Tabs. |
| 255 |
$self->start_controls_tabs( |
| 256 |
'copy_button_style_tabs' |
| 257 |
); |
| 258 |
|
| 259 |
// button Style Normal. |
| 260 |
$self->start_controls_tab( |
| 261 |
'copy_button_style_normal_tab', |
| 262 |
[ |
| 263 |
'label' => esc_html__( 'Normal', 'copy-the-code' ), |
| 264 |
] |
| 265 |
); |
| 266 |
|
| 267 |
$self->add_group_control( |
| 268 |
Group_Control_Background::get_type(), |
| 269 |
[ |
| 270 |
'name' => 'copy_button_normal_background', |
| 271 |
'types' => [ 'classic', 'gradient' ], |
| 272 |
'selector' => '{{WRAPPER}} .ctc-block-copy', |
| 273 |
'exclude' => [ 'image' ], |
| 274 |
] |
| 275 |
); |
| 276 |
|
| 277 |
$self->add_control( |
| 278 |
'copy_button_normal_text_color', |
| 279 |
[ |
| 280 |
'label' => esc_html__( 'Text Color', 'copy-the-code' ), |
| 281 |
'type' => Controls_Manager::COLOR, |
| 282 |
'selectors' => [ |
| 283 |
'{{WRAPPER}} .ctc-block-copy' => 'color: {{VALUE}};', |
| 284 |
'{{WRAPPER}} .ctc-block-copy svg' => 'fill: {{VALUE}};', |
| 285 |
], |
| 286 |
] |
| 287 |
); |
| 288 |
|
| 289 |
$self->add_group_control( |
| 290 |
Group_Control_Typography::get_type(), |
| 291 |
[ |
| 292 |
'name' => 'copy_button_normal_typography', |
| 293 |
'selector' => '{{WRAPPER}} .ctc-block-copy', |
| 294 |
] |
| 295 |
); |
| 296 |
|
| 297 |
$self->add_group_control( |
| 298 |
Group_Control_Border::get_type(), |
| 299 |
[ |
| 300 |
'name' => 'copy_button_normal_border', |
| 301 |
'selector' => '{{WRAPPER}} .ctc-block-copy', |
| 302 |
] |
| 303 |
); |
| 304 |
|
| 305 |
$self->add_group_control( |
| 306 |
Group_Control_Box_Shadow::get_type(), |
| 307 |
[ |
| 308 |
'name' => 'copy_button_normal_box_shadow', |
| 309 |
'selector' => '{{WRAPPER}} .ctc-block-copy', |
| 310 |
] |
| 311 |
); |
| 312 |
|
| 313 |
$self->end_controls_tab(); |
| 314 |
// button Style Normal End. |
| 315 |
|
| 316 |
// button Style Hover. |
| 317 |
$self->start_controls_tab( |
| 318 |
'copy_button_style_hover_tab', |
| 319 |
[ |
| 320 |
'label' => esc_html__( 'Hover', 'copy-the-code' ), |
| 321 |
] |
| 322 |
); |
| 323 |
|
| 324 |
$self->add_group_control( |
| 325 |
Group_Control_Background::get_type(), |
| 326 |
[ |
| 327 |
'name' => 'copy_button_hover_background', |
| 328 |
'types' => [ 'classic', 'gradient' ], |
| 329 |
'selector' => '{{WRAPPER}} .ctc-block-copy:hover', |
| 330 |
'exclude' => [ 'image' ], |
| 331 |
] |
| 332 |
); |
| 333 |
|
| 334 |
$self->add_control( |
| 335 |
'copy_button_hover_text_color', |
| 336 |
[ |
| 337 |
'label' => esc_html__( 'Text Color', 'copy-the-code' ), |
| 338 |
'type' => Controls_Manager::COLOR, |
| 339 |
'selectors' => [ |
| 340 |
'{{WRAPPER}} .ctc-block-copy:hover' => 'color: {{VALUE}};', |
| 341 |
'{{WRAPPER}} .ctc-block-copy:hover svg' => 'fill: {{VALUE}};', |
| 342 |
], |
| 343 |
] |
| 344 |
); |
| 345 |
|
| 346 |
$self->add_group_control( |
| 347 |
Group_Control_Border::get_type(), |
| 348 |
[ |
| 349 |
'name' => 'copy_button_hover_border', |
| 350 |
'selector' => '{{WRAPPER}} .ctc-block-copy:hover', |
| 351 |
] |
| 352 |
); |
| 353 |
|
| 354 |
$self->add_group_control( |
| 355 |
Group_Control_Box_Shadow::get_type(), |
| 356 |
[ |
| 357 |
'name' => 'copy_button_hover_box_shadow', |
| 358 |
'selector' => '{{WRAPPER}} .ctc-block-copy:hover', |
| 359 |
] |
| 360 |
); |
| 361 |
|
| 362 |
$self->end_controls_tab(); |
| 363 |
// button Style Hover End. |
| 364 |
|
| 365 |
$self->end_controls_tabs(); |
| 366 |
// button Tabs. |
| 367 |
|
| 368 |
$self->add_responsive_control( |
| 369 |
'copy_button_border_radius', |
| 370 |
[ |
| 371 |
'label' => esc_html__( 'Border Radius', 'copy-the-code' ), |
| 372 |
'type' => Controls_Manager::DIMENSIONS, |
| 373 |
'size_units' => [ 'px', '%' ], |
| 374 |
'selectors' => [ |
| 375 |
'{{WRAPPER}} .ctc-block-copy' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 376 |
], |
| 377 |
] |
| 378 |
); |
| 379 |
|
| 380 |
$self->add_responsive_control( |
| 381 |
'copy_button_padding', |
| 382 |
[ |
| 383 |
'label' => esc_html__( 'Padding', 'copy-the-code' ), |
| 384 |
'type' => Controls_Manager::DIMENSIONS, |
| 385 |
'size_units' => [ 'px', 'em', '%' ], |
| 386 |
'selectors' => [ |
| 387 |
'{{WRAPPER}} .ctc-block-copy' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 388 |
], |
| 389 |
] |
| 390 |
); |
| 391 |
|
| 392 |
$self->add_responsive_control( |
| 393 |
'copy_button_margin', |
| 394 |
[ |
| 395 |
'label' => esc_html__( 'Margin', 'copy-the-code' ), |
| 396 |
'type' => Controls_Manager::DIMENSIONS, |
| 397 |
'size_units' => [ 'px', 'em', '%' ], |
| 398 |
'selectors' => [ |
| 399 |
'{{WRAPPER}} .ctc-block-copy' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 400 |
], |
| 401 |
] |
| 402 |
); |
| 403 |
|
| 404 |
$self->end_controls_section(); |
| 405 |
// button Style Section End. |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* Render "Copy Button" |
| 410 |
* |
| 411 |
* @param object $self |
| 412 |
*/ |
| 413 |
public static function render_copy_button( $self ) { |
| 414 |
$button_text = $self->get_settings_for_display( 'copy_button_text' ); |
| 415 |
$button_text_copied = $self->get_settings_for_display( 'copy_button_text_copied' ); |
| 416 |
$show_icon = $self->get_settings_for_display( 'copy_show_icon' ); |
| 417 |
$icon_direction = $self->get_settings_for_display( 'copy_icon_direction' ); |
| 418 |
|
| 419 |
echo Helpers::get_copy_button( [ |
| 420 |
'as_raw' => 'yes', |
| 421 |
'button_text' => $button_text, |
| 422 |
'button_text_copied' => $button_text_copied, |
| 423 |
'icon_direction' => $icon_direction, |
| 424 |
'show_icon' => $show_icon, |
| 425 |
] ); |
| 426 |
} |
| 427 |
|
| 428 |
/** |
| 429 |
* Render "Copy Content" |
| 430 |
* |
| 431 |
* @param object $self |
| 432 |
*/ |
| 433 |
public static function render_copy_content( $self ) { |
| 434 |
$copy_content = $self->get_settings_for_display( 'copy_content' ); |
| 435 |
?> |
| 436 |
<textarea class="ctc-copy-content" style="display: none;"><?php echo esc_html( $copy_content ); ?></textarea> |
| 437 |
<?php |
| 438 |
} |
| 439 |
|
| 440 |
} |