| @@ -1,85 +1,105 @@ | ||
| 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 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace Getwid\Blocks; | |
| 4 | + | |
| 5 | +abstract class AbstractBlock { | |
| 6 | + | |
| 7 | + protected $block_name; | |
| 8 | + | |
| 9 | + public function __construct( $block_name ) { | |
| 10 | + | |
| 11 | + $this->block_name = $block_name; | |
| 12 | + | |
| 13 | + if ( $this->is_disabled() ) { | |
| 14 | + | |
| 15 | + // https://developer.wordpress.org/reference/functions/render_block/ | |
| 16 | + add_filter( 'pre_render_block', array( $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 has_block() { | |
| 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->block_name ); | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Determines whether a $post contains a specific block. | |
| 35 | + */ | |
| 36 | + return apply_filters( 'getwid/blocks/has_block', $has_block, $this->block_name ); | |
| 37 | + } | |
| 38 | + | |
| 39 | + public function get_block_name() { | |
| 40 | + return $this->block_name; | |
| 41 | + } | |
| 42 | + | |
| 43 | + public function get_label() { | |
| 44 | + return $this->block_name; | |
| 45 | + } | |
| 46 | + | |
| 47 | + public function get_disabled_option_key() { | |
| 48 | + return $this->block_name . '::disabled'; | |
| 49 | + } | |
| 50 | + | |
| 51 | + public function is_disabled() { | |
| 52 | + | |
| 53 | + $disabled = rest_sanitize_boolean( get_option( $this->get_disabled_option_key(), false ) ); | |
| 54 | + | |
| 55 | + getwid_maybe_add_option( $this->get_disabled_option_key(), false, true ); | |
| 56 | + | |
| 57 | + return apply_filters( 'getwid/blocks/is_disabled', $disabled, $this->block_name ); | |
| 58 | + } | |
| 59 | + | |
| 60 | + public function is_enabled() { | |
| 61 | + | |
| 62 | + return ! $this->is_disabled(); | |
| 63 | + } | |
| 64 | + | |
| 65 | + public function can_be_disabled() { | |
| 66 | + return true; | |
| 67 | + } | |
| 68 | + | |
| 69 | + public function pre_render_block( $block_content, $block ) { | |
| 70 | + | |
| 71 | + if ( $block['blockName'] === $this->block_name ) { | |
| 72 | + | |
| 73 | + $block_content = '<!-- ' . esc_html( $block['blockName'] ) . ' block is disabled -->' . PHP_EOL; | |
| 74 | + | |
| 75 | + if ( current_user_can( 'manage_options' ) ) { | |
| 76 | + $block_content .= '<p>'; | |
| 77 | + $block_content .= sprintf( | |
| 78 | + // translators: %1$s is a block name, %2$s is a link | |
| 79 | + __( '%1$s block is disabled in plugin settings. <a href="%2$s">Manage Blocks</a>', 'getwid' ), | |
| 80 | + esc_html( $this->get_label() ), | |
| 81 | + esc_url( getwid()->settingsPage()->getTabUrl( 'blocks' ) ) | |
| 82 | + ); | |
| 83 | + $block_content .= '</p>'; | |
| 84 | + } | |
| 85 | + } | |
| 86 | + | |
| 87 | + return $block_content; | |
| 88 | + } | |
| 89 | + | |
| 90 | + protected function validate_heading_html_tag( $tag ) { | |
| 91 | + | |
| 92 | + $allowed_tags = array( | |
| 93 | + 'h1', | |
| 94 | + 'h2', | |
| 95 | + 'h3', | |
| 96 | + 'h4', | |
| 97 | + 'h5', | |
| 98 | + 'h6', | |
| 99 | + 'span', | |
| 100 | + 'p', | |
| 101 | + ); | |
| 102 | + | |
| 103 | + return in_array( strtolower( $tag ), $allowed_tags ) ? $tag : 'span'; | |
| 104 | + } | |
| 105 | +} | |