| 1 |
<?php |
| 2 |
|
| 3 |
namespace Getwid\Blocks; |
| 4 |
|
| 5 |
abstract class AbstractBlock { |
| 6 |
|
| 7 |
private $blockName; |
| 8 |
|
| 9 |
public function __construct( $blockName ) { |
| 10 |
|
| 11 |
$this->blockName = $blockName; |
| 12 |
|
| 13 |
if ( $this->isDisabled() ) { |
| 14 |
|
| 15 |
// https://developer.wordpress.org/reference/functions/render_block/ |
| 16 |
add_filter( 'pre_render_block', [ $this, 'pre_render_block' ], 10, 2 ); |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Assets optimization. Currently in Beta. |
| 22 |
* @since 1.5.3 |
| 23 |
*/ |
| 24 |
public function hasBlock() { |
| 25 |
|
| 26 |
/** |
| 27 |
* has_block doesn't return true when a block is inside a reusable block |
| 28 |
* https://github.com/WordPress/gutenberg/issues/18272 |
| 29 |
*/ |
| 30 |
|
| 31 |
$has_block = has_block( $this->blockName ); |
| 32 |
|
| 33 |
/** |
| 34 |
* Determines whether a $post contains a specific block. |
| 35 |
*/ |
| 36 |
return apply_filters( 'getwid/blocks/has_block', $has_block, $this->blockName ); |
| 37 |
} |
| 38 |
|
| 39 |
public static function getBlockName() { |
| 40 |
return static::$blockName; |
| 41 |
} |
| 42 |
|
| 43 |
public function getLabel() { |
| 44 |
return static::$blockName; |
| 45 |
} |
| 46 |
|
| 47 |
public function getDisabledOptionKey() { |
| 48 |
return $this->blockName . '::disabled'; |
| 49 |
} |
| 50 |
|
| 51 |
public function isDisabled() { |
| 52 |
|
| 53 |
$disabled = rest_sanitize_boolean( get_option( $this->getDisabledOptionKey(), false ) ); |
| 54 |
|
| 55 |
getwid_maybe_add_option( $this->getDisabledOptionKey(), false, true ); |
| 56 |
|
| 57 |
return apply_filters( 'getwid/blocks/is_disabled', $disabled, $this->blockName ); |
| 58 |
} |
| 59 |
|
| 60 |
public function isEnabled() { |
| 61 |
|
| 62 |
return ! $this->isDisabled(); |
| 63 |
} |
| 64 |
|
| 65 |
public function pre_render_block( $block_content, $block ) { |
| 66 |
|
| 67 |
if ( $block['blockName'] === static::$blockName ) { |
| 68 |
|
| 69 |
$block_content = '<!-- ' . esc_html( $block['blockName'] ) . ' block is disabled -->' . PHP_EOL; |
| 70 |
|
| 71 |
if ( current_user_can('manage_options') ) { |
| 72 |
$block_content .= '<p>'; |
| 73 |
$block_content .= sprintf( |
| 74 |
//translators: %1$s is a block name, %2$s is a link |
| 75 |
__( '%1$s block is disabled in plugin settings. <a href="%2$s">Manage Blocks</a>', 'getwid'), |
| 76 |
esc_html( $this->getLabel() ), |
| 77 |
esc_url( getwid()->settingsPage()->getTabUrl('blocks') ) |
| 78 |
); |
| 79 |
$block_content .= '</p>'; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return $block_content; |
| 84 |
} |
| 85 |
|
| 86 |
protected function validateHeadingHTMLTag( $tag ) { |
| 87 |
|
| 88 |
$allowed_tags = array( |
| 89 |
'h1', |
| 90 |
'h2', |
| 91 |
'h3', |
| 92 |
'h4', |
| 93 |
'h5', |
| 94 |
'h6', |
| 95 |
'span', |
| 96 |
'p', |
| 97 |
); |
| 98 |
|
| 99 |
return in_array( strtolower( $tag ), $allowed_tags ) ? $tag : 'span'; |
| 100 |
} |
| 101 |
} |
| 102 |
|