| 1 |
<?php |
| 2 |
/** |
| 3 |
* Block Registry |
| 4 |
* |
| 5 |
* Central registry for email editor blocks with Free/Pro availability. |
| 6 |
* |
| 7 |
* @package Forge12\DoubleOptIn\EmailTemplates |
| 8 |
* @since 4.2.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Forge12\DoubleOptIn\EmailTemplates; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Class BlockRegistry |
| 19 |
* |
| 20 |
* Manages block availability based on Free/Pro status. |
| 21 |
*/ |
| 22 |
class BlockRegistry { |
| 23 |
|
| 24 |
/** |
| 25 |
* Blocks available in the Free version. |
| 26 |
* |
| 27 |
* @var string[] |
| 28 |
*/ |
| 29 |
const FREE_BLOCKS = array( |
| 30 |
'email-wrapper', |
| 31 |
'row', |
| 32 |
'columns-1', |
| 33 |
'heading', |
| 34 |
'text', |
| 35 |
'button', |
| 36 |
'spacer', |
| 37 |
'divider', |
| 38 |
'placeholder-confirm-link', |
| 39 |
'placeholder-optout-link', |
| 40 |
'placeholder-date', |
| 41 |
'placeholder-time', |
| 42 |
'placeholder-url', |
| 43 |
'placeholder-custom', |
| 44 |
); |
| 45 |
|
| 46 |
/** |
| 47 |
* Blocks that require the Pro version. |
| 48 |
* |
| 49 |
* @var string[] |
| 50 |
*/ |
| 51 |
const PRO_BLOCKS = array( |
| 52 |
'columns-2', |
| 53 |
'columns-2-sidebar', |
| 54 |
'columns-3', |
| 55 |
'image', |
| 56 |
'social-icons', |
| 57 |
'header', |
| 58 |
'footer', |
| 59 |
'conditional-content', |
| 60 |
); |
| 61 |
|
| 62 |
/** |
| 63 |
* Get the template limit based on Pro status. |
| 64 |
* |
| 65 |
* @return int Maximum number of published templates allowed. |
| 66 |
*/ |
| 67 |
public function getTemplateLimit(): int { |
| 68 |
return $this->isProActive() ? PHP_INT_MAX : 1; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Check if Pro is active. |
| 73 |
* |
| 74 |
* @return bool |
| 75 |
*/ |
| 76 |
public function isProActive(): bool { |
| 77 |
/** |
| 78 |
* Filter to check if the Pro version is active and licensed. |
| 79 |
* |
| 80 |
* @param bool $isActive Whether Pro is active. Default false. |
| 81 |
* |
| 82 |
* @since 4.2.0 |
| 83 |
*/ |
| 84 |
return (bool) apply_filters( 'f12_doi_is_pro_active', false ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get block availability for all blocks. |
| 89 |
* |
| 90 |
* Returns an associative array where each key is a block type |
| 91 |
* and the value indicates its availability status. |
| 92 |
* |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
public function getBlockAvailability(): array { |
| 96 |
$isProActive = $this->isProActive(); |
| 97 |
$availability = array(); |
| 98 |
|
| 99 |
foreach ( self::FREE_BLOCKS as $blockType ) { |
| 100 |
$availability[ $blockType ] = array( |
| 101 |
'type' => $blockType, |
| 102 |
'available' => true, |
| 103 |
'requiresPro' => false, |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
foreach ( self::PRO_BLOCKS as $blockType ) { |
| 108 |
$availability[ $blockType ] = array( |
| 109 |
'type' => $blockType, |
| 110 |
'available' => $isProActive, |
| 111 |
'requiresPro' => true, |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Filter block availability. |
| 117 |
* |
| 118 |
* Allows extensions to modify block availability. |
| 119 |
* |
| 120 |
* @param array $availability The block availability array. |
| 121 |
* @param bool $isProActive Whether Pro is active. |
| 122 |
* |
| 123 |
* @since 4.2.0 |
| 124 |
*/ |
| 125 |
return apply_filters( 'f12_doi_block_availability', $availability, $isProActive ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Check if a specific block type is available. |
| 130 |
* |
| 131 |
* @param string $blockType The block type to check. |
| 132 |
* |
| 133 |
* @return bool True if the block is available. |
| 134 |
*/ |
| 135 |
public function isBlockAvailable( string $blockType ): bool { |
| 136 |
if ( in_array( $blockType, self::FREE_BLOCKS, true ) ) { |
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
if ( in_array( $blockType, self::PRO_BLOCKS, true ) ) { |
| 141 |
return $this->isProActive(); |
| 142 |
} |
| 143 |
|
| 144 |
// Unknown block types are allowed by default |
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Validate blocks recursively, checking that no Pro blocks are used without a license. |
| 150 |
* |
| 151 |
* @param array $blocks The blocks array to validate. |
| 152 |
* |
| 153 |
* @return array Array of invalid block types found. Empty if all valid. |
| 154 |
*/ |
| 155 |
public function validateBlocks( array $blocks ): array { |
| 156 |
$invalidBlocks = array(); |
| 157 |
$this->validateBlocksRecursive( $blocks, $invalidBlocks ); |
| 158 |
|
| 159 |
return $invalidBlocks; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Recursively validate blocks. |
| 164 |
* |
| 165 |
* @param array $blocks The blocks to validate. |
| 166 |
* @param array $invalidBlocks Reference to collect invalid block types. |
| 167 |
* |
| 168 |
* @return void |
| 169 |
*/ |
| 170 |
private function validateBlocksRecursive( array $blocks, array &$invalidBlocks ): void { |
| 171 |
foreach ( $blocks as $block ) { |
| 172 |
$type = $block['type'] ?? ''; |
| 173 |
|
| 174 |
if ( ! empty( $type ) && ! $this->isBlockAvailable( $type ) ) { |
| 175 |
$invalidBlocks[] = $type; |
| 176 |
} |
| 177 |
|
| 178 |
if ( ! empty( $block['children'] ) && is_array( $block['children'] ) ) { |
| 179 |
$this->validateBlocksRecursive( $block['children'], $invalidBlocks ); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|