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 |
||
| 6 | class Jetpack_Connection_Banner { |
||
| 7 | /** |
||
| 8 | * @var Jetpack_Connection_Banner |
||
| 9 | **/ |
||
| 10 | private static $instance = null; |
||
| 11 | |||
| 12 | static function init() { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Jetpack_Connection_Banner constructor. |
||
| 22 | * |
||
| 23 | * Since we call the Jetpack_Connection_Banner:init() method from the `Jetpack` class, and after |
||
| 24 | * the admin_init action fires, we know that the admin is initialized at this point. |
||
| 25 | */ |
||
| 26 | private function __construct() { |
||
| 27 | add_action( 'current_screen', array( $this, 'maybe_initialize_hooks' ) ); |
||
| 28 | |||
| 29 | if ( \Automattic\Jetpack\Constants::is_true( 'JETPACK_SHOULD_USE_CONNECTION_IFRAME' ) ) { |
||
| 30 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_connect_button_scripts' ) ); |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Given a string for the the banner was added, and an int that represents the slide to |
||
| 36 | * a URL for, this function returns a connection URL with a from parameter that will |
||
| 37 | * support split testing. |
||
| 38 | * |
||
| 39 | * @since 7.2 Event key format is now banner-connect-banner-72-dashboard or connect-banner-72-plugins. |
||
| 40 | * The param $slide_num was removed since we removed all slides but the first one. |
||
| 41 | * @since 4.4.0 |
||
| 42 | * |
||
| 43 | * @param string $jp_version_banner_added A short version of when the banner was added. Ex. 44 |
||
| 44 | * |
||
| 45 | * @return string |
||
| 46 | */ |
||
| 47 | function build_connect_url_for_slide( $jp_version_banner_added ) { |
||
| 48 | global $current_screen; |
||
| 49 | $url = Jetpack::init()->build_connect_url( |
||
| 50 | true, |
||
| 51 | false, |
||
| 52 | sprintf( 'connect-banner-%s-%s', $jp_version_banner_added, $current_screen->base ) |
||
| 53 | ); |
||
| 54 | // Add a tracks event corresponding to the A/B version displayed |
||
| 55 | $ab_test = Jetpack_Options::get_option( 'ab_connect_banner_green_bar' ); |
||
| 56 | if ( in_array( $ab_test, array( 'a', 'b' ), true ) ) { |
||
| 57 | $url = add_query_arg( 'ab_connect_banner_green_bar', $ab_test, $url ); |
||
| 58 | } |
||
| 59 | return add_query_arg( 'auth_approved', 'true', $url ); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Will initialize hooks to display the new (as of 4.4) connection banner if the current user can |
||
| 64 | * connect Jetpack, if Jetpack has not been deactivated, and if the current page is the plugins page. |
||
| 65 | * |
||
| 66 | * This method should not be called if the site is connected to WordPress.com or if the site is in development mode. |
||
| 67 | * |
||
| 68 | * @since 4.4.0 |
||
| 69 | * @since 4.5.0 Made the new (as of 4.4) connection banner display to everyone by default. |
||
| 70 | * @since 5.3.0 Running another split test between 4.4 banner and a new one in 5.3. |
||
| 71 | * @since 7.2 B test was removed. |
||
| 72 | * |
||
| 73 | * @param $current_screen |
||
| 74 | */ |
||
| 75 | function maybe_initialize_hooks( $current_screen ) { |
||
| 76 | |||
| 77 | // Kill if banner has been dismissed |
||
| 78 | if ( Jetpack_Options::get_option( 'dismissed_connection_banner' ) ) { |
||
| 79 | return; |
||
| 80 | } |
||
| 81 | |||
| 82 | // Don't show the connect notice anywhere but the plugins.php after activating |
||
| 83 | if ( 'plugins' !== $current_screen->base && 'dashboard' !== $current_screen->base ) { |
||
| 84 | return; |
||
| 85 | } |
||
| 86 | |||
| 87 | if ( ! current_user_can( 'jetpack_connect' ) ) { |
||
| 88 | return; |
||
| 89 | } |
||
| 90 | |||
| 91 | add_action( 'admin_notices', array( $this, 'render_banner' ) ); |
||
| 92 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_banner_scripts' ) ); |
||
| 93 | add_action( 'admin_print_styles', array( Jetpack::init(), 'admin_banner_styles' ) ); |
||
| 94 | |||
| 95 | if ( Jetpack::state( 'network_nag' ) ) { |
||
| 96 | add_action( 'network_admin_notices', array( $this, 'network_connect_notice' ) ); |
||
| 97 | } |
||
| 98 | |||
| 99 | // Only fires immediately after plugin activation |
||
| 100 | if ( get_transient( 'activated_jetpack' ) ) { |
||
| 101 | add_action( 'admin_notices', array( $this, 'render_connect_prompt_full_screen' ) ); |
||
| 102 | delete_transient( 'activated_jetpack' ); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Enqueues JavaScript for new connection banner. |
||
| 108 | * |
||
| 109 | * @since 4.4.0 |
||
| 110 | */ |
||
| 111 | View Code Duplication | public static function enqueue_banner_scripts() { |
|
| 132 | |||
| 133 | public static function enqueue_connect_button_scripts() { |
||
| 134 | wp_enqueue_script( |
||
| 135 | 'jetpack-connect-button', |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Performs an A/B test showing or hiding the green bar at the top of the connection dialog displayed in Dashboard or Plugins. |
||
| 148 | * We save which version we're showing so we always show the same to the same user. |
||
| 149 | * The "A" version displays the green bar at the top. |
||
| 150 | * The "B" version doesn't display it. |
||
| 151 | * |
||
| 152 | * @return void |
||
| 153 | */ |
||
| 154 | function get_ab_banner_top_bar() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Renders the new connection banner as of 4.4.0. |
||
| 175 | * |
||
| 176 | * @since 7.2 Copy and visual elements reduced to show the new focus of Jetpack on Security and Performance. |
||
| 177 | * @since 4.4.0 |
||
| 178 | */ |
||
| 179 | function render_banner() { ?> |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Renders the full-screen connection prompt. Only shown once and on plugin activation. |
||
| 255 | */ |
||
| 256 | public static function render_connect_prompt_full_screen() { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Renders the legacy network connection banner. |
||
| 347 | */ |
||
| 348 | function network_connect_notice() { |
||
| 367 | } |
||
| 368 |