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, 'register_settings' ) ); |
||
| 26 | add_action( 'login_init', array( $this, 'login_init' ) ); |
||
| 27 | add_action( 'delete_user', array( $this, 'delete_connection_for_user' ) ); |
||
| 28 | add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ) ); |
||
| 29 | add_action( 'init', array( $this, 'maybe_logout_user' ), 5 ); |
||
| 30 | add_action( 'jetpack_modules_loaded', array( $this, 'module_configure_button' ) ); |
||
| 31 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
||
| 32 | add_action( 'login_form_logout', array( $this, 'store_wpcom_profile_cookies_on_logout' ) ); |
||
| 33 | add_action( 'wp_login', array( 'Jetpack_SSO', 'clear_wpcom_profile_cookies' ) ); |
||
| 34 | add_action( 'jetpack_unlinked_user', array( $this, 'delete_connection_for_user') ); |
||
| 35 | |||
| 36 | // Adding this action so that on login_init, the action won't be sanitized out of the $action global. |
||
| 37 | add_action( 'login_form_jetpack-sso', '__return_true' ); |
||
| 38 | |||
| 39 | if ( |
||
| 40 | $this->should_hide_login_form() && |
||
| 41 | /** |
||
| 42 | * Filter the display of the disclaimer message appearing when default WordPress login form is disabled. |
||
| 43 | * |
||
| 44 | * @module sso |
||
| 45 | * |
||
| 46 | * @since 2.8.0 |
||
| 47 | * |
||
| 48 | * @param bool true Should the disclaimer be displayed. Default to true. |
||
| 49 | */ |
||
| 50 | apply_filters( 'jetpack_sso_display_disclaimer', true ) |
||
| 51 | ) { |
||
| 52 | add_action( 'login_message', array( $this, 'msg_login_by_jetpack' ) ); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Returns the single instance of the Jetpack_SSO object |
||
| 58 | * |
||
| 59 | * @since 2.8 |
||
| 60 | * @return Jetpack_SSO |
||
| 61 | **/ |
||
| 62 | public static function get_instance() { |
||
| 63 | if ( ! is_null( self::$instance ) ) { |
||
| 64 | return self::$instance; |
||
| 65 | } |
||
| 66 | |||
| 67 | return self::$instance = new Jetpack_SSO; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Add configure button and functionality to the module card on the Jetpack screen |
||
| 72 | **/ |
||
| 73 | public static function module_configure_button() { |
||
| 74 | Jetpack::enable_module_configurable( __FILE__ ); |
||
| 75 | Jetpack::module_configuration_load( __FILE__, array( __CLASS__, 'module_configuration_load' ) ); |
||
| 76 | Jetpack::module_configuration_head( __FILE__, array( __CLASS__, 'module_configuration_head' ) ); |
||
| 77 | Jetpack::module_configuration_screen( __FILE__, array( __CLASS__, 'module_configuration_screen' ) ); |
||
| 78 | } |
||
| 79 | |||
| 80 | public static function module_configuration_load() {} |
||
| 81 | |||
| 82 | public static function module_configuration_head() {} |
||
| 83 | |||
| 84 | public static function module_configuration_screen() { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * If jetpack_force_logout == 1 in current user meta the user will be forced |
||
| 96 | * to logout and reauthenticate with the site. |
||
| 97 | **/ |
||
| 98 | public function maybe_logout_user() { |
||
| 99 | global $current_user; |
||
| 100 | |||
| 101 | if ( 1 == $current_user->jetpack_force_logout ) { |
||
| 102 | delete_user_meta( $current_user->ID, 'jetpack_force_logout' ); |
||
| 103 | self::delete_connection_for_user( $current_user->ID ); |
||
| 104 | wp_logout(); |
||
| 105 | wp_safe_redirect( wp_login_url() ); |
||
| 106 | exit; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * Adds additional methods the WordPress xmlrpc API for handling SSO specific features |
||
| 113 | * |
||
| 114 | * @param array $methods |
||
| 115 | * @return array |
||
| 116 | **/ |
||
| 117 | public function xmlrpc_methods( $methods ) { |
||
| 118 | $methods['jetpack.userDisconnect'] = array( $this, 'xmlrpc_user_disconnect' ); |
||
| 119 | return $methods; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Marks a user's profile for disconnect from WordPress.com and forces a logout |
||
| 124 | * the next time the user visits the site. |
||
| 125 | **/ |
||
| 126 | public function xmlrpc_user_disconnect( $user_id ) { |
||
| 127 | $user_query = new WP_User_Query( |
||
| 128 | array( |
||
| 129 | 'meta_key' => 'wpcom_user_id', |
||
| 130 | 'meta_value' => $user_id, |
||
| 131 | ) |
||
| 132 | ); |
||
| 133 | $user = $user_query->get_results(); |
||
| 134 | $user = $user[0]; |
||
| 135 | |||
| 136 | if ( $user instanceof WP_User ) { |
||
| 137 | $user = wp_set_current_user( $user->ID ); |
||
| 138 | update_user_meta( $user->ID, 'jetpack_force_logout', '1' ); |
||
| 139 | self::delete_connection_for_user( $user->ID ); |
||
| 140 | return true; |
||
| 141 | } |
||
| 142 | return false; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Enqueues scripts and styles necessary for SSO login. |
||
| 147 | */ |
||
| 148 | public function login_enqueue_scripts() { |
||
| 149 | global $action; |
||
| 150 | |||
| 151 | if ( ! in_array( $action, array( 'jetpack-sso', 'login' ) ) ) { |
||
| 152 | return; |
||
| 153 | } |
||
| 154 | |||
| 155 | wp_enqueue_style( 'jetpack-sso-login', plugins_url( 'modules/sso/jetpack-sso-login.css', JETPACK__PLUGIN_FILE ), array( 'login', 'genericons' ), JETPACK__VERSION ); |
||
| 156 | wp_enqueue_script( 'jetpack-sso-login', plugins_url( 'modules/sso/jetpack-sso-login.js', JETPACK__PLUGIN_FILE ), array( 'jquery' ), JETPACK__VERSION ); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Enqueue styles neceessary for Jetpack SSO on users' profiles |
||
| 161 | */ |
||
| 162 | public function admin_enqueue_scripts() { |
||
| 163 | $screen = get_current_screen(); |
||
| 164 | |||
| 165 | if ( empty( $screen ) || ! in_array( $screen->base, array( 'edit-user', 'profile' ) ) ) { |
||
| 166 | return; |
||
| 167 | } |
||
| 168 | |||
| 169 | wp_enqueue_style( 'jetpack-sso-profile', plugins_url( 'modules/sso/jetpack-sso-profile.css', JETPACK__PLUGIN_FILE ), array( 'genericons' ), JETPACK__VERSION ); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Adds Jetpack SSO classes to login body |
||
| 174 | * |
||
| 175 | * @param array $classes Array of classes to add to body tag |
||
| 176 | * @return array Array of classes to add to body tag |
||
| 177 | */ |
||
| 178 | public function login_body_class( $classes ) { |
||
| 179 | global $action; |
||
| 180 | |||
| 181 | if ( ! in_array( $action, array( 'jetpack-sso', 'login' ) ) ) { |
||
| 182 | return $classes; |
||
| 183 | } |
||
| 184 | |||
| 185 | // If jetpack-sso-default-form, show the default login form. |
||
| 186 | if ( isset( $_GET['jetpack-sso-default-form'] ) && 1 == $_GET['jetpack-sso-default-form'] ) { |
||
| 187 | return $classes; |
||
| 188 | } |
||
| 189 | |||
| 190 | $classes[] = 'jetpack-sso-body'; |
||
| 191 | return $classes; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Adds settings fields to Settings > General > Single Sign On that allows users to |
||
| 196 | * turn off the login form on wp-login.php |
||
| 197 | * |
||
| 198 | * @since 2.7 |
||
| 199 | **/ |
||
| 200 | public function register_settings() { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Builds the display for the checkbox allowing user to require two step |
||
| 247 | * auth be enabled on WordPress.com accounts before login. Displays in Settings > General |
||
| 248 | * |
||
| 249 | * @since 2.7 |
||
| 250 | **/ |
||
| 251 | public function render_require_two_step() { |
||
| 252 | /** This filter is documented in modules/sso.php */ |
||
| 253 | $require_two_step = ( 1 == apply_filters( 'jetpack_sso_require_two_step', get_option( 'jetpack_sso_require_two_step' ) ) ); |
||
| 254 | $disabled = $require_two_step ? ' disabled="disabled"' : ''; |
||
| 255 | echo '<label>'; |
||
| 256 | echo '<input type="checkbox" name="jetpack_sso_require_two_step" ' . checked( $require_two_step, true, false ) . "$disabled>"; |
||
| 257 | esc_html_e( 'Require Two-Step Authentication' , 'jetpack' ); |
||
| 258 | echo '</label>'; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Validate the require two step checkbox in Settings > General |
||
| 263 | * |
||
| 264 | * @since 2.7 |
||
| 265 | * @return boolean |
||
| 266 | **/ |
||
| 267 | public function validate_jetpack_sso_require_two_step( $input ) { |
||
| 268 | return ( ! empty( $input ) ) ? 1 : 0; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Builds the display for the checkbox allowing the user to allow matching logins by email |
||
| 273 | * Displays in Settings > General |
||
| 274 | * |
||
| 275 | * @since 2.9 |
||
| 276 | **/ |
||
| 277 | public function render_match_by_email() { |
||
| 278 | $match_by_email = 1 == $this->match_by_email(); |
||
| 279 | $disabled = $match_by_email ? ' disabled="disabled"' : ''; |
||
| 280 | echo '<label>'; |
||
| 281 | echo '<input type="checkbox" name="jetpack_sso_match_by_email"' . checked( $match_by_email, true, false ) . "$disabled>"; |
||
| 282 | esc_html_e( 'Match by Email', 'jetpack' ); |
||
| 283 | echo '</label>'; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Validate the match by email check in Settings > General |
||
| 288 | * |
||
| 289 | * @since 2.9 |
||
| 290 | * @return boolean |
||
| 291 | **/ |
||
| 292 | public function validate_jetpack_sso_match_by_email( $input ) { |
||
| 293 | return ( ! empty( $input ) ) ? 1 : 0; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Checks to determine if the user wants to login on wp-login |
||
| 298 | * |
||
| 299 | * This function mostly exists to cover the exceptions to login |
||
| 300 | * that may exist as other parameters to $_GET[action] as $_GET[action] |
||
| 301 | * does not have to exist. By default WordPress assumes login if an action |
||
| 302 | * is not set, however this may not be true, as in the case of logout |
||
| 303 | * where $_GET[loggedout] is instead set |
||
| 304 | * |
||
| 305 | * @return boolean |
||
| 306 | **/ |
||
| 307 | private function wants_to_login() { |
||
| 322 | |||
| 323 | private function bypass_login_forward_wpcom() { |
||
| 335 | |||
| 336 | function login_init() { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Ensures that we can get a nonce from WordPress.com via XML-RPC before setting |
||
| 387 | * up the hooks required to display the SSO form. |
||
| 388 | */ |
||
| 389 | public function display_sso_login_form() { |
||
| 390 | $sso_nonce = self::request_initial_nonce(); |
||
| 391 | if ( is_wp_error( $sso_nonce ) ) { |
||
| 392 | return; |
||
| 393 | } |
||
| 394 | |||
| 395 | add_action( 'login_form', array( $this, 'login_form' ) ); |
||
| 396 | add_filter( 'login_body_class', array( $this, 'login_body_class' ) ); |
||
| 397 | add_action( 'login_enqueue_scripts', array( $this, 'login_enqueue_scripts' ) ); |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Conditionally save the redirect_to url as a cookie. |
||
| 402 | */ |
||
| 403 | public static function maybe_save_cookie_redirect() { |
||
| 404 | if ( headers_sent() ) { |
||
| 405 | return new WP_Error( 'headers_sent', __( 'Cannot deal with cookie redirects, as headers are already sent.', 'jetpack' ) ); |
||
| 406 | } |
||
| 407 | |||
| 408 | if ( ! empty( $_GET['redirect_to'] ) ) { |
||
| 409 | // If we have something to redirect to |
||
| 410 | $url = esc_url_raw( $_GET['redirect_to'] ); |
||
| 411 | setcookie( 'jetpack_sso_redirect_to', $url, time() + HOUR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, false, true ); |
||
| 412 | |||
| 413 | } elseif ( ! empty( $_COOKIE['jetpack_sso_redirect_to'] ) ) { |
||
| 414 | // Otherwise, if it's already set, purge it. |
||
| 415 | setcookie( 'jetpack_sso_redirect_to', ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); |
||
| 416 | } |
||
| 417 | |||
| 418 | if ( ! empty( $_GET['rememberme'] ) ) { |
||
| 419 | setcookie( 'jetpack_sso_remember_me', '1', time() + HOUR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, false, true ); |
||
| 420 | } elseif ( ! empty( $_COOKIE['jetpack_sso_remember_me'] ) ) { |
||
| 421 | setcookie( 'jetpack_sso_remember_me', ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN ); |
||
| 422 | } |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Determine if the login form should be hidden or not |
||
| 427 | * |
||
| 428 | * Method is private only because it is only used in this class so far. |
||
| 429 | * Feel free to change it later |
||
| 430 | * |
||
| 431 | * @return bool |
||
| 432 | **/ |
||
| 433 | private function should_hide_login_form() { |
||
| 434 | /** |
||
| 435 | * Remove the default log in form, only leave the WordPress.com log in button. |
||
| 436 | * |
||
| 437 | * @module sso |
||
| 438 | * |
||
| 439 | * @since 3.1.0 |
||
| 440 | * |
||
| 441 | * @param bool get_option( 'jetpack_sso_remove_login_form', false ) Should the default log in form be removed. Default to false. |
||
| 442 | */ |
||
| 443 | return apply_filters( 'jetpack_remove_login_form', get_option( 'jetpack_sso_remove_login_form', false ) ); |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Outputs the Jetpack SSO button and description as well as the toggle link |
||
| 448 | * for switching between Jetpack SSO and default login. |
||
| 449 | */ |
||
| 450 | function login_form() { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Clear the cookies that store the profile information for the last |
||
| 526 | * WPCOM user to connect. |
||
| 527 | */ |
||
| 528 | static function clear_wpcom_profile_cookies() { |
||
| 549 | |||
| 550 | static function delete_connection_for_user( $user_id ) { |
||
| 571 | |||
| 572 | View Code Duplication | static function request_initial_nonce() { |
|
| 573 | Jetpack::load_xml_rpc_client(); |
||
| 574 | $xml = new Jetpack_IXR_Client( array( |
||
| 575 | 'user_id' => get_current_user_id(), |
||
| 576 | ) ); |
||
| 577 | $xml->query( 'jetpack.sso.requestNonce' ); |
||
| 578 | |||
| 579 | if ( $xml->isError() ) { |
||
| 580 | return new WP_Error( $xml->getErrorCode(), $xml->getErrorMessage() ); |
||
| 581 | } |
||
| 582 | |||
| 583 | return $xml->getResponse(); |
||
| 584 | } |
||
| 585 | |||
| 586 | /** |
||
| 587 | * The function that actually handles the login! |
||
| 588 | */ |
||
| 589 | function handle_login() { |
||
| 779 | |||
| 780 | static function profile_page_url() { |
||
| 781 | return admin_url( 'profile.php' ); |
||
| 782 | } |
||
| 783 | |||
| 784 | static function match_by_email() { |
||
| 785 | $match_by_email = ( 1 == get_option( 'jetpack_sso_match_by_email', true ) ) ? true: false; |
||
| 786 | $match_by_email = defined( 'WPCC_MATCH_BY_EMAIL' ) ? WPCC_MATCH_BY_EMAIL : $match_by_email; |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Link the local account to an account on WordPress.com using the same email address. |
||
| 790 | * |
||
| 791 | * @module sso |
||
| 792 | * |
||
| 793 | * @since 2.6.0 |
||
| 794 | * |
||
| 795 | * @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. |
||
| 796 | */ |
||
| 797 | return apply_filters( 'jetpack_sso_match_by_email', $match_by_email ); |
||
| 798 | } |
||
| 799 | |||
| 800 | static function new_user_override() { |
||
| 801 | $new_user_override = defined( 'WPCC_NEW_USER_OVERRIDE' ) ? WPCC_NEW_USER_OVERRIDE : false; |
||
| 802 | |||
| 803 | /** |
||
| 804 | * Allow users to register on your site with a WordPress.com account, even though you disallow normal registrations. |
||
| 805 | * |
||
| 806 | * @module sso |
||
| 807 | * |
||
| 808 | * @since 2.6.0 |
||
| 809 | * |
||
| 810 | * @param bool $new_user_override Allow users to register on your site with a WordPress.com account. Default to false. |
||
| 811 | */ |
||
| 812 | return apply_filters( 'jetpack_sso_new_user_override', $new_user_override ); |
||
| 813 | } |
||
| 814 | |||
| 815 | function allowed_redirect_hosts( $hosts ) { |
||
| 816 | if ( empty( $hosts ) ) { |
||
| 817 | $hosts = array(); |
||
| 818 | } |
||
| 819 | $hosts[] = 'wordpress.com'; |
||
| 820 | $hosts[] = 'jetpack.wordpress.com'; |
||
| 821 | |||
| 822 | return array_unique( $hosts ); |
||
| 823 | } |
||
| 824 | |||
| 825 | /** |
||
| 826 | * Builds the "Login to WordPress.com" button that is displayed on the login page as well as user profile page. |
||
| 827 | * |
||
| 828 | * @param array $args An array of arguments to add to the SSO URL. |
||
| 829 | * @param boolean $is_primary Should the button have the `button-primary` class? |
||
| 830 | * @return string Returns the HTML markup for the button. |
||
| 831 | */ |
||
| 832 | function build_sso_button( $args = array(), $is_primary = false ) { |
||
| 833 | $url = $this->build_sso_button_url( $args ); |
||
| 834 | $classes = $is_primary |
||
| 835 | ? 'jetpack-sso button button-primary' |
||
| 836 | : 'jetpack-sso button'; |
||
| 837 | |||
| 838 | return sprintf( |
||
| 839 | '<a href="%1$s" class="%2$s">%3$s</a>', |
||
| 840 | esc_url( $url ), |
||
| 841 | $classes, |
||
| 842 | esc_html__( 'Log in with WordPress.com', 'jetpack' ) |
||
| 843 | ); |
||
| 844 | } |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Builds a URL with `jetpack-sso` action and option args which is used to setup SSO. |
||
| 848 | * |
||
| 849 | * @param array $args An array of arguments to add to the SSO URL. |
||
| 850 | * @return string The URL used for SSO. |
||
| 851 | */ |
||
| 852 | function build_sso_button_url( $args = array() ) { |
||
| 853 | $defaults = array( |
||
| 854 | 'action' => 'jetpack-sso', |
||
| 855 | ); |
||
| 856 | |||
| 857 | $args = wp_parse_args( $args, $defaults ); |
||
| 858 | |||
| 859 | if ( ! empty( $_GET['redirect_to'] ) ) { |
||
| 860 | $args['redirect_to'] = esc_url_raw( $_GET['redirect_to'] ); |
||
| 861 | } |
||
| 862 | |||
| 863 | return add_query_arg( $args, wp_login_url() ); |
||
| 864 | } |
||
| 865 | |||
| 866 | /** |
||
| 867 | * Retrieves a WordPress.com SSO URL with appropriate query parameters or dies. |
||
| 868 | * |
||
| 869 | * @param boolean $reauth Should the user be forced to reauthenticate on WordPress.com? |
||
| 870 | * @param array $args Optional query parameters. |
||
| 871 | * @return string The WordPress.com SSO URL. |
||
| 872 | */ |
||
| 873 | function get_sso_url_or_die( $reauth = false, $args = array() ) { |
||
| 874 | if ( empty( $reauth ) ) { |
||
| 875 | $sso_redirect = $this->build_sso_url( $args ); |
||
| 876 | } else { |
||
| 877 | self::clear_wpcom_profile_cookies(); |
||
| 878 | $sso_redirect = $this->build_reauth_and_sso_url( $args ); |
||
| 879 | } |
||
| 880 | |||
| 881 | // If there was an error retrieving the SSO URL, then error. |
||
| 882 | if ( is_wp_error( $sso_redirect ) ) { |
||
| 883 | wp_die( sprintf( '%s: %s', $sso_redirect->get_error_code(), $sso_redirect->get_error_message() ) ); |
||
| 884 | } |
||
| 885 | |||
| 886 | return $sso_redirect; |
||
| 887 | } |
||
| 888 | |||
| 889 | /** |
||
| 890 | * Build WordPress.com SSO URL with appropriate query parameters. |
||
| 891 | * |
||
| 892 | * @param array $args Optional query parameters. |
||
| 893 | * @return string WordPress.com SSO URL |
||
| 894 | */ |
||
| 895 | function build_sso_url( $args = array() ) { |
||
| 896 | $sso_nonce = ! empty( $args['sso_nonce'] ) ? $args['sso_nonce'] : self::request_initial_nonce(); |
||
| 897 | $defaults = array( |
||
| 898 | 'action' => 'jetpack-sso', |
||
| 899 | 'site_id' => Jetpack_Options::get_option( 'id' ), |
||
| 900 | 'sso_nonce' => $sso_nonce, |
||
| 901 | ); |
||
| 902 | |||
| 903 | $args = wp_parse_args( $args, $defaults ); |
||
| 904 | |||
| 905 | if ( is_wp_error( $args['sso_nonce'] ) ) { |
||
| 906 | return $args['sso_nonce']; |
||
| 907 | } |
||
| 908 | |||
| 909 | return add_query_arg( $args, 'https://wordpress.com/wp-login.php' ); |
||
| 910 | } |
||
| 911 | |||
| 912 | /** |
||
| 913 | * Build WordPress.com SSO URL with appropriate query parameters, |
||
| 914 | * including the parameters necessary to force the user to reauthenticate |
||
| 915 | * on WordPress.com. |
||
| 916 | * |
||
| 917 | * @param array $args Optional query parameters. |
||
| 918 | * @return string WordPress.com SSO URL |
||
| 919 | */ |
||
| 920 | function build_reauth_and_sso_url( $args = array() ) { |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Determines local user associated with a given WordPress.com user ID. |
||
| 947 | * |
||
| 948 | * @since 2.6.0 |
||
| 949 | * |
||
| 950 | * @param int $wpcom_user_id User ID from WordPress.com |
||
| 951 | * @return object Local user object if found, null if not. |
||
| 952 | */ |
||
| 953 | static function get_user_by_wpcom_id( $wpcom_user_id ) { |
||
| 954 | $user_query = new WP_User_Query( array( |
||
| 955 | 'meta_key' => 'wpcom_user_id', |
||
| 956 | 'meta_value' => intval( $wpcom_user_id ), |
||
| 957 | 'number' => 1, |
||
| 958 | ) ); |
||
| 959 | |||
| 960 | $users = $user_query->get_results(); |
||
| 961 | return $users ? array_shift( $users ) : null; |
||
| 962 | } |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Error message displayed on the login form when two step is required and |
||
| 966 | * the user's account on WordPress.com does not have two step enabled. |
||
| 967 | * |
||
| 968 | * @since 2.7 |
||
| 969 | * @param string $message |
||
| 970 | * @return string |
||
| 971 | **/ |
||
| 972 | public function error_msg_enable_two_step( $message ) { |
||
| 973 | $err = __( sprintf( 'This site requires two step authentication be enabled for your user account on WordPress.com. Please visit the <a href="%1$s" target="_blank"> Security Settings</a> page to enable two step', 'https://wordpress.com/me/security/two-step' ) , 'jetpack' ); |
||
| 974 | |||
| 975 | $message .= sprintf( '<p class="message" id="login_error">%s</p>', $err ); |
||
| 976 | |||
| 977 | return $message; |
||
| 978 | } |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Error message displayed when the user tries to SSO, but match by email |
||
| 982 | * is off and they already have an account with their email address on |
||
| 983 | * this site. |
||
| 984 | * |
||
| 985 | * @param string $message |
||
| 986 | * @return string |
||
| 987 | */ |
||
| 988 | public function error_msg_email_already_exists( $message ) { |
||
| 989 | $err = __( sprintf( 'You already have an account on this site. Please visit your <a href="%1$s">profile page</a> page to link your account to WordPress.com!', admin_url( 'profile.php' ) ) , 'jetpack' ); |
||
| 990 | |||
| 991 | $message .= sprintf( '<p class="message" id="login_error">%s</p>', $err ); |
||
| 992 | |||
| 993 | return $message; |
||
| 994 | } |
||
| 995 | |||
| 996 | /** |
||
| 997 | * Message displayed when the site admin has disabled the default WordPress |
||
| 998 | * login form in Settings > General > Single Sign On |
||
| 999 | * |
||
| 1000 | * @since 2.7 |
||
| 1001 | * @param string $message |
||
| 1002 | * @return string |
||
| 1003 | **/ |
||
| 1004 | public function msg_login_by_jetpack( $message ) { |
||
| 1005 | |||
| 1006 | $msg = __( sprintf( 'Jetpack authenticates through WordPress.com — to log in, enter your WordPress.com username and password, or <a href="%1$s" target="_blank">visit WordPress.com</a> to create a free account now.', 'http://wordpress.com/signup' ) , 'jetpack' ); |
||
| 1007 | |||
| 1008 | /** |
||
| 1009 | * Filter the message displayed when the default WordPress login form is disabled. |
||
| 1010 | * |
||
| 1011 | * @module sso |
||
| 1012 | * |
||
| 1013 | * @since 2.8.0 |
||
| 1014 | * |
||
| 1015 | * @param string $msg Disclaimer when default WordPress login form is disabled. |
||
| 1016 | */ |
||
| 1017 | $msg = apply_filters( 'jetpack_sso_disclaimer_message', $msg ); |
||
| 1018 | |||
| 1019 | $message .= sprintf( '<p class="message">%s</p>', $msg ); |
||
| 1020 | return $message; |
||
| 1021 | } |
||
| 1022 | |||
| 1023 | /** |
||
| 1024 | * Error message displayed on the login form when the user attempts |
||
| 1025 | * to post to the login form and it is disabled. |
||
| 1026 | * |
||
| 1027 | * @since 2.8 |
||
| 1028 | * @param string $message |
||
| 1029 | * @param string |
||
| 1030 | **/ |
||
| 1031 | public function error_msg_login_method_not_allowed( $message ) { |
||
| 1032 | $err = __( 'Login method not allowed' , 'jetpack' ); |
||
| 1033 | $message .= sprintf( '<p class="message" id="login_error">%s</p>', $err ); |
||
| 1034 | |||
| 1035 | return $message; |
||
| 1036 | } |
||
| 1037 | function cant_find_user( $message ) { |
||
| 1038 | if ( self::match_by_email() ) { |
||
| 1039 | $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' ); |
||
| 1040 | } else { |
||
| 1041 | $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' ); |
||
| 1042 | } |
||
| 1043 | $err = sprintf( $err_format, $this->user_data->email, get_bloginfo( 'name' ) ); |
||
| 1044 | $message .= sprintf( '<p class="message" id="login_error">%s</p>', $err ); |
||
| 1045 | return $message; |
||
| 1046 | } |
||
| 1047 | |||
| 1048 | /** |
||
| 1049 | * When jetpack-sso-auth-redirect query parameter is set, will redirect user to |
||
| 1050 | * WordPress.com authorization flow. |
||
| 1051 | * |
||
| 1052 | * We redirect here instead of in handle_login() because Jetpack::init()->build_connect_url |
||
| 1053 | * calls menu_page_url() which doesn't work properly until admin menus are registered. |
||
| 1054 | */ |
||
| 1055 | function maybe_authorize_user_after_sso() { |
||
| 1082 | |||
| 1083 | /** |
||
| 1084 | * Cache user's display name and Gravatar so it can be displayed on the login screen. These cookies are |
||
| 1085 | * stored when the user logs out, and then deleted when the user logs in. |
||
| 1086 | */ |
||
| 1087 | function store_wpcom_profile_cookies_on_logout() { |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * Determines if a local user is connected to WordPress.com |
||
| 1119 | * |
||
| 1120 | * @since 2.8 |
||
| 1121 | * @param integer $user_id - Local user id |
||
| 1122 | * @return boolean |
||
| 1123 | **/ |
||
| 1124 | public function is_user_connected( $user_id ) { |
||
| 1125 | return $this->get_user_data( $user_id ); |
||
| 1126 | } |
||
| 1127 | |||
| 1128 | /** |
||
| 1129 | * Retrieves a user's WordPress.com data |
||
| 1130 | * |
||
| 1131 | * @since 2.8 |
||
| 1132 | * @param integer $user_id - Local user id |
||
| 1133 | * @return mixed null or stdClass |
||
| 1134 | **/ |
||
| 1135 | public function get_user_data( $user_id ) { |
||
| 1136 | return get_user_meta( $user_id, 'wpcom_user_data', true ); |
||
| 1137 | } |
||
| 1138 | } |
||
| 1139 | |||
| 1140 | Jetpack_SSO::get_instance(); |
||
| 1141 |
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.