Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * This class will handle everything involved with fixing an Identity Crisis. |
||
| 5 | * |
||
| 6 | * @since 4.4.0 |
||
| 7 | */ |
||
| 8 | class Jetpack_IDC { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * @var Jetpack_IDC |
||
| 12 | **/ |
||
| 13 | private static $instance = null; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * The wpcom value of the home URL |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | static $wpcom_home_url; |
||
|
0 ignored issues
–
show
|
|||
| 20 | |||
| 21 | /** |
||
| 22 | * The link to the support document used to explain Safe Mode to users |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | const SAFE_MODE_DOC_LINK = 'https://jetpack.com/support/safe-mode'; |
||
| 26 | |||
| 27 | static function init() { |
||
| 28 | if ( is_null( self::$instance ) ) { |
||
| 29 | self::$instance = new Jetpack_IDC; |
||
| 30 | } |
||
| 31 | |||
| 32 | return self::$instance; |
||
| 33 | } |
||
| 34 | |||
| 35 | private function __construct() { |
||
| 36 | if ( false === $urls_in_crisis = Jetpack::check_identity_crisis() ) { |
||
| 37 | return; |
||
| 38 | } |
||
| 39 | self::$wpcom_home_url = $urls_in_crisis['wpcom_home']; |
||
| 40 | add_action( 'init', array( $this, 'wordpress_init' ) ); |
||
| 41 | } |
||
| 42 | |||
| 43 | function wordpress_init() { |
||
| 44 | if ( ! $this->should_show_idc_notice() ) { |
||
| 45 | return; |
||
| 46 | } |
||
| 47 | add_action( 'admin_notices', array( $this, 'display_idc_notice' ) ); |
||
| 48 | add_action( 'admin_enqueue_scripts', array( $this,'enqueue_idc_notice_files' ) ); |
||
| 49 | } |
||
| 50 | |||
| 51 | function should_show_idc_notice() { |
||
| 52 | return ( |
||
| 53 | current_user_can( 'jetpack_disconnect' ) |
||
| 54 | && Jetpack::is_active() |
||
| 55 | && ! Jetpack::is_development_mode() |
||
| 56 | && ! Jetpack_Options::get_option( 'safe_mode_confirmed', false ) |
||
| 57 | ); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * First "step" of the IDC mitigation. Will provide some messaging and two options/buttons. |
||
| 62 | * "Confirm Staging" - Dismiss the notice and continue on with our lives in staging mode. |
||
| 63 | * "Fix Jetpack Connection" - Will disconnect the site and start the mitigation... |
||
| 64 | */ |
||
| 65 | function display_idc_notice() { ?> |
||
| 66 | <div class="jp-idc-notice notice notice-warning"> |
||
| 67 | <div class="jp-idc-notice__header"> |
||
| 68 | <div class="jp-idc-notice__header__emblem"> |
||
| 69 | <?php echo Jetpack::get_jp_emblem(); ?> |
||
| 70 | </div> |
||
| 71 | <p class="jp-idc-notice__header__text"> |
||
| 72 | <?php esc_html_e( 'Jetpack Safe Mode', 'jetpack' ); ?> |
||
| 73 | </p> |
||
| 74 | </div> |
||
| 75 | |||
| 76 | <?php $this->render_notice_first_step(); ?> |
||
| 77 | <?php $this->render_notice_second_step(); ?> |
||
| 78 | </div> |
||
| 79 | <?php } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Enqueue scripts for the notice |
||
| 83 | */ |
||
| 84 | function enqueue_idc_notice_files() { |
||
| 85 | |||
| 86 | wp_enqueue_script( |
||
| 87 | 'jetpack-idc-js', |
||
| 88 | plugins_url( '_inc/idc-notice.js', JETPACK__PLUGIN_FILE ), |
||
| 89 | array( 'jquery' ), |
||
| 90 | JETPACK__VERSION, |
||
| 91 | true |
||
| 92 | ); |
||
| 93 | |||
| 94 | wp_localize_script( |
||
| 95 | 'jetpack-idc-js', |
||
| 96 | 'idcL10n', |
||
| 97 | array( |
||
| 98 | 'apiRoot' => esc_url_raw( rest_url() ), |
||
| 99 | 'nonce' => wp_create_nonce( 'wp_rest' ), |
||
| 100 | ) |
||
| 101 | ); |
||
| 102 | |||
| 103 | wp_register_style( |
||
| 104 | 'jetpack-dops-style', |
||
| 105 | plugins_url( '_inc/build/admin.dops-style.css', JETPACK__PLUGIN_FILE ), |
||
| 106 | array(), |
||
| 107 | JETPACK__VERSION |
||
| 108 | ); |
||
| 109 | |||
| 110 | wp_enqueue_style( |
||
| 111 | 'jetpack-idc-css', |
||
| 112 | plugins_url( 'css/jetpack-idc.css', JETPACK__PLUGIN_FILE ), |
||
| 113 | array( 'jetpack-dops-style' ), |
||
| 114 | JETPACK__VERSION |
||
| 115 | ); |
||
| 116 | } |
||
| 117 | |||
| 118 | function render_notice_first_step() { ?> |
||
| 119 | <div class="jp-idc-notice__first-step"> |
||
| 120 | <div class="jp-idc-notice__content-header"> |
||
| 121 | <h3 class="jp-idc-notice__content-header__lead"> |
||
| 122 | <?php |
||
| 123 | echo wp_kses( |
||
| 124 | sprintf( |
||
| 125 | __( |
||
| 126 | 'Jetpack has been placed into <a href="%1$s">Safe mode</a> because we noticed this is an exact copy of <a href="%2$s">%2$s</a>.', |
||
| 127 | 'jetpack' |
||
| 128 | ), |
||
| 129 | esc_url( self::SAFE_MODE_DOC_LINK ), |
||
| 130 | esc_url( untrailingslashit( self::$wpcom_home_url ) ) |
||
| 131 | ), |
||
| 132 | array( 'a' => array( 'href' => array() ) ) |
||
| 133 | ); |
||
| 134 | ?> |
||
| 135 | </h3> |
||
| 136 | |||
| 137 | <p class="jp-idc-notice__content-header__explanation"> |
||
| 138 | <?php |
||
| 139 | echo wp_kses( |
||
| 140 | sprintf( |
||
| 141 | __( |
||
| 142 | 'Please confirm Safe Mode or fix the Jetpack connection. Select one of the options below or <a href="%1$s">learn |
||
| 143 | more about Safe Mode</a>.', |
||
| 144 | 'jetpack' |
||
| 145 | ), |
||
| 146 | esc_url( self::SAFE_MODE_DOC_LINK ) |
||
| 147 | ), |
||
| 148 | array( 'a' => array( 'href' => array() ) ) |
||
| 149 | ); |
||
| 150 | ?> |
||
| 151 | </p> |
||
| 152 | </div> |
||
| 153 | |||
| 154 | <div class="jp-idc-notice__actions"> |
||
| 155 | <div class="jp-idc-notice__action"> |
||
| 156 | <p class="jp-idc-notice__action__explanation"> |
||
| 157 | <?php |
||
| 158 | echo wp_kses( |
||
| 159 | sprintf( |
||
| 160 | __( |
||
| 161 | 'Is this website a temporary duplicate of <a href="%1$s">%1$s</a> for the purposes |
||
| 162 | of testing, staging or development? If so, we recommend keeping it in Safe Mode.', |
||
| 163 | 'jetpack' |
||
| 164 | ), |
||
| 165 | esc_url( untrailingslashit( self::$wpcom_home_url ) ) |
||
| 166 | ), |
||
| 167 | array( 'a' => array( 'href' => array() ) ) |
||
| 168 | ); |
||
| 169 | ?> |
||
| 170 | </p> |
||
| 171 | <button id="jp-idc-confirm-safe-mode-action" class="dops-button"> |
||
| 172 | <?php esc_html_e( 'Confirm Safe Mode' ); ?> |
||
| 173 | </button> |
||
| 174 | </div> |
||
| 175 | |||
| 176 | <div class="jp-idc-notice__action"> |
||
| 177 | <p class="jp-idc-notice__action__explanation"> |
||
| 178 | <?php |
||
| 179 | echo wp_kses( |
||
| 180 | sprintf( |
||
| 181 | __( |
||
| 182 | 'If this is a separate and new website, or the new home of <a href="%1$s">%1$s</a>, |
||
| 183 | we recommend turning Safe Mode off, and re-establishing your connection to WordPress.com.', |
||
| 184 | 'jetpack' |
||
| 185 | ), |
||
| 186 | esc_url( untrailingslashit( self::$wpcom_home_url ) ) |
||
| 187 | ), |
||
| 188 | array( 'a' => array( 'href' => array() ) ) |
||
| 189 | ); |
||
| 190 | ?> |
||
| 191 | </p> |
||
| 192 | <button id="jp-idc-fix-connection-action" class="dops-button"> |
||
| 193 | <?php esc_html_e( "Fix Jetpack's Connection" ); ?> |
||
| 194 | </button> |
||
| 195 | </div> |
||
| 196 | </div> |
||
| 197 | </div> |
||
| 198 | <?php } |
||
| 199 | |||
| 200 | function render_notice_second_step() { ?> |
||
| 201 | <div class="jp-idc-notice__second-step"> |
||
| 202 | <div class="jp-idc-notice__content-header"> |
||
| 203 | <h3 class="jp-idc-notice__content-header__lead"> |
||
| 204 | <?php |
||
| 205 | printf( |
||
| 206 | esc_html__( |
||
| 207 | 'Is %1$s the new home of %2$s?', |
||
| 208 | 'jetpack' |
||
| 209 | ), |
||
| 210 | untrailingslashit( Jetpack::normalize_url_protocol_agnostic( get_home_url() ) ), |
||
| 211 | untrailingslashit( Jetpack::normalize_url_protocol_agnostic( esc_url_raw( self::$wpcom_home_url ) ) ) |
||
| 212 | ) |
||
| 213 | ?> |
||
| 214 | </h3> |
||
| 215 | </div> |
||
| 216 | |||
| 217 | <div class="jp-idc-notice__actions"> |
||
| 218 | <div class="jp-idc-notice__action"> |
||
| 219 | <p class="jp-idc-notice__action__explanation"> |
||
| 220 | <?php |
||
| 221 | printf( |
||
| 222 | esc_html__( |
||
| 223 | 'Yes. %1$s is replacing %2$s. I would like to migrate my stats and subscribers from |
||
| 224 | %2$s to %1$s.', |
||
| 225 | 'jetpack' |
||
| 226 | ), |
||
| 227 | untrailingslashit( Jetpack::normalize_url_protocol_agnostic( get_home_url() ) ), |
||
| 228 | untrailingslashit( Jetpack::normalize_url_protocol_agnostic( esc_url_raw( self::$wpcom_home_url ) ) ) |
||
| 229 | ) |
||
| 230 | ?> |
||
| 231 | </p> |
||
| 232 | <button id="jp-idc-migrate-action" class="dops-button"> |
||
| 233 | <?php esc_html_e( 'Migrate stats & and Subscribers' ); ?> |
||
| 234 | </button> |
||
| 235 | </div> |
||
| 236 | |||
| 237 | <div class="jp-idc-notice__action"> |
||
| 238 | <p class="jp-idc-notice__action__explanation"> |
||
| 239 | <?php |
||
| 240 | printf( |
||
| 241 | esc_html__( |
||
| 242 | 'No. %1$s is a new and different website that\'s separate from %2$s. It requires |
||
| 243 | a new connection to WordPress.com for new stats and subscribers.', |
||
| 244 | 'jetpack' |
||
| 245 | ), |
||
| 246 | untrailingslashit( Jetpack::normalize_url_protocol_agnostic( get_home_url() ) ), |
||
| 247 | untrailingslashit( Jetpack::normalize_url_protocol_agnostic( esc_url_raw( self::$wpcom_home_url ) ) ) |
||
| 248 | ) |
||
| 249 | ?> |
||
| 250 | </p> |
||
| 251 | <button id="jp-idc-reconnect-site-action" class="dops-button"> |
||
| 252 | <?php esc_html_e( 'Start fresh & create new connection' ); ?> |
||
| 253 | </button> |
||
| 254 | </div> |
||
| 255 | </div> |
||
| 256 | </div> |
||
| 257 | <?php } |
||
| 258 | } |
||
| 259 | |||
| 260 | Jetpack_IDC::init(); |
||
| 261 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.