|
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
|
|
|
); |
|
23
|
|
|
|
|
24
|
|
|
protected function result() { |
|
25
|
|
|
|
|
26
|
|
|
$modules = $this->get_modules(); |
|
27
|
|
|
|
|
28
|
|
|
if ( ! $this->bulk && ! empty( $modules ) ) { |
|
29
|
|
|
return array_pop( $modules ); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return array( 'modules' => $modules ); |
|
33
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Walks through either the submitted modules or list of themes and creates the global array |
|
38
|
|
|
* @param $theme |
|
39
|
|
|
* |
|
40
|
|
|
* @return bool |
|
41
|
|
|
*/ |
|
42
|
|
View Code Duplication |
protected function validate_input( $module) { |
|
43
|
|
|
$args = $this->input(); |
|
44
|
|
|
// lets set what modules were requested, and validate them |
|
45
|
|
|
if ( ! isset( $module ) || empty( $module ) ) { |
|
46
|
|
|
|
|
47
|
|
|
if ( ! $args['modules'] || empty( $args['modules'] ) ) { |
|
48
|
|
|
return new WP_Error( 'missing_module', __( 'You are required to specify a module.', 'jetpack' ), 400 ); |
|
49
|
|
|
} |
|
50
|
|
|
if ( is_array( $args['modules'] ) ) { |
|
51
|
|
|
$this->modules = $args['modules']; |
|
52
|
|
|
} else { |
|
53
|
|
|
$this->modules[] = $args['modules']; |
|
54
|
|
|
} |
|
55
|
|
|
} else { |
|
56
|
|
|
$this->modules[] = urldecode( $module ); |
|
57
|
|
|
$this->bulk = false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ( is_wp_error( $error = $this->validate_modules() ) ) { |
|
61
|
|
|
return $error; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return parent::validate_input( $module ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Walks through submitted themes to make sure they are valid |
|
69
|
|
|
* @return bool|WP_Error |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function validate_modules() { |
|
72
|
|
|
foreach ( $this->modules as $module ) { |
|
73
|
|
|
if ( ! Jetpack::is_module( $module ) ) { |
|
74
|
|
|
return new WP_Error( 'unknown_jetpack_module', sprintf( __( 'Module not found: `%s`.', 'jetpack' ), $module ), 404 ); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected static function format_module( $module_slug ) { |
|
81
|
|
|
$module_data = Jetpack::get_module( $module_slug ); |
|
82
|
|
|
|
|
83
|
|
|
$module = array(); |
|
84
|
|
|
$module['id'] = $module_slug; |
|
85
|
|
|
$module['active'] = Jetpack::is_module_active( $module_slug ); |
|
86
|
|
|
$module['name'] = $module_data['name']; |
|
87
|
|
|
$module['short_description'] = $module_data['description']; |
|
88
|
|
|
$module['sort'] = $module_data['sort']; |
|
89
|
|
|
$module['introduced'] = $module_data['introduced']; |
|
90
|
|
|
$module['changed'] = $module_data['changed']; |
|
91
|
|
|
$module['free'] = $module_data['free']; |
|
92
|
|
|
$module['module_tags'] = $module_data['module_tags']; |
|
93
|
|
|
|
|
94
|
|
|
// Fetch the HTML formatted long description |
|
95
|
|
|
ob_start(); |
|
96
|
|
|
/** This action is documented in class.jetpack-modules-list-table.php */ |
|
97
|
|
|
do_action( 'jetpack_module_more_info_' . $module_slug ); |
|
98
|
|
|
$module['description'] = ob_get_clean(); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
return $module; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Format a list of modules for public display, using the supplied offset and limit args |
|
105
|
|
|
* @uses WPCOM_JSON_API_Endpoint::query_args() |
|
106
|
|
|
* @return array Public API modules objects |
|
107
|
|
|
*/ |
|
108
|
|
View Code Duplication |
protected function get_modules() { |
|
109
|
|
|
$modules = array_values( $this->modules ); |
|
110
|
|
|
// do offset & limit - we've already returned a 400 error if they're bad numbers |
|
111
|
|
|
$args = $this->query_args(); |
|
112
|
|
|
|
|
113
|
|
|
if ( isset( $args['offset'] ) ) |
|
114
|
|
|
$modules = array_slice( $modules, (int) $args['offset'] ); |
|
115
|
|
|
if ( isset( $args['limit'] ) ) |
|
116
|
|
|
$modules = array_slice( $modules, 0, (int) $args['limit'] ); |
|
117
|
|
|
|
|
118
|
|
|
return array_map( array( $this, 'format_module' ), $modules ); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
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.