| 1 |
<?php |
| 2 |
/** |
| 3 |
* Icon Block |
| 4 |
* |
| 5 |
* @package Copy the Code |
| 6 |
* @since 3.7.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace CopyTheCode\Gutenberg\Blocks; |
| 10 |
|
| 11 |
use CopyTheCode\Helpers; |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* Icon class. |
| 17 |
*/ |
| 18 |
class Icon { |
| 19 |
|
| 20 |
/** |
| 21 |
* Constructor. |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
add_action( 'init', [ $this, 'init' ] ); |
| 25 |
add_action( 'enqueue_block_assets', [ $this, 'enqueue_block_assets' ] ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Enqueue block assets. |
| 30 |
*/ |
| 31 |
public function enqueue_block_assets() { |
| 32 |
if ( ! has_block( 'copy-the-code/icon' ) ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
wp_enqueue_script( |
| 37 |
'ctc-gb-icon', |
| 38 |
COPY_THE_CODE_URI . 'classes/gutenberg/blocks/icon/js/icon.js', |
| 39 |
[ 'wp-blocks', 'wp-element', 'wp-editor', 'wp-server-side-render' ], |
| 40 |
COPY_THE_CODE_VER |
| 41 |
); |
| 42 |
|
| 43 |
wp_enqueue_style( |
| 44 |
'ctc-gb-icon', |
| 45 |
COPY_THE_CODE_URI . 'classes/gutenberg/blocks/icon/css/style.css', |
| 46 |
[], |
| 47 |
COPY_THE_CODE_VER, |
| 48 |
'all' |
| 49 |
); |
| 50 |
|
| 51 |
// Core. |
| 52 |
wp_enqueue_script( 'ctc-core', COPY_THE_CODE_URI . 'classes/blocks/assets/js/core.js', [ 'jquery' ], COPY_THE_CODE_VER, true ); |
| 53 |
wp_enqueue_script( 'ctc-clipboard', COPY_THE_CODE_URI . 'assets/js/clipboard.js', [ 'jquery' ], COPY_THE_CODE_VER, true ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Initialize. |
| 58 |
*/ |
| 59 |
public function init() { |
| 60 |
register_block_type( |
| 61 |
COPY_THE_CODE_GUTENBERG_BLOCKS . 'blocks/icon/block.json', |
| 62 |
[ |
| 63 |
'render_callback' => [ $this, 'render' ], |
| 64 |
] |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Render. |
| 70 |
* |
| 71 |
* @param array $attributes Block attributes. |
| 72 |
* @param string $content Block content. |
| 73 |
* |
| 74 |
* @return string |
| 75 |
*/ |
| 76 |
public function render( $attributes, $content ) { |
| 77 |
$alignment = isset( $attributes['alignment'] ) ? $attributes['alignment'] : 'left'; |
| 78 |
$copy_content = isset( $attributes['content'] ) ? $attributes['content'] : ''; |
| 79 |
ob_start(); |
| 80 |
?> |
| 81 |
<div class="ctc-block ctc-copy-icon" style="text-align: <?php echo esc_attr( $alignment ); ?>"> |
| 82 |
<span copy-as-raw="yes" class="ctc-block-copy ctc-block-copy-icon" role="button" aria-label="Copied"> |
| 83 |
<?php echo Helpers::get_svg_copy_icon(); ?> |
| 84 |
<?php echo Helpers::get_svg_checked_icon(); ?> |
| 85 |
</span> |
| 86 |
<textarea class="ctc-copy-content" style="display: none;"><?php echo wp_kses_post( apply_shortcodes( $copy_content ) ); ?></textarea> |
| 87 |
</div> |
| 88 |
<?php |
| 89 |
return ob_get_clean(); |
| 90 |
} |
| 91 |
} |
| 92 |
|