Completed
Push — update/set-videopress-extensio... ( 404de9...1a9329 )
by
unknown
40:28 queued 34:02
created

check_videopress_availability()   C

Complexity

Conditions 15
Paths 10

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
nc 10
nop 0
dl 0
loc 51
rs 5.9166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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, 'set_extension_availability' ) );
33
		add_action( 'init', array( $this, 'register_video_block_with_videopress' ) );
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( 'WPCOM_Store', 'get_bundle_subscription' )
51
			&& method_exists( 'Store_Product_List', 'get_feature_list' )
52
		) {
53
			$current_plan = WPCOM_Store::get_bundle_subscription( get_current_blog_id() );
54
			$features     = Store_Product_List::get_feature_list();
55
			foreach ( $features as $feature ) {
56
				if ( 'videopress' === $feature['product_slug'] ) {
57
					$has_feature = array_key_exists( $current_plan->product_id, $feature['plans'] );
58
					break;
59
				}
60
			}
61
			if ( isset( $has_feature ) && $has_feature ) {
62
				return array( 'available' => true );
63
			} else {
64
				return array(
65
					'available'          => false,
66
					'unavailable_reason' => 'missing_plan',
67
				);
68
			}
69
		}
70
71
		// It is available on Jetpack Sites having the module active.
72
		if (
73
			method_exists( 'Jetpack', 'is_active' ) && Jetpack::is_active()
74
			&& method_exists( 'Jetpack', 'is_module_active' )
75
			&& method_exists( 'Jetpack', 'active_plan_supports' )
76
		) {
77
			if ( Jetpack::is_module_active( 'videopress' ) ) {
78
				return array( 'available' => true );
79
			} elseif ( ! Jetpack::active_plan_supports( 'videopress' ) ) {
80
				return array(
81
					'available'          => false,
82
					'unavailable_reason' => 'missing_plan',
83
				);
84
			} else {
85
				return array(
86
					'available'          => false,
87
					'unavailable_reason' => 'missing_module',
88
				);
89
			}
90
		}
91
92
		return array(
93
			'available'          => false,
94
			'unavailable_reason' => 'unknown',
95
		);
96
	}
97
98
	/**
99
	 * Set the Jetpack Gutenberg extension availability.
100
	 */
101
	public function set_extension_availability() {
102
		$availability = $this->check_videopress_availability();
103
		if ( $availability['available'] ) {
104
			Jetpack_Gutenberg::set_extension_available( 'jetpack/videopress' );
105
		} else {
106
			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...
107
		}
108
	}
109
110
	/**
111
	 * Register the core video block as a dynamic block.
112
	 *
113
	 * It defines a server-side rendering that adds VideoPress support to the core video block.
114
	 */
115
	public function register_video_block_with_videopress() {
116
		jetpack_register_block_type(
117
			'core/video',
118
			array(
119
				'render_callback' => array( $this, 'render_video_block_with_videopress' ),
120
			)
121
		);
122
	}
123
124
	/**
125
	 * Render the core video block replacing the src attribute with the VideoPress URL
126
	 *
127
	 * @param array  $attributes Array containing the video block attributes.
128
	 * @param string $content    String containing the video block content.
129
	 *
130
	 * @return string
131
	 */
132
	public function render_video_block_with_videopress( $attributes, $content ) {
133
		if ( ! isset( $attributes['id'] ) || isset( $attributes['guid'] ) ) {
134
			return $content;
135
		}
136
137
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
138
			$blog_id = get_current_blog_id();
139
		} elseif ( method_exists( 'Jetpack', 'is_active' ) && Jetpack::is_active() ) {
140
			/**
141
			 * We're intentionally not using `get_current_blog_id` because it was returning unexpected values.
142
			 *
143
			 * @see https://github.com/Automattic/jetpack/pull/11193#issuecomment-457883886
144
			 * @see https://github.com/Automattic/jetpack/pull/11193/commits/215cf789f3d8bd03ff9eb1bbdb693acb8831d273
145
			 */
146
			$blog_id = Jetpack_Options::get_option( 'id' );
147
		}
148
149
		if ( ! isset( $blog_id ) ) {
150
			return $content;
151
		}
152
153
		$post_id         = absint( $attributes['id'] );
154
		$videopress_id   = video_get_info_by_blogpostid( $blog_id, $post_id )->guid;
155
		$videopress_data = videopress_get_video_details( $videopress_id );
156
157
		if ( empty( $videopress_data->file_url_base->https ) || empty( $videopress_data->files->hd->mp4 ) ) {
158
			return $content;
159
		}
160
161
		$videopress_url = $videopress_data->file_url_base->https . $videopress_data->files->hd->mp4;
162
163
		$pattern = '/(\s)src=([\'"])(?:(?!\2).)+?\2/';
164
165
		return preg_replace(
166
			$pattern,
167
			sprintf(
168
				'\1src="%1$s"',
169
				esc_url_raw( $videopress_url )
170
			),
171
			$content,
172
			1
173
		);
174
	}
175
}
176
177
VideoPress_Gutenberg::init();
178