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-icon / 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-icon/class-block.php

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