Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 15 | class Jetpack_Wizard_Banner { |
||
| 16 | /** |
||
| 17 | * Jetpack_Wizard_Banner |
||
| 18 | * |
||
| 19 | * @var Jetpack_Wizard_Banner |
||
| 20 | **/ |
||
| 21 | private static $instance = null; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Factory method |
||
| 25 | */ |
||
| 26 | public static function init() { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Jetpack_Wizard_Banner constructor. |
||
| 36 | */ |
||
| 37 | private function __construct() { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Initialize hooks to display the banner |
||
| 43 | */ |
||
| 44 | View Code Duplication | public function maybe_initialize_hooks() { |
|
| 45 | if ( ! $this->can_be_displayed() ) { |
||
| 46 | return; |
||
| 47 | } |
||
| 48 | |||
| 49 | add_action( 'admin_print_styles', array( $this, 'admin_banner_styles' ) ); |
||
| 50 | add_action( 'admin_notices', array( $this, 'render_banner' ) ); |
||
| 51 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_banner_scripts' ) ); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Can we display the banner? |
||
| 56 | */ |
||
| 57 | View Code Duplication | private function can_be_displayed() { |
|
| 58 | if ( ! Jetpack_Wizard::can_be_displayed() ) { |
||
| 59 | return false; |
||
| 60 | } |
||
| 61 | |||
| 62 | // Only the dashboard and plugins pages should see the banner. |
||
| 63 | if ( ! in_array( get_current_screen()->id, array( 'dashboard', 'plugins' ), true ) ) { |
||
| 64 | return false; |
||
| 65 | } |
||
| 66 | |||
| 67 | if ( ! current_user_can( 'jetpack_manage_modules' ) ) { |
||
| 68 | return false; |
||
| 69 | } |
||
| 70 | |||
| 71 | // Kill if banner has been dismissed. |
||
| 72 | if ( Jetpack_Options::get_option( 'dismissed_wizard_banner' ) ) { |
||
| 73 | return false; |
||
| 74 | } |
||
| 75 | |||
| 76 | if ( ! in_array( Jetpack_Options::get_option( 'setup_wizard_status', 'not-started' ), array( 'not-started', 'intro-page' ), true ) ) { |
||
| 77 | return false; |
||
| 78 | } |
||
| 79 | |||
| 80 | return true; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Enqueue JavaScript files. |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function enqueue_banner_scripts() { |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Include the needed styles |
||
| 110 | */ |
||
| 111 | public function admin_banner_styles() { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * AJAX callback |
||
| 125 | */ |
||
| 126 | public static function ajax_callback() { |
||
| 127 | check_ajax_referer( 'jp-wizard-banner-nonce', 'nonce' ); |
||
| 128 | |||
| 129 | $tracking = new Tracking(); |
||
| 130 | |||
| 131 | if ( isset( $_REQUEST['personal'] ) ) { |
||
| 132 | $tracking->record_user_event( 'setup_wizard_banner_click', array( 'button' => 'personal' ) ); |
||
| 133 | } |
||
| 134 | |||
| 135 | if ( isset( $_REQUEST['business'] ) ) { |
||
| 136 | $tracking->record_user_event( 'setup_wizard_banner_click', array( 'button' => 'business' ) ); |
||
| 137 | } |
||
| 138 | |||
| 139 | if ( isset( $_REQUEST['skip'] ) ) { |
||
| 140 | $tracking->record_user_event( 'setup_wizard_banner_click', array( 'button' => 'skip' ) ); |
||
| 141 | } |
||
| 142 | |||
| 143 | View Code Duplication | if ( |
|
| 144 | current_user_can( 'jetpack_manage_modules' ) |
||
| 145 | && isset( $_REQUEST['dismissBanner'] ) |
||
| 146 | ) { |
||
| 147 | Jetpack_Options::update_option( 'dismissed_wizard_banner', 1 ); |
||
| 148 | $tracking->record_user_event( 'setup_wizard_banner_dismiss' ); |
||
| 149 | wp_send_json_success(); |
||
| 150 | } |
||
| 151 | |||
| 152 | wp_die(); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Renders the Wizard Banner |
||
| 157 | * |
||
| 158 | * Since this HTML replicates the contents of _inc/client/setup-wizard/intro-page/index.jsx, |
||
| 159 | * every time one is changed, the other should also be. |
||
| 160 | */ |
||
| 161 | public function render_banner() { |
||
| 239 | } |
||
| 240 |