abstract-block.php
2.01 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace Getwid\Blocks;
abstract class AbstractBlock {
private $blockName;
public function __construct( $blockName ) {
$this->blockName = $blockName;
if ( $this->isDisabled() ) {
// https://developer.wordpress.org/reference/functions/render_block/
add_filter( 'pre_render_block', [ $this, 'pre_render_block' ], 10, 2 );
}
}
/**
* Assets optimization. Currently in Beta.
* @since 1.5.3
*/
public function hasBlock() {
/**
* has_block doesn't return true when a block is inside a reusable block
* https://github.com/WordPress/gutenberg/issues/18272
*/
$has_block = has_block( $this->blockName );
return apply_filters( 'getwid/blocks/has_block', $has_block, $this->blockName );
}
public static function getBlockName() {
return static::$blockName;
}
public function getLabel() {
return static::$blockName;
}
public function getDisabledOptionKey() {
return $this->blockName . '::disabled';
}
public function isDisabled() {
$disabled = rest_sanitize_boolean( get_option( $this->getDisabledOptionKey(), false ) );
getwid_maybe_add_option( $this->getDisabledOptionKey(), false, true );
return apply_filters( 'getwid/blocks/is_disabled', $disabled, $this->blockName );
}
public function isEnabled() {
return ! $this->isDisabled();
}
public function pre_render_block( $block_content, $block ) {
if ( $block['blockName'] === static::$blockName ) {
$block_content = '<!-- ' . esc_html( $block['blockName'] ) . ' block is disabled -->' . PHP_EOL;
if ( current_user_can('manage_options') ) {
$block_content .= '<p>';
$block_content .= sprintf(
//translators: %1$s is a block name, %2$s is a link
__( '%1$s block is disabled in plugin settings. <a href="%2$s">Manage Blocks</a>', 'getwid'),
esc_html( $this->getLabel() ),
esc_url( getwid()->settingsPage()->getTabUrl('blocks') )
);
$block_content .= '</p>';
}
}
return $block_content;
}
}