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:
Complex classes like Jetpack_SSO 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 Jetpack_SSO, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Jetpack_SSO { |
||
| 20 | static $instance = null; |
||
|
|
|||
| 21 | |||
| 22 | private function __construct() { |
||
| 23 | |||
| 24 | self::$instance = $this; |
||
| 25 | |||
| 26 | add_action( 'admin_init', array( $this, 'maybe_authorize_user_after_sso' ), 1 ); |
||
| 27 | add_action( 'admin_init', array( $this, 'register_settings' ) ); |
||
| 28 | add_action( 'login_init', array( $this, 'login_init' ) ); |
||
| 29 | add_action( 'delete_user', array( $this, 'delete_connection_for_user' ) ); |
||
| 30 | add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
||
| 31 | add_action( 'init', array( $this, 'maybe_logout_user' ), 5 ); |
||
| 32 | add_action( 'jetpack_modules_loaded', array( $this, 'module_configure_button' ) ); |
||
| 33 | add_action( 'login_form_logout', array( $this, 'store_wpcom_profile_cookies_on_logout' ) ); |
||
| 34 | add_action( 'wp_login', array( 'Jetpack_SSO', 'clear_wpcom_profile_cookies' ) ); |
||
| 35 | add_action( 'jetpack_unlinked_user', array( $this, 'delete_connection_for_user') ); |
||
| 36 | |||
| 37 | // Adding this action so that on login_init, the action won't be sanitized out of the $action global. |
||
| 38 | add_action( 'login_form_jetpack-sso', '__return_true' ); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Returns the single instance of the Jetpack_SSO object |
||
| 43 | * |
||
| 44 | * @since 2.8 |
||
| 45 | * @return Jetpack_SSO |
||
| 46 | **/ |
||
| 47 | public static function get_instance() { |
||
| 48 | if ( ! is_null( self::$instance ) ) { |
||
| 49 | return self::$instance; |
||
| 50 | } |
||
| 51 | |||
| 52 | return self::$instance = new Jetpack_SSO; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Add configure button and functionality to the module card on the Jetpack screen |
||
| 57 | **/ |
||
| 58 | public static function module_configure_button() { |
||
| 59 | Jetpack::enable_module_configurable( __FILE__ ); |
||
| 60 | Jetpack::module_configuration_load( __FILE__, array( __CLASS__, 'module_configuration_load' ) ); |
||
| 61 | Jetpack::module_configuration_head( __FILE__, array( __CLASS__, 'module_configuration_head' ) ); |
||
| 62 | Jetpack::module_configuration_screen( __FILE__, array( __CLASS__, 'module_configuration_screen' ) ); |
||
| 63 | } |
||
| 64 | |||
| 65 | public static function module_configuration_load() {} |
||
| 66 | |||
| 67 | public static function module_configuration_head() {} |
||
| 68 | |||
| 69 | public static function module_configuration_screen() { |
||
| 70 | ?> |
||
| 71 | <form method="post" action="options.php"> |
||
| 72 | <?php settings_fields( 'jetpack-sso' ); ?> |
||
| 73 | <?php do_settings_sections( 'jetpack-sso' ); ?> |
||
| 74 | <?php submit_button(); ?> |
||
| 75 | </form> |
||
| 76 | <?php |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * If jetpack_force_logout == 1 in current user meta the user will be forced |
||
| 81 | * to logout and reauthenticate with the site. |
||
| 82 | **/ |
||
| 83 | public function maybe_logout_user() { |
||
| 84 | global $current_user; |
||
| 85 | |||
| 86 | if ( 1 == $current_user->jetpack_force_logout ) { |
||
| 87 | delete_user_meta( $current_user->ID, 'jetpack_force_logout' ); |
||
| 88 | self::delete_connection_for_user( $current_user->ID ); |
||
| 89 | wp_logout(); |
||
| 90 | wp_safe_redirect( wp_login_url() ); |
||
| 91 | exit; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Adds additional methods the WordPress xmlrpc API for handling SSO specific features |
||
| 97 | * |
||
| 98 | * @param array $methods |
||
| 99 | * @return array |
||
| 100 | **/ |
||
| 101 | public function xmlrpc_methods( $methods ) { |
||
| 102 | $methods['jetpack.userDisconnect'] = array( $this, 'xmlrpc_user_disconnect' ); |
||
| 103 | return $methods; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Marks a user's profile for disconnect from WordPress.com and forces a logout |
||
| 108 | * the next time the user visits the site. |
||
| 109 | **/ |
||
| 110 | public function xmlrpc_user_disconnect( $user_id ) { |
||
| 111 | $user_query = new WP_User_Query( |
||
| 112 | array( |
||
| 113 | 'meta_key' => 'wpcom_user_id', |
||
| 114 | 'meta_value' => $user_id, |
||
| 115 | ) |
||
| 116 | ); |
||
| 117 | $user = $user_query->get_results(); |
||
| 118 | $user = $user[0]; |
||
| 119 | |||
| 120 | if ( $user instanceof WP_User ) { |
||
| 121 | $user = wp_set_current_user( $user->ID ); |
||
| 122 | update_user_meta( $user->ID, 'jetpack_force_logout', '1' ); |
||
| 123 | self::delete_connection_for_user( $user->ID ); |
||
| 124 | return true; |
||
| 125 | } |
||
| 126 | return false; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Enqueues scripts and styles necessary for SSO login. |
||
| 131 | */ |
||
| 132 | public function login_enqueue_scripts() { |
||
| 133 | global $action; |
||
| 134 | |||
| 135 | if ( ! Jetpack_SSO_Helpers::display_sso_form_for_action( $action ) ) { |
||
| 136 | return; |
||
| 137 | } |
||
| 138 | |||
| 139 | if ( is_rtl() ) { |
||
| 140 | wp_enqueue_style( 'jetpack-sso-login', plugins_url( 'modules/sso/jetpack-sso-login-rtl.css', JETPACK__PLUGIN_FILE ), array( 'login', 'genericons' ), JETPACK__VERSION ); |
||
| 141 | } else { |
||
| 142 | wp_enqueue_style( 'jetpack-sso-login', plugins_url( 'modules/sso/jetpack-sso-login.css', JETPACK__PLUGIN_FILE ), array( 'login', 'genericons' ), JETPACK__VERSION ); |
||
| 143 | } |
||
| 144 | |||
| 145 | wp_enqueue_script( 'jetpack-sso-login', plugins_url( 'modules/sso/jetpack-sso-login.js', JETPACK__PLUGIN_FILE ), array( 'jquery' ), JETPACK__VERSION ); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Adds Jetpack SSO classes to login body |
||
| 150 | * |
||
| 151 | * @param array $classes Array of classes to add to body tag |
||
| 152 | * @return array Array of classes to add to body tag |
||
| 153 | */ |
||
| 154 | public function login_body_class( $classes ) { |
||
| 155 | global $action; |
||
| 156 | |||
| 157 | if ( ! Jetpack_SSO_Helpers::display_sso_form_for_action( $action ) ) { |
||
| 158 | return $classes; |
||
| 159 | } |
||
| 160 | |||
| 161 | // Always add the jetpack-sso class so that we can add SSO specific styling even when the SSO form isn't being displayed. |
||
| 162 | $classes[] = 'jetpack-sso'; |
||
| 163 | |||
| 164 | if ( ! Jetpack::is_staging_site() ) { |
||
| 165 | /** |
||
| 166 | * Should we show the SSO login form? |
||
| 167 | * |
||
| 168 | * $_GET['jetpack-sso-default-form'] is used to provide a fallback in case JavaScript is not enabled. |
||
| 169 | * |
||
| 170 | * The default_to_sso_login() method allows us to dynamically decide whether we show the SSO login form or not. |
||
| 171 | * The SSO module uses the method to display the default login form if we can not find a user to log in via SSO. |
||
| 172 | * But, the method could be filtered by a site admin to always show the default login form if that is preferred. |
||
| 173 | */ |
||
| 174 | if ( empty( $_GET['jetpack-sso-show-default-form'] ) && Jetpack_SSO_Helpers::show_sso_login() ) { |
||
| 175 | $classes[] = 'jetpack-sso-form-display'; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | return $classes; |
||
| 180 | } |
||
| 181 | |||
| 182 | public function print_inline_admin_css() { |
||
| 183 | ?> |
||
| 184 | <style> |
||
| 185 | .jetpack-sso .message { |
||
| 186 | margin-top: 20px; |
||
| 187 | } |
||
| 188 | |||
| 189 | .jetpack-sso #login .message:first-child, |
||
| 190 | .jetpack-sso #login h1 + .message { |
||
| 191 | margin-top: 0; |
||
| 192 | } |
||
| 193 | </style> |
||
| 194 | <?php |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Adds settings fields to Settings > General > Single Sign On that allows users to |
||
| 199 | * turn off the login form on wp-login.php |
||
| 200 | * |
||
| 201 | * @since 2.7 |
||
| 202 | **/ |
||
| 203 | public function register_settings() { |
||
| 204 | |||
| 205 | add_settings_section( |
||
| 206 | 'jetpack_sso_settings', |
||
| 207 | __( 'Single Sign On' , 'jetpack' ), |
||
| 208 | '__return_false', |
||
| 209 | 'jetpack-sso' |
||
| 210 | ); |
||
| 211 | |||
| 212 | /* |
||
| 213 | * Settings > General > Single Sign On |
||
| 214 | * Require two step authentication |
||
| 215 | */ |
||
| 216 | register_setting( |
||
| 217 | 'jetpack-sso', |
||
| 218 | 'jetpack_sso_require_two_step', |
||
| 219 | array( $this, 'validate_jetpack_sso_require_two_step' ) |
||
| 220 | ); |
||
| 221 | |||
| 222 | add_settings_field( |
||
| 223 | 'jetpack_sso_require_two_step', |
||
| 224 | '', // __( 'Require Two-Step Authentication' , 'jetpack' ), |
||
| 225 | array( $this, 'render_require_two_step' ), |
||
| 226 | 'jetpack-sso', |
||
| 227 | 'jetpack_sso_settings' |
||
| 228 | ); |
||
| 229 | |||
| 230 | /* |
||
| 231 | * Settings > General > Single Sign On |
||
| 232 | */ |
||
| 233 | register_setting( |
||
| 234 | 'jetpack-sso', |
||
| 235 | 'jetpack_sso_match_by_email', |
||
| 236 | array( $this, 'validate_jetpack_sso_match_by_email' ) |
||
| 237 | ); |
||
| 238 | |||
| 239 | add_settings_field( |
||
| 240 | 'jetpack_sso_match_by_email', |
||
| 241 | '', // __( 'Match by Email' , 'jetpack' ), |
||
| 242 | array( $this, 'render_match_by_email' ), |
||
| 243 | 'jetpack-sso', |
||
| 244 | 'jetpack_sso_settings' |
||
| 245 | ); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Builds the display for the checkbox allowing user to require two step |
||
| 250 | * auth be enabled on WordPress.com accounts before login. Displays in Settings > General |
||
| 251 | * |
||
| 252 | * @since 2.7 |
||
| 253 | **/ |
||
| 254 | public function render_require_two_step() { |
||
| 255 | ?> |
||
| 256 | <label> |
||
| 257 | <input |
||
| 258 | type="checkbox" |
||
| 259 | name="jetpack_sso_require_two_step" |
||
| 260 | <?php checked( Jetpack_SSO_Helpers::is_two_step_required() ); ?> |
||
| 261 | <?php disabled( Jetpack_SSO_Helpers::is_require_two_step_checkbox_disabled() ); ?> |
||
| 262 | > |
||
| 263 | <?php esc_html_e( 'Require Two-Step Authentication' , 'jetpack' ); ?> |
||
| 264 | </label> |
||
| 265 | <?php |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Validate the require two step checkbox in Settings > General |
||
| 270 | * |
||
| 271 | * @since 2.7 |
||
| 272 | * @return boolean |
||
| 273 | **/ |
||
| 274 | public function validate_jetpack_sso_require_two_step( $input ) { |
||
| 275 | return ( ! empty( $input ) ) ? 1 : 0; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Builds the display for the checkbox allowing the user to allow matching logins by email |
||
| 280 | * Displays in Settings > General |
||
| 281 | * |
||
| 282 | * @since 2.9 |
||
| 283 | **/ |
||
| 284 | public function render_match_by_email() { |
||
| 285 | ?> |
||
| 286 | <label> |
||
| 287 | <input |
||
| 288 | type="checkbox" |
||
| 289 | name="jetpack_sso_match_by_email" |
||
| 290 | <?php checked( Jetpack_SSO_Helpers::match_by_email() ); ?> |
||
| 291 | <?php disabled( Jetpack_SSO_Helpers::is_match_by_email_checkbox_disabled() ); ?> |
||
| 292 | > |
||
| 293 | <?php esc_html_e( 'Match by Email', 'jetpack' ); ?> |
||
| 294 | </label> |
||
| 295 | <?php |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Validate the match by email check in Settings > General |
||
| 300 | * |
||
| 301 | * @since 2.9 |
||
| 302 | * @return boolean |
||
| 303 | **/ |
||
| 304 | public function validate_jetpack_sso_match_by_email( $input ) { |
||
| 305 | return ( ! empty( $input ) ) ? 1 : 0; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Checks to determine if the user wants to login on wp-login |
||
| 310 | * |
||
| 311 | * This function mostly exists to cover the exceptions to login |
||
| 312 | * that may exist as other parameters to $_GET[action] as $_GET[action] |
||
| 313 | * does not have to exist. By default WordPress assumes login if an action |
||
| 314 | * is not set, however this may not be true, as in the case of logout |
||
| 315 | * where $_GET[loggedout] is instead set |
||
| 316 | * |
||
| 317 | * @return boolean |
||
| 318 | **/ |
||
| 319 | private function wants_to_login() { |
||
| 320 | $wants_to_login = false; |
||
| 321 | |||
| 322 | // Cover default WordPress behavior |
||
| 323 | $action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'login'; |
||
| 324 | |||
| 325 | // And now the exceptions |
||
| 326 | $action = isset( $_GET['loggedout'] ) ? 'loggedout' : $action; |
||
| 327 | |||
| 328 | if ( Jetpack_SSO_Helpers::display_sso_form_for_action( $action ) ) { |
||
| 329 | $wants_to_login = true; |
||
| 330 | } |
||
| 331 | |||
| 332 | return $wants_to_login; |
||
| 333 | } |
||
| 334 | |||
| 335 | function login_init() { |
||
| 336 | global $action; |
||
| 337 | |||
| 338 | if ( Jetpack_SSO_Helpers::should_hide_login_form() ) { |
||
| 339 | /** |
||
| 340 | * Since the default authenticate filters fire at priority 20 for checking username and password, |
||
| 341 | * let's fire at priority 30. wp_authenticate_spam_check is fired at priority 99, but since we return a |
||
| 342 | * WP_Error in disable_default_login_form, then we won't trigger spam processing logic. |
||
| 343 | */ |
||
| 344 | add_filter( 'authenticate', array( 'Jetpack_SSO_Notices', 'disable_default_login_form' ), 30 ); |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Filter the display of the disclaimer message appearing when default WordPress login form is disabled. |
||
| 348 | * |
||
| 349 | * @module sso |
||
| 350 | * |
||
| 351 | * @since 2.8.0 |
||
| 352 | * |
||
| 353 | * @param bool true Should the disclaimer be displayed. Default to true. |
||
| 354 | */ |
||
| 355 | $display_sso_disclaimer = apply_filters( 'jetpack_sso_display_disclaimer', true ); |
||
| 356 | if ( $display_sso_disclaimer ) { |
||
| 357 | add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'msg_login_by_jetpack' ) ); |
||
| 358 | } |
||
| 359 | } |
||
| 360 | |||
| 361 | if ( 'jetpack-sso' === $action ) { |
||
| 362 | if ( isset( $_GET['result'], $_GET['user_id'], $_GET['sso_nonce'] ) && 'success' == $_GET['result'] ) { |
||
| 363 | $this->handle_login(); |
||
| 364 | $this->display_sso_login_form(); |
||
| 365 | } else { |
||
| 366 | if ( Jetpack::is_staging_site() ) { |
||
| 367 | add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'sso_not_allowed_in_staging' ) ); |
||
| 368 | View Code Duplication | } else { |
|
| 369 | // Is it wiser to just use wp_redirect than do this runaround to wp_safe_redirect? |
||
| 370 | add_filter( 'allowed_redirect_hosts', array( 'Jetpack_SSO_Helpers', 'allowed_redirect_hosts' ) ); |
||
| 371 | $reauth = ! empty( $_GET['force_reauth'] ); |
||
| 372 | $sso_url = $this->get_sso_url_or_die( $reauth ); |
||
| 373 | JetpackTracking::record_user_event( 'sso_login_redirect_success' ); |
||
| 374 | wp_safe_redirect( $sso_url ); |
||
| 375 | exit; |
||
| 376 | } |
||
| 377 | } |
||
| 378 | } else if ( Jetpack_SSO_Helpers::display_sso_form_for_action( $action ) ) { |
||
| 379 | |||
| 380 | // Save cookies so we can handle redirects after SSO |
||
| 381 | $this->save_cookies(); |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Check to see if the site admin wants to automagically forward the user |
||
| 385 | * to the WordPress.com login page AND that the request to wp-login.php |
||
| 386 | * is not something other than login (Like logout!) |
||
| 387 | */ |
||
| 388 | View Code Duplication | if ( Jetpack_SSO_Helpers::bypass_login_forward_wpcom() && $this->wants_to_login() ) { |
|
| 389 | add_filter( 'allowed_redirect_hosts', array( 'Jetpack_SSO_Helpers', 'allowed_redirect_hosts' ) ); |
||
| 390 | $reauth = ! empty( $_GET['force_reauth'] ); |
||
| 391 | $sso_url = $this->get_sso_url_or_die( $reauth ); |
||
| 392 | JetpackTracking::record_user_event( 'sso_login_redirect_bypass_success' ); |
||
| 393 | wp_safe_redirect( $sso_url ); |
||
| 394 | exit; |
||
| 395 | } |
||
| 396 | |||
| 397 | $this->display_sso_login_form(); |
||
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Ensures that we can get a nonce from WordPress.com via XML-RPC before setting |
||
| 403 | * up the hooks required to display the SSO form. |
||
| 404 | */ |
||
| 405 | public function display_sso_login_form() { |
||
| 406 | add_filter( 'login_body_class', array( $this, 'login_body_class' ) ); |
||
| 407 | add_action( 'login_head', array( $this, 'print_inline_admin_css' ) ); |
||
| 408 | |||
| 409 | if ( Jetpack::is_staging_site() ) { |
||
| 410 | add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'sso_not_allowed_in_staging' ) ); |
||
| 411 | return; |
||
| 412 | } |
||
| 413 | |||
| 414 | $sso_nonce = self::request_initial_nonce(); |
||
| 415 | if ( is_wp_error( $sso_nonce ) ) { |
||
| 416 | return; |
||
| 417 | } |
||
| 418 | |||
| 419 | add_action( 'login_form', array( $this, 'login_form' ) ); |
||
| 420 | add_action( 'login_enqueue_scripts', array( $this, 'login_enqueue_scripts' ) ); |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Conditionally save the redirect_to url as a cookie. |
||
| 425 | * |
||
| 426 | * @since 4.6.0 Renamed to save_cookies from maybe_save_redirect_cookies |
||
| 427 | */ |
||
| 428 | public static function save_cookies() { |
||
| 429 | if ( headers_sent() ) { |
||
| 430 | return new WP_Error( 'headers_sent', __( 'Cannot deal with cookie redirects, as headers are already sent.', 'jetpack' ) ); |
||
| 431 | } |
||
| 432 | |||
| 433 | setcookie( |
||
| 434 | 'jetpack_sso_original_request', |
||
| 435 | esc_url_raw( set_url_scheme( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) ), |
||
| 436 | time() + HOUR_IN_SECONDS, |
||
| 437 | COOKIEPATH, |
||
| 438 | COOKIE_DOMAIN, |
||
| 439 | false, |
||
| 440 | true |
||
| 441 | ); |
||
| 442 | |||
| 443 | if ( ! empty( $_GET['redirect_to'] ) ) { |
||
| 444 | // If we have something to redirect to |
||
| 445 | $url = esc_url_raw( $_GET['redirect_to'] ); |
||
| 446 | setcookie( 'jetpack_sso_redirect_to', $url, time() + HOUR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, false, true ); |
||
| 447 | } elseif ( ! empty( $_COOKIE['jetpack_sso_redirect_to'] ) ) { |
||
| 448 | // Otherwise, if it's already set, purge it. |
||
| 449 | setcookie( 'jetpack_sso_redirect_to', ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); |
||
| 450 | } |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Outputs the Jetpack SSO button and description as well as the toggle link |
||
| 455 | * for switching between Jetpack SSO and default login. |
||
| 456 | */ |
||
| 457 | function login_form() { |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Clear the cookies that store the profile information for the last |
||
| 533 | * WPCOM user to connect. |
||
| 534 | */ |
||
| 535 | static function clear_wpcom_profile_cookies() { |
||
| 556 | |||
| 557 | static function delete_connection_for_user( $user_id ) { |
||
| 558 | if ( ! $wpcom_user_id = get_user_meta( $user_id, 'wpcom_user_id', true ) ) { |
||
| 559 | return; |
||
| 560 | } |
||
| 561 | Jetpack::load_xml_rpc_client(); |
||
| 562 | $xml = new Jetpack_IXR_Client( array( |
||
| 563 | 'wpcom_user_id' => $user_id, |
||
| 564 | ) ); |
||
| 565 | $xml->query( 'jetpack.sso.removeUser', $wpcom_user_id ); |
||
| 566 | |||
| 567 | if ( $xml->isError() ) { |
||
| 568 | return false; |
||
| 569 | } |
||
| 570 | |||
| 571 | // Clean up local data stored for SSO |
||
| 572 | delete_user_meta( $user_id, 'wpcom_user_id' ); |
||
| 573 | delete_user_meta( $user_id, 'wpcom_user_data' ); |
||
| 574 | self::clear_wpcom_profile_cookies(); |
||
| 575 | |||
| 576 | return $xml->getResponse(); |
||
| 577 | } |
||
| 578 | |||
| 579 | View Code Duplication | static function request_initial_nonce() { |
|
| 592 | |||
| 593 | /** |
||
| 594 | * The function that actually handles the login! |
||
| 595 | */ |
||
| 596 | function handle_login() { |
||
| 597 | $wpcom_nonce = sanitize_key( $_GET['sso_nonce'] ); |
||
| 598 | $wpcom_user_id = (int) $_GET['user_id']; |
||
| 599 | |||
| 600 | Jetpack::load_xml_rpc_client(); |
||
| 601 | $xml = new Jetpack_IXR_Client( array( |
||
| 602 | 'user_id' => get_current_user_id(), |
||
| 603 | ) ); |
||
| 604 | $xml->query( 'jetpack.sso.validateResult', $wpcom_nonce, $wpcom_user_id ); |
||
| 605 | |||
| 606 | $user_data = $xml->isError() ? false : $xml->getResponse(); |
||
| 607 | if ( empty( $user_data ) ) { |
||
| 608 | add_filter( 'jetpack_sso_default_to_sso_login', '__return_false' ); |
||
| 609 | add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'error_invalid_response_data' ) ); |
||
| 610 | return; |
||
| 611 | } |
||
| 612 | |||
| 613 | $user_data = (object) $user_data; |
||
| 614 | $user = null; |
||
| 787 | |||
| 788 | static function profile_page_url() { |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Builds the "Login to WordPress.com" button that is displayed on the login page as well as user profile page. |
||
| 794 | * |
||
| 795 | * @param array $args An array of arguments to add to the SSO URL. |
||
| 796 | * @param boolean $is_primary Should the button have the `button-primary` class? |
||
| 797 | * @return string Returns the HTML markup for the button. |
||
| 798 | */ |
||
| 799 | function build_sso_button( $args = array(), $is_primary = false ) { |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Builds a URL with `jetpack-sso` action and option args which is used to setup SSO. |
||
| 816 | * |
||
| 817 | * @param array $args An array of arguments to add to the SSO URL. |
||
| 818 | * @return string The URL used for SSO. |
||
| 819 | */ |
||
| 820 | function build_sso_button_url( $args = array() ) { |
||
| 833 | |||
| 834 | /** |
||
| 835 | * Retrieves a WordPress.com SSO URL with appropriate query parameters or dies. |
||
| 836 | * |
||
| 837 | * @param boolean $reauth Should the user be forced to reauthenticate on WordPress.com? |
||
| 838 | * @param array $args Optional query parameters. |
||
| 839 | * @return string The WordPress.com SSO URL. |
||
| 840 | */ |
||
| 841 | function get_sso_url_or_die( $reauth = false, $args = array() ) { |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Build WordPress.com SSO URL with appropriate query parameters. |
||
| 865 | * |
||
| 866 | * @param array $args Optional query parameters. |
||
| 867 | * @return string WordPress.com SSO URL |
||
| 868 | */ |
||
| 869 | function build_sso_url( $args = array() ) { |
||
| 886 | |||
| 887 | /** |
||
| 888 | * Build WordPress.com SSO URL with appropriate query parameters, |
||
| 889 | * including the parameters necessary to force the user to reauthenticate |
||
| 890 | * on WordPress.com. |
||
| 891 | * |
||
| 892 | * @param array $args Optional query parameters. |
||
| 893 | * @return string WordPress.com SSO URL |
||
| 894 | */ |
||
| 895 | function build_reauth_and_sso_url( $args = array() ) { |
||
| 920 | |||
| 921 | /** |
||
| 922 | * Determines local user associated with a given WordPress.com user ID. |
||
| 923 | * |
||
| 924 | * @since 2.6.0 |
||
| 925 | * |
||
| 926 | * @param int $wpcom_user_id User ID from WordPress.com |
||
| 927 | * @return object Local user object if found, null if not. |
||
| 928 | */ |
||
| 929 | static function get_user_by_wpcom_id( $wpcom_user_id ) { |
||
| 939 | |||
| 940 | /** |
||
| 941 | * When jetpack-sso-auth-redirect query parameter is set, will redirect user to |
||
| 942 | * WordPress.com authorization flow. |
||
| 943 | * |
||
| 944 | * We redirect here instead of in handle_login() because Jetpack::init()->build_connect_url |
||
| 945 | * calls menu_page_url() which doesn't work properly until admin menus are registered. |
||
| 946 | */ |
||
| 947 | function maybe_authorize_user_after_sso() { |
||
| 974 | |||
| 975 | /** |
||
| 976 | * Cache user's display name and Gravatar so it can be displayed on the login screen. These cookies are |
||
| 977 | * stored when the user logs out, and then deleted when the user logs in. |
||
| 978 | */ |
||
| 979 | function store_wpcom_profile_cookies_on_logout() { |
||
| 1008 | |||
| 1009 | /** |
||
| 1010 | * Determines if a local user is connected to WordPress.com |
||
| 1011 | * |
||
| 1012 | * @since 2.8 |
||
| 1013 | * @param integer $user_id - Local user id |
||
| 1014 | * @return boolean |
||
| 1015 | **/ |
||
| 1016 | public function is_user_connected( $user_id ) { |
||
| 1019 | |||
| 1020 | /** |
||
| 1021 | * Retrieves a user's WordPress.com data |
||
| 1022 | * |
||
| 1023 | * @since 2.8 |
||
| 1024 | * @param integer $user_id - Local user id |
||
| 1025 | * @return mixed null or stdClass |
||
| 1026 | **/ |
||
| 1027 | public function get_user_data( $user_id ) { |
||
| 1030 | } |
||
| 1031 | |||
| 1033 |
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.