true, 'render_callback' => [ self::class, 'render' ], ] ); if ( ! $type instanceof \WP_Block_Type || empty( $type->editor_script_handles ) ) { return; } // The editor's data (coins, currencies) is built only when an editor loads, not on every page. add_action( 'enqueue_block_editor_assets', static function () use ( $type ): void { foreach ( $type->editor_script_handles as $handle ) { wp_add_inline_script( $handle, 'window.cryptowayButtonEditor = ' . wp_json_encode( self::editor_data() ) . ';', 'before' ); } } ); } /** * The block on a page. $content is always empty (skip_inner_blocks). The paid part goes along as * stored, so it can be found for a payer even when the block is drawn outside post content (a * template part, a block widget); it is never drawn here: a payer gets it rendered from the post * (Button::hidden_content()). * * @param array $attributes Block attributes. * @param string $content Empty. * @param \WP_Block $block The block instance. */ public static function render( $attributes, $content, $block ): string { $paid = null; if ( $block instanceof \WP_Block && 'content' === ( $attributes['after'] ?? '' ) ) { $paid = [ 'kind' => 'blocks', 'raw' => serialize_blocks( (array) ( $block->parsed_block['innerBlocks'] ?? [] ) ) ]; } // The outer wrapper carries the block's own classes (alignment, custom class), so the theme lays the // block out like any other; the markup inside is built and escaped piece by piece in Button::render(). return Button::render( is_array( $attributes ) ? $attributes : [], 0, get_block_wrapper_attributes(), '', $paid ); } /** * @param array $categories */ public static function add_category( $categories ): array { $categories = is_array( $categories ) ? $categories : []; foreach ( $categories as $category ) { if ( is_array( $category ) && 'cryptoway' === ( $category['slug'] ?? '' ) ) { return $categories; } } // Right after the first group ("Text"), where a payment button is looked for first. array_splice( $categories, 1, 0, [ [ 'slug' => 'cryptoway', 'title' => 'Cryptoway', 'icon' => null, ] ] ); return $categories; } private static function editor_data(): array { $coins = []; foreach ( Coins::picker() as $symbol => $label ) { $chip = Coins::chip( $symbol ); $coins[] = [ 'id' => $symbol, 'label' => $label, 'ticker' => $chip['ticker'], 'net' => $chip['network'], 'icon' => $chip['icon'] ]; } return [ 'currencies' => Settings::CURRENCIES, 'defaultCurrency' => Settings::currency(), 'defaultMin' => Button::DEFAULT_MIN, 'coins' => $coins, 'keys' => Settings::keys_present(), 'settingsUrl' => Admin::settings_url(), 'canManage' => current_user_can( 'manage_options' ), ]; } }