Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.43% |
81 / 84 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Video_Block_Email_Renderer | |
96.43% |
81 / 84 |
|
57.14% |
4 / 7 |
37 | |
0.00% |
0 / 1 |
| render | |
96.43% |
27 / 28 |
|
0.00% |
0 / 1 |
10 | |||
| render_poster | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
| get_poster_url | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
9.05 | |||
| get_public_video_details | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
5 | |||
| layout_width | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
5.05 | |||
| render_link | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
3 | |||
| get_videopress_url | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * VideoPress video block email renderer class. |
| 4 | * |
| 5 | * @package automattic/jetpack-videopress |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\VideoPress; |
| 9 | |
| 10 | /** |
| 11 | * Handles rendering VideoPress video blocks for email contexts. |
| 12 | */ |
| 13 | class Video_Block_Email_Renderer { |
| 14 | |
| 15 | /** |
| 16 | * Render VideoPress video block for email. |
| 17 | * |
| 18 | * @param string $block_content The original block HTML content. |
| 19 | * @param array $parsed_block The parsed block data including attributes. |
| 20 | * @param object $rendering_context Email rendering context. |
| 21 | * |
| 22 | * @return string |
| 23 | */ |
| 24 | public static function render( $block_content, $parsed_block, $rendering_context ) { |
| 25 | // Validate input parameters. |
| 26 | if ( ! isset( $parsed_block['attrs'] ) || ! is_array( $parsed_block['attrs'] ) ) { |
| 27 | return ''; |
| 28 | } |
| 29 | |
| 30 | $attributes = $parsed_block['attrs']; |
| 31 | |
| 32 | // Get the VideoPress URL from the guid attribute. |
| 33 | $videopress_url = self::get_videopress_url( $attributes ); |
| 34 | |
| 35 | if ( empty( $videopress_url ) ) { |
| 36 | return ''; |
| 37 | } |
| 38 | |
| 39 | // For private videos, render a simple link to the post since the video isn't accessible on VideoPress. |
| 40 | // The isPrivate attribute is pre-computed by the block editor based on video and site settings. |
| 41 | if ( isset( $attributes['isPrivate'] ) && true === $attributes['isPrivate'] ) { |
| 42 | return self::render_link( $parsed_block ); |
| 43 | } |
| 44 | |
| 45 | $poster = self::get_poster_url( $attributes, self::layout_width( $parsed_block, $rendering_context ) ); |
| 46 | |
| 47 | if ( '' !== $poster && class_exists( '\Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks\Video' ) ) { |
| 48 | return self::render_poster( $poster, $videopress_url, $parsed_block['email_attrs'] ?? array(), $rendering_context ); |
| 49 | } |
| 50 | |
| 51 | if ( ! class_exists( '\Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks\Embed' ) ) { |
| 52 | return ''; |
| 53 | } |
| 54 | |
| 55 | // Without a poster, let WooCommerce's embed renderer fetch the oEmbed thumbnail. |
| 56 | $mock_embed_block = array( |
| 57 | 'blockName' => 'core/embed', |
| 58 | 'attrs' => array( |
| 59 | 'url' => $videopress_url, |
| 60 | 'providerNameSlug' => 'videopress', |
| 61 | ), |
| 62 | 'innerHTML' => sprintf( |
| 63 | '<figure class="wp-block-embed is-type-video is-provider-videopress"><div class="wp-block-embed__wrapper">%s</div></figure>', |
| 64 | esc_url( $videopress_url ) |
| 65 | ), |
| 66 | ); |
| 67 | |
| 68 | // Preserve email_attrs if present (used for spacing/width calculations). |
| 69 | if ( ! empty( $parsed_block['email_attrs'] ) ) { |
| 70 | $mock_embed_block['email_attrs'] = $parsed_block['email_attrs']; |
| 71 | } |
| 72 | |
| 73 | // Use WooCommerce's core embed renderer. |
| 74 | // @phan-suppress-next-line PhanUndeclaredClassMethod -- Optional WooCommerce dependency, checked with class_exists() above. |
| 75 | $woo_embed_renderer = new \Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks\Embed(); |
| 76 | |
| 77 | // @phan-suppress-next-line PhanUndeclaredClassMethod -- Optional WooCommerce dependency, checked with class_exists() above. |
| 78 | return $woo_embed_renderer->render( $mock_embed_block['innerHTML'], $mock_embed_block, $rendering_context ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * A poster with a play button linking to the video, through WooCommerce's Video renderer. |
| 83 | * |
| 84 | * @param string $poster Poster URL. |
| 85 | * @param string $video_url URL the poster links to. |
| 86 | * @param array $email_attrs The email editor's attributes for the block, if any. |
| 87 | * @param object $rendering_context Email rendering context. |
| 88 | * |
| 89 | * @return string |
| 90 | */ |
| 91 | public static function render_poster( $poster, $video_url, $email_attrs, $rendering_context ) { |
| 92 | $video_block = array( |
| 93 | 'blockName' => 'core/video', |
| 94 | 'attrs' => array( |
| 95 | 'poster' => $poster, |
| 96 | 'videoUrl' => $video_url, |
| 97 | ), |
| 98 | 'innerHTML' => '<figure class="wp-block-video"></figure>', |
| 99 | ); |
| 100 | |
| 101 | if ( ! empty( $email_attrs ) ) { |
| 102 | $video_block['email_attrs'] = $email_attrs; |
| 103 | } |
| 104 | |
| 105 | // @phan-suppress-next-line PhanUndeclaredClassMethod -- Optional WooCommerce dependency, callers check class_exists(). |
| 106 | $video_renderer = new \Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks\Video(); |
| 107 | |
| 108 | // @phan-suppress-next-line PhanUndeclaredClassMethod -- Optional WooCommerce dependency, callers check class_exists(). |
| 109 | return $video_renderer->render( $video_block['innerHTML'], $video_block, $rendering_context ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * The block's poster, else the video's own, resized for 2x screens; '' when there is neither. |
| 114 | * |
| 115 | * @param array $attributes Block attributes, with a valid guid. |
| 116 | * @param int $width Block width in the email, in pixels. |
| 117 | * |
| 118 | * @return string |
| 119 | */ |
| 120 | private static function get_poster_url( $attributes, $width ) { |
| 121 | $poster = isset( $attributes['poster'] ) && is_string( $attributes['poster'] ) ? $attributes['poster'] : ''; |
| 122 | |
| 123 | // Blocks uploaded without a custom poster don't save one. |
| 124 | if ( '' === $poster ) { |
| 125 | $details = self::get_public_video_details( $attributes['guid'] ); |
| 126 | $poster = $details && ! empty( $details->poster ) && is_string( $details->poster ) ? $details->poster : ''; |
| 127 | } |
| 128 | |
| 129 | $poster = esc_url_raw( $poster, array( 'http', 'https' ) ); |
| 130 | if ( '' === $poster ) { |
| 131 | return ''; |
| 132 | } |
| 133 | |
| 134 | // Drop a saved size so Photon neither keeps it nor replicates its crop. |
| 135 | $poster = remove_query_arg( array( 'w', 'h', 'fit', 'resize', 'crop' ), $poster ); |
| 136 | |
| 137 | $args = array( 'w' => 2 * $width ); |
| 138 | |
| 139 | // WordPress.com defines the function but doesn't hook the filter while sending newsletters. |
| 140 | if ( function_exists( 'jetpack_photon_url' ) ) { |
| 141 | // @phan-suppress-next-line PhanUndeclaredFunction -- Provided by the Jetpack plugin or WordPress.com at runtime; guarded by `function_exists` above. |
| 142 | return (string) jetpack_photon_url( $poster, $args ); |
| 143 | } |
| 144 | |
| 145 | return (string) apply_filters( 'jetpack_photon_url', $poster, $args ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * A video's details as anyone may see them, or null when they can't be looked up or the video is private. |
| 150 | * |
| 151 | * @param string $guid Video GUID. |
| 152 | * |
| 153 | * @return object|null |
| 154 | */ |
| 155 | public static function get_public_video_details( $guid ) { |
| 156 | $details = function_exists( 'videopress_get_video_details' ) ? videopress_get_video_details( $guid ) : null; |
| 157 | |
| 158 | return is_object( $details ) && ! is_wp_error( $details ) && empty( $details->is_private ) ? $details : null; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * The block's width in the email, e.g. narrower inside a column; else the content width, else 600px. |
| 163 | * |
| 164 | * @param array $parsed_block The parsed block data, with the email editor's email_attrs. |
| 165 | * @param object $rendering_context Email rendering context. |
| 166 | * |
| 167 | * @return int Width in pixels. |
| 168 | */ |
| 169 | public static function layout_width( $parsed_block, $rendering_context ) { |
| 170 | $block_width = (int) ( $parsed_block['email_attrs']['width'] ?? 0 ); |
| 171 | if ( $block_width > 0 ) { |
| 172 | return $block_width; |
| 173 | } |
| 174 | |
| 175 | if ( is_object( $rendering_context ) && method_exists( $rendering_context, 'get_layout_width_without_padding' ) ) { |
| 176 | $width = (int) $rendering_context->get_layout_width_without_padding(); |
| 177 | if ( $width > 0 ) { |
| 178 | return $width; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return 600; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Render a simple link for private VideoPress videos in emails. |
| 187 | * Links to the post containing the video since private videos aren't accessible on VideoPress. |
| 188 | * |
| 189 | * @param array $parsed_block The parsed block data. |
| 190 | * |
| 191 | * @return string The rendered link HTML. |
| 192 | */ |
| 193 | private static function render_link( $parsed_block ) { |
| 194 | $post_url = get_the_permalink(); |
| 195 | |
| 196 | if ( empty( $post_url ) ) { |
| 197 | return ''; |
| 198 | } |
| 199 | |
| 200 | $link_text = __( 'Visit the post to watch the video', 'jetpack-videopress-pkg' ); |
| 201 | |
| 202 | $link_html = sprintf( |
| 203 | '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>', |
| 204 | esc_url( $post_url ), |
| 205 | esc_html( $link_text ) |
| 206 | ); |
| 207 | |
| 208 | // Wrap with spacing if email_attrs are present. |
| 209 | $email_attrs = $parsed_block['email_attrs'] ?? array(); |
| 210 | if ( ! empty( $email_attrs['padding'] ) ) { |
| 211 | $link_html = sprintf( |
| 212 | '<p style="padding: %s;">%s</p>', |
| 213 | esc_attr( $email_attrs['padding'] ), |
| 214 | $link_html |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | return $link_html; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Get the VideoPress URL from block attributes for email rendering. |
| 223 | * |
| 224 | * @param array $attributes Block attributes. |
| 225 | * |
| 226 | * @return string VideoPress URL or empty string. |
| 227 | */ |
| 228 | private static function get_videopress_url( $attributes ) { |
| 229 | // Construct URL from guid attribute. |
| 230 | if ( ! empty( $attributes['guid'] ) ) { |
| 231 | $guid = $attributes['guid']; |
| 232 | |
| 233 | // VideoPress guids are alphanumeric only (e.g., "nfbj0J36"). |
| 234 | // Validate format to prevent any injection issues. |
| 235 | if ( preg_match( '/^[a-zA-Z0-9]+$/', $guid ) ) { |
| 236 | return 'https://videopress.com/v/' . $guid; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | return ''; |
| 241 | } |
| 242 | } |