| Total Complexity | 43 |
| Total Lines | 305 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MonsterInsights_Dashboard_Widget 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_Dashboard_Widget, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class MonsterInsights_Dashboard_Widget { |
||
| 19 | |||
| 20 | const WIDGET_KEY = 'monsterinsights_reports_widget'; |
||
| 21 | /** |
||
| 22 | * The default options for the widget. |
||
| 23 | * |
||
| 24 | * @var array $default_options |
||
| 25 | */ |
||
| 26 | public static $default_options = array( |
||
| 27 | 'width' => 'regular', |
||
| 28 | 'interval' => '30', |
||
| 29 | 'reports' => array( |
||
| 30 | 'overview' => array( |
||
| 31 | 'toppages' => true, |
||
| 32 | 'newvsreturn' => true, |
||
| 33 | 'devices' => true, |
||
| 34 | ), |
||
| 35 | 'publisher' => array( |
||
| 36 | 'landingpages' => false, |
||
| 37 | 'exitpages' => false, |
||
| 38 | 'outboundlinks' => false, |
||
| 39 | 'affiliatelinks' => false, |
||
| 40 | 'downloadlinks' => false, |
||
| 41 | ), |
||
| 42 | 'ecommerce' => array( |
||
| 43 | 'infobox' => false, // E-commerce Overview. |
||
| 44 | 'products' => false, // Top Products. |
||
| 45 | 'conversions' => false, // Top Products. |
||
| 46 | 'addremove' => false, // Total Add/Remove. |
||
| 47 | 'days' => false, // Time to purchase. |
||
| 48 | 'sessions' => false, // Sessions to purchase. |
||
| 49 | ), |
||
| 50 | 'notice30day' => false, |
||
| 51 | ), |
||
| 52 | ); |
||
| 53 | /** |
||
| 54 | * The widget options. |
||
| 55 | * |
||
| 56 | * @var array $options |
||
| 57 | */ |
||
| 58 | public $options; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * MonsterInsights_Dashboard_Widget constructor. |
||
| 62 | */ |
||
| 63 | public function __construct() { |
||
| 64 | // Allow dashboard widget to be hidden on multisite installs |
||
| 65 | $show_widget = is_multisite() ? apply_filters( 'monsterinsights_show_dashboard_widget', true ) : true; |
||
| 66 | if ( ! $show_widget ) { |
||
| 67 | return false; |
||
| 68 | } |
||
| 69 | |||
| 70 | // Check if reports should be visible. |
||
| 71 | $dashboards_disabled = monsterinsights_get_option( 'dashboards_disabled', false ); |
||
| 72 | if ( ! current_user_can( 'monsterinsights_view_dashboard' ) || 'disabled' === $dashboards_disabled ) { |
||
| 73 | return false; |
||
| 74 | } |
||
| 75 | |||
| 76 | add_action( 'wp_dashboard_setup', array( $this, 'register_dashboard_widget' ) ); |
||
| 77 | |||
| 78 | add_action( 'admin_enqueue_scripts', array( $this, 'widget_scripts' ) ); |
||
| 79 | |||
| 80 | add_action( 'wp_ajax_monsterinsights_save_widget_state', array( $this, 'save_widget_state' ) ); |
||
| 81 | |||
| 82 | // Reminder notice. |
||
| 83 | add_action( 'admin_footer', array( $this, 'load_notice' ) ); |
||
| 84 | |||
| 85 | add_action( 'wp_ajax_monsterinsights_mark_notice_closed', array( $this, 'mark_notice_closed' ) ); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Register the dashboard widget. |
||
| 90 | */ |
||
| 91 | public function register_dashboard_widget() { |
||
| 92 | global $wp_meta_boxes; |
||
| 93 | |||
| 94 | wp_add_dashboard_widget( |
||
| 95 | self::WIDGET_KEY, |
||
| 96 | esc_html__( 'MonsterInsights', 'google-analytics-for-wordpress' ), |
||
| 97 | array( $this, 'dashboard_widget_content' ) |
||
| 98 | ); |
||
| 99 | |||
| 100 | // Attept to place the widget at the top. |
||
| 101 | $normal_dashboard = $wp_meta_boxes['dashboard']['normal']['core']; |
||
| 102 | $widget_instance = array( self::WIDGET_KEY => $normal_dashboard[ self::WIDGET_KEY ] ); |
||
| 103 | unset( $normal_dashboard[ self::WIDGET_KEY ] ); |
||
| 104 | $sorted_dashboard = array_merge( $widget_instance, $normal_dashboard ); |
||
| 105 | $wp_meta_boxes['dashboard']['normal']['core'] = $sorted_dashboard; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Load the widget content. |
||
| 110 | */ |
||
| 111 | public function dashboard_widget_content() { |
||
| 112 | $is_authed = ( MonsterInsights()->auth->is_authed() || MonsterInsights()->auth->is_network_authed() ); |
||
|
|
|||
| 113 | |||
| 114 | if ( ! $is_authed ) { |
||
| 115 | $this->widget_content_no_auth(); |
||
| 116 | } else { |
||
| 117 | monsterinsights_settings_error_page( 'monsterinsights-dashboard-widget', '', '0' ); |
||
| 118 | monsterinsights_settings_inline_js(); |
||
| 119 | } |
||
| 120 | |||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Message to display when the plugin is not authenticated. |
||
| 125 | */ |
||
| 126 | public function widget_content_no_auth() { |
||
| 127 | |||
| 128 | $url = is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights-onboarding' ) : admin_url( 'admin.php?page=monsterinsights-onboarding' ); |
||
| 129 | ?> |
||
| 130 | <div class="mi-dw-not-authed"> |
||
| 131 | <h2><?php esc_html_e( 'Website Analytics is not Setup', 'google-analytics-for-wordpress' ); ?></h2> |
||
| 132 | <p><?php esc_html_e( 'To see your website stats, please connect MonsterInsights to Google Analytics.', 'google-analytics-for-wordpress' ); ?></p> |
||
| 133 | <a href="<?php echo esc_url( $url ); ?>" class="mi-dw-btn-large"><?php esc_html_e( 'Setup Website Analytics', 'google-analytics-for-wordpress' ); ?></a> |
||
| 134 | </div> |
||
| 135 | <?php |
||
| 136 | } |
||
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * Load widget-specific scripts. |
||
| 141 | */ |
||
| 142 | public function widget_scripts() { |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Remove assets added by other plugins which conflict. |
||
| 214 | */ |
||
| 215 | public function remove_conflicting_asset_files() { |
||
| 216 | $scripts = array( |
||
| 217 | 'jetpack-onboarding-vendor', // Jetpack Onboarding Bluehost. |
||
| 218 | ); |
||
| 219 | |||
| 220 | if ( ! empty( $scripts ) ) { |
||
| 221 | foreach ( $scripts as $script ) { |
||
| 222 | wp_dequeue_script( $script ); // Remove JS file. |
||
| 223 | wp_deregister_script( $script ); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Store the widget state in the db using an Ajax call. |
||
| 230 | */ |
||
| 231 | public function save_widget_state() { |
||
| 232 | |||
| 233 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
| 234 | |||
| 235 | $default = self::$default_options; |
||
| 236 | $current_options = $this->get_options(); |
||
| 237 | |||
| 238 | $reports = $default['reports']; |
||
| 239 | if ( isset( $_POST['reports'] ) ) { |
||
| 240 | $reports = json_decode( sanitize_text_field( wp_unslash( $_POST['reports'] ) ), true ); |
||
| 241 | foreach ( $reports as $report => $reports_sections ) { |
||
| 242 | $reports[ $report ] = array_map( 'boolval', $reports_sections ); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | $options = array( |
||
| 247 | 'width' => ! empty( $_POST['width'] ) ? sanitize_text_field( wp_unslash( $_POST['width'] ) ) : $default['width'], |
||
| 248 | 'interval' => ! empty( $_POST['interval'] ) ? absint( wp_unslash( $_POST['interval'] ) ) : $default['interval'], |
||
| 249 | 'reports' => $reports, |
||
| 250 | 'notice30day' => $current_options['notice30day'], |
||
| 251 | ); |
||
| 252 | |||
| 253 | array_walk( $options, 'sanitize_text_field' ); |
||
| 254 | update_user_meta( get_current_user_id(), 'monsterinsights_user_preferences', $options ); |
||
| 255 | |||
| 256 | wp_send_json_success(); |
||
| 257 | |||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Load & store the dashboard widget settings. |
||
| 262 | * |
||
| 263 | * @return array |
||
| 264 | */ |
||
| 265 | public function get_options() { |
||
| 266 | if ( ! isset( $this->options ) ) { |
||
| 267 | $this->options = self::wp_parse_args_recursive( get_user_meta( get_current_user_id(), 'monsterinsights_user_preferences', true ), self::$default_options ); |
||
| 268 | } |
||
| 269 | |||
| 270 | return apply_filters( 'monsterinsights_dashboard_widget_options', $this->options ); |
||
| 271 | |||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Recursive wp_parse_args. |
||
| 276 | * |
||
| 277 | * @param string|array|object $a Value to merge with $b. |
||
| 278 | * @param array $b The array with the default values. |
||
| 279 | * |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | public static function wp_parse_args_recursive( $a, $b ) { |
||
| 283 | $a = (array) $a; |
||
| 284 | $b = (array) $b; |
||
| 285 | $result = $b; |
||
| 286 | foreach ( $a as $k => &$v ) { |
||
| 287 | if ( is_array( $v ) && isset( $result[ $k ] ) ) { |
||
| 288 | $result[ $k ] = self::wp_parse_args_recursive( $v, $result[ $k ] ); |
||
| 289 | } else { |
||
| 290 | $result[ $k ] = $v; |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | return $result; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Reminder notice markup. |
||
| 299 | */ |
||
| 300 | public function load_notice() { |
||
| 307 | <?php |
||
| 308 | } |
||
| 309 | |||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Mark notice as dismissed. |
||
| 314 | */ |
||
| 315 | public function mark_notice_closed() { |
||
| 323 | } |
||
| 324 | } |
||
| 325 |