Completed
Push — update/premium-blocks-module ( 36e9a9...4e8d1b )
by
unknown
31:32 queued 23:20
created

Premium_Blocks::get_instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Block Editor functionality for Premium Blocks.
4
 *
5
 * @package Automattic\Jetpack\Extensions
6
 */
7
8
namespace Automattic\Jetpack\Extensions;
9
10
/**
11
 * Class Premium_Blocks.
12
 *
13
 * @package Automattic\Jetpack\Extensions
14
 */
15
class Premium_Blocks {
16
17
	/**
18
	 * List of premium blocks.
19
	 *
20
	 * @var array
21
	 */
22
	public $extensions = array(
23
		'core/audio',
24
		'core/cover',
25
		'core/video',
26
		'premium-content/container',
27
	);
28
29
	/**
30
	 * Plan level required to access premium blocks.
31
	 *
32
	 * @var string
33
	 */
34
	public $required_plan = 'jetpack_premium';
35
36
	/**
37
	 * Whether the current site is on WP.com.
38
	 *
39
	 * @var bool
40
	 */
41
	public $is_simple_site = false;
42
43
	/**
44
	 * Singleton.
45
	 */
46
	public static function get_instance() {
47
		static $instance = false;
48
49
		if ( ! $instance ) {
50
			$instance = new self();
51
		}
52
53
		return $instance;
54
	}
55
56
	/**
57
	 * Premium_Blocks constructor.
58
	 */
59
	private function __construct() {
60
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
61
			$this->is_simple_site = true;
62
			$this->required_plan  = 'value_bundle';
63
		}
64
65
		// Add extensions.
66
		add_filter(
67
			'jetpack_set_available_extensions',
68
			function ( $extensions ) {
69
				return array_merge( $extensions, $this->extensions );
70
			}
71
		);
72
73
		// Set extensions availability.
74
		add_action( 'jetpack_register_gutenberg_extensions', array( $this, 'set_extension_availability' ) );
75
	}
76
77
	/**
78
	 * Returns the availability status for an extension.
79
	 *
80
	 * @param string $extension_name Extension name.
81
	 * @return array
82
	 */
83
	public function check_extension_availability( $extension_name ) {
84
		if ( $this->is_simple_site && class_exists( 'Store_Product_List' ) ) {
85
			$features = \Store_Product_List::get_site_specific_features_data();
86
87
			if ( ! in_array( $extension_name, $features['active'], true ) ) {
88
				return array(
89
					'available'          => false,
90
					'unavailable_reason' => 'missing_plan',
91
				);
92
			}
93
		}
94
95
		return array( 'available' => true );
96
	}
97
98
	/**
99
	 * Set the Jetpack Gutenberg extension availability.
100
	 */
101
	public function set_extension_availability() {
102
		foreach ( $this->extensions as $extension ) {
103
			$availability = $this->check_extension_availability( $extension );
104
105
			if ( $availability['available'] ) {
106
				\Jetpack_Gutenberg::set_extension_available( $extension );
107
			} else {
108
				\Jetpack_Gutenberg::set_extension_unavailable(
109
					$extension,
110
					$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...
111
					array(
112
						'required_feature' => $extension,
113
						'required_plan'    => $this->required_plan,
114
					)
115
				);
116
			}
117
		}
118
	}
119
}
120