Total Complexity | 111 |
Total Lines | 641 |
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 | add_action( 'wp_ajax_monsterinsights_vue_get_report_data', array( $this, 'get_report_data' ) ); |
||
25 | add_action( 'wp_ajax_monsterinsights_vue_install_plugin', array( $this, 'install_plugin' ) ); |
||
26 | |||
27 | add_action( 'wp_ajax_monsterinsights_handle_settings_import', array( $this, 'handle_settings_import' ) ); |
||
28 | |||
29 | add_action( 'admin_notices', array( $this, 'hide_old_notices' ), 0 ); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Ajax handler for grabbing the license |
||
34 | */ |
||
35 | public function get_license() { |
||
36 | |||
37 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
38 | |||
39 | if ( ! current_user_can( 'monsterinsights_view_dashboard' ) ) { |
||
40 | return; |
||
41 | } |
||
42 | |||
43 | $site_license = array( |
||
44 | 'key' => MonsterInsights()->license->get_site_license_key(), |
||
45 | 'type' => MonsterInsights()->license->get_site_license_type(), |
||
46 | 'is_disabled' => MonsterInsights()->license->site_license_disabled(), |
||
47 | 'is_expired' => MonsterInsights()->license->site_license_expired(), |
||
48 | 'is_invalid' => MonsterInsights()->license->site_license_invalid(), |
||
49 | ); |
||
50 | $network_license = array( |
||
51 | 'key' => MonsterInsights()->license->get_network_license_key(), |
||
52 | 'type' => MonsterInsights()->license->get_network_license_type(), |
||
53 | 'is_disabled' => MonsterInsights()->license->network_license_disabled(), |
||
54 | 'is_expired' => MonsterInsights()->license->network_license_expired(), |
||
55 | 'is_invalid' => MonsterInsights()->license->network_license_disabled(), |
||
56 | ); |
||
57 | |||
58 | wp_send_json( array( |
||
59 | 'site' => $site_license, |
||
60 | 'network' => $network_license, |
||
61 | ) ); |
||
62 | |||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Ajax handler for grabbing the license |
||
67 | */ |
||
68 | public function get_profile() { |
||
69 | |||
70 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
71 | |||
72 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
73 | return; |
||
74 | } |
||
75 | |||
76 | wp_send_json( array( |
||
77 | 'ua' => MonsterInsights()->auth->get_ua(), |
||
78 | 'viewname' => MonsterInsights()->auth->get_viewname(), |
||
79 | 'manual_ua' => MonsterInsights()->auth->get_manual_ua(), |
||
80 | 'network_ua' => MonsterInsights()->auth->get_network_ua(), |
||
81 | 'network_viewname' => MonsterInsights()->auth->get_network_viewname(), |
||
82 | 'network_manual_ua' => MonsterInsights()->auth->get_network_manual_ua(), |
||
83 | ) ); |
||
84 | |||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Ajax handler for grabbing the license |
||
89 | */ |
||
90 | public function get_settings() { |
||
91 | |||
92 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
93 | |||
94 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
95 | return; |
||
96 | } |
||
97 | |||
98 | $options = monsterinsights_get_options(); |
||
99 | |||
100 | // Array fields are needed even if empty. |
||
101 | $array_fields = array( 'view_reports', 'save_settings', 'ignore_users' ); |
||
102 | foreach ( $array_fields as $array_field ) { |
||
103 | if ( ! isset( $options[ $array_field ] ) ) { |
||
104 | $options[ $array_field ] = array(); |
||
105 | } |
||
106 | } |
||
107 | if ( isset( $options['custom_code'] ) ) { |
||
108 | $options['custom_code'] = stripslashes( $options['custom_code'] ); |
||
109 | } |
||
110 | |||
111 | wp_send_json( $options ); |
||
112 | |||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Ajax handler for grabbing the license |
||
117 | */ |
||
118 | public function update_settings() { |
||
119 | |||
120 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
121 | |||
122 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
123 | return; |
||
124 | } |
||
125 | |||
126 | if ( isset( $_POST['setting'] ) ) { |
||
127 | $setting = sanitize_text_field( wp_unslash( $_POST['setting'] ) ); |
||
128 | if ( isset( $_POST['value'] ) ) { |
||
129 | $value = $this->handle_sanitization( $setting, $_POST['value'] ); |
||
130 | monsterinsights_update_option( $setting, $value ); |
||
131 | } else { |
||
132 | monsterinsights_update_option( $setting, false ); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | wp_send_json_success(); |
||
137 | |||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Sanitization specific to each field. |
||
142 | * |
||
143 | * @param string $field The key of the field to sanitize. |
||
144 | * @param string $value The value of the field to sanitize. |
||
145 | * |
||
146 | * @return mixed The sanitized input. |
||
147 | */ |
||
148 | private function handle_sanitization( $field, $value ) { |
||
149 | |||
150 | $value = wp_unslash( $value ); |
||
151 | |||
152 | // Textarea fields. |
||
153 | $textarea_fields = array( |
||
154 | 'custom_code', |
||
155 | ); |
||
156 | |||
157 | if ( in_array( $field, $textarea_fields, true ) ) { |
||
158 | if ( function_exists( 'sanitize_textarea_field' ) ) { |
||
159 | return sanitize_textarea_field( $value ); |
||
|
|||
160 | } else { |
||
161 | return wp_kses( $value, array() ); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | $array_value = json_decode( $value, true ); |
||
166 | if ( is_array( $array_value ) ) { |
||
167 | $value = $array_value; |
||
168 | // Don't save empty values. |
||
169 | foreach ( $value as $key => $item ) { |
||
170 | if ( is_array( $item ) ) { |
||
171 | $empty = true; |
||
172 | foreach ( $item as $item_value ) { |
||
173 | if ( ! empty( $item_value ) ) { |
||
174 | $empty = false; |
||
175 | } |
||
176 | } |
||
177 | if ( $empty ) { |
||
178 | unset( $value[ $key ] ); |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | |||
183 | // Reset array keys because JavaScript can't handle arrays with non-sequential keys. |
||
184 | $value = array_values( $value ); |
||
185 | |||
186 | return $value; |
||
187 | } |
||
188 | |||
189 | return sanitize_text_field( $value ); |
||
190 | |||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Return the state of the addons ( installed, activated ) |
||
195 | */ |
||
196 | public function get_addons() { |
||
197 | |||
198 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
199 | |||
200 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
201 | return; |
||
202 | } |
||
203 | |||
204 | if ( isset( $_POST['network'] ) && intval( $_POST['network'] ) > 0 ) { |
||
205 | define( 'WP_NETWORK_ADMIN', true ); |
||
206 | } |
||
207 | |||
208 | $addons_data = monsterinsights_get_addons(); |
||
209 | $parsed_addons = array(); |
||
210 | $installed_plugins = get_plugins(); |
||
211 | |||
212 | if ( ! is_array( $addons_data ) ) { |
||
213 | $addons_data = array(); |
||
214 | } |
||
215 | |||
216 | foreach ( $addons_data as $addons_type => $addons ) { |
||
217 | foreach ( $addons as $addon ) { |
||
218 | $slug = 'monsterinsights-' . $addon->slug; |
||
219 | if ( 'monsterinsights-ecommerce' === $slug ) { |
||
220 | $addon = $this->get_addon( $installed_plugins, $addons_type, $addon, $slug ); |
||
221 | if ( empty( $addon->installed ) ) { |
||
222 | $slug = 'ga-ecommerce'; |
||
223 | $addon = $this->get_addon( $installed_plugins, $addons_type, $addon, $slug ); |
||
224 | } |
||
225 | } else { |
||
226 | $addon = $this->get_addon( $installed_plugins, $addons_type, $addon, $slug ); |
||
227 | } |
||
228 | $parsed_addons[ $addon->slug ] = $addon; |
||
229 | } |
||
230 | } |
||
231 | |||
232 | // Include data about the plugins needed by some addons ( WooCommerce, EDD, Google AMP, CookieBot, etc ). |
||
233 | // WooCommerce. |
||
234 | $parsed_addons['woocommerce'] = array( |
||
235 | 'active' => class_exists( 'WooCommerce' ), |
||
236 | ); |
||
237 | // Edd. |
||
238 | $parsed_addons['easy_digital_downloads'] = array( |
||
239 | 'active' => class_exists( 'Easy_Digital_Downloads' ), |
||
240 | ); |
||
241 | // MemberPress. |
||
242 | $parsed_addons['memberpress'] = array( |
||
243 | 'active' => defined( 'MEPR_VERSION' ) && version_compare( MEPR_VERSION, '1.3.43', '>' ), |
||
244 | ); |
||
245 | // Cookiebot. |
||
246 | $parsed_addons['cookiebot'] = array( |
||
247 | 'active' => function_exists( 'cookiebot_active' ) && cookiebot_active(), |
||
248 | ); |
||
249 | // Cookie Notice. |
||
250 | $parsed_addons['cookie_notice'] = array( |
||
251 | 'active' => class_exists( 'Cookie_Notice' ), |
||
252 | ); |
||
253 | // Fb Instant Articles. |
||
254 | $parsed_addons['instant_articles'] = array( |
||
255 | 'active' => defined( 'IA_PLUGIN_VERSION' ) && version_compare( IA_PLUGIN_VERSION, '3.3.4', '>' ), |
||
256 | ); |
||
257 | // Google AMP. |
||
258 | $parsed_addons['google_amp'] = array( |
||
259 | 'active' => defined( 'AMP__FILE__' ), |
||
260 | ); |
||
261 | // WPForms. |
||
262 | $parsed_addons['wpforms'] = array( |
||
263 | 'active' => function_exists( 'wpforms' ), |
||
264 | 'icon' => plugin_dir_url( MONSTERINSIGHTS_PLUGIN_FILE ) . 'assets/images/plugin-wpforms.png', |
||
265 | 'title' => 'WPForms', |
||
266 | 'excerpt' => __( 'The most beginner friendly drag & drop WordPress forms plugin allowing you to create beautiful contact forms, subscription forms, payment forms, and more in minutes, not hours!', 'google-analytics-for-wordpress' ), |
||
267 | 'installed' => array_key_exists( 'wpforms-lite/wpforms.php', $installed_plugins ), |
||
268 | 'slug' => 'wpforms-lite', |
||
269 | ); |
||
270 | // OptinMonster. |
||
271 | $parsed_addons['optinmonster'] = array( |
||
272 | 'active' => class_exists( 'OMAPI' ), |
||
273 | 'icon' => plugin_dir_url( MONSTERINSIGHTS_PLUGIN_FILE ) . 'assets/images/plugin-om.png', |
||
274 | 'title' => 'OptinMonster', |
||
275 | 'excerpt' => __( 'Our high-converting optin forms like Exit-Intent® popups, Fullscreen Welcome Mats, and Scroll boxes help you dramatically boost conversions and get more email subscribers.', 'google-analytics-for-wordpress' ), |
||
276 | 'installed' => array_key_exists( 'optinmonster/optin-monster-wp-api.php', $installed_plugins ), |
||
277 | 'basename' => 'optinmonster/optin-monster-wp-api.php', |
||
278 | 'slug' => 'optinmonster', |
||
279 | ); |
||
280 | // OptinMonster. |
||
281 | $parsed_addons['wp-mail-smtp'] = array( |
||
282 | 'active' => function_exists( 'wp_mail_smtp' ), |
||
283 | 'icon' => plugin_dir_url( MONSTERINSIGHTS_PLUGIN_FILE ) . 'assets/images/plugin-smtp.png', |
||
284 | 'title' => 'WP Mail SMTP', |
||
285 | 'excerpt' => __( 'SMTP (Simple Mail Transfer Protocol) is an industry standard for sending emails. SMTP helps increase email deliverability by using proper authentication', 'google-analytics-for-wordpress' ), |
||
286 | 'installed' => array_key_exists( 'optinmonster/optin-monster-wp-api.php', $installed_plugins ), |
||
287 | 'basename' => 'wp-mail-smtp/wp_mail_smtp.php', |
||
288 | 'slug' => 'wp-mail-smtp', |
||
289 | ); |
||
290 | // Gravity Forms. |
||
291 | $parsed_addons['gravity_forms'] = array( |
||
292 | 'active' => class_exists( 'GFCommon' ), |
||
293 | ); |
||
294 | // Formidable Forms. |
||
295 | $parsed_addons['formidable_forms'] = array( |
||
296 | 'active' => class_exists( 'FrmHooksController' ), |
||
297 | ); |
||
298 | // Manual UA Addon. |
||
299 | if ( ! isset( $parsed_addons['manual_ua'] ) ) { |
||
300 | $parsed_addons['manual_ua'] = array( |
||
301 | 'active' => class_exists( 'MonsterInsights_Manual_UA' ), |
||
302 | ); |
||
303 | } |
||
304 | |||
305 | wp_send_json( $parsed_addons ); |
||
306 | } |
||
307 | |||
308 | public function get_addon( $installed_plugins, $addons_type, $addon, $slug ) { |
||
309 | $active = false; |
||
310 | $installed = false; |
||
311 | $plugin_basename = monsterinsights_get_plugin_basename_from_slug( $slug ); |
||
312 | |||
313 | if ( isset( $installed_plugins[ $plugin_basename ] ) ) { |
||
314 | $installed = true; |
||
315 | $ms_active = is_plugin_active_for_network( $plugin_basename ); |
||
316 | $ss_active = is_plugin_active( $plugin_basename ); |
||
317 | |||
318 | if ( is_multisite() && is_network_admin() ) { |
||
319 | $active = is_plugin_active_for_network( $plugin_basename ); |
||
320 | } else { |
||
321 | $active = is_plugin_active( $plugin_basename ); |
||
322 | } |
||
323 | } |
||
324 | if ( empty( $addon->url ) ) { |
||
325 | $addon->url = ''; |
||
326 | } |
||
327 | |||
328 | $addon->type = $addons_type; |
||
329 | $addon->installed = $installed; |
||
330 | $addon->active = $active; |
||
331 | $addon->basename = $plugin_basename; |
||
332 | |||
333 | return $addon; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Use custom notices in the Vue app on the Settings screen. |
||
338 | */ |
||
339 | public function hide_old_notices() { |
||
340 | |||
341 | global $wp_version; |
||
342 | if ( version_compare( $wp_version, '4.6', '<' ) ) { |
||
343 | // remove_all_actions triggers an infinite loop on older versions. |
||
344 | return; |
||
345 | } |
||
346 | |||
347 | $screen = get_current_screen(); |
||
348 | // Bail if we're not on a MonsterInsights screen. |
||
349 | if ( empty( $screen->id ) || strpos( $screen->id, 'monsterinsights' ) === false ) { |
||
350 | return; |
||
351 | } |
||
352 | |||
353 | // Hide admin notices on the settings screen. |
||
354 | if ( monsterinsights_is_settings_page() ) { |
||
355 | remove_all_actions( 'admin_notices' ); |
||
356 | } |
||
357 | |||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Update manual ua. |
||
362 | */ |
||
363 | public function update_manual_ua() { |
||
364 | |||
365 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
366 | |||
367 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
368 | return; |
||
369 | } |
||
370 | |||
371 | $manual_ua_code = isset( $_POST['manual_ua_code'] ) ? sanitize_text_field( wp_unslash( $_POST['manual_ua_code'] ) ) : ''; |
||
372 | $manual_ua_code = monsterinsights_is_valid_ua( $manual_ua_code ); // Also sanitizes the string. |
||
373 | $manual_ua_code_old = MonsterInsights()->auth->get_manual_ua(); |
||
374 | if ( ! empty( $_REQUEST['isnetwork'] ) && sanitize_text_field( wp_unslash( $_REQUEST['isnetwork'] ) ) ) { |
||
375 | define( 'WP_NETWORK_ADMIN', true ); |
||
376 | } |
||
377 | |||
378 | if ( $manual_ua_code && $manual_ua_code_old && $manual_ua_code_old === $manual_ua_code ) { |
||
379 | // Same code we had before |
||
380 | // Do nothing. |
||
381 | wp_send_json_success(); |
||
382 | } else if ( $manual_ua_code && $manual_ua_code_old && $manual_ua_code_old !== $manual_ua_code ) { |
||
383 | // Different UA code. |
||
384 | if ( is_network_admin() ) { |
||
385 | MonsterInsights()->auth->set_network_manual_ua( $manual_ua_code ); |
||
386 | } else { |
||
387 | MonsterInsights()->auth->set_manual_ua( $manual_ua_code ); |
||
388 | } |
||
389 | } else if ( $manual_ua_code && empty( $manual_ua_code_old ) ) { |
||
390 | // Move to manual. |
||
391 | if ( is_network_admin() ) { |
||
392 | MonsterInsights()->auth->set_network_manual_ua( $manual_ua_code ); |
||
393 | } else { |
||
394 | MonsterInsights()->auth->set_manual_ua( $manual_ua_code ); |
||
395 | } |
||
396 | } else if ( empty( $manual_ua_code ) && $manual_ua_code_old ) { |
||
397 | // Deleted manual. |
||
398 | if ( is_network_admin() ) { |
||
399 | MonsterInsights()->auth->delete_network_manual_ua(); |
||
400 | } else { |
||
401 | MonsterInsights()->auth->delete_manual_ua(); |
||
402 | } |
||
403 | } else if ( isset( $_POST['manual_ua_code'] ) && empty( $manual_ua_code ) ) { |
||
404 | wp_send_json_error( array( |
||
405 | 'error' => __( 'Invalid UA code', 'google-analytics-for-wordpress' ), |
||
406 | ) ); |
||
407 | } |
||
408 | |||
409 | wp_send_json_success(); |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * |
||
414 | */ |
||
415 | public function handle_settings_import() { |
||
481 | |||
482 | } |
||
483 | |||
484 | /** |
||
485 | * Generic Ajax handler for grabbing report data in JSON. |
||
486 | */ |
||
487 | public function get_report_data() { |
||
563 | |||
564 | } |
||
565 | |||
566 | /** |
||
567 | * Install plugins which are not addons. |
||
568 | */ |
||
569 | public function install_plugin() { |
||
652 | } |
||
653 | } |
||
654 |