| Total Complexity | 41 |
| Total Lines | 205 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MonsterInsights_Tracking 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_Tracking, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class MonsterInsights_Tracking { |
||
| 24 | |||
| 25 | public function __construct() { |
||
| 32 | } |
||
| 33 | |||
| 34 | private function get_data() { |
||
| 35 | $data = array(); |
||
| 36 | |||
| 37 | // Retrieve current theme info |
||
| 38 | $theme_data = wp_get_theme(); |
||
| 39 | $tracking_mode = monsterinsights_get_option( 'tracking_mode', 'analytics' ); |
||
| 40 | $events_mode = monsterinsights_get_option( 'events_mode', 'none' ); |
||
| 41 | $update_mode = monsterinsights_get_option( 'automatic_updates', false ); |
||
| 42 | |||
| 43 | if ( $tracking_mode === false ) { |
||
|
|
|||
| 44 | $tracking_mode = 'analytics'; |
||
| 45 | } |
||
| 46 | |||
| 47 | if ( $events_mode === false ) { |
||
| 48 | $events_mode = 'none'; |
||
| 49 | } |
||
| 50 | |||
| 51 | if ( $update_mode === false ) { |
||
| 52 | $update_mode = 'none'; |
||
| 53 | } |
||
| 54 | |||
| 55 | $count_b = 1; |
||
| 56 | if ( is_multisite() ) { |
||
| 57 | if ( function_exists( 'get_blog_count' ) ) { |
||
| 58 | $count_b = get_blog_count(); |
||
| 59 | } else { |
||
| 60 | $count_b = 'Not Set'; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | $data['php_version'] = phpversion(); |
||
| 65 | $data['mi_version'] = MONSTERINSIGHTS_VERSION; |
||
| 66 | $data['wp_version'] = get_bloginfo( 'version' ); |
||
| 67 | $data['server'] = isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : ''; |
||
| 68 | $data['over_time'] = get_option( 'monsterinsights_over_time', array() ); |
||
| 69 | $data['multisite'] = is_multisite(); |
||
| 70 | $data['url'] = home_url(); |
||
| 71 | $data['themename'] = $theme_data->Name; |
||
| 72 | $data['themeversion'] = $theme_data->Version; |
||
| 73 | $data['email'] = get_bloginfo( 'admin_email' ); |
||
| 74 | $data['key'] = monsterinsights_get_license_key(); |
||
| 75 | $data['sas'] = monsterinsights_get_shareasale_id(); |
||
| 76 | $data['settings'] = monsterinsights_get_options(); |
||
| 77 | $data['tracking_mode'] = $tracking_mode; |
||
| 78 | $data['events_mode'] = $events_mode; |
||
| 79 | $data['autoupdate'] = $update_mode; |
||
| 80 | $data['pro'] = (int) monsterinsights_is_pro_version(); |
||
| 81 | $data['sites'] = $count_b; |
||
| 82 | $data['usagetracking'] = get_option( 'monsterinsights_usage_tracking_config', $tracking ); |
||
| 83 | $data['usercount'] = function_exists( 'get_user_count' ) ? get_user_count() : 'Not Set'; |
||
| 84 | |||
| 85 | // Retrieve current plugin information |
||
| 86 | if( ! function_exists( 'get_plugins' ) ) { |
||
| 87 | include ABSPATH . '/wp-admin/includes/plugin.php'; |
||
| 88 | } |
||
| 89 | |||
| 90 | $plugins = array_keys( get_plugins() ); |
||
| 91 | $active_plugins = get_option( 'active_plugins', array() ); |
||
| 92 | |||
| 93 | foreach ( $plugins as $key => $plugin ) { |
||
| 94 | if ( in_array( $plugin, $active_plugins ) ) { |
||
| 95 | // Remove active plugins from list so we can show active and inactive separately |
||
| 96 | unset( $plugins[ $key ] ); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | $data['active_plugins'] = $active_plugins; |
||
| 101 | $data['inactive_plugins'] = $plugins; |
||
| 102 | $data['locale'] = get_locale(); |
||
| 103 | |||
| 104 | return $data; |
||
| 105 | } |
||
| 106 | |||
| 107 | public function send_checkin( $override = false, $ignore_last_checkin = false ) { |
||
| 108 | |||
| 109 | $home_url = trailingslashit( home_url() ); |
||
| 110 | if ( strpos( $home_url, 'monsterinsights.com' ) !== false ) { |
||
| 111 | return false; |
||
| 112 | } |
||
| 113 | |||
| 114 | if( ! $this->tracking_allowed() && ! $override ) { |
||
| 115 | return false; |
||
| 116 | } |
||
| 117 | |||
| 118 | // Send a maximum of once per week |
||
| 119 | $last_send = get_option( 'monsterinsights_usage_tracking_last_checkin' ); |
||
| 120 | if ( is_numeric( $last_send ) && $last_send > strtotime( '-1 week' ) && ! $ignore_last_checkin ) { |
||
| 121 | return false; |
||
| 122 | } |
||
| 123 | |||
| 124 | $request = wp_remote_post( 'https://miusage.com/v1/checkin/', array( |
||
| 125 | 'method' => 'POST', |
||
| 126 | 'timeout' => 5, |
||
| 127 | 'redirection' => 5, |
||
| 128 | 'httpversion' => '1.1', |
||
| 129 | 'blocking' => false, |
||
| 130 | 'body' => $this->get_data(), |
||
| 131 | 'user-agent' => 'MI/' . MONSTERINSIGHTS_VERSION . '; ' . get_bloginfo( 'url' ) |
||
| 132 | ) ); |
||
| 133 | |||
| 134 | // If we have completed successfully, recheck in 1 week |
||
| 135 | update_option( 'monsterinsights_usage_tracking_last_checkin', time() ); |
||
| 136 | return true; |
||
| 137 | } |
||
| 138 | |||
| 139 | private function tracking_allowed() { |
||
| 140 | return (bool) monsterinsights_get_option( 'anonymous_data', false ) || monsterinsights_is_pro_version(); |
||
| 141 | } |
||
| 142 | |||
| 143 | public function schedule_send() { |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | public function check_for_settings_optin() { |
||
| 174 | } |
||
| 175 | |||
| 176 | } |
||
| 177 | |||
| 178 | public function check_for_optin() { |
||
| 179 | if ( ! ( ! empty( $_REQUEST['mi_action'] ) && 'opt_into_tracking' === $_REQUEST['mi_action'] ) ) { |
||
| 180 | return; |
||
| 181 | } |
||
| 182 | |||
| 183 | if ( monsterinsights_get_option( 'anonymous_data', false ) ) { |
||
| 184 | return; |
||
| 185 | } |
||
| 186 | |||
| 187 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
| 188 | return; |
||
| 189 | } |
||
| 190 | |||
| 191 | if ( monsterinsights_is_pro_version() ) { |
||
| 192 | return; |
||
| 193 | } |
||
| 194 | |||
| 195 | monsterinsights_update_option( 'anonymous_data', 1 ); |
||
| 196 | $this->send_checkin( true, true ); |
||
| 197 | update_option( 'monsterinsights_tracking_notice', 1 ); |
||
| 198 | } |
||
| 199 | |||
| 200 | public function check_for_optout() { |
||
| 219 | } |
||
| 220 | |||
| 221 | public function add_schedules( $schedules = array() ) { |
||
| 222 | // Adds once weekly to the existing schedules. |
||
| 228 | } |
||
| 229 | } |
||
| 230 | new MonsterInsights_Tracking(); |