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'], |
|
|
|
|
111
|
|
|
array( |
112
|
|
|
'required_feature' => $extension, |
113
|
|
|
'required_plan' => $this->required_plan, |
114
|
|
|
) |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.