PluginProbe
Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code / 5.5.4
Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code v5.5.4
5.5.4 5.5.3 3.1.0 3.2.0 3.2.1 3.3.0 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.6.0 3.7.0 3.8.0 3.8.1 3.8.2 3.8.3 4.0.0 4.0.2 4.0.3 4.0.4 4.0.5 4.1.0 All 79 releases
copy-the-code / includes / gutenberg / blocks / copy-button / class-block.php

class-block.php in Copy Anything to Clipboard for WordPress – Copy Button, Copy Text & Copy Code 5.5.4, at includes/gutenberg/blocks/copy-button/class-block.php

127 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Copy Button Block
4 *
5 * @package CTC
6 * @since 5.0.0
7 */
8
9 namespace CTC\Gutenberg\Blocks;
10
11 defined( 'ABSPATH' ) || exit;
12
13 /**
14 * Copy Button class.
15 */
16 class Copy_Button {
17
18 /**
19 * Instance
20 *
21 * @var Copy_Button|null
22 */
23 private static $instance = null;
24
25 /**
26 * Get instance.
27 *
28 * @return Copy_Button
29 */
30 public static function get() {
31 if ( null === self::$instance ) {
32 self::$instance = new self();
33 }
34 return self::$instance;
35 }
36
37 /**
38 * Constructor.
39 */
40 private function __construct() {
41 add_action( 'init', [ $this, 'init' ] );
42 add_action( 'enqueue_block_assets', [ $this, 'enqueue_frontend_assets' ] );
43 }
44
45 /**
46 * Initialize the block.
47 */
48 public function init() {
49 register_block_type_from_metadata(
50 __DIR__,
51 [
52 'render_callback' => [ $this, 'render' ],
53 ]
54 );
55 }
56
57 /**
58 * Enqueue frontend assets (copy engine + analytics) when Copy Button block is present.
59 */
60 public function enqueue_frontend_assets() {
61 if ( is_admin() ) {
62 return;
63 }
64
65 $post = get_post();
66 if ( ! $post instanceof \WP_Post ) {
67 return;
68 }
69
70 if ( ! has_block( 'ctc/copy-button', $post ) ) {
71 return;
72 }
73
74 wp_enqueue_script( 'ctc-lib-core', CTC_URI . 'assets/frontend/js/lib/ctc.js', [ 'jquery' ], CTC_VER, true );
75 wp_enqueue_script( 'ctc-core', CTC_URI . 'includes/assets/js/core.js', [ 'ctc-lib-core' ], CTC_VER, true );
76
77 wp_localize_script(
78 'ctc-core',
79 'ctcBlockAnalytics',
80 [
81 'eventsUrl' => rest_url( 'ctc/v1/analytics/events' ),
82 'postId' => $post->ID,
83 'postType' => $post->post_type,
84 ]
85 );
86 }
87
88 /**
89 * Render the block.
90 *
91 * @param array $attributes Block attributes.
92 * @param string $content Block content.
93 * @param WP_Block $block Block instance.
94 * @return string
95 */
96 public function render( $attributes = [], $content = '', $block = null ) {
97 $post_id = $block->context['postId'] ?? null;
98 $post_content = $post_id ? get_post_field( 'post_content', $post_id ) : null;
99 if ( $post_content ) {
100 $post_content = wp_strip_all_tags( $post_content );
101 $post_content = preg_replace( '/<br\s*\/?>/', "\n", $post_content );
102 }
103
104 // Block attributes.
105 $button_text = $attributes['buttonText'] ?? esc_html__( 'Copy to Clipboard', 'ctc' );
106 $content = ! empty( $attributes['content'] ) ? $attributes['content'] : $post_content;
107 $button_text_copied = $attributes['buttonTextCopied'] ?? esc_html__( 'Copied!', 'ctc' );
108 $wrapper_attributes = get_block_wrapper_attributes(
109 [
110 'class' => 'ctc-block-copy ctc-copy-button',
111 'data-copied-text' => $button_text_copied,
112 ]
113 );
114
115 ob_start();
116 ?>
117 <div class="ctc-block ctc-copy-button-wrap" data-ctc-analytics="1" data-ctc-source="gutenberg-block" data-ctc-block-type="ctc/copy-button">
118 <button type="button" <?php echo $wrapper_attributes; // phpcs:ignore ?> data-copied="<?php echo esc_attr( $button_text_copied ); ?>">
119 <span class="ctc-button-text"><?php echo esc_html( $button_text ); ?></span>
120 </button>
121 <textarea class="ctc-copy-content ctc-copy-button-textarea" readonly><?php echo wp_kses_post( $content ); ?></textarea>
122 </div>
123 <?php
124 return ob_get_clean();
125 }
126 }
127