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 |
||
| 17 | class Jetpack_SSO { |
||
| 18 | static $instance = null; |
||
|
|
|||
| 19 | |||
| 20 | private function __construct() { |
||
| 21 | |||
| 22 | self::$instance = $this; |
||
| 23 | |||
| 24 | add_action( 'admin_init', array( $this, 'maybe_authorize_user_after_sso' ), 1 ); |
||
| 25 | add_action( 'admin_init', array( $this, 'admin_init' ) ); |
||
| 26 | add_action( 'admin_init', array( $this, 'register_settings' ) ); |
||
| 27 | add_action( 'login_init', array( $this, 'login_init' ) ); |
||
| 28 | add_action( 'delete_user', array( $this, 'delete_connection_for_user' ) ); |
||
| 29 | add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
||
| 30 | add_action( 'init', array( $this, 'maybe_logout_user' ), 5 ); |
||
| 31 | add_action( 'jetpack_modules_loaded', array( $this, 'module_configure_button' ) ); |
||
| 32 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
||
| 33 | |||
| 34 | // Adding this action so that on login_init, the action won't be sanitized out of the $action global. |
||
| 35 | add_action( 'login_form_jetpack-sso', '__return_true' ); |
||
| 36 | |||
| 37 | if ( |
||
| 38 | $this->should_hide_login_form() && |
||
| 39 | /** |
||
| 40 | * Filter the display of the disclaimer message appearing when default WordPress login form is disabled. |
||
| 41 | * |
||
| 42 | * @module sso |
||
| 43 | * |
||
| 44 | * @since 2.8.0 |
||
| 45 | * |
||
| 46 | * @param bool true Should the disclaimer be displayed. Default to true. |
||
| 47 | */ |
||
| 48 | apply_filters( 'jetpack_sso_display_disclaimer', true ) |
||
| 49 | ) { |
||
| 50 | add_action( 'login_message', array( $this, 'msg_login_by_jetpack' ) ); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Returns the single instance of the Jetpack_SSO object |
||
| 56 | * |
||
| 57 | * @since 2.8 |
||
| 58 | * @return Jetpack_SSO |
||
| 59 | **/ |
||
| 60 | public static function get_instance() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Add configure button and functionality to the module card on the Jetpack screen |
||
| 70 | **/ |
||
| 71 | public static function module_configure_button() { |
||
| 72 | Jetpack::enable_module_configurable( __FILE__ ); |
||
| 73 | Jetpack::module_configuration_load( __FILE__, array( __CLASS__, 'module_configuration_load' ) ); |
||
| 74 | Jetpack::module_configuration_head( __FILE__, array( __CLASS__, 'module_configuration_head' ) ); |
||
| 75 | Jetpack::module_configuration_screen( __FILE__, array( __CLASS__, 'module_configuration_screen' ) ); |
||
| 76 | } |
||
| 77 | |||
| 78 | public static function module_configuration_load() { |
||
| 79 | // wp_safe_redirect( admin_url( 'options-general.php#configure-sso' ) ); |
||
| 80 | // exit; |
||
| 81 | } |
||
| 82 | |||
| 83 | public static function module_configuration_head() {} |
||
| 84 | |||
| 85 | public static function module_configuration_screen() { |
||
| 86 | ?> |
||
| 87 | <form method="post" action="options.php"> |
||
| 88 | <?php settings_fields( 'jetpack-sso' ); ?> |
||
| 89 | <?php do_settings_sections( 'jetpack-sso' ); ?> |
||
| 90 | <?php submit_button(); ?> |
||
| 91 | </form> |
||
| 92 | <?php |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * If jetpack_force_logout == 1 in current user meta the user will be forced |
||
| 97 | * to logout and reauthenticate with the site. |
||
| 98 | **/ |
||
| 99 | public function maybe_logout_user() { |
||
| 100 | global $current_user; |
||
| 101 | |||
| 102 | if ( 1 == $current_user->jetpack_force_logout ) { |
||
| 103 | delete_user_meta( $current_user->ID, 'jetpack_force_logout' ); |
||
| 104 | self::delete_connection_for_user( $current_user->ID ); |
||
| 105 | wp_logout(); |
||
| 106 | wp_safe_redirect( wp_login_url() ); |
||
| 107 | exit; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Adds additional methods the WordPress xmlrpc API for handling SSO specific features |
||
| 114 | * |
||
| 115 | * @param array $methods |
||
| 116 | * @return array |
||
| 117 | **/ |
||
| 118 | public function xmlrpc_methods( $methods ) { |
||
| 119 | $methods['jetpack.userDisconnect'] = array( $this, 'xmlrpc_user_disconnect' ); |
||
| 120 | return $methods; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Marks a user's profile for disconnect from WordPress.com and forces a logout |
||
| 125 | * the next time the user visits the site. |
||
| 126 | **/ |
||
| 127 | public function xmlrpc_user_disconnect( $user_id ) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Enqueues scripts and styles necessary for SSO login. |
||
| 148 | */ |
||
| 149 | public function login_enqueue_scripts() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Enqueue styles neceessary for Jetpack SSO on users' profiles |
||
| 162 | */ |
||
| 163 | public function admin_enqueue_scripts() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Adds Jetpack SSO classes to login body |
||
| 175 | * |
||
| 176 | * @param array $classes Array of classes to add to body tag |
||
| 177 | * @return array Array of classes to add to body tag |
||
| 178 | */ |
||
| 179 | public function login_body_class( $classes ) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Adds settings fields to Settings > General > Single Sign On that allows users to |
||
| 197 | * turn off the login form on wp-login.php |
||
| 198 | * |
||
| 199 | * @since 2.7 |
||
| 200 | **/ |
||
| 201 | public function register_settings() { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Builds the display for the checkbox allowing user to require two step |
||
| 268 | * auth be enabled on WordPress.com accounts before login. Displays in Settings > General |
||
| 269 | * |
||
| 270 | * @since 2.7 |
||
| 271 | **/ |
||
| 272 | public function render_require_two_step() { |
||
| 273 | /** This filter is documented in modules/sso.php */ |
||
| 274 | $require_two_step = ( 1 == apply_filters( 'jetpack_sso_require_two_step', get_option( 'jetpack_sso_require_two_step' ) ) ); |
||
| 275 | $disabled = $require_two_step ? ' disabled="disabled"' : ''; |
||
| 276 | echo '<label>'; |
||
| 277 | echo '<input type="checkbox" name="jetpack_sso_require_two_step" ' . checked( $require_two_step, true, false ) . "$disabled>"; |
||
| 278 | esc_html_e( 'Require Two-Step Authentication' , 'jetpack' ); |
||
| 279 | echo '</label>'; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Validate the require two step checkbox in Settings > General |
||
| 284 | * |
||
| 285 | * @since 2.7 |
||
| 286 | * @return boolean |
||
| 287 | **/ |
||
| 288 | public function validate_jetpack_sso_require_two_step( $input ) { |
||
| 289 | return ( ! empty( $input ) ) ? 1 : 0; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Builds the display for the checkbox allowing the user to allow matching logins by email |
||
| 294 | * Displays in Settings > General |
||
| 295 | * |
||
| 296 | * @since 2.9 |
||
| 297 | **/ |
||
| 298 | public function render_match_by_email() { |
||
| 299 | $match_by_email = 1 == $this->match_by_email(); |
||
| 300 | $disabled = $match_by_email ? ' disabled="disabled"' : ''; |
||
| 301 | echo '<label>'; |
||
| 302 | echo '<input type="checkbox" name="jetpack_sso_match_by_email"' . checked( $match_by_email, true, false ) . "$disabled>"; |
||
| 303 | esc_html_e( 'Match by Email', 'jetpack' ); |
||
| 304 | echo '</label>'; |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Validate the match by email check in Settings > General |
||
| 309 | * |
||
| 310 | * @since 2.9 |
||
| 311 | * @return boolean |
||
| 312 | **/ |
||
| 313 | public function validate_jetpack_sso_match_by_email( $input ) { |
||
| 314 | return ( ! empty( $input ) ) ? 1 : 0; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Builds the display for the checkbox allowing users to remove the default |
||
| 319 | * WordPress login form from wp-login.php. Displays in Settings > General |
||
| 320 | * |
||
| 321 | * @since 2.7 |
||
| 322 | **/ |
||
| 323 | public function render_remove_login_form_checkbox() { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Validate settings input from Settings > General |
||
| 336 | * |
||
| 337 | * @since 2.7 |
||
| 338 | * @return boolean |
||
| 339 | **/ |
||
| 340 | public function validate_settings_remove_login_form_checkbox( $input ) { |
||
| 341 | return ( isset( $input['remove_login_form'] ) )? 1: 0; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Checks to determine if the user wants to login on wp-login |
||
| 346 | * |
||
| 347 | * This function mostly exists to cover the exceptions to login |
||
| 348 | * that may exist as other parameters to $_GET[action] as $_GET[action] |
||
| 349 | * does not have to exist. By default WordPress assumes login if an action |
||
| 350 | * is not set, however this may not be true, as in the case of logout |
||
| 351 | * where $_GET[loggedout] is instead set |
||
| 352 | * |
||
| 353 | * @return boolean |
||
| 354 | **/ |
||
| 355 | private function wants_to_login() { |
||
| 370 | |||
| 371 | private function bypass_login_forward_wpcom() { |
||
| 372 | /** |
||
| 373 | * Redirect the site's log in form to WordPress.com's log in form. |
||
| 374 | * |
||
| 375 | * @module sso |
||
| 376 | * |
||
| 377 | * @since 3.1.0 |
||
| 378 | * |
||
| 379 | * @param bool false Should the site's log in form be automatically forwarded to WordPress.com's log in form. |
||
| 380 | */ |
||
| 381 | return apply_filters( 'jetpack_sso_bypass_login_forward_wpcom', false ); |
||
| 382 | } |
||
| 383 | |||
| 384 | function login_init() { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Ensures that we can get a nonce from WordPress.com via XML-RPC before setting |
||
| 435 | * up the hooks required to display the SSO form. |
||
| 436 | */ |
||
| 437 | public function display_sso_login_form() { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Conditionally save the redirect_to url as a cookie. |
||
| 450 | */ |
||
| 451 | public static function maybe_save_cookie_redirect() { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Determine if the login form should be hidden or not |
||
| 475 | * |
||
| 476 | * Method is private only because it is only used in this class so far. |
||
| 477 | * Feel free to change it later |
||
| 478 | * |
||
| 479 | * @return bool |
||
| 480 | **/ |
||
| 481 | private function should_hide_login_form() { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Outputs the Jetpack SSO button and description as well as the toggle link |
||
| 496 | * for switching between Jetpack SSO and default login. |
||
| 497 | */ |
||
| 498 | function login_form() { |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Clear the cookies that store the profile information for the last |
||
| 574 | * WPCOM user to connect. |
||
| 575 | */ |
||
| 576 | static function clear_wpcom_profile_cookies() { |
||
| 597 | |||
| 598 | View Code Duplication | static function delete_connection_for_user( $user_id ) { |
|
| 616 | |||
| 617 | View Code Duplication | static function request_initial_nonce() { |
|
| 630 | |||
| 631 | /** |
||
| 632 | * The function that actually handles the login! |
||
| 633 | */ |
||
| 634 | function handle_login() { |
||
| 635 | $wpcom_nonce = sanitize_key( $_GET['sso_nonce'] ); |
||
| 636 | $wpcom_user_id = (int) $_GET['user_id']; |
||
| 637 | $result = sanitize_key( $_GET['result'] ); |
||
| 638 | |||
| 639 | Jetpack::load_xml_rpc_client(); |
||
| 640 | $xml = new Jetpack_IXR_Client( array( |
||
| 641 | 'user_id' => get_current_user_id(), |
||
| 642 | ) ); |
||
| 643 | $xml->query( 'jetpack.sso.validateResult', $wpcom_nonce, $wpcom_user_id ); |
||
| 644 | |||
| 645 | if ( $xml->isError() ) { |
||
| 646 | wp_die( sprintf( '%s: %s', $xml->getErrorCode(), $xml->getErrorMessage() ) ); |
||
| 647 | } |
||
| 648 | |||
| 649 | $user_data = $xml->getResponse(); |
||
| 650 | |||
| 651 | if ( empty( $user_data ) ) { |
||
| 652 | wp_die( __( 'Error, invalid response data.', 'jetpack' ) ); |
||
| 653 | } |
||
| 654 | |||
| 655 | $user_data = (object) $user_data; |
||
| 656 | $user = null; |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Fires before Jetpack's SSO modifies the log in form. |
||
| 660 | * |
||
| 661 | * @module sso |
||
| 662 | * |
||
| 663 | * @since 2.6.0 |
||
| 664 | * |
||
| 665 | * @param object $user_data User login information. |
||
| 666 | */ |
||
| 667 | do_action( 'jetpack_sso_pre_handle_login', $user_data ); |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Is it required to have 2-step authentication enabled on WordPress.com to use SSO? |
||
| 671 | * |
||
| 672 | * @module sso |
||
| 673 | * |
||
| 674 | * @since 2.8.0 |
||
| 675 | * |
||
| 676 | * @param bool get_option( 'jetpack_sso_require_two_step' ) Does SSO require 2-step authentication? |
||
| 677 | */ |
||
| 678 | $require_two_step = apply_filters( 'jetpack_sso_require_two_step', get_option( 'jetpack_sso_require_two_step' ) ); |
||
| 679 | if ( $require_two_step && 0 == (int) $user_data->two_step_enabled ) { |
||
| 680 | $this->user_data = $user_data; |
||
| 681 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
| 682 | do_action( 'wp_login_failed', $user_data->login ); |
||
| 683 | add_action( 'login_message', array( $this, 'error_msg_enable_two_step' ) ); |
||
| 684 | return; |
||
| 685 | } |
||
| 686 | |||
| 687 | if ( isset( $_GET['state'] ) && ( 0 < strpos( $_GET['state'], '|' ) ) ) { |
||
| 688 | list( $state, $nonce ) = explode( '|', $_GET['state'] ); |
||
| 689 | |||
| 690 | if ( wp_verify_nonce( $nonce, $state ) ) { |
||
| 691 | if ( 'sso-link-user' == $state ) { |
||
| 692 | $user = wp_get_current_user(); |
||
| 693 | update_user_meta( $user->ID, 'wpcom_user_id', $user_data->ID ); |
||
| 694 | add_filter( 'login_redirect', array( __CLASS__, 'profile_page_url' ) ); |
||
| 695 | } |
||
| 696 | } else { |
||
| 697 | wp_nonce_ays(); |
||
| 698 | } |
||
| 699 | } |
||
| 700 | |||
| 701 | if ( empty( $user ) ) { |
||
| 702 | $user = $this->get_user_by_wpcom_id( $user_data->ID ); |
||
| 703 | } |
||
| 704 | |||
| 705 | // If we don't have one by wpcom_user_id, try by the email? |
||
| 706 | if ( empty( $user ) && self::match_by_email() ) { |
||
| 707 | $user = get_user_by( 'email', $user_data->email ); |
||
| 708 | if ( $user ) { |
||
| 709 | update_user_meta( $user->ID, 'wpcom_user_id', $user_data->ID ); |
||
| 710 | } |
||
| 711 | } |
||
| 712 | |||
| 713 | // If we've still got nothing, create the user. |
||
| 714 | if ( empty( $user ) && ( get_option( 'users_can_register' ) || self::new_user_override() ) ) { |
||
| 715 | // If not matching by email we still need to verify the email does not exist |
||
| 716 | // or this blows up |
||
| 717 | /** |
||
| 718 | * If match_by_email is true, we know the email doesn't exist, as it would have |
||
| 719 | * been found in the first pass. If get_user_by( 'email' ) doesn't find the |
||
| 720 | * user, then we know that email is unused, so it's safe to add. |
||
| 721 | */ |
||
| 722 | if ( self::match_by_email() || ! get_user_by( 'email', $user_data->email ) ) { |
||
| 723 | $username = $user_data->login; |
||
| 724 | |||
| 725 | if ( username_exists( $username ) ) { |
||
| 726 | $username = $user_data->login . '_' . $user_data->ID; |
||
| 727 | } |
||
| 728 | |||
| 729 | $tries = 0; |
||
| 730 | while ( username_exists( $username ) ) { |
||
| 731 | $username = $user_data->login . '_' . $user_data->ID . '_' . mt_rand(); |
||
| 732 | if ( $tries++ >= 5 ) { |
||
| 733 | wp_die( __( "Error: Couldn't create suitable username.", 'jetpack' ) ); |
||
| 734 | } |
||
| 735 | } |
||
| 736 | |||
| 737 | $password = wp_generate_password( 20 ); |
||
| 738 | $user_id = wp_create_user( $username, $password, $user_data->email ); |
||
| 739 | $user = get_userdata( $user_id ); |
||
| 740 | |||
| 741 | $user->display_name = $user_data->display_name; |
||
| 742 | $user->first_name = $user_data->first_name; |
||
| 743 | $user->last_name = $user_data->last_name; |
||
| 744 | $user->url = $user_data->url; |
||
| 745 | $user->description = $user_data->description; |
||
| 746 | wp_update_user( $user ); |
||
| 747 | |||
| 748 | update_user_meta( $user->ID, 'wpcom_user_id', $user_data->ID ); |
||
| 749 | } else { |
||
| 750 | $this->user_data = $user_data; |
||
| 751 | // do_action( 'wp_login_failed', $user_data->login ); |
||
| 752 | add_action( 'login_message', array( $this, 'error_msg_email_already_exists' ) ); |
||
| 753 | return; |
||
| 754 | } |
||
| 755 | } |
||
| 756 | |||
| 757 | /** |
||
| 758 | * Fires after we got login information from WordPress.com. |
||
| 759 | * |
||
| 760 | * @module sso |
||
| 761 | * |
||
| 762 | * @since 2.6.0 |
||
| 763 | * |
||
| 764 | * @param array $user WordPress.com User information. |
||
| 765 | * @param object $user_data User Login information. |
||
| 766 | */ |
||
| 767 | do_action( 'jetpack_sso_handle_login', $user, $user_data ); |
||
| 768 | |||
| 769 | if ( $user ) { |
||
| 770 | // Cache the user's details, so we can present it back to them on their user screen |
||
| 771 | update_user_meta( $user->ID, 'wpcom_user_data', $user_data ); |
||
| 772 | |||
| 773 | // Cache user's display name and Gravatar so it can be displayed on the login screen |
||
| 774 | setcookie( |
||
| 775 | 'jetpack_sso_wpcom_name' . COOKIEHASH, |
||
| 776 | $user_data->display_name, |
||
| 777 | time() + YEAR_IN_SECONDS, |
||
| 778 | COOKIEPATH, |
||
| 779 | COOKIE_DOMAIN |
||
| 780 | ); |
||
| 781 | |||
| 782 | setcookie( |
||
| 783 | 'jetpack_sso_wpcom_gravatar' . COOKIEHASH, |
||
| 784 | get_avatar_url( |
||
| 785 | $user_data->email, |
||
| 786 | array( 'size' => 72, 'default' => 'mystery' ) |
||
| 787 | ), |
||
| 788 | time() + YEAR_IN_SECONDS, |
||
| 789 | COOKIEPATH, |
||
| 790 | COOKIE_DOMAIN |
||
| 791 | ); |
||
| 792 | |||
| 793 | $remember = false; |
||
| 794 | View Code Duplication | if ( ! empty( $_COOKIE['jetpack_sso_remember_me'] ) ) { |
|
| 795 | $remember = true; |
||
| 796 | // And then purge it |
||
| 797 | setcookie( 'jetpack_sso_remember_me', ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); |
||
| 798 | } |
||
| 799 | /** |
||
| 800 | * Filter the remember me value. |
||
| 801 | * |
||
| 802 | * @module sso |
||
| 803 | * |
||
| 804 | * @since 2.8.0 |
||
| 805 | * |
||
| 806 | * @param bool $remember Is the remember me option checked? |
||
| 807 | */ |
||
| 808 | $remember = apply_filters( 'jetpack_remember_login', $remember ); |
||
| 809 | wp_set_auth_cookie( $user->ID, $remember ); |
||
| 810 | |||
| 811 | /** This filter is documented in core/src/wp-includes/user.php */ |
||
| 812 | do_action( 'wp_login', $user->user_login, $user ); |
||
| 813 | |||
| 814 | $_request_redirect_to = isset( $_REQUEST['redirect_to'] ) ? esc_url_raw( $_REQUEST['redirect_to'] ) : ''; |
||
| 815 | $redirect_to = user_can( $user, 'edit_posts' ) ? admin_url() : self::profile_page_url(); |
||
| 816 | |||
| 817 | // If we have a saved redirect to request in a cookie |
||
| 818 | View Code Duplication | if ( ! empty( $_COOKIE['jetpack_sso_redirect_to'] ) ) { |
|
| 819 | // Set that as the requested redirect to |
||
| 820 | $redirect_to = $_request_redirect_to = esc_url_raw( $_COOKIE['jetpack_sso_redirect_to'] ); |
||
| 821 | // And then purge it |
||
| 822 | setcookie( 'jetpack_sso_redirect_to', ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); |
||
| 823 | } |
||
| 824 | |||
| 825 | if ( ! Jetpack::is_user_connected( $user->ID ) ) { |
||
| 826 | wp_safe_redirect( |
||
| 827 | add_query_arg( |
||
| 828 | array( |
||
| 829 | 'redirect_to' => $redirect_to, |
||
| 830 | 'request_redirect_to' => $_request_redirect_to, |
||
| 831 | 'jetpack-sso-auth-redirect' => '1', |
||
| 832 | ), |
||
| 833 | admin_url() |
||
| 834 | ) |
||
| 835 | ); |
||
| 836 | exit; |
||
| 837 | } |
||
| 838 | |||
| 839 | wp_safe_redirect( |
||
| 840 | /** This filter is documented in core/src/wp-login.php */ |
||
| 841 | apply_filters( 'login_redirect', $redirect_to, $_request_redirect_to, $user ) |
||
| 842 | ); |
||
| 843 | exit; |
||
| 844 | } |
||
| 845 | |||
| 846 | $this->user_data = $user_data; |
||
| 847 | /** This filter is documented in core/src/wp-includes/pluggable.php */ |
||
| 848 | do_action( 'wp_login_failed', $user_data->login ); |
||
| 849 | add_action( 'login_message', array( $this, 'cant_find_user' ) ); |
||
| 850 | } |
||
| 851 | |||
| 852 | static function profile_page_url() { |
||
| 853 | return admin_url( 'profile.php' ); |
||
| 854 | } |
||
| 855 | |||
| 856 | static function match_by_email() { |
||
| 857 | $match_by_email = ( 1 == get_option( 'jetpack_sso_match_by_email', true ) ) ? true: false; |
||
| 858 | $match_by_email = defined( 'WPCC_MATCH_BY_EMAIL' ) ? WPCC_MATCH_BY_EMAIL : $match_by_email; |
||
| 859 | |||
| 860 | /** |
||
| 861 | * Link the local account to an account on WordPress.com using the same email address. |
||
| 862 | * |
||
| 863 | * @module sso |
||
| 864 | * |
||
| 865 | * @since 2.6.0 |
||
| 866 | * |
||
| 867 | * @param bool $match_by_email Should we link the local account to an account on WordPress.com using the same email address. Default to false. |
||
| 868 | */ |
||
| 869 | return apply_filters( 'jetpack_sso_match_by_email', $match_by_email ); |
||
| 870 | } |
||
| 871 | |||
| 872 | static function new_user_override() { |
||
| 873 | $new_user_override = defined( 'WPCC_NEW_USER_OVERRIDE' ) ? WPCC_NEW_USER_OVERRIDE : false; |
||
| 874 | |||
| 875 | /** |
||
| 876 | * Allow users to register on your site with a WordPress.com account, even though you disallow normal registrations. |
||
| 877 | * |
||
| 878 | * @module sso |
||
| 879 | * |
||
| 880 | * @since 2.6.0 |
||
| 881 | * |
||
| 882 | * @param bool $new_user_override Allow users to register on your site with a WordPress.com account. Default to false. |
||
| 883 | */ |
||
| 884 | return apply_filters( 'jetpack_sso_new_user_override', $new_user_override ); |
||
| 885 | } |
||
| 886 | |||
| 887 | function allowed_redirect_hosts( $hosts ) { |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Builds the "Login to WordPress.com" button that is displayed on the login page as well as user profile page. |
||
| 899 | * |
||
| 900 | * @param array $args An array of arguments to add to the SSO URL. |
||
| 901 | * @param boolean $is_primary Should the button have the `button-primary` class? |
||
| 902 | * @return string Returns the HTML markup for the button. |
||
| 903 | */ |
||
| 904 | function build_sso_button( $args = array(), $is_primary = false ) { |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Builds a URL with `jetpack-sso` action and option args which is used to setup SSO. |
||
| 920 | * |
||
| 921 | * @param array $args An array of arguments to add to the SSO URL. |
||
| 922 | * @return string The URL used for SSO. |
||
| 923 | */ |
||
| 924 | function build_sso_button_url( $args = array() ) { |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Retrieves a WordPress.com SSO URL with appropriate query parameters or dies. |
||
| 940 | * |
||
| 941 | * @param boolean $reauth Should the user be forced to reauthenticate on WordPress.com? |
||
| 942 | * @param array $args Optional query parameters. |
||
| 943 | * @return string The WordPress.com SSO URL. |
||
| 944 | */ |
||
| 945 | function get_sso_url_or_die( $reauth = false, $args = array() ) { |
||
| 960 | |||
| 961 | /** |
||
| 962 | * Build WordPress.com SSO URL with appropriate query parameters. |
||
| 963 | * |
||
| 964 | * @param array $args Optional query parameters. |
||
| 965 | * @return string WordPress.com SSO URL |
||
| 966 | */ |
||
| 967 | function build_sso_url( $args = array() ) { |
||
| 987 | |||
| 988 | /** |
||
| 989 | * Build WordPress.com SSO URL with appropriate query parameters, |
||
| 990 | * including the parameters necessary to force the user to reauthenticate |
||
| 991 | * on WordPress.com. |
||
| 992 | * |
||
| 993 | * @param array $args Optional query parameters. |
||
| 994 | * @return string WordPress.com SSO URL |
||
| 995 | */ |
||
| 996 | function build_reauth_and_sso_url( $args = array() ) { |
||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * Determines local user associated with a given WordPress.com user ID. |
||
| 1023 | * |
||
| 1024 | * @since 2.6.0 |
||
| 1025 | * |
||
| 1026 | * @param int $wpcom_user_id User ID from WordPress.com |
||
| 1027 | * @return object Local user object if found, null if not. |
||
| 1028 | */ |
||
| 1029 | static function get_user_by_wpcom_id( $wpcom_user_id ) { |
||
| 1030 | $user_query = new WP_User_Query( array( |
||
| 1031 | 'meta_key' => 'wpcom_user_id', |
||
| 1032 | 'meta_value' => intval( $wpcom_user_id ), |
||
| 1033 | 'number' => 1, |
||
| 1034 | ) ); |
||
| 1035 | |||
| 1036 | $users = $user_query->get_results(); |
||
| 1037 | return $users ? array_shift( $users ) : null; |
||
| 1038 | } |
||
| 1039 | |||
| 1040 | /** |
||
| 1041 | * Error message displayed on the login form when two step is required and |
||
| 1042 | * the user's account on WordPress.com does not have two step enabled. |
||
| 1043 | * |
||
| 1044 | * @since 2.7 |
||
| 1045 | * @param string $message |
||
| 1046 | * @return string |
||
| 1047 | **/ |
||
| 1048 | public function error_msg_enable_two_step( $message ) { |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * Error message displayed when the user tries to SSO, but match by email |
||
| 1058 | * is off and they already have an account with their email address on |
||
| 1059 | * this site. |
||
| 1060 | * |
||
| 1061 | * @param string $message |
||
| 1062 | * @return string |
||
| 1063 | */ |
||
| 1064 | public function error_msg_email_already_exists( $message ) { |
||
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Message displayed when the site admin has disabled the default WordPress |
||
| 1074 | * login form in Settings > General > Single Sign On |
||
| 1075 | * |
||
| 1076 | * @since 2.7 |
||
| 1077 | * @param string $message |
||
| 1078 | * @return string |
||
| 1079 | **/ |
||
| 1080 | public function msg_login_by_jetpack( $message ) { |
||
| 1098 | |||
| 1099 | /** |
||
| 1100 | * Error message displayed on the login form when the user attempts |
||
| 1101 | * to post to the login form and it is disabled. |
||
| 1102 | * |
||
| 1103 | * @since 2.8 |
||
| 1104 | * @param string $message |
||
| 1105 | * @param string |
||
| 1106 | **/ |
||
| 1107 | public function error_msg_login_method_not_allowed( $message ) { |
||
| 1108 | $err = __( 'Login method not allowed' , 'jetpack' ); |
||
| 1109 | $message .= sprintf( '<p class="message" id="login_error">%s</p>', $err ); |
||
| 1110 | |||
| 1111 | return $message; |
||
| 1112 | } |
||
| 1113 | function cant_find_user( $message ) { |
||
| 1114 | if ( self::match_by_email() ) { |
||
| 1115 | $err_format = __( 'We couldn\'t find an account with the email <strong><code>%1$s</code></strong> to log you in with. If you already have an account on <strong>%2$s</strong>, please make sure that <strong><code>%1$s</code></strong> is configured as the email address, or that you have connected to WordPress.com on your profile page.', 'jetpack' ); |
||
| 1116 | } else { |
||
| 1117 | $err_format = __( 'We couldn\'t find any account on <strong>%2$s</strong> that is linked to your WordPress.com account to log you in with. If you already have an account on <strong>%2$s</strong>, please make sure that you have connected to WordPress.com on your profile page.', 'jetpack' ); |
||
| 1118 | } |
||
| 1119 | $err = sprintf( $err_format, $this->user_data->email, get_bloginfo( 'name' ) ); |
||
| 1120 | $message .= sprintf( '<p class="message" id="login_error">%s</p>', $err ); |
||
| 1121 | return $message; |
||
| 1122 | } |
||
| 1123 | |||
| 1124 | /** |
||
| 1125 | * When jetpack-sso-auth-redirect query parameter is set, will redirect user to |
||
| 1126 | * WordPress.com authorization flow. |
||
| 1127 | * |
||
| 1128 | * We redirect here instead of in handle_login() because Jetpack::init()->build_connect_url |
||
| 1129 | * calls menu_page_url() which doesn't work properly until admin menus are registered. |
||
| 1130 | */ |
||
| 1131 | function maybe_authorize_user_after_sso() { |
||
| 1158 | |||
| 1159 | /** |
||
| 1160 | * Deal with user connections... |
||
| 1161 | */ |
||
| 1162 | function admin_init() { |
||
| 1178 | |||
| 1179 | /** |
||
| 1180 | * Determines if a local user is connected to WordPress.com |
||
| 1181 | * |
||
| 1182 | * @since 2.8 |
||
| 1183 | * @param integer $user_id - Local user id |
||
| 1184 | * @return boolean |
||
| 1185 | **/ |
||
| 1186 | public function is_user_connected( $user_id ) { |
||
| 1189 | |||
| 1190 | /** |
||
| 1191 | * Retrieves a user's WordPress.com data |
||
| 1192 | * |
||
| 1193 | * @since 2.8 |
||
| 1194 | * @param integer $user_id - Local user id |
||
| 1195 | * @return mixed null or stdClass |
||
| 1196 | **/ |
||
| 1197 | public function get_user_data( $user_id ) { |
||
| 1198 | return get_user_meta( $user_id, 'wpcom_user_data', true ); |
||
| 1199 | } |
||
| 1200 | |||
| 1201 | function edit_profile_fields( $user ) { |
||
| 1244 | } |
||
| 1245 | |||
| 1246 | Jetpack_SSO::get_instance(); |
||
| 1247 |
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.