Completed
Push — add/contact-form-tracking ( 49d904...012227 )
by Jeremy
06:41
created

VideoPress_Gutenberg::override_video_upload()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * Block Editor functionality for VideoPress users.
4
 *
5
 * @package Jetpack
6
 */
7
8
/**
9
 * Register a VideoPress extension to replace the default Core Video block.
10
 */
11
class VideoPress_Gutenberg {
12
13
	/**
14
	 * Singleton
15
	 */
16
	public static function init() {
17
		static $instance = false;
18
19
		if ( ! $instance ) {
20
			$instance = new VideoPress_Gutenberg();
21
		}
22
23
		return $instance;
24
	}
25
26
	/**
27
	 * VideoPress_Gutenberg constructor.
28
	 *
29
	 * Initialize the VideoPress Gutenberg extension
30
	 */
31
	private function __construct() {
32
		add_action( 'init', array( $this, 'register_video_block_with_videopress' ) );
33
		add_action( 'jetpack_register_gutenberg_extensions', array( $this, 'set_extension_availability' ) );
34
	}
35
36
	/**
37
	 * Used to check whether VideoPress is enabled for given site.
38
	 *
39
	 * @todo Create a global `jetpack_check_module_availability( $module )` helper so we can re-use it on other modules.
40
	 *       This global helper should be created in a file synced with WordPress.com so we can use it there too.
41
	 * @see https://github.com/Automattic/jetpack/pull/11321#discussion_r255477815
42
	 *
43
	 * @return array Associative array indicating if the module is available (key `available`) and the reason why it is
44
	 * unavailable (key `unavailable_reason`)
45
	 */
46
	public function check_videopress_availability() {
47
		// It is available on Simple Sites having the appropriate a plan.
48
		if (
49
			defined( 'IS_WPCOM' ) && IS_WPCOM
50
			&& method_exists( 'Store_Product_List', 'get_site_specific_features_data' )
51
		) {
52
			$features = Store_Product_List::get_site_specific_features_data();
53
			if ( in_array( 'videopress', $features['active'], true ) ) {
54
				return array( 'available' => true );
55
			} else {
56
				return array(
57
					'available'          => false,
58
					'unavailable_reason' => 'missing_plan',
59
				);
60
			}
61
		}
62
63
		// It is available on Jetpack Sites having the module active.
64
		if (
65
			method_exists( 'Jetpack', 'is_active' ) && Jetpack::is_active()
66
			&& method_exists( 'Jetpack', 'is_module_active' )
67
			&& method_exists( 'Jetpack_Plan', 'supports' )
68
		) {
69
			if ( Jetpack::is_module_active( 'videopress' ) ) {
70
				return array( 'available' => true );
71
			} elseif ( ! Jetpack_Plan::supports( 'videopress' ) ) {
72
				return array(
73
					'available'          => false,
74
					'unavailable_reason' => 'missing_plan',
75
				);
76
			} else {
77
				return array(
78
					'available'          => false,
79
					'unavailable_reason' => 'missing_module',
80
				);
81
			}
82
		}
83
84
		return array(
85
			'available'          => false,
86
			'unavailable_reason' => 'unknown',
87
		);
88
	}
89
90
	/**
91
	 * Set the Jetpack Gutenberg extension availability.
92
	 */
93
	public function set_extension_availability() {
94
		$availability = $this->check_videopress_availability();
95
		if ( $availability['available'] ) {
96
			Jetpack_Gutenberg::set_extension_available( 'jetpack/videopress' );
97
		} else {
98
			Jetpack_Gutenberg::set_extension_unavailable( 'jetpack/videopress', $availability['unavailable_reason'] );
0 ignored issues
show
Bug introduced by
It seems like $availability['unavailable_reason'] can also be of type boolean; however, Jetpack_Gutenberg::set_extension_unavailable() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
99
		}
100
	}
101
102
	/**
103
	 * Register the core video block as a dynamic block.
104
	 *
105
	 * It defines a server-side rendering that adds VideoPress support to the core video block.
106
	 */
107
	public function register_video_block_with_videopress() {
108
		jetpack_register_block(
109
			'core/video',
110
			array(
111
				'render_callback' => array( $this, 'render_video_block_with_videopress' ),
112
			)
113
		);
114
	}
115
116
	/**
117
	 * Render the core video block replacing the src attribute with the VideoPress URL
118
	 *
119
	 * @param array  $attributes Array containing the video block attributes.
120
	 * @param string $content    String containing the video block content.
121
	 *
122
	 * @return string
123
	 */
124
	public function render_video_block_with_videopress( $attributes, $content ) {
125
		if ( ! isset( $attributes['id'] ) || isset( $attributes['guid'] ) ) {
126
			return $content;
127
		}
128
129
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
130
			$blog_id = get_current_blog_id();
131
		} elseif ( method_exists( 'Jetpack', 'is_active' ) && Jetpack::is_active() ) {
132
			/**
133
			 * We're intentionally not using `get_current_blog_id` because it was returning unexpected values.
134
			 *
135
			 * @see https://github.com/Automattic/jetpack/pull/11193#issuecomment-457883886
136
			 * @see https://github.com/Automattic/jetpack/pull/11193/commits/215cf789f3d8bd03ff9eb1bbdb693acb8831d273
137
			 */
138
			$blog_id = Jetpack_Options::get_option( 'id' );
139
		}
140
141
		if ( ! isset( $blog_id ) ) {
142
			return $content;
143
		}
144
145
		$post_id         = absint( $attributes['id'] );
146
		$videopress_id   = video_get_info_by_blogpostid( $blog_id, $post_id )->guid;
147
		$videopress_data = videopress_get_video_details( $videopress_id );
148
149
		if ( empty( $videopress_data->file_url_base->https ) || empty( $videopress_data->files->hd->mp4 ) ) {
150
			return $content;
151
		}
152
153
		$videopress_url = $videopress_data->file_url_base->https . $videopress_data->files->hd->mp4;
154
155
		$pattern = '/(\s)src=([\'"])(?:(?!\2).)+?\2/';
156
157
		return preg_replace(
158
			$pattern,
159
			sprintf(
160
				'\1src="%1$s"',
161
				esc_url_raw( $videopress_url )
162
			),
163
			$content,
164
			1
165
		);
166
	}
167
}
168
169
VideoPress_Gutenberg::init();
170