storefront = $storefront; } /** * Register hooks. * * @return void */ public function init() { add_action( 'init', array( $this, 'register_block' ) ); } /** * Register the block from its block.json metadata. * * @return void */ public function register_block() { if ( ! function_exists( 'register_block_type' ) ) { return; } register_block_type( AISTHETIX_TRYON_PATH . 'blocks/aisthetix-tryon', array( 'render_callback' => array( $this, 'render' ), ) ); } /** * Server-side render callback. * * @param array $attributes Block attributes. * @return string HTML. */ public function render( $attributes ) { $label = isset( $attributes['label'] ) ? sanitize_text_field( $attributes['label'] ) : ''; $html = $this->storefront->render_button( array( 'label' => $label, 'surface' => 'gutenberg_block' ) ); if ( '' === $html ) { // In the editor, show a placeholder so the block is visible to authors. if ( $this->is_editor_preview() ) { return sprintf( '
%s
', esc_html__( 'aisthetix Try-On button (appears on product pages when active).', 'aisthetix-virtual-try-on' ) ); } return ''; } return $html; } /** * Whether we are rendering inside the block editor REST preview. * * @return bool */ private function is_editor_preview() { return defined( 'REST_REQUEST' ) && REST_REQUEST; } }