1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Base class for working with Jetpack Modules. |
5
|
|
|
*/ |
6
|
|
|
abstract class Jetpack_JSON_API_Modules_Endpoint extends Jetpack_JSON_API_Endpoint { |
7
|
|
|
|
8
|
|
|
protected $modules = array(); |
9
|
|
|
|
10
|
|
|
protected $bulk = true; |
11
|
|
|
|
12
|
|
|
static $_response_format = array( |
|
|
|
|
13
|
|
|
'id' => '(string) The module\'s ID', |
14
|
|
|
'active' => '(boolean) The module\'s status.', |
15
|
|
|
'name' => '(string) The module\'s name.', |
16
|
|
|
'description' => '(safehtml) The module\'s description.', |
17
|
|
|
'sort' => '(int) The module\'s display order.', |
18
|
|
|
'introduced' => '(string) The Jetpack version when the module was introduced.', |
19
|
|
|
'changed' => '(string) The Jetpack version when the module was changed.', |
20
|
|
|
'free' => '(boolean) The module\'s Free or Paid status.', |
21
|
|
|
'module_tags' => '(array) The module\'s tags.', |
22
|
|
|
'override' => '(string) The module\'s override. Empty if no override, otherwise \'active\' or \'inactive\'', |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
protected function result() { |
26
|
|
|
|
27
|
|
|
$modules = $this->get_modules(); |
28
|
|
|
|
29
|
|
|
if ( ! $this->bulk && ! empty( $modules ) ) { |
30
|
|
|
return array_pop( $modules ); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return array( 'modules' => $modules ); |
34
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Walks through either the submitted modules or list of themes and creates the global array |
39
|
|
|
* @param $theme |
40
|
|
|
* |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
protected function validate_input( $module) { |
44
|
|
|
$args = $this->input(); |
45
|
|
|
// lets set what modules were requested, and validate them |
46
|
|
|
if ( ! isset( $module ) || empty( $module ) ) { |
47
|
|
|
|
48
|
|
|
if ( ! $args['modules'] || empty( $args['modules'] ) ) { |
49
|
|
|
return new WP_Error( 'missing_module', __( 'You are required to specify a module.', 'jetpack' ), 400 ); |
50
|
|
|
} |
51
|
|
|
if ( is_array( $args['modules'] ) ) { |
52
|
|
|
$this->modules = $args['modules']; |
53
|
|
|
} else { |
54
|
|
|
$this->modules[] = $args['modules']; |
55
|
|
|
} |
56
|
|
|
} else { |
57
|
|
|
$this->modules[] = urldecode( $module ); |
58
|
|
|
$this->bulk = false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ( is_wp_error( $error = $this->validate_modules() ) ) { |
62
|
|
|
return $error; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return parent::validate_input( $module ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Walks through submitted themes to make sure they are valid |
70
|
|
|
* @return bool|WP_Error |
71
|
|
|
*/ |
72
|
|
|
protected function validate_modules() { |
73
|
|
|
foreach ( $this->modules as $module ) { |
74
|
|
|
if ( ! Jetpack::is_module( $module ) ) { |
75
|
|
|
return new WP_Error( 'unknown_jetpack_module', sprintf( __( 'Module not found: `%s`.', 'jetpack' ), $module ), 404 ); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected static function format_module( $module_slug ) { |
82
|
|
|
$module_data = Jetpack::get_module( $module_slug ); |
83
|
|
|
|
84
|
|
|
$module = array(); |
85
|
|
|
$module['id'] = $module_slug; |
86
|
|
|
$module['active'] = Jetpack::is_module_active( $module_slug ); |
87
|
|
|
$module['name'] = $module_data['name']; |
88
|
|
|
$module['short_description'] = $module_data['description']; |
89
|
|
|
$module['sort'] = $module_data['sort']; |
90
|
|
|
$module['introduced'] = $module_data['introduced']; |
91
|
|
|
$module['changed'] = $module_data['changed']; |
92
|
|
|
$module['free'] = $module_data['free']; |
93
|
|
|
$module['module_tags'] = $module_data['module_tags']; |
94
|
|
|
|
95
|
|
|
$overrides_instance = Jetpack_Modules_Overrides::instance(); |
96
|
|
|
$module['override'] = $overrides_instance->get_module_override( $module_slug ); |
97
|
|
|
|
98
|
|
|
// Fetch the HTML formatted long description |
99
|
|
|
ob_start(); |
100
|
|
|
/** This action is documented in class.jetpack-modules-list-table.php */ |
101
|
|
|
do_action( 'jetpack_module_more_info_' . $module_slug ); |
102
|
|
|
$module['description'] = ob_get_clean(); |
|
|
|
|
103
|
|
|
|
104
|
|
|
return $module; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Format a list of modules for public display, using the supplied offset and limit args |
109
|
|
|
* @uses WPCOM_JSON_API_Endpoint::query_args() |
110
|
|
|
* @return array Public API modules objects |
111
|
|
|
*/ |
112
|
|
View Code Duplication |
protected function get_modules() { |
113
|
|
|
$modules = array_values( $this->modules ); |
114
|
|
|
// do offset & limit - we've already returned a 400 error if they're bad numbers |
115
|
|
|
$args = $this->query_args(); |
116
|
|
|
|
117
|
|
|
if ( isset( $args['offset'] ) ) |
118
|
|
|
$modules = array_slice( $modules, (int) $args['offset'] ); |
119
|
|
|
if ( isset( $args['limit'] ) ) |
120
|
|
|
$modules = array_slice( $modules, 0, (int) $args['limit'] ); |
121
|
|
|
|
122
|
|
|
return array_map( array( $this, 'format_module' ), $modules ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.