Repository navigation
Expand file tree
/
Copy pathcopy-button.php
More file actions
60 lines (52 loc) · 1.84 KB
/
Copy pathcopy-button.php
File metadata and controls
60 lines (52 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/**
* Plugin Name: Copy Button
* Description: A block to create a button to copy some text to clipboard
* Requires at least: 6.1
* Requires PHP: 7.0
* Version: 0.1.1
* Author: Dotcamp
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: dotcamp/copy-button
*
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
function dotcamp_render_copy_button_block($attributes) {
$text = isset($attributes['text']) ? esc_attr($attributes['text']) : '';
$button_text = isset($attributes['buttonText']) ? esc_html($attributes['buttonText']) : 'Copy';
$copied_text = isset($attributes['copiedText']) ? esc_html($attributes['copiedText']) : 'Copied!';
$button_color = isset($attributes['buttonColor']) ? esc_attr($attributes['buttonColor']) : '#0073aa';
$text_color = isset($attributes['textColor']) ? esc_attr($attributes['textColor']) : '#ffffff';
$button_size = isset($attributes['buttonSize']) ? esc_attr($attributes['buttonSize']) : 'medium';
$alignment = isset($attributes['alignment']) ? esc_attr($attributes['alignment']) : 'left';
$wrapper_attributes = get_block_wrapper_attributes([
'style' => "text-align: $alignment;",
]);
$button_style = sprintf(
'background-color: %s; color: %s;',
$button_color,
$text_color
);
$button_attributes = sprintf(
'class="copy-button %s" style="%s" data-text="%s" data-copied-text="%s"',
$button_size,
$button_style,
$text,
$copied_text
);
return sprintf(
'<div %s><button %s>%s</button></div>',
$wrapper_attributes,
$button_attributes,
$button_text
);
}
function dotcamp_copy_button_block_init() {
register_block_type( __DIR__ . '/build', [
'render_callback' => 'dotcamp_render_copy_button_block'
] );
}
add_action( 'init', 'dotcamp_copy_button_block_init' );