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