Completed
Push — add/videopress-src-core-video-... ( f0c27f...8450d1 )
by
unknown
15:32 queued 06:49
created

VideoPress_Gutenberg::should_initialize()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 4
nop 0
dl 0
loc 19
rs 9.0111
c 0
b 0
f 0
1
<?php
2
3
class VideoPress_Gutenberg {
4
5
	/**
6
	 * Initialize the VideoPress Gutenberg extension
7
	 */
8
	public static function init() {
9
		if ( self::should_initialize() ) {
10
			add_action( 'init', array( __CLASS__, 'register_video_block_with_videopress' ) );
11
		}
12
	}
13
14
	/**
15
	 * Check whether conditions indicate the VideoPress Gutenberg extension should be initialized
16
	 *
17
	 * @return bool
18
	 */
19
	public static function should_initialize() {
20
		// Should not initialize if Gutenberg is not available
21
		if ( ! function_exists( 'register_block_type' ) ) {
22
			return false;
23
		}
24
25
		// Should initialize if this is a WP.com site
26
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
27
			return true;
28
		}
29
30
		// Should not initialize if this is a Jetpack site but Jetpack is not active (unless the dev mode is enabled)
31
		if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) {
32
			return false;
33
		}
34
35
		// Should initialize by default
36
		return true;
37
	}
38
39
	/**
40
	 * Register the Jetpack Gutenberg extension that adds VideoPress support to the core video block.
41
	 */
42
	public static function register_video_block_with_videopress() {
43
		// We intentionally don't use `jetpack_register_block` because the current Jetpack extensions
44
		// registration doesn't fit our needs. Right now, any extension registered with `jetpack_register_block`
45
		// needs to have a name that is equal to the name of the registered block (our extension name would be
46
		// "videopress" and the name of the block we need to register "core/video").
47
		register_block_type( 'core/video', array(
48
			'render_callback' => array( __CLASS__, 'render_video_block_with_videopress' ),
49
		) );
50
	}
51
52
	/**
53
	 * Render the core video block replacing the src attribute with the VideoPress URL
54
	 *
55
	 * @param array  $attributes Array containing the video block attributes.
56
	 * @param string $content    String containing the video block content.
57
	 *
58
	 * @return string
59
	 */
60
	public static function render_video_block_with_videopress( $attributes, $content ) {
61
		if ( ! isset( $attributes['id'] ) ) {
62
			return $content;
63
		}
64
65 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
66
			$blog_id = get_current_blog_id();
67
		} else {
68
			$blog_id = Jetpack_Options::get_option( 'id' );
69
		}
70
71
		$post_id = absint( $attributes['id'] );
72
		$videopress_id = video_get_info_by_blogpostid( $blog_id, $post_id )->guid;
73
		$videopress_data = videopress_get_video_details( $videopress_id );
74
75
		if ( empty( $videopress_data->file_url_base->https ) || empty( $videopress_data->files->hd->mp4 ) ) {
76
			return $content;
77
		}
78
79
		$videopress_url = $videopress_data->file_url_base->https . $videopress_data->files->hd->mp4;
80
81
		$pattern = '/(\s)src=([\'"])(?:(?!\2).)+?\2/';
82
83
		return preg_replace(
84
			$pattern,
85
			sprintf(
86
				'\1src="%1$s"',
87
				esc_url_raw( $videopress_url )
88
			),
89
			$content,
90
			1
91
		);
92
	}
93
}
94
95
VideoPress_Gutenberg::init();
96