Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Block_Replacement
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 2
90
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 replace_media_text_with_videopress
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2/**
3 * Media block replacement class.
4 *
5 * @package automattic/jetpack-videopress
6 **/
7
8namespace Automattic\Jetpack\VideoPress;
9
10/**
11 * Class Block_Replacement
12 **/
13class Block_Replacement {
14    /**
15     * Whether the class has been initiated.
16     *
17     * @var bool
18     */
19    private static $initiated = false;
20
21    /**
22     * Initialize replacement.
23     */
24    public static function init() {
25        if ( self::$initiated ) {
26            return;
27        }
28        add_filter( 'render_block', array( self::class, 'replace_media_text_with_videopress' ), 10, 2 );
29    }
30
31    /**
32     * Replace video in Media & Text block with Videopress shortcode.
33     *
34     * @param string $block_content The block content.
35     * @param array  $block         The block.
36     * @return string
37     */
38    public static function replace_media_text_with_videopress( $block_content, $block ) {
39        if ( isset( $block['blockName'] ) && $block['blockName'] === 'core/media-text' ) {
40
41            // Make sure we have a $post_id that could be valid; if we don't, then video_get_info_by_blogpostid()
42            // will fail, so there's no point in calling it.
43            $post_id = isset( $block['attrs']['mediaId'] ) ? (int) $block['attrs']['mediaId'] : 0;
44            if ( $post_id <= 0 ) {
45                return $block_content;
46            }
47
48            $video_info = video_get_info_by_blogpostid( get_current_blog_id(), $post_id );
49            if ( $video_info && $video_info->guid ) {
50                $videopress_shortcode = sprintf( '[videopress %s]', esc_attr( $video_info->guid ) );
51                $block_content        = preg_replace( '/<video.*?<\/video>/is', do_shortcode( $videopress_shortcode ), $block_content );
52            }
53        }
54        return $block_content;
55    }
56}