Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: enhancement

Premium Content Block: added email rendering callback
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ function register_block() {
Blocks::jetpack_register_block(
__DIR__,
array(
'render_callback' => __NAMESPACE__ . '\render_block',
'supports' => array(
'render_callback' => __NAMESPACE__ . '\render_block',
'render_email_callback' => __NAMESPACE__ . '\render_block_email',
'supports' => array(
'layout' => array(
'allowSwitching' => false,
'allowInheriting' => false,
Expand Down Expand Up @@ -65,3 +66,35 @@ function render_block( $attributes, $content ) {

return $content;
}

/**
* Render email callback.
*
* @param string $block_content The block content.
* @param array $parsed_block The parsed block data.
* @param object $rendering_context The email rendering context.
*
* @return string
*/
function render_block_email( $block_content, array $parsed_block, $rendering_context ) {
if ( ! class_exists( '\Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Layout\Flex_Layout_Renderer' ) ) {
return '';
}

// Ignore font size set on the buttons block.
// We rely on TypographyPreprocessor to set the font size on the buttons.
// Rendering font size on the wrapper causes unwanted whitespace below the buttons.
if ( isset( $parsed_block['attrs']['style']['typography']['fontSize'] ) ) {
unset( $parsed_block['attrs']['style']['typography']['fontSize'] );
}

// We are checking for the class existence above, so we know it exists.
$flex_layout_renderer = new \Automattic\WooCommerce\EmailEditor\Engine\Renderer\ContentRenderer\Layout\Flex_Layout_Renderer();

if ( ! method_exists( $flex_layout_renderer, 'render_inner_blocks_in_layout' ) ) {
return '';
}

// We are checking for the method existence above, so we know it exists.
return $flex_layout_renderer->render_inner_blocks_in_layout( $parsed_block, $rendering_context );
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ function register_login_button_block() {
Blocks::jetpack_register_block(
LOGIN_BUTTON_NAME,
array(
'render_callback' => __NAMESPACE__ . '\render_login_button_block',
'render_callback' => __NAMESPACE__ . '\render_login_button_block',
'render_email_callback' => __NAMESPACE__ . '\render_login_button_block_email',
)
);
}
Expand Down Expand Up @@ -107,3 +108,14 @@ function render_login_button_block( $attributes, $content ) {

return preg_replace( '/(<a\b[^><]*)>/i', '$1 href="' . esc_url( $url ) . '">', $content );
}

/**
* Render email callback.
*
* @return string
*/
function render_login_button_block_email() {
// We don't want to render the login button in emails.
// The subscriber is already considered logged in in emails.
return '';
}
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,37 @@ public function render_button( $attributes, $content = null, $block = null ) {
return $this->deprecated_render_button_v1( $attributes, $plan_id );
}

/**
* Render email callback.
*
* @param string $block_content The block content.
* @param array $parsed_block The parsed block data.
* @param object $rendering_context The email rendering context.
*
* @return string
*/
public function render_button_email( $block_content, array $parsed_block, $rendering_context ) {
// Get the first inner block, which should be the button block.
$button_block = $parsed_block['innerBlocks'][0] ?? array();

// We should only accept button blocks.
if ( empty( $button_block['blockName'] ) || $button_block['blockName'] !== 'jetpack/button' ) {
return '';
}

if ( ! isset( $button_block['attrs'] ) || ! is_array( $button_block['attrs'] ) || ! function_exists( '\Automattic\Jetpack\Extensions\Button\render_email' ) || ! class_exists( '\Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks\Button' ) ) {
return '';
}

// If the button block is missing text or url, return empty string.
if ( empty( $button_block['attrs']['text'] ) || empty( $button_block['attrs']['url'] ) ) {
return '';
}

// Reuse the button block's email rendering method.
return \Automattic\Jetpack\Extensions\Button\render_email( $block_content, $button_block, $rendering_context );
}

/**
* Builds subscription URL for this membership using the current blog and
* supplied plan IDs.
Expand Down Expand Up @@ -987,9 +1018,10 @@ public function register_gutenberg_block() {
Blocks::jetpack_register_block(
'jetpack/recurring-payments',
array(
'render_callback' => array( $this, 'render_button' ),
'uses_context' => array( 'isPremiumContentChild' ),
'provides_context' => array(
'render_callback' => array( $this, 'render_button' ),
'render_email_callback' => array( $this, 'render_button_email' ),
'uses_context' => array( 'isPremiumContentChild' ),
'provides_context' => array(
'jetpack/parentBlockWidth' => 'width',
),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Payment Buttons Block Email Rendering tests
*
* @package automattic/jetpack
*/

require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/payment-buttons/payment-buttons.php';

// Include mock class for WooCommerce Email Editor Flex Layout Renderer
require_once __DIR__ . '/class-mock-flex-layout-renderer.php';

use PHPUnit\Framework\Attributes\CoversFunction;

/**
* Payment Buttons Block Email Rendering tests.
*
* These tests verify the render_block_email function works correctly for various scenarios.
*
* @covers ::Automattic\Jetpack\Extensions\PaymentButtons\render_block_email
*/
#[CoversFunction( 'Automattic\Jetpack\Extensions\PaymentButtons\render_block_email' )]
class Payment_Buttons_Block_Email_Test extends WP_UnitTestCase {
use \Automattic\Jetpack\PHPUnit\WP_UnitTestCase_Fix;

/**
* Helper to create a rendering context mock.
*
* @param string $width The width to return from get_layout_width_without_padding.
* @return object Mock rendering context.
*/
private function create_rendering_context_mock( $width = '600px' ) {
return new class( $width ) {
private $width;

public function __construct( $width ) {
$this->width = $width;
}

public function get_layout_width_without_padding() {
return $this->width;
}
};
}

/**
* Test render_block_email renders two buttons.
*/
public function test_render_block_email_with_valid_payment_buttons() {
$mock_context = $this->create_rendering_context_mock();

$parsed_block = array(
'blockName' => 'jetpack/payment-buttons',
'innerBlocks' => array(
array(
'blockName' => 'jetpack/recurring-payments',
),
array(
'blockName' => 'jetpack/recurring-payments',
),
),
);

$result = \Automattic\Jetpack\Extensions\PaymentButtons\render_block_email( '', $parsed_block, $mock_context );
$block_count = substr_count( $result, 'jetpack/recurring-payments' );

$this->assertSame( 2, $block_count );
}

/**
* Test render_block_email render with no inner blocks.
*/
public function test_render_block_email_with_no_inner_blocks() {
$mock_context = $this->create_rendering_context_mock();

$parsed_block = array(
'blockName' => 'jetpack/payment-buttons',
'innerBlocks' => array(),
);

$result = \Automattic\Jetpack\Extensions\PaymentButtons\render_block_email( '', $parsed_block, $mock_context );

// Expected result is an empty table.
$this->assertSame( '<table role="presentation" style="border-collapse:collapse;width:100%;max-width:600px;"><tr></tr></table>', $result );
}

/**
* Test render_block_email returns empty when class is missing.
*/
public function test_render_block_email_returns_empty_when_class_missing() {
define( 'SKIP_FLEX_LAYOUT_RENDERER_MOCK', true );

require_once __DIR__ . '/class-mock-flex-layout-renderer.php';
require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/payment-buttons/payment-buttons.php';

$mock_context = $this->create_rendering_context_mock();

$parsed_block = array(
'blockName' => 'jetpack/payment-buttons',
'innerBlocks' => array(),
);

$result = \Automattic\Jetpack\Extensions\PaymentButtons\render_block_email( '', $parsed_block, $mock_context );

// Expected result is an empty table.
$this->assertSame( '<table role="presentation" style="border-collapse:collapse;width:100%;max-width:600px;"><tr></tr></table>', $result );
}
}
Loading
Loading