| 1 |
<?php |
| 2 |
/** |
| 3 |
* "Restrict This Block" editor panel (StoreEngine core). |
| 4 |
* |
| 5 |
* Enqueues the block-visibility inspector control on EVERY block, always — even |
| 6 |
* when the Membership addon is inactive — so the panel can double as a |
| 7 |
* promotion: when Membership is off it shows an "activate" prompt instead of the |
| 8 |
* controls. When Membership is on, it exposes the real access-group picker and |
| 9 |
* the enforcement runs server-side (see |
| 10 |
* StoreEngine\Addons\Membership\BlockRestriction::maybe_restrict_block()). |
| 11 |
* |
| 12 |
* The editor script lives with the membership addon on disk (always present, |
| 13 |
* only booted when active), so core can reference it regardless of activation. |
| 14 |
* |
| 15 |
* @package StoreEngine\Blocks |
| 16 |
*/ |
| 17 |
|
| 18 |
namespace StoreEngine\Blocks; |
| 19 |
|
| 20 |
use StoreEngine\Utils\Helper; |
| 21 |
|
| 22 |
if ( ! defined( 'ABSPATH' ) ) { |
| 23 |
exit; |
| 24 |
} |
| 25 |
|
| 26 |
class BlockVisibility { |
| 27 |
|
| 28 |
const HANDLE = 'storeengine-membership-block-restriction'; |
| 29 |
|
| 30 |
public static function init() { |
| 31 |
$self = new self(); |
| 32 |
add_action( 'enqueue_block_editor_assets', [ $self, 'enqueue' ] ); |
| 33 |
} |
| 34 |
|
| 35 |
public function enqueue() { |
| 36 |
$active = Helper::get_addon_active_status( 'membership' ); |
| 37 |
|
| 38 |
wp_enqueue_script( |
| 39 |
self::HANDLE, |
| 40 |
STOREENGINE_PLUGIN_ROOT_URI . 'addons/membership/assets/js/block-restriction.js', |
| 41 |
[ 'wp-blocks', 'wp-hooks', 'wp-element', 'wp-components', 'wp-block-editor', 'wp-compose', 'wp-i18n', 'wp-data' ], |
| 42 |
STOREENGINE_VERSION, |
| 43 |
true |
| 44 |
); |
| 45 |
|
| 46 |
wp_set_script_translations( self::HANDLE, 'storeengine' ); |
| 47 |
|
| 48 |
$groups = []; |
| 49 |
if ( $active && class_exists( '\StoreEngine\Addons\Membership\Access' ) ) { |
| 50 |
$groups = \StoreEngine\Addons\Membership\Access::get_group_options(); |
| 51 |
} |
| 52 |
|
| 53 |
wp_add_inline_script( |
| 54 |
self::HANDLE, |
| 55 |
'window.StoreEngineMembershipBlocks = ' . wp_json_encode( [ |
| 56 |
'membershipActive' => (bool) $active, |
| 57 |
'groups' => $groups, |
| 58 |
'activateUrl' => admin_url( 'admin.php?page=' . STOREENGINE_PLUGIN_SLUG . '-addons' ), |
| 59 |
] ) . ';', |
| 60 |
'before' |
| 61 |
); |
| 62 |
} |
| 63 |
} |
| 64 |
|