payment-buttons.php
102 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Payment Buttons Block. |
| 4 | * |
| 5 | * @since 11.3 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\PaymentButtons; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit( 0 ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Registers the block for use in Gutenberg |
| 20 | * This is done via an action so that we can disable |
| 21 | * registration if we need to. |
| 22 | */ |
| 23 | function register_block() { |
| 24 | require_once JETPACK__PLUGIN_DIR . '/modules/memberships/class-jetpack-memberships.php'; |
| 25 | if ( \Jetpack_Memberships::should_enable_monetize_blocks_in_editor() ) { |
| 26 | Blocks::jetpack_register_block( |
| 27 | __DIR__, |
| 28 | array( |
| 29 | 'render_callback' => __NAMESPACE__ . '\render_block', |
| 30 | 'render_email_callback' => __NAMESPACE__ . '\render_block_email', |
| 31 | 'plan_check' => true, |
| 32 | 'supports' => array( |
| 33 | 'layout' => array( |
| 34 | 'allowSwitching' => false, |
| 35 | 'allowInheriting' => false, |
| 36 | 'default' => array( |
| 37 | 'type' => 'flex', |
| 38 | ), |
| 39 | ), |
| 40 | ), |
| 41 | ) |
| 42 | ); |
| 43 | } else { |
| 44 | $required_plan = ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ? 'personal-bundle' : 'jetpack_personal'; |
| 45 | \Jetpack_Gutenberg::set_extension_unavailable( |
| 46 | 'payment-buttons', |
| 47 | 'missing_plan', |
| 48 | array( |
| 49 | 'required_feature' => 'memberships', |
| 50 | 'required_plan' => $required_plan, |
| 51 | ) |
| 52 | ); |
| 53 | } |
| 54 | } |
| 55 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 56 | |
| 57 | /** |
| 58 | * Render callback. |
| 59 | * |
| 60 | * @param array $attributes Array containing the block attributes. |
| 61 | * @param string $content String containing the block content. |
| 62 | * |
| 63 | * @return string |
| 64 | */ |
| 65 | function render_block( $attributes, $content ) { |
| 66 | \Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 67 | |
| 68 | return $content; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Render email callback. |
| 73 | * |
| 74 | * @param string $block_content The block content. |
| 75 | * @param array $parsed_block The parsed block data. |
| 76 | * @param object $rendering_context The email rendering context. |
| 77 | * |
| 78 | * @return string |
| 79 | */ |
| 80 | function render_block_email( $block_content, array $parsed_block, $rendering_context ) { |
| 81 | if ( ! class_exists( '\Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Layout\Flex_Layout_Renderer' ) ) { |
| 82 | return ''; |
| 83 | } |
| 84 | |
| 85 | // Ignore font size set on the buttons block. |
| 86 | // We rely on TypographyPreprocessor to set the font size on the buttons. |
| 87 | // Rendering font size on the wrapper causes unwanted whitespace below the buttons. |
| 88 | if ( isset( $parsed_block['attrs']['style']['typography']['fontSize'] ) ) { |
| 89 | unset( $parsed_block['attrs']['style']['typography']['fontSize'] ); |
| 90 | } |
| 91 | |
| 92 | // We are checking for the class existence above, so we know it exists. |
| 93 | $flex_layout_renderer = new \Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Layout\Flex_Layout_Renderer(); |
| 94 | |
| 95 | if ( ! method_exists( $flex_layout_renderer, 'render_inner_blocks_in_layout' ) ) { |
| 96 | return ''; |
| 97 | } |
| 98 | |
| 99 | // We are checking for the method existence above, so we know it exists. |
| 100 | return $flex_layout_renderer->render_inner_blocks_in_layout( $parsed_block, $rendering_context ); |
| 101 | } |
| 102 |