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

138 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * 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 * Icon class.
17 */
18 class Icon {
19
20 /**
21 * Instance
22 *
23 * @var Icon|null
24 */
25 private static $instance = null;
26
27 /**
28 * Get instance.
29 *
30 * @return 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_editor_assets', [ $this, 'enqueue_editor_assets' ] );
45 add_action( 'enqueue_block_assets', [ $this, 'enqueue_frontend_assets' ] );
46 }
47
48 /**
49 * Enqueue editor-only assets (block registration JS).
50 *
51 * Styles for this block are registered via block.json (style/editorStyle).
52 */
53 public function enqueue_editor_assets() {
54 wp_enqueue_script(
55 'ctc-gb-icon',
56 CTC_GUTENBERG_URI . 'blocks/icon/js/icon.js',
57 [ 'wp-blocks', 'wp-element', 'wp-editor', 'wp-server-side-render' ],
58 CTC_VER,
59 true
60 );
61 }
62
63 /**
64 * Enqueue frontend assets (styles and copy functionality).
65 * Only enqueues when the Icon block is present in the current post/content.
66 */
67 public function enqueue_frontend_assets() {
68 if ( is_admin() ) {
69 return;
70 }
71 if ( ! $this->current_content_has_icon_block() ) {
72 return;
73 }
74
75 wp_enqueue_script( 'ctc-lib-core', CTC_URI . 'assets/frontend/js/lib/ctc.js', [ 'jquery' ], CTC_VER, true );
76 wp_enqueue_script( 'ctc-core', CTC_URI . 'includes/assets/js/core.js', [ 'ctc-lib-core' ], CTC_VER, true );
77
78 $post = get_post();
79 wp_localize_script(
80 'ctc-core',
81 'ctcBlockAnalytics',
82 [
83 'eventsUrl' => rest_url( 'ctc/v1/analytics/events' ),
84 'postId' => $post instanceof \WP_Post ? $post->ID : null,
85 'postType' => $post instanceof \WP_Post ? $post->post_type : null,
86 ]
87 );
88 }
89
90 /**
91 * Whether the current queried post (or global post) contains the Icon block.
92 *
93 * @return bool
94 */
95 private function current_content_has_icon_block() {
96 $post = get_post();
97 if ( ! $post instanceof \WP_Post ) {
98 return false;
99 }
100 return has_block( 'copy-the-code/icon', $post );
101 }
102
103 /**
104 * Initialize.
105 */
106 public function init() {
107 register_block_type(
108 CTC_GUTENBERG_DIR . 'blocks/icon/block.json',
109 [
110 'render_callback' => [ $this, 'render' ],
111 ]
112 );
113 }
114
115 /**
116 * Render.
117 *
118 * @param array $attributes Block attributes.
119 * @param string $content Block content.
120 * @return string
121 */
122 public function render( $attributes, $content ) {
123 $alignment = isset( $attributes['alignment'] ) ? $attributes['alignment'] : 'left';
124 $copy_content = isset( $attributes['content'] ) ? $attributes['content'] : '';
125 ob_start();
126 ?>
127 <div class="ctc-block ctc-copy-icon" data-ctc-analytics="1" data-ctc-source="gutenberg-block" data-ctc-block-type="copy-the-code/icon" style="text-align: <?php echo esc_attr( $alignment ); ?>">
128 <span copy-as-raw="yes" class="ctc-block-copy ctc-block-copy-icon" role="button" aria-label="Copied">
129 <?php echo Helpers::get_svg_copy_icon(); // phpcs:ignore ?>
130 <?php echo Helpers::get_svg_checked_icon(); // phpcs:ignore ?>
131 </span>
132 <textarea class="ctc-copy-content" style="display: none;"><?php echo wp_kses_post( apply_shortcodes( $copy_content ) ); ?></textarea>
133 </div>
134 <?php
135 return ob_get_clean();
136 }
137 }
138