Total Complexity | 61 |
Total Lines | 352 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like MonsterInsights_Rest_Routes often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MonsterInsights_Rest_Routes, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class MonsterInsights_Rest_Routes { |
||
12 | |||
13 | /** |
||
14 | * MonsterInsights_Rest_Routes constructor. |
||
15 | */ |
||
16 | public function __construct() { |
||
17 | |||
18 | add_action( 'wp_ajax_monsterinsights_vue_get_license', array( $this, 'get_license' ) ); |
||
19 | add_action( 'wp_ajax_monsterinsights_vue_get_profile', array( $this, 'get_profile' ) ); |
||
20 | add_action( 'wp_ajax_monsterinsights_vue_get_settings', array( $this, 'get_settings' ) ); |
||
21 | add_action( 'wp_ajax_monsterinsights_vue_update_settings', array( $this, 'update_settings' ) ); |
||
22 | add_action( 'wp_ajax_monsterinsights_vue_get_addons', array( $this, 'get_addons' ) ); |
||
23 | add_action( 'wp_ajax_monsterinsights_update_manual_ua', array( $this, 'update_manual_ua' ) ); |
||
24 | |||
25 | add_action( 'admin_notices', array( $this, 'hide_old_notices' ), 0 ); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Ajax handler for grabbing the license |
||
30 | */ |
||
31 | public function get_license() { |
||
32 | |||
33 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
34 | |||
35 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
36 | return; |
||
37 | } |
||
38 | |||
39 | $site_license = array( |
||
40 | 'key' => MonsterInsights()->license->get_site_license_key(), |
||
41 | 'type' => MonsterInsights()->license->get_site_license_type(), |
||
42 | 'is_disabled' => MonsterInsights()->license->site_license_disabled(), |
||
43 | 'is_expired' => MonsterInsights()->license->site_license_expired(), |
||
44 | 'is_invalid' => MonsterInsights()->license->site_license_invalid(), |
||
45 | ); |
||
46 | $network_license = array( |
||
47 | 'key' => MonsterInsights()->license->get_network_license_key(), |
||
48 | 'type' => MonsterInsights()->license->get_network_license_type(), |
||
49 | 'is_disabled' => MonsterInsights()->license->network_license_disabled(), |
||
50 | 'is_expired' => MonsterInsights()->license->network_license_expired(), |
||
51 | 'is_invalid' => MonsterInsights()->license->network_license_disabled(), |
||
52 | ); |
||
53 | |||
54 | wp_send_json( array( |
||
55 | 'site' => $site_license, |
||
56 | 'network' => $network_license, |
||
57 | ) ); |
||
58 | |||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Ajax handler for grabbing the license |
||
63 | */ |
||
64 | public function get_profile() { |
||
79 | ) ); |
||
80 | |||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Ajax handler for grabbing the license |
||
85 | */ |
||
86 | public function get_settings() { |
||
87 | |||
88 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
89 | |||
90 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
91 | return; |
||
92 | } |
||
93 | |||
94 | $options = monsterinsights_get_options(); |
||
95 | |||
96 | // Array fields are needed even if empty. |
||
97 | $array_fields = array( 'view_reports', 'save_settings', 'ignore_users' ); |
||
98 | foreach ( $array_fields as $array_field ) { |
||
99 | if ( ! isset( $options[ $array_field ] ) ) { |
||
100 | $options[ $array_field ] = array(); |
||
101 | } |
||
102 | } |
||
103 | if ( isset( $options['custom_code'] ) ) { |
||
104 | $options['custom_code'] = stripslashes( $options['custom_code'] ); |
||
105 | } |
||
106 | |||
107 | wp_send_json( $options ); |
||
108 | |||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Ajax handler for grabbing the license |
||
113 | */ |
||
114 | public function update_settings() { |
||
115 | |||
116 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
117 | |||
118 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
119 | return; |
||
120 | } |
||
121 | |||
122 | if ( isset( $_POST['setting'] ) ) { |
||
123 | $setting = sanitize_text_field( wp_unslash( $_POST['setting'] ) ); |
||
124 | if ( isset( $_POST['value'] ) ) { |
||
125 | $value = $this->handle_sanitization( $setting, $_POST['value'] ); |
||
126 | monsterinsights_update_option( $setting, $value ); |
||
127 | } else { |
||
128 | monsterinsights_update_option( $setting, false ); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | wp_send_json_success(); |
||
133 | |||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Sanitization specific to each field. |
||
138 | * |
||
139 | * @param string $field The key of the field to sanitize. |
||
140 | * @param string $value The value of the field to sanitize. |
||
141 | * |
||
142 | * @return mixed The sanitized input. |
||
143 | */ |
||
144 | private function handle_sanitization( $field, $value ) { |
||
145 | |||
146 | $value = wp_unslash( $value ); |
||
147 | |||
148 | // Textarea fields. |
||
149 | $textarea_fields = array( |
||
150 | 'custom_code', |
||
151 | ); |
||
152 | |||
153 | if ( in_array( $field, $textarea_fields, true ) ) { |
||
154 | if ( function_exists( 'sanitize_textarea_field' ) ) { |
||
155 | return sanitize_textarea_field( $value ); |
||
|
|||
156 | } else { |
||
157 | return wp_kses( $value, array() ); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | $array_value = json_decode( $value, true ); |
||
162 | if ( is_array( $array_value ) ) { |
||
163 | $value = $array_value; |
||
164 | // Don't save empty values. |
||
165 | foreach ( $value as $key => $item ) { |
||
166 | if ( is_array( $item ) ) { |
||
167 | $empty = true; |
||
168 | foreach ( $item as $item_value ) { |
||
169 | if ( ! empty( $item_value ) ) { |
||
170 | $empty = false; |
||
171 | } |
||
172 | } |
||
173 | if ( $empty ) { |
||
174 | unset( $value[ $key ] ); |
||
175 | } |
||
176 | } |
||
177 | } |
||
178 | |||
179 | // Reset array keys because JavaScript can't handle arrays with non-sequential keys. |
||
180 | $value = array_values( $value ); |
||
181 | |||
182 | return $value; |
||
183 | } |
||
184 | |||
185 | return sanitize_text_field( $value ); |
||
186 | |||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Return the state of the addons ( installed, activated ) |
||
191 | */ |
||
192 | public function get_addons() { |
||
263 | } |
||
264 | |||
265 | public function get_addon( $installed_plugins, $addons_type, $addon, $slug ) { |
||
266 | $active = false; |
||
267 | $installed = false; |
||
268 | $plugin_basename = monsterinsights_get_plugin_basename_from_slug( $slug ); |
||
269 | |||
270 | if ( isset( $installed_plugins[ $plugin_basename ] ) ) { |
||
271 | $installed = true; |
||
272 | $ms_active = is_plugin_active_for_network( $plugin_basename ); |
||
273 | $ss_active = is_plugin_active( $plugin_basename ); |
||
274 | |||
275 | if ( is_multisite() && is_network_admin() ) { |
||
276 | $active = is_plugin_active_for_network( $plugin_basename ); |
||
277 | } else { |
||
278 | $active = is_plugin_active( $plugin_basename ); |
||
279 | } |
||
280 | } |
||
281 | if ( empty( $addon->url ) ) { |
||
282 | $addon->url = ''; |
||
283 | } |
||
284 | |||
285 | $addon->type = $addons_type; |
||
286 | $addon->installed = $installed; |
||
287 | $addon->active = $active; |
||
288 | $addon->basename = $plugin_basename; |
||
289 | return $addon; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Use custom notices in the Vue app on the Settings screen. |
||
294 | */ |
||
295 | public function hide_old_notices() { |
||
311 | } |
||
312 | |||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Update manual ua. |
||
317 | */ |
||
318 | public function update_manual_ua() { |
||
365 |