| 1 |
<?php |
| 2 |
/** |
| 3 |
* Shortcode |
| 4 |
* |
| 5 |
* @package Copy the Code |
| 6 |
* @since 2.2.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
use CopyTheCode\Helpers; |
| 10 |
|
| 11 |
if ( ! class_exists( 'Copy_The_Code_Shortcode' ) ) : |
| 12 |
|
| 13 |
/** |
| 14 |
* Shortcode |
| 15 |
* |
| 16 |
* @since 2.2.0 |
| 17 |
*/ |
| 18 |
class Copy_The_Code_Shortcode { |
| 19 |
|
| 20 |
/** |
| 21 |
* Instance |
| 22 |
* |
| 23 |
* @access private |
| 24 |
* @var object Class Instance. |
| 25 |
* @since 2.2.0 |
| 26 |
*/ |
| 27 |
private static $instance; |
| 28 |
|
| 29 |
/** |
| 30 |
* Initiator |
| 31 |
* |
| 32 |
* @since 2.2.0 |
| 33 |
* @return object initialized object of class. |
| 34 |
*/ |
| 35 |
public static function get_instance() { |
| 36 |
if ( ! isset( self::$instance ) ) { |
| 37 |
self::$instance = new self; |
| 38 |
} |
| 39 |
return self::$instance; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor |
| 44 |
*/ |
| 45 |
public function __construct() { |
| 46 |
add_shortcode( 'copy', array( $this, 'shortcode_markup' ) ); |
| 47 |
add_shortcode( 'copy_inline', array( $this, 'copy_inline_markup' ) ); |
| 48 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Enqueue scripts |
| 53 |
* |
| 54 |
* @since 3.1.0 |
| 55 |
*/ |
| 56 |
public function enqueue_scripts() { |
| 57 |
// Enqueue script only if shortcode is used. |
| 58 |
if ( Helpers::is_shortcode_used( 'copy_inline' ) ) { |
| 59 |
// Core. |
| 60 |
wp_enqueue_script( 'ctc-clipboard', COPY_THE_CODE_URI . 'assets/js/clipboard.js', array( 'jquery' ), COPY_THE_CODE_VER, true ); |
| 61 |
|
| 62 |
// Shortcode. |
| 63 |
wp_enqueue_style( 'ctc-copy-inline', COPY_THE_CODE_URI . 'assets/css/copy-inline.css', array(), COPY_THE_CODE_VER ); |
| 64 |
wp_enqueue_script( 'ctc-copy-inline', COPY_THE_CODE_URI . 'assets/js/copy-inline.js', array( 'jquery' ), COPY_THE_CODE_VER, true ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Copy inline markup |
| 70 |
* |
| 71 |
* @since 3.1.0 |
| 72 |
* |
| 73 |
* @param array $atts Shortcode parameters. |
| 74 |
* @param mixed $content Shortcode content. |
| 75 |
* |
| 76 |
* @return mixed |
| 77 |
*/ |
| 78 |
public function copy_inline_markup( $atts = array(), $content = '' ) { |
| 79 |
$atts = shortcode_atts( [ |
| 80 |
'text' => '', |
| 81 |
'display' => '', |
| 82 |
'hidden' => '', |
| 83 |
'style' => '', |
| 84 |
'tooltip' => esc_html__( 'Copied', 'copy-the-code' ), |
| 85 |
], $atts ); |
| 86 |
|
| 87 |
$display = $atts['display'] ? $atts['display'] : $atts['text']; |
| 88 |
$style = $atts['style'] ? 'ctc-inline-style-' . $atts['style'] : ''; |
| 89 |
$hidden = 'yes' === $atts['hidden'] ? 'ctc-inline-hidden' : ''; |
| 90 |
$tooltip = esc_html__( 'Copied', 'copy-the-code' ); |
| 91 |
|
| 92 |
ob_start(); |
| 93 |
?> |
| 94 |
<span class="ctc-inline-copy <?php echo esc_attr( $style ); ?>" aria-label="<?php echo esc_attr( $tooltip ); ?>"> |
| 95 |
<span class="ctc-inline-copy-text <?php echo esc_attr( $hidden ); ?>"><?php echo esc_html( $display ); ?></span> |
| 96 |
<span class="ctc-inline-copy-icon" role="button" aria-label="<?php echo esc_attr( $tooltip ); ?>"> |
| 97 |
<?php echo Helpers::get_svg_copy_icon(); ?> |
| 98 |
<?php echo Helpers::get_svg_checked_icon(); ?> |
| 99 |
</span> |
| 100 |
</span> |
| 101 |
<?php |
| 102 |
return ob_get_clean(); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Shortcode markup |
| 107 |
* |
| 108 |
* @since 2.2.0 |
| 109 |
* @param array $atts Shortcode parameters. |
| 110 |
* @param mixed $content Shortcode content. |
| 111 |
* @return mixed |
| 112 |
*/ |
| 113 |
public function shortcode_markup( $atts = array(), $content = '' ) { |
| 114 |
$atts = apply_filters( |
| 115 |
'copy_the_code_shortcode_atts', |
| 116 |
shortcode_atts( |
| 117 |
array( |
| 118 |
'target' => '', |
| 119 |
'title' => __( 'Copy to Clipboard', 'copy-the-code' ), |
| 120 |
'text' => $content, |
| 121 |
'copied-text' => 'Copied to Clipboard', |
| 122 |
'tag' => 'span', |
| 123 |
'class' => '', |
| 124 |
'copy-as' => 'text', |
| 125 |
'content' => $content, |
| 126 |
'link' => isset( $atts['link'] ) ? $atts['link'] : '', |
| 127 |
'style' => '', |
| 128 |
'color' => '', |
| 129 |
'icon-color' => '', |
| 130 |
), |
| 131 |
$atts |
| 132 |
) |
| 133 |
); |
| 134 |
|
| 135 |
$color = $atts['color']; |
| 136 |
$display_content = wp_kses_post( $atts['text'] ); |
| 137 |
if ( 'icon' === $atts['style'] ) { |
| 138 |
$icon_color = ! empty( $atts['icon-color'] ) ? $atts['icon-color'] : '#b5b5b5'; |
| 139 |
|
| 140 |
$display_content = '<svg style="fill: ' . esc_attr( $icon_color ) . '" viewBox="-21 0 512 512" xmlns="http://www.w3.org/2000/svg"><path d="m186.667969 416c-49.984375 0-90.667969-40.683594-90.667969-90.667969v-218.664062h-37.332031c-32.363281 0-58.667969 26.300781-58.667969 58.664062v288c0 32.363281 26.304688 58.667969 58.667969 58.667969h266.664062c32.363281 0 58.667969-26.304688 58.667969-58.667969v-37.332031zm0 0"></path><path d="m469.332031 58.667969c0-32.40625-26.261719-58.667969-58.664062-58.667969h-224c-32.40625 0-58.667969 26.261719-58.667969 58.667969v266.664062c0 32.40625 26.261719 58.667969 58.667969 58.667969h224c32.402343 0 58.664062-26.261719 58.664062-58.667969zm0 0"></path></svg>'; |
| 141 |
} |
| 142 |
|
| 143 |
return '<' . esc_html( $atts['tag'] ) . ' title="' . esc_attr( $atts['title'] ) . '" class="copy-the-code-shortcode ' . esc_attr( $atts['class'] ) . '" data-target="' . esc_attr( $atts['target'] ) . '" data-button-text="' . esc_attr( $atts['text'] ) . '" data-button-copy-text="' . esc_attr( $atts['copied-text'] ) . '" data-content="' . esc_attr( wp_strip_all_tags( $atts['content'] ) ) . '" data-copy-as="' . esc_attr( $atts['copy-as'] ) . '" data-link="' . esc_attr( $atts['link'] ) . '" style="color: ' . esc_attr( $color ) . '" >' . $display_content . '</' . esc_html( $atts['tag'] ) . '>'; |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Kicking this off by calling 'get_instance()' method |
| 150 |
*/ |
| 151 |
Copy_The_Code_Shortcode::get_instance(); |
| 152 |
|
| 153 |
endif; |
| 154 |
|