PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 13.7.2
Jetpack – WP Security, Backup, Speed, & Growth v13.7.2
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / json-endpoints / jetpack / class.jetpack-json-api-modules-endpoint.php

class.jetpack-json-api-modules-endpoint.php in Jetpack – WP Security, Backup, Speed, & Growth 13.7.2, at json-endpoints/jetpack/class.jetpack-json-api-modules-endpoint.php

158 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
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 /**
9 * The modules.
10 *
11 * @var array
12 */
13 protected $modules = array();
14
15 /**
16 * If we're working in bulk.
17 *
18 * @var boolean
19 */
20 protected $bulk = true;
21
22 /**
23 * Response format.
24 *
25 * @var array
26 */
27 public static $_response_format = array( // phpcs:ignore PSR2.Classes.PropertyDeclaration.Underscore
28 'id' => '(string) The module\'s ID',
29 'active' => '(boolean) The module\'s status.',
30 'name' => '(string) The module\'s name.',
31 'description' => '(safehtml) The module\'s description.',
32 'sort' => '(int) The module\'s display order.',
33 'introduced' => '(string) The Jetpack version when the module was introduced.',
34 'changed' => '(string) The Jetpack version when the module was changed.',
35 'free' => '(boolean) The module\'s Free or Paid status.',
36 'module_tags' => '(array) The module\'s tags.',
37 'override' => '(string) The module\'s override. Empty if no override, otherwise \'active\' or \'inactive\'',
38 );
39
40 /**
41 * The result.
42 *
43 * @return array
44 */
45 protected function result() {
46
47 $modules = $this->get_modules();
48
49 if ( ! $this->bulk && ! empty( $modules ) ) {
50 return array_pop( $modules );
51 }
52
53 return array( 'modules' => $modules );
54 }
55
56 /**
57 * Walks through either the submitted modules or list of themes and creates the global array.
58 *
59 * @param string $module - the modules.
60 *
61 * @return bool|WP_Error
62 */
63 protected function validate_input( $module ) {
64 $args = $this->input();
65 // lets set what modules were requested, and validate them
66 if ( ! isset( $module ) || empty( $module ) ) {
67
68 if ( ! $args['modules'] || empty( $args['modules'] ) ) {
69 return new WP_Error( 'missing_module', __( 'You are required to specify a module.', 'jetpack' ), 400 );
70 }
71 if ( is_array( $args['modules'] ) ) {
72 $this->modules = $args['modules'];
73 } else {
74 $this->modules[] = $args['modules'];
75 }
76 } else {
77 $this->modules[] = urldecode( $module );
78 $this->bulk = false;
79 }
80
81 $error = $this->validate_modules();
82 if ( is_wp_error( $error ) ) {
83 return $error;
84 }
85
86 return parent::validate_input( $module );
87 }
88
89 /**
90 * Walks through submitted themes to make sure they are valid
91 *
92 * @return bool|WP_Error
93 */
94 protected function validate_modules() {
95 foreach ( $this->modules as $module ) {
96 if ( ! Jetpack::is_module( $module ) ) {
97 // Translators: the module that's not found.
98 return new WP_Error( 'unknown_jetpack_module', sprintf( __( 'Module not found: `%s`.', 'jetpack' ), $module ), 404 );
99 }
100 }
101 return true;
102 }
103
104 /**
105 * Format the module.
106 *
107 * @param string $module_slug - the module slug.
108 *
109 * @return array
110 */
111 protected static function format_module( $module_slug ) {
112 $module_data = Jetpack::get_module( $module_slug );
113
114 $module = array();
115 $module['id'] = $module_slug;
116 $module['active'] = Jetpack::is_module_active( $module_slug );
117 $module['name'] = $module_data['name'];
118 $module['short_description'] = $module_data['description'];
119 $module['sort'] = $module_data['sort'];
120 $module['introduced'] = $module_data['introduced'];
121 $module['changed'] = $module_data['changed'];
122 $module['free'] = $module_data['free'];
123 $module['module_tags'] = array_map( 'jetpack_get_module_i18n_tag', $module_data['module_tags'] );
124
125 $overrides_instance = Jetpack_Modules_Overrides::instance();
126 $module['override'] = $overrides_instance->get_module_override( $module_slug );
127
128 // Fetch the HTML formatted long description
129 ob_start();
130 /** This action is documented in class.jetpack-modules-list-table.php */
131 do_action( 'jetpack_module_more_info_' . $module_slug );
132 $module['description'] = ob_get_clean();
133
134 return $module;
135 }
136
137 /**
138 * Format a list of modules for public display, using the supplied offset and limit args
139 *
140 * @uses WPCOM_JSON_API_Endpoint::query_args()
141 * @return array Public API modules objects
142 */
143 protected function get_modules() {
144 $modules = array_values( $this->modules );
145 // do offset & limit - we've already returned a 400 error if they're bad numbers
146 $args = $this->query_args();
147
148 if ( isset( $args['offset'] ) ) {
149 $modules = array_slice( $modules, (int) $args['offset'] );
150 }
151 if ( isset( $args['limit'] ) ) {
152 $modules = array_slice( $modules, 0, (int) $args['limit'] );
153 }
154
155 return array_map( array( $this, 'format_module' ), $modules );
156 }
157 }
158