Complex classes like PostPromoterPro 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 PostPromoterPro, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class PostPromoterPro { |
||
| 25 | private static $ppp_instance; |
||
| 26 | |||
| 27 | private function __construct() { |
||
| 28 | add_action( 'init', array( $this, 'ppp_loaddomain' ), 1 ); |
||
| 29 | |||
| 30 | if ( ! is_callable( 'curl_init' ) ) { |
||
| 31 | add_action( 'admin_notices', array( $this, 'no_curl' ) ); |
||
| 32 | } else { |
||
| 33 | global $ppp_options, $ppp_social_settings, $ppp_share_settings; |
||
| 34 | |||
| 35 | include PPP_PATH . '/includes/general-functions.php'; |
||
| 36 | include PPP_PATH . '/includes/share-functions.php'; |
||
| 37 | include PPP_PATH . '/includes/cron-functions.php'; |
||
| 38 | include PPP_PATH . '/includes/filters.php'; |
||
| 39 | include PPP_PATH . '/includes/libs/social-loader.php'; |
||
| 40 | |||
| 41 | if( ! class_exists( 'WP_Logging' ) ) { |
||
| 42 | include PPP_PATH . '/includes/libs/class-wp-logging.php'; |
||
| 43 | } |
||
| 44 | |||
| 45 | if ( is_admin() ) { |
||
| 46 | include PPP_PATH . '/includes/admin/upgrades.php'; |
||
| 47 | include PPP_PATH . '/includes/admin/do-upgrades.php'; |
||
| 48 | include PPP_PATH . '/includes/admin/actions.php'; |
||
| 49 | include PPP_PATH . '/includes/admin/admin-pages.php'; |
||
| 50 | include PPP_PATH . '/includes/admin/admin-ajax.php'; |
||
| 51 | include PPP_PATH . '/includes/admin/meta-boxes.php'; |
||
| 52 | include PPP_PATH . '/includes/admin/welcome.php'; |
||
| 53 | include PPP_PATH . '/includes/admin/dashboard.php'; |
||
| 54 | } |
||
| 55 | |||
| 56 | $ppp_options = get_option( 'ppp_options' ); |
||
| 57 | $ppp_social_settings = get_option( 'ppp_social_settings' ); |
||
| 58 | $ppp_share_settings = get_option( 'ppp_share_settings' ); |
||
| 59 | |||
| 60 | // Do some leg work on the social settings for Issue #257 |
||
| 61 | if ( is_array( $ppp_share_settings ) && ! array_key_exists( 'share_on_publish', $ppp_share_settings ) ) { |
||
| 62 | $tw_share_on_publish = ! empty( $ppp_share_settings['twitter']['share_on_publish'] ) ? true : false; |
||
| 63 | $fb_share_on_publish = ! empty( $ppp_share_settings['facebook']['share_on_publish'] ) ? true : false; |
||
| 64 | $li_share_on_publish = ! empty( $ppp_share_settings['linkedin']['share_on_publish'] ) ? true : false; |
||
| 65 | |||
| 66 | unset( |
||
| 67 | $ppp_share_settings['twitter']['share_on_publish'], |
||
| 68 | $ppp_share_settings['facebook']['share_on_publish'], |
||
| 69 | $ppp_share_settings['linkedin']['share_on_publish'] |
||
| 70 | ); |
||
| 71 | |||
| 72 | $post_types = ppp_supported_post_types(); |
||
| 73 | foreach ( $post_types as $key => $post_type ) { |
||
| 74 | $ppp_share_settings['share_on_publish'][ $key ]['twitter'] = $tw_share_on_publish; |
||
| 75 | $ppp_share_settings['share_on_publish'][ $key ]['facebook'] = $fb_share_on_publish; |
||
| 76 | $ppp_share_settings['share_on_publish'][ $key ]['linkedin'] = $li_share_on_publish; |
||
| 77 | } |
||
| 78 | |||
| 79 | update_option( 'ppp_share_settings', $ppp_share_settings ); |
||
| 80 | } |
||
| 81 | |||
| 82 | $this->hooks(); |
||
| 83 | } |
||
| 84 | |||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get the singleton instance of our plugin |
||
| 89 | * @return class The Instance |
||
| 90 | * @access public |
||
| 91 | */ |
||
| 92 | 1 | public static function getInstance() { |
|
| 93 | 1 | if ( !self::$ppp_instance ) { |
|
| 94 | self::$ppp_instance = new PostPromoterPro(); |
||
| 95 | } |
||
| 96 | |||
| 97 | 1 | return self::$ppp_instance; |
|
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Nag if cURL is disabled |
||
| 102 | * @return void |
||
| 103 | */ |
||
| 104 | public function no_curl() { |
||
| 105 | ?> |
||
| 106 | <div class="no-curl"> |
||
| 107 | <p><?php _e( 'Post Promoter Pro requires cURL to be enabled. Please enable it to continue using the plugin.', 'ppp-txt' ); ?></p> |
||
| 108 | </div> |
||
| 109 | <?php |
||
| 110 | } |
||
| 111 | |||
| 112 | private function hooks() { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Queue up the JavaScript file for the admin page, only on our admin page |
||
| 145 | * @param string $hook The current page in the admin |
||
| 146 | * @return void |
||
| 147 | * @access public |
||
| 148 | */ |
||
| 149 | public function load_custom_scripts( $hook ) { |
||
| 150 | |||
| 151 | $allowed_pages = array( |
||
| 152 | 'toplevel_page_ppp-options', |
||
| 153 | 'post-promoter_page_ppp-social-settings', |
||
| 154 | 'post-new.php', |
||
| 155 | 'post.php', |
||
| 156 | 'post-promoter_page_ppp-schedule-info', |
||
| 157 | 'profile.php', |
||
| 158 | 'user-edit.php', |
||
| 159 | ); |
||
| 160 | |||
| 161 | $allowed_pages = apply_filters( 'ppp_admin_scripts_pages', $allowed_pages, $hook ); |
||
| 162 | |||
| 163 | if ( ! in_array( $hook, $allowed_pages ) ) { |
||
| 164 | return; |
||
| 165 | } |
||
| 166 | |||
| 167 | wp_enqueue_script( 'jquery-ui-core' ); |
||
| 168 | wp_enqueue_script( 'jquery-ui-datepicker' ); |
||
| 169 | |||
| 170 | $jquery_ui_timepicker_path = PPP_URL . 'includes/scripts/libs/jquery-ui-timepicker-addon.js'; |
||
| 171 | wp_enqueue_script( 'ppp_timepicker_js', $jquery_ui_timepicker_path , array( 'jquery', 'jquery-ui-core' ), PPP_VERSION, true ); |
||
| 172 | wp_enqueue_script( 'ppp_core_custom_js', PPP_URL.'includes/scripts/js/ppp_custom.js', 'jquery', PPP_VERSION, true ); |
||
| 173 | |||
| 174 | } |
||
| 175 | |||
| 176 | public function load_styles() { |
||
| 177 | |||
| 178 | global $wp_styles; |
||
| 179 | |||
| 180 | // List of people who make it impossible to override their jQuery UI as it's in their core CSS...so only |
||
| 181 | // load ours if they don't exist |
||
| 182 | if ( ! wp_style_is( 'ot-admin-css' ) && ! wp_style_is( 'jquery-ui-css' ) ) { |
||
| 183 | wp_enqueue_style( 'jquery-ui-css', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/flick/jquery-ui.css' ); |
||
| 184 | } |
||
| 185 | |||
| 186 | wp_register_style( 'ppp_admin_css', PPP_URL . 'includes/scripts/css/admin-style.css', false, PPP_VERSION ); |
||
| 187 | wp_enqueue_style( 'ppp_admin_css' ); |
||
| 188 | |||
| 189 | $sources = array_map( 'basename', (array) wp_list_pluck( $wp_styles->registered, 'src' ) ); |
||
| 190 | if ( ! in_array( 'font-awesome.css', $sources ) || in_array( 'font-awesome.min.css', $sources ) ) { |
||
| 191 | wp_register_style( 'font-awesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css', false, null ); |
||
| 192 | wp_enqueue_style( 'font-awesome' ); |
||
| 193 | } |
||
| 194 | |||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Adds the Settings and Post Promoter Pro Link to the Settings page list |
||
| 199 | * @param array $links The current list of links |
||
| 200 | * @param string $file The plugin file |
||
| 201 | * @return array The new list of links, with our additional ones added |
||
| 202 | * @access public |
||
| 203 | */ |
||
| 204 | public function plugin_settings_links( $links, $file ) { |
||
| 205 | if ( $file != PPP_FILE ) { |
||
| 206 | return $links; |
||
| 207 | } |
||
| 208 | |||
| 209 | $settings_link = sprintf( '<a href="%s">%s</a>', admin_url( 'admin.php?page=ppp-options' ), __( 'Settings', 'ppp-txt' ) ); |
||
| 210 | |||
| 211 | array_unshift( $links, $settings_link ); |
||
| 212 | |||
| 213 | return $links; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Returns the capability (or role) required to manage the plugin. |
||
| 218 | 1 | * |
|
| 219 | 1 | * @return string A WordPress capability or role name. |
|
| 220 | */ |
||
| 221 | public static function get_manage_capability() { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Add the Pushover Notifications item to the Settings menu |
||
| 227 | * @return void |
||
| 228 | * @access public |
||
| 229 | */ |
||
| 230 | public function ppp_setup_admin_menu() { |
||
| 231 | $capability = self::get_manage_capability(); |
||
| 232 | |||
| 233 | add_menu_page( |
||
| 234 | __( 'Post Promoter', 'ppp-txt' ), |
||
| 235 | __( 'Post Promoter', 'ppp-txt' ), |
||
| 236 | $capability, |
||
| 237 | 'ppp-options', |
||
| 238 | 'ppp_admin_page' |
||
| 239 | ); |
||
| 240 | |||
| 241 | add_submenu_page( |
||
| 242 | 'ppp-options', |
||
| 243 | __( 'Social Settings', 'ppp-txt' ), |
||
| 244 | __( 'Social Settings', 'ppp-txt' ), |
||
| 245 | $capability, |
||
| 246 | 'ppp-social-settings', |
||
| 247 | 'ppp_display_social' |
||
| 248 | ); |
||
| 249 | |||
| 250 | add_submenu_page( |
||
| 251 | 'ppp-options', |
||
| 252 | __( 'Schedule', 'ppp-txt' ), |
||
| 253 | __( 'Schedule', 'ppp-txt' ), |
||
| 254 | $capability, |
||
| 255 | 'ppp-schedule-info', |
||
| 256 | 'ppp_display_schedule' |
||
| 257 | ); |
||
| 258 | |||
| 259 | add_submenu_page( |
||
| 260 | 'ppp-options', |
||
| 261 | __( 'System Info', 'ppp-txt' ), |
||
| 262 | __( 'System Info', 'ppp-txt' ), |
||
| 263 | $capability, |
||
| 264 | 'ppp-system-info', |
||
| 265 | 'ppp_display_sysinfo' |
||
| 266 | ); |
||
| 267 | |||
| 268 | add_submenu_page( |
||
| 269 | null, |
||
| 270 | __( 'PPP Upgrades', 'ppp-txt' ), |
||
| 271 | __( 'PPP Upgrades', 'ppp-txt' ), |
||
| 272 | $capability, |
||
| 273 | 'ppp-upgrades', |
||
| 274 | 'ppp_upgrades_screen' |
||
| 275 | ); |
||
| 276 | |||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Register/Whitelist our settings on the settings page, allow extensions and other plugins to hook into this |
||
| 281 | * @return void |
||
| 282 | * @access public |
||
| 283 | */ |
||
| 284 | public function ppp_register_settings() { |
||
| 285 | register_setting( 'ppp-options', 'ppp_options' ); |
||
| 286 | register_setting( 'ppp-options', '_ppp_license_key', array( $this, 'ppp_sanitize_license' ) ); |
||
| 287 | |||
| 288 | register_setting( 'ppp-social-settings', 'ppp_social_settings' ); |
||
| 289 | register_setting( 'ppp-share-settings', 'ppp_share_settings' ); |
||
| 290 | do_action( 'ppp_register_additional_settings' ); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Load the Text Domain for i18n |
||
| 295 | * @return void |
||
| 296 | * @access public |
||
| 297 | */ |
||
| 298 | public function ppp_loaddomain() { |
||
| 299 | load_plugin_textdomain( 'ppp-txt', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Sets up the EDD SL Plugin updated class |
||
| 304 | * @return void |
||
| 305 | */ |
||
| 306 | public function plugin_updater() { |
||
| 307 | global $ppp_options; |
||
| 308 | if ( defined( 'NO_AUTO_UPDATE' ) && true === NO_AUTO_UPDATE ) { |
||
| 309 | return; |
||
| 310 | } |
||
| 311 | |||
| 312 | $license_key = trim( get_option( '_ppp_license_key' ) ); |
||
| 313 | |||
| 314 | if ( empty( $license_key ) ) { |
||
| 315 | add_action( 'admin_notices', array( $this, 'no_license_nag' ) ); |
||
| 316 | return; |
||
| 317 | } |
||
| 318 | |||
| 319 | // setup the updater |
||
| 320 | $edd_updater = new EDD_SL_Plugin_Updater( PPP_STORE_URL, __FILE__, array( |
||
|
|
|||
| 321 | 'version' => PPP_VERSION, // current version number |
||
| 322 | 'license' => $license_key, // license key (used get_option above to retrieve from DB) |
||
| 323 | 'item_name' => PPP_PLUGIN_NAME, // name of this plugin |
||
| 324 | 'author' => 'Post Promoter Pro', // author of this plugin |
||
| 325 | 'beta' => ! empty( $ppp_options['enable_betas'] ) ? true : false, // If we should install beta versions |
||
| 326 | ) |
||
| 327 | ); |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * If no license key is saved, show a notice |
||
| 332 | * @return void |
||
| 333 | */ |
||
| 334 | public function no_license_nag() { |
||
| 335 | ?> |
||
| 336 | <div class="updated"> |
||
| 337 | <p> |
||
| 338 | <?php printf( |
||
| 339 | __( 'Post Promoter Pro requires your license key to work, please <a href="%s">enter it now</a>.', 'ppp-txt' ), |
||
| 340 | admin_url( 'admin.php?page=ppp-options' ) |
||
| 341 | ); |
||
| 342 | ?> |
||
| 343 | </p> |
||
| 344 | </div> |
||
| 345 | <?php |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * If site is detected as local, show notice |
||
| 350 | * @return void |
||
| 351 | */ |
||
| 352 | public function local_site_nag() { |
||
| 384 | |||
| 385 | public function general_notices() { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Deactivates the license key |
||
| 402 | * @return void |
||
| 403 | */ |
||
| 404 | public function deactivate_license() { |
||
| 405 | // listen for our activate button to be clicked |
||
| 406 | if( isset( $_POST['ppp_license_deactivate'] ) ) { |
||
| 407 | |||
| 408 | // run a quick security check |
||
| 409 | if( ! check_admin_referer( 'ppp_deactivate_nonce', 'ppp_deactivate_nonce' ) ) { |
||
| 410 | return; |
||
| 411 | } |
||
| 412 | // get out if we didn't click the Activate button |
||
| 413 | |||
| 414 | // retrieve the license from the database |
||
| 415 | $license = trim( get_option( '_ppp_license_key' ) ); |
||
| 416 | |||
| 417 | |||
| 418 | // data to send in our API request |
||
| 419 | $api_params = array( |
||
| 420 | 'edd_action'=> 'deactivate_license', |
||
| 421 | 'license' => $license, |
||
| 422 | 'item_name' => urlencode( PPP_PLUGIN_NAME ) // the name of our product in EDD |
||
| 423 | ); |
||
| 424 | |||
| 425 | // Call the custom API. |
||
| 426 | $response = wp_remote_get( add_query_arg( $api_params, PPP_STORE_URL ), array( 'timeout' => 15, 'sslverify' => false ) ); |
||
| 427 | |||
| 428 | // make sure the response came back okay |
||
| 429 | if ( is_wp_error( $response ) ) { |
||
| 430 | return false; |
||
| 431 | } |
||
| 432 | |||
| 433 | // decode the license data |
||
| 434 | $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
||
| 435 | |||
| 436 | // $license_data->license will be either "deactivated" or "failed" |
||
| 437 | if( $license_data->license == 'deactivated' ) { |
||
| 438 | delete_option( '_ppp_license_key_status' ); |
||
| 439 | } |
||
| 440 | |||
| 441 | } |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Activates the license key provided |
||
| 446 | * @return void |
||
| 447 | */ |
||
| 448 | public function activate_license() { |
||
| 449 | // listen for our activate button to be clicked |
||
| 450 | if( isset( $_POST['ppp_license_activate'] ) ) { |
||
| 451 | |||
| 452 | // run a quick security check |
||
| 453 | if( ! check_admin_referer( 'ppp_activate_nonce', 'ppp_activate_nonce' ) ) { |
||
| 454 | return; |
||
| 455 | } |
||
| 456 | // get out if we didn't click the Activate button |
||
| 457 | |||
| 458 | // retrieve the license from the database |
||
| 459 | $license = trim( get_option( '_ppp_license_key' ) ); |
||
| 460 | |||
| 461 | |||
| 462 | // data to send in our API request |
||
| 463 | $api_params = array( |
||
| 464 | 'edd_action' => 'activate_license', |
||
| 465 | 'license' => $license, |
||
| 466 | 'item_name' => urlencode( PPP_PLUGIN_NAME ), |
||
| 467 | ); |
||
| 468 | |||
| 469 | // Call the custom API. |
||
| 470 | $response = wp_remote_get( add_query_arg( $api_params, PPP_STORE_URL ), array( 'timeout' => 15, 'sslverify' => false ) ); |
||
| 471 | |||
| 472 | // make sure the response came back okay |
||
| 473 | if ( is_wp_error( $response ) ) { |
||
| 474 | return false; |
||
| 475 | } |
||
| 476 | |||
| 477 | // decode the license data |
||
| 478 | $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
||
| 479 | |||
| 480 | // $license_data->license will be either "active" or "inactive" |
||
| 481 | |||
| 482 | update_option( '_ppp_license_key_status', $license_data->license ); |
||
| 483 | |||
| 484 | } |
||
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Sanatize the liscense key being provided |
||
| 489 | * @param string $new The License key provided |
||
| 490 | * @return string Sanitized license key |
||
| 491 | */ |
||
| 492 | 1 | public function ppp_sanitize_license( $new ) { |
|
| 493 | $old = get_option( '_ppp_license_key' ); |
||
| 494 | if( $old && $old != $new ) { |
||
| 495 | delete_option( '_ppp_license_key_status' ); // new license has been entered, so must reactivate |
||
| 496 | } |
||
| 497 | return $new; |
||
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Hook to listen for our actions |
||
| 502 | * |
||
| 503 | * @return void |
||
| 504 | */ |
||
| 505 | public function get_actions() { |
||
| 506 | if ( isset( $_GET['ppp_action'] ) ) { |
||
| 507 | do_action( 'ppp_' . $_GET['ppp_action'], $_GET ); |
||
| 508 | } |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Register our log type for when items are shared |
||
| 513 | * |
||
| 514 | * @since 2.3 |
||
| 515 | * @param array $log_types Array of log types |
||
| 516 | * @return array |
||
| 517 | */ |
||
| 518 | public function register_log_type( $log_types ) { |
||
| 522 | |||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Load and access the one true instance of Post Promoter Pro |
||
| 527 | * |
||
| 528 | * @return object The Post_Promoter_Pro instance |
||
| 529 | */ |
||
| 530 | function post_promoter_pro() { |
||
| 531 | return PostPromoterPro::getInstance(); |
||
| 532 | } |
||
| 533 | add_action( 'plugins_loaded', 'post_promoter_pro' ); |
||
| 534 | |||
| 535 | /** |
||
| 536 | * On activation, setup the default options |
||
| 537 | * @return void |
||
| 559 | register_activation_hook( PPP_FILE, 'post_promoter_pro_activation_setup' ); |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.