- is_user_connected() ) {
- Jetpack_SSO_Helpers::delete_connection_for_user( get_current_user_id() );
- }
- }
-
- /**
- * Retrieves nonce used for SSO form.
- *
- * @return string|WP_Error
- */
- public static function request_initial_nonce() {
- $nonce = ! empty( $_COOKIE['jetpack_sso_nonce'] )
- ? sanitize_key( wp_unslash( $_COOKIE['jetpack_sso_nonce'] ) )
- : false;
-
- if ( ! $nonce ) {
- $xml = new Jetpack_IXR_Client();
- $xml->query( 'jetpack.sso.requestNonce' );
-
- if ( $xml->isError() ) {
- return new WP_Error( $xml->getErrorCode(), $xml->getErrorMessage() );
- }
-
- $nonce = sanitize_key( $xml->getResponse() );
-
- setcookie(
- 'jetpack_sso_nonce',
- $nonce,
- time() + ( 10 * MINUTE_IN_SECONDS ),
- COOKIEPATH,
- COOKIE_DOMAIN,
- is_ssl(),
- true
- );
- }
-
- return $nonce;
- }
-
- /**
- * The function that actually handles the login!
- */
- public function handle_login() {
- $wpcom_nonce = isset( $_GET['sso_nonce'] ) ? sanitize_key( $_GET['sso_nonce'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
- $wpcom_user_id = isset( $_GET['user_id'] ) ? (int) $_GET['user_id'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
-
- $xml = new Jetpack_IXR_Client();
- $xml->query( 'jetpack.sso.validateResult', $wpcom_nonce, $wpcom_user_id );
-
- $user_data = $xml->isError() ? false : $xml->getResponse();
- if ( empty( $user_data ) ) {
- add_filter( 'jetpack_sso_default_to_sso_login', '__return_false' );
- add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'error_invalid_response_data' ) );
- return;
- }
-
- $user_data = (object) $user_data;
- $user = null;
-
- /**
- * Fires before Jetpack's SSO modifies the log in form.
- *
- * @module sso
- *
- * @since 2.6.0
- *
- * @param object $user_data WordPress.com User information.
- */
- do_action( 'jetpack_sso_pre_handle_login', $user_data );
-
- $tracking = new Tracking();
-
- if ( Jetpack_SSO_Helpers::is_two_step_required() && 0 === (int) $user_data->two_step_enabled ) {
- $this->user_data = $user_data;
-
- $tracking->record_user_event(
- 'sso_login_failed',
- array(
- 'error_message' => 'error_msg_enable_two_step',
- )
- );
-
- $error = new WP_Error( 'two_step_required', __( 'You must have Two-Step Authentication enabled on your WordPress.com account.', 'jetpack' ) );
-
- /** This filter is documented in core/src/wp-includes/pluggable.php */
- do_action( 'wp_login_failed', $user_data->login, $error );
- add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'error_msg_enable_two_step' ) );
- return;
- }
-
- $user_found_with = '';
- if ( empty( $user ) && isset( $user_data->external_user_id ) ) {
- $user_found_with = 'external_user_id';
- $user = get_user_by( 'id', (int) $user_data->external_user_id );
- if ( $user ) {
- $expected_id = get_user_meta( $user->ID, 'wpcom_user_id', true );
- if ( $expected_id && $expected_id != $user_data->ID ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison, Universal.Operators.StrictComparisons.LooseNotEqual
- $error = new WP_Error( 'expected_wpcom_user', __( 'Something got a little mixed up and an unexpected WordPress.com user logged in.', 'jetpack' ) );
-
- $tracking->record_user_event(
- 'sso_login_failed',
- array(
- 'error_message' => 'error_unexpected_wpcom_user',
- )
- );
-
- /** This filter is documented in core/src/wp-includes/pluggable.php */
- do_action( 'wp_login_failed', $user_data->login, $error );
- add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'error_invalid_response_data' ) ); // @todo Need to have a better notice. This is only for the sake of testing the validation.
- return;
- }
- update_user_meta( $user->ID, 'wpcom_user_id', $user_data->ID );
- }
- }
-
- // If we don't have one by wpcom_user_id, try by the email?
- if ( empty( $user ) && Jetpack_SSO_Helpers::match_by_email() ) {
- $user_found_with = 'match_by_email';
- $user = get_user_by( 'email', $user_data->email );
- if ( $user ) {
- update_user_meta( $user->ID, 'wpcom_user_id', $user_data->ID );
- }
- }
-
- // If we've still got nothing, create the user.
- $new_user_override_role = Jetpack_SSO_Helpers::new_user_override( $user_data );
- if ( empty( $user ) && ( get_option( 'users_can_register' ) || $new_user_override_role ) ) {
- /**
- * If not matching by email we still need to verify the email does not exist
- * or this blows up
- *
- * If match_by_email is true, we know the email doesn't exist, as it would have
- * been found in the first pass. If get_user_by( 'email' ) doesn't find the
- * user, then we know that email is unused, so it's safe to add.
- */
- if ( Jetpack_SSO_Helpers::match_by_email() || ! get_user_by( 'email', $user_data->email ) ) {
-
- if ( $new_user_override_role ) {
- $user_data->role = $new_user_override_role;
- }
-
- $user = Jetpack_SSO_Helpers::generate_user( $user_data );
- if ( ! $user ) {
- $tracking->record_user_event(
- 'sso_login_failed',
- array(
- 'error_message' => 'could_not_create_username',
- )
- );
- add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'error_unable_to_create_user' ) );
- return;
- }
-
- $user_found_with = $new_user_override_role
- ? 'user_created_new_user_override'
- : 'user_created_users_can_register';
- } else {
- $tracking->record_user_event(
- 'sso_login_failed',
- array(
- 'error_message' => 'error_msg_email_already_exists',
- )
- );
-
- $this->user_data = $user_data;
- add_action( 'login_message', array( 'Jetpack_SSO_Notices', 'error_msg_email_already_exists' ) );
- return;
- }
- }
-
- /**
- * Fires after we got login information from WordPress.com.
- *
- * @module sso
- *
- * @since 2.6.0
- *
- * @param WP_User|false|null $user Local User information.
- * @param object $user_data WordPress.com User Login information.
- */
- do_action( 'jetpack_sso_handle_login', $user, $user_data );
-
- if ( $user ) {
- // Cache the user's details, so we can present it back to them on their user screen.
- update_user_meta( $user->ID, 'wpcom_user_data', $user_data );
-
- add_filter( 'auth_cookie_expiration', array( 'Jetpack_SSO_Helpers', 'extend_auth_cookie_expiration_for_sso' ) );
- wp_set_auth_cookie( $user->ID, true );
- remove_filter( 'auth_cookie_expiration', array( 'Jetpack_SSO_Helpers', 'extend_auth_cookie_expiration_for_sso' ) );
-
- /** This filter is documented in core/src/wp-includes/user.php */
- do_action( 'wp_login', $user->user_login, $user );
-
- wp_set_current_user( $user->ID );
-
- $_request_redirect_to = isset( $_REQUEST['redirect_to'] ) ? esc_url_raw( wp_unslash( $_REQUEST['redirect_to'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
- $redirect_to = user_can( $user, 'edit_posts' ) ? admin_url() : self::profile_page_url();
-
- // If we have a saved redirect to request in a cookie.
- if ( ! empty( $_COOKIE['jetpack_sso_redirect_to'] ) ) {
- // Set that as the requested redirect to.
- $redirect_to = esc_url_raw( wp_unslash( $_COOKIE['jetpack_sso_redirect_to'] ) );
- $_request_redirect_to = $redirect_to;
- }
-
- $json_api_auth_environment = Jetpack_SSO_Helpers::get_json_api_auth_environment();
-
- $is_json_api_auth = ! empty( $json_api_auth_environment );
- $is_user_connected = Jetpack_SSO_Helpers::is_user_connected( $user->ID );
- $roles = new Roles();
- $tracking->record_user_event(
- 'sso_user_logged_in',
- array(
- 'user_found_with' => $user_found_with,
- 'user_connected' => (bool) $is_user_connected,
- 'user_role' => $roles->translate_current_user_to_role(),
- 'is_json_api_auth' => (bool) $is_json_api_auth,
- )
- );
-
- if ( $is_json_api_auth ) {
- $authorize_json_api = new Authorize_Json_Api();
- $authorize_json_api->verify_json_api_authorization_request( $json_api_auth_environment );
- $authorize_json_api->store_json_api_authorization_token( $user->user_login, $user );
-
- } elseif ( ! $is_user_connected ) {
- wp_safe_redirect(
- add_query_arg(
- array(
- 'redirect_to' => $redirect_to,
- 'request_redirect_to' => $_request_redirect_to,
- 'calypso_env' => ( new Host() )->get_calypso_env(),
- 'jetpack-sso-auth-redirect' => '1',
- ),
- admin_url()
- )
- );
- exit;
- }
-
- add_filter( 'allowed_redirect_hosts', array( 'Jetpack_SSO_Helpers', 'allowed_redirect_hosts' ) );
- wp_safe_redirect(
- /** This filter is documented in core/src/wp-login.php */
- apply_filters( 'login_redirect', $redirect_to, $_request_redirect_to, $user )
- );
- exit;
- }
-
- add_filter( 'jetpack_sso_default_to_sso_login', '__return_false' );
-
- $tracking->record_user_event(
- 'sso_login_failed',
- array(
- 'error_message' => 'cant_find_user',
- )
- );
-
- $this->user_data = $user_data;
-
- $error = new WP_Error( 'account_not_found', __( 'Account not found. If you already have an account, make sure you have connected to WordPress.com.', 'jetpack' ) );
-
- /** This filter is documented in core/src/wp-includes/pluggable.php */
- do_action( 'wp_login_failed', $user_data->login, $error );
- add_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'cant_find_user' ) );
- }
-
- /**
- * Retrieve the admin profile page URL.
- */
- public static function profile_page_url() {
- return admin_url( 'profile.php' );
- }
-
- /**
- * Builds the "Login to WordPress.com" button that is displayed on the login page as well as user profile page.
- *
- * @param array $args An array of arguments to add to the SSO URL.
- * @param boolean $is_primary If the button have the `button-primary` class.
- * @return string Returns the HTML markup for the button.
- */
- public function build_sso_button( $args = array(), $is_primary = false ) {
- $url = $this->build_sso_button_url( $args );
- $classes = $is_primary
- ? 'jetpack-sso button button-primary'
- : 'jetpack-sso button';
-
- return sprintf(
- '%3$s %4$s',
- esc_url( $url ),
- $classes,
- '',
- esc_html__( 'Log in with WordPress.com', 'jetpack' )
- );
- }
-
- /**
- * Builds a URL with `jetpack-sso` action and option args which is used to setup SSO.
- *
- * @param array $args An array of arguments to add to the SSO URL.
- * @return string The URL used for SSO.
- */
- public function build_sso_button_url( $args = array() ) {
- $defaults = array(
- 'action' => 'jetpack-sso',
- );
-
- $args = wp_parse_args( $args, $defaults );
-
- if ( ! empty( $_GET['redirect_to'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
- $args['redirect_to'] = rawurlencode( esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
- }
-
- return add_query_arg( $args, wp_login_url() );
- }
-
- /**
- * Retrieves a WordPress.com SSO URL with appropriate query parameters or dies.
- *
- * @param boolean $reauth If the user be forced to reauthenticate on WordPress.com.
- * @param array $args Optional query parameters.
- * @return string The WordPress.com SSO URL.
- */
- public function get_sso_url_or_die( $reauth = false, $args = array() ) {
- $custom_login_url = Jetpack_SSO_Helpers::get_custom_login_url();
- if ( $custom_login_url ) {
- $args['login_url'] = rawurlencode( $custom_login_url );
- }
-
- if ( empty( $reauth ) ) {
- $sso_redirect = $this->build_sso_url( $args );
- } else {
- Jetpack_SSO_Helpers::clear_wpcom_profile_cookies();
- $sso_redirect = $this->build_reauth_and_sso_url( $args );
- }
-
- // If there was an error retrieving the SSO URL, then error.
- if ( is_wp_error( $sso_redirect ) ) {
- $error_message = sanitize_text_field(
- sprintf( '%s: %s', $sso_redirect->get_error_code(), $sso_redirect->get_error_message() )
- );
- $tracking = new Tracking();
- $tracking->record_user_event(
- 'sso_login_redirect_failed',
- array(
- 'error_message' => $error_message,
- )
- );
- wp_die( esc_html( $error_message ) );
- }
-
- return $sso_redirect;
- }
-
- /**
- * Build WordPress.com SSO URL with appropriate query parameters.
- *
- * @param array $args Optional query parameters.
- * @return string|WP_Error WordPress.com SSO URL
- */
- public function build_sso_url( $args = array() ) {
- $sso_nonce = ! empty( $args['sso_nonce'] ) ? $args['sso_nonce'] : self::request_initial_nonce();
- $defaults = array(
- 'action' => 'jetpack-sso',
- 'site_id' => Jetpack_Options::get_option( 'id' ),
- 'sso_nonce' => $sso_nonce,
- 'calypso_auth' => '1',
- );
-
- $args = wp_parse_args( $args, $defaults );
-
- if ( is_wp_error( $sso_nonce ) ) {
- return $sso_nonce;
- }
-
- return add_query_arg( $args, 'https://wordpress.com/wp-login.php' );
- }
-
- /**
- * Build WordPress.com SSO URL with appropriate query parameters,
- * including the parameters necessary to force the user to reauthenticate
- * on WordPress.com.
- *
- * @param array $args Optional query parameters.
- * @return string|WP_Error WordPress.com SSO URL
- */
- public function build_reauth_and_sso_url( $args = array() ) {
- $sso_nonce = ! empty( $args['sso_nonce'] ) ? $args['sso_nonce'] : self::request_initial_nonce();
- $redirect = $this->build_sso_url(
- array(
- 'force_auth' => '1',
- 'sso_nonce' => $sso_nonce,
- )
- );
-
- if ( is_wp_error( $redirect ) ) {
- return $redirect;
- }
-
- $defaults = array(
- 'action' => 'jetpack-sso',
- 'site_id' => Jetpack_Options::get_option( 'id' ),
- 'sso_nonce' => $sso_nonce,
- 'reauth' => '1',
- 'redirect_to' => rawurlencode( $redirect ),
- 'calypso_auth' => '1',
- );
-
- $args = wp_parse_args( $args, $defaults );
-
- if ( is_wp_error( $args['sso_nonce'] ) ) {
- return $args['sso_nonce'];
- }
-
- return add_query_arg( $args, 'https://wordpress.com/wp-login.php' );
- }
-
- /**
- * Determines local user associated with a given WordPress.com user ID.
- *
- * @since 2.6.0
- *
- * @param int $wpcom_user_id User ID from WordPress.com.
- * @return object Local user object if found, null if not.
- */
- public static function get_user_by_wpcom_id( $wpcom_user_id ) {
- $user_query = new WP_User_Query(
- array(
- 'meta_key' => 'wpcom_user_id',
- 'meta_value' => (int) $wpcom_user_id,
- 'number' => 1,
- )
- );
-
- $users = $user_query->get_results();
- return $users ? array_shift( $users ) : null;
- }
-
- /**
- * When jetpack-sso-auth-redirect query parameter is set, will redirect user to
- * WordPress.com authorization flow.
- *
- * We redirect here instead of in handle_login() because Jetpack::init()->build_connect_url
- * calls menu_page_url() which doesn't work properly until admin menus are registered.
- */
- public function maybe_authorize_user_after_sso() {
- if ( empty( $_GET['jetpack-sso-auth-redirect'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
- return;
- }
-
- $redirect_to = ! empty( $_GET['redirect_to'] ) ? esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) : admin_url(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
- $request_redirect_to = ! empty( $_GET['request_redirect_to'] ) ? esc_url_raw( wp_unslash( $_GET['request_redirect_to'] ) ) : $redirect_to; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
-
- /** This filter is documented in core/src/wp-login.php */
- $redirect_after_auth = apply_filters( 'login_redirect', $redirect_to, $request_redirect_to, wp_get_current_user() );
-
- /**
- * Since we are passing this redirect to WordPress.com and therefore can not use wp_safe_redirect(),
- * let's sanitize it here to make sure it's safe. If the redirect is not safe, then use admin_url().
- */
- $redirect_after_auth = wp_sanitize_redirect( $redirect_after_auth );
- $redirect_after_auth = wp_validate_redirect( $redirect_after_auth, admin_url() );
-
- /**
- * Return the raw connect URL with our redirect and attribute connection to SSO.
- * We remove any other filters that may be turning on the in-place connection
- * since we will be redirecting the user as opposed to iFraming.
- */
- remove_all_filters( 'jetpack_use_iframe_authorization_flow' );
- add_filter( 'jetpack_use_iframe_authorization_flow', '__return_false' );
- $connect_url = Jetpack::init()->build_connect_url( true, $redirect_after_auth, 'sso' );
-
- add_filter( 'allowed_redirect_hosts', array( 'Jetpack_SSO_Helpers', 'allowed_redirect_hosts' ) );
- wp_safe_redirect( $connect_url );
- exit;
- }
-
- /**
- * Cache user's display name and Gravatar so it can be displayed on the login screen. These cookies are
- * stored when the user logs out, and then deleted when the user logs in.
- */
- public function store_wpcom_profile_cookies_on_logout() {
- $user_id = get_current_user_id();
-
- if ( ! Jetpack_SSO_Helpers::is_user_connected( $user_id ) ) {
- return;
- }
-
- $user_data = $this->get_user_data( $user_id );
- if ( ! $user_data ) {
- return;
- }
-
- setcookie(
- 'jetpack_sso_wpcom_name_' . COOKIEHASH,
- $user_data->display_name,
- time() + WEEK_IN_SECONDS,
- COOKIEPATH,
- COOKIE_DOMAIN,
- is_ssl(),
- true
- );
-
- setcookie(
- 'jetpack_sso_wpcom_gravatar_' . COOKIEHASH,
- get_avatar_url(
- $user_data->email,
- array(
- 'size' => 144,
- 'default' => 'mystery',
- )
- ),
- time() + WEEK_IN_SECONDS,
- COOKIEPATH,
- COOKIE_DOMAIN,
- is_ssl(),
- true
- );
- }
-
- /**
- * Determines if a local user is connected to WordPress.com
- *
- * @since 2.8
- * @param integer $user_id - Local user id.
- * @return boolean
- **/
- public function is_user_connected( $user_id ) {
- return $this->get_user_data( $user_id );
- }
-
- /**
- * Retrieves a user's WordPress.com data
- *
- * @since 2.8
- * @param integer $user_id - Local user id.
- * @return mixed null or stdClass
- **/
- public function get_user_data( $user_id ) {
- return get_user_meta( $user_id, 'wpcom_user_data', true );
- }
-}
-
-Jetpack_SSO::get_instance();
+/**
+ * Legacy, deprecated class.
+ *
+ * @deprecated $$next-version$$
+ */
+class Jetpack_SSO extends SSO {}
diff --git a/projects/plugins/jetpack/modules/sso/class-jetpack-force-2fa.php b/projects/plugins/jetpack/modules/sso/class-jetpack-force-2fa.php
index 8dc845a9632c9..36bf05372b851 100644
--- a/projects/plugins/jetpack/modules/sso/class-jetpack-force-2fa.php
+++ b/projects/plugins/jetpack/modules/sso/class-jetpack-force-2fa.php
@@ -4,11 +4,17 @@
*
* Ported from original repo at https://github.com/automattic/jetpack-force-2fa
*
+ * @deprecated $$next-version$$ Use Automattic\Jetpack\Connection\Manager\SSO instead.
+ *
+ * phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
+ *
* @package automattic/jetpack
*/
/**
* Force users to use two factor authentication.
+ *
+ * @deprecated $$next-version$$
*/
class Jetpack_Force_2FA {
@@ -18,157 +24,47 @@ class Jetpack_Force_2FA {
* Defaults to manage_options via the plugins_loaded function.
* Can be modified with the jetpack_force_2fa_cap filter.
*
+ * @deprecated $$next-version$$
+ *
* @var string
*/
private $role;
/**
* Constructor.
+ *
+ * @deprecated $$next-version$$
*/
public function __construct() {
- add_action( 'after_setup_theme', array( $this, 'plugins_loaded' ) );
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\Manager\\SSO\\Force_2FA::__construct' );
}
/**
* Load the plugin via the plugins_loaded hook.
+ *
+ * @deprecated $$next-version$$
*/
public function plugins_loaded() {
- /**
- * Filter the role to force 2FA for.
- * Defaults to manage_options.
- *
- * @param string $role The role to force 2FA for.
- * @return string
- * @since 12.7
- * @module SSO
- */
- $this->role = apply_filters( 'jetpack_force_2fa_cap', 'manage_options' );
-
- // Bail if Jetpack SSO is not active
- if ( ! class_exists( 'Jetpack' ) || ! Jetpack::is_active() || ! Jetpack::is_module_active( 'sso' ) ) {
- add_action( 'admin_notices', array( $this, 'admin_notice' ) );
- return;
- }
-
- $this->force_2fa();
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\Manager\\SSO\\Force_2FA::plugins_loaded' );
}
/**
* Display an admin notice if Jetpack SSO is not active.
- */
- public function admin_notice() {
- /**
- * Filter if an admin notice is deplayed when Force 2FA is required, but SSO is not enabled.
- * Defaults to true.
- *
- * @param bool $display_notice Whether to display the notice.
- * @return bool
- * @since 12.7
- * @module SSO
- */
- if ( apply_filters( 'jetpack_force_2fa_dependency_notice', true ) && current_user_can( $this->role ) ) {
- printf( '
%2$s
', 'notice notice-warning', 'Jetpack Force 2FA requires Jetpack and the Jetpack SSO module.' );
- }
- }
-
- /**
- * Force 2FA when using Jetpack SSO and force Jetpack SSO.
*
- * @return void
+ * @deprecated $$next-version$$
*/
- private function force_2fa() {
- // Allows WP.com login to a local account if it matches the local account.
- add_filter( 'jetpack_sso_match_by_email', '__return_true', 9999 );
-
- // multisite
- if ( is_multisite() ) {
-
- // Hide the login form
- add_filter( 'jetpack_remove_login_form', '__return_true', 9999 );
- add_filter( 'jetpack_sso_bypass_login_forward_wpcom', '__return_true', 9999 );
- add_filter( 'jetpack_sso_display_disclaimer', '__return_false', 9999 );
-
- add_filter(
- 'wp_authenticate_user',
- function () {
- return new WP_Error( 'wpcom-required', $this->get_login_error_message() ); },
- 9999
- );
-
- add_filter( 'jetpack_sso_require_two_step', '__return_true' );
-
- add_filter( 'allow_password_reset', '__return_false' );
- } else {
- // Not multisite.
-
- // Completely disable the standard login form for admins.
- add_filter(
- 'wp_authenticate_user',
- function ( $user ) {
- if ( is_wp_error( $user ) ) {
- return $user;
- }
- if ( $user->has_cap( $this->role ) ) {
- return new WP_Error( 'wpcom-required', $this->get_login_error_message(), $user->user_login );
- }
- return $user;
- },
- 9999
- );
-
- add_filter(
- 'allow_password_reset',
- function ( $allow, $user_id ) {
- if ( user_can( $user_id, $this->role ) ) {
- return false;
- }
- return $allow; },
- 9999,
- 2
- );
-
- add_action( 'jetpack_sso_pre_handle_login', array( $this, 'jetpack_set_two_step' ) );
- }
+ public function admin_notice() {
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\Manager\\SSO\\Force_2FA::admin_notice' );
}
/**
* Specifically set the two step filter for Jetpack SSO.
*
- * @param Object $user_data The user data from WordPress.com.
+ * @deprecated $$next-version$$
*
- * @return void
+ * @param Object $user_data The user data from WordPress.com.
*/
public function jetpack_set_two_step( $user_data ) {
- $user = Jetpack_SSO::get_user_by_wpcom_id( $user_data->ID );
-
- // Borrowed from Jetpack. Ignores the match_by_email setting.
- if ( empty( $user ) ) {
- $user = get_user_by( 'email', $user_data->email );
- }
-
- if ( $user && $user->has_cap( $this->role ) ) {
- add_filter( 'jetpack_sso_require_two_step', '__return_true' );
- }
- }
-
- /**
- * Get the login error message.
- *
- * @return string
- */
- private function get_login_error_message() {
- /**
- * Filter the login error message.
- * Defaults to a message that explains the user must use a WordPress.com account with 2FA enabled.
- *
- * @param string $message The login error message.
- * @return string
- * @since 12.7
- * @module SSO
- */
- return apply_filters(
- 'jetpack_force_2fa_login_error_message',
- sprintf( 'For added security, please log in using your WordPress.com account.
Note: Your account must have Two Step Authentication enabled, which can be configured from Security Settings.', 'https://support.wordpress.com/security/two-step-authentication/', 'https://wordpress.com/me/security/two-step' )
- );
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\Manager\\SSO\\Force_2FA::jetpack_set_two_step' );
}
}
diff --git a/projects/plugins/jetpack/modules/sso/class.jetpack-sso-helpers.php b/projects/plugins/jetpack/modules/sso/class.jetpack-sso-helpers.php
index af544b26384d0..6f2dfd6061260 100644
--- a/projects/plugins/jetpack/modules/sso/class.jetpack-sso-helpers.php
+++ b/projects/plugins/jetpack/modules/sso/class.jetpack-sso-helpers.php
@@ -2,91 +2,65 @@
/**
* A collection of helper functions used in the SSO module.
*
+ * @deprecated $$next-version$$ Use Automattic\Jetpack\Connection\Manager\SSO instead.
+ *
* @package automattic/jetpack
*/
-use Automattic\Jetpack\Connection\Manager as Connection_Manager;
+use Automattic\Jetpack\Connection\Manager;
+use Automattic\Jetpack\Connection\SSO\Helpers;
+use Automattic\Jetpack\Connection\Utils;
if ( ! class_exists( 'Jetpack_SSO_Helpers' ) ) :
/**
* A collection of helper functions used in the SSO module.
*
+ * @deprecated $$next-version$$
+ *
* @since 4.1.0
*/
class Jetpack_SSO_Helpers {
/**
* Determine if the login form should be hidden or not
*
+ * @deprecated $$next-version$$
+ *
* @return bool
**/
public static function should_hide_login_form() {
- /**
- * Remove the default log in form, only leave the WordPress.com log in button.
- *
- * @module sso
- *
- * @since 3.1.0
- *
- * @param bool get_option( 'jetpack_sso_remove_login_form', false ) Should the default log in form be removed. Default to false.
- */
- return (bool) apply_filters( 'jetpack_remove_login_form', get_option( 'jetpack_sso_remove_login_form', false ) );
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\Helpers::should_hide_login_form' );
+
+ return Helpers::should_hide_login_form();
}
/**
* Returns a boolean value for whether logging in by matching the WordPress.com user email to a
* Jetpack site user's email is allowed.
*
+ * @deprecated $$next-version$$
+ *
* @return bool
*/
public static function match_by_email() {
- $match_by_email = defined( 'WPCC_MATCH_BY_EMAIL' ) ? WPCC_MATCH_BY_EMAIL : (bool) get_option( 'jetpack_sso_match_by_email', true );
-
- /**
- * Link the local account to an account on WordPress.com using the same email address.
- *
- * @module sso
- *
- * @since 2.6.0
- *
- * @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.
- */
- return (bool) apply_filters( 'jetpack_sso_match_by_email', $match_by_email );
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\Helpers::match_by_email' );
+
+ return Helpers::match_by_email();
}
/**
* Returns a boolean for whether users are allowed to register on the Jetpack site with SSO,
* even though the site disallows normal registrations.
*
+ * @deprecated $$next-version$$
+ *
* @param object|null $user_data WordPress.com user information.
* @return bool
*/
public static function new_user_override( $user_data = null ) {
- $new_user_override = defined( 'WPCC_NEW_USER_OVERRIDE' ) ? WPCC_NEW_USER_OVERRIDE : false;
-
- /**
- * Allow users to register on your site with a WordPress.com account, even though you disallow normal registrations.
- * If you return a string that corresponds to a user role, the user will be given that role.
- *
- * @module sso
- *
- * @since 2.6.0
- * @since 4.6 $user_data object is now passed to the jetpack_sso_new_user_override filter
- *
- * @param bool $new_user_override Allow users to register on your site with a WordPress.com account. Default to false.
- * @param object|null $user_data An object containing the user data returned from WordPress.com.
- */
- $role = apply_filters( 'jetpack_sso_new_user_override', $new_user_override, $user_data );
-
- if ( $role ) {
- if ( is_string( $role ) && get_role( $role ) ) {
- return $role;
- } else {
- return get_option( 'default_role' );
- }
- }
-
- return false;
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\Helpers::new_user_override' );
+
+ return Helpers::new_user_override( $user_data );
}
/**
@@ -94,38 +68,28 @@ public static function new_user_override( $user_data = null ) {
*
* @since 4.1.0
*
+ * @deprecated $$next-version$$
+ *
* @return bool
*/
public static function is_two_step_required() {
- /**
- * Is it required to have 2-step authentication enabled on WordPress.com to use SSO?
- *
- * @module sso
- *
- * @since 2.8.0
- *
- * @param bool get_option( 'jetpack_sso_require_two_step' ) Does SSO require 2-step authentication?
- */
- return (bool) apply_filters( 'jetpack_sso_require_two_step', get_option( 'jetpack_sso_require_two_step', false ) );
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\Helpers::is_two_step_required' );
+
+ return Helpers::is_two_step_required();
}
/**
* Returns a boolean for whether a user that is attempting to log in will be automatically
* redirected to WordPress.com to begin the SSO flow.
*
+ * @deprecated $$next-version$$
+ *
* @return bool
*/
public static function bypass_login_forward_wpcom() {
- /**
- * Redirect the site's log in form to WordPress.com's log in form.
- *
- * @module sso
- *
- * @since 3.1.0
- *
- * @param bool false Should the site's log in form be automatically forwarded to WordPress.com's log in form.
- */
- return (bool) apply_filters( 'jetpack_sso_bypass_login_forward_wpcom', false );
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\Helpers::bypass_login_forward_wpcom' );
+
+ return Helpers::bypass_login_forward_wpcom();
}
/**
@@ -134,23 +98,14 @@ public static function bypass_login_forward_wpcom() {
*
* @since 4.1.0
*
+ * @deprecated $$next-version$$
+ *
* @return bool
*/
public static function show_sso_login() {
- if ( self::should_hide_login_form() ) {
- return true;
- }
-
- /**
- * Display the SSO login form as the default when both the default and SSO login forms are enabled.
- *
- * @module sso
- *
- * @since 4.1.0
- *
- * @param bool true Should the SSO login form be displayed by default when the default login form is also enabled?
- */
- return (bool) apply_filters( 'jetpack_sso_default_to_sso_login', true );
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\Helpers::show_sso_login' );
+
+ return Helpers::show_sso_login();
}
/**
@@ -158,10 +113,14 @@ public static function show_sso_login() {
*
* @since 4.1.0
*
+ * @deprecated $$next-version$$
+ *
* @return bool
*/
public static function is_require_two_step_checkbox_disabled() {
- return (bool) has_filter( 'jetpack_sso_require_two_step' );
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\Helpers::is_require_two_step_checkbox_disabled' );
+
+ return Helpers::is_require_two_step_checkbox_disabled();
}
/**
@@ -169,10 +128,14 @@ public static function is_require_two_step_checkbox_disabled() {
*
* @since 4.1.0
*
+ * @deprecated $$next-version$$
+ *
* @return bool
*/
public static function is_match_by_email_checkbox_disabled() {
- return defined( 'WPCC_MATCH_BY_EMAIL' ) || has_filter( 'jetpack_sso_match_by_email' );
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\Helpers::is_match_by_email_checkbox_disabled' );
+
+ return Helpers::is_match_by_email_checkbox_disabled();
}
/**
@@ -184,97 +147,43 @@ public static function is_match_by_email_checkbox_disabled() {
* @since 4.3.0
* @since 4.6.0 Added public-api.wordpress.com as an allowed redirect
*
+ * @deprecated $$next-version$$
+ *
* @param array $hosts Allowed redirect hosts.
* @param string $api_base Base API URL.
*
* @return array
*/
public static function allowed_redirect_hosts( $hosts, $api_base = JETPACK__API_BASE ) {
- if ( empty( $hosts ) ) {
- $hosts = array();
- }
-
- $hosts[] = 'wordpress.com';
- $hosts[] = 'jetpack.wordpress.com';
- $hosts[] = 'public-api.wordpress.com';
- $hosts[] = 'jetpack.com';
-
- if ( ! str_contains( $api_base, 'jetpack.wordpress.com/jetpack' ) ) {
- $base_url_parts = wp_parse_url( esc_url_raw( $api_base ) );
- if ( $base_url_parts && ! empty( $base_url_parts['host'] ) ) {
- $hosts[] = $base_url_parts['host'];
- }
- }
-
- return array_unique( $hosts );
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\Helpers::allowed_redirect_hosts' );
+
+ return Helpers::allowed_redirect_hosts( $hosts, $api_base );
}
/**
* Generate a new user from a SSO attempt.
*
+ * @deprecated $$next-version$$
+ *
* @param object $user_data WordPress.com user information.
*/
public static function generate_user( $user_data ) {
- $username = $user_data->login;
- /**
- * Determines how many times the SSO module can attempt to randomly generate a user.
- *
- * @module sso
- *
- * @since 4.3.2
- *
- * @param int 5 By default, SSO will attempt to random generate a user up to 5 times.
- */
- $num_tries = (int) apply_filters( 'jetpack_sso_allowed_username_generate_retries', 5 );
-
- $exists = username_exists( $username );
- $tries = 0;
- while ( $exists && $tries++ < $num_tries ) {
- $username = $user_data->login . '_' . $user_data->ID . '_' . wp_rand();
- $exists = username_exists( $username );
- }
-
- if ( $exists ) {
- return false;
- }
-
- $user = (object) array();
- $user->user_pass = wp_generate_password( 20 );
- $user->user_login = wp_slash( $username );
- $user->user_email = wp_slash( $user_data->email );
- $user->display_name = $user_data->display_name;
- $user->first_name = $user_data->first_name;
- $user->last_name = $user_data->last_name;
- $user->url = $user_data->url;
- $user->description = $user_data->description;
-
- if ( isset( $user_data->role ) && $user_data->role ) {
- $user->role = $user_data->role;
- }
-
- $created_user_id = wp_insert_user( $user );
-
- update_user_meta( $created_user_id, 'wpcom_user_id', $user_data->ID );
- return get_userdata( $created_user_id );
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\Utils::generate_user' );
+
+ return Utils::generate_user( $user_data );
}
/**
* Determines how long the auth cookie is valid for when a user logs in with SSO.
*
+ * @deprecated $$next-version$$
+ *
* @return int result of the jetpack_sso_auth_cookie_expiration filter.
*/
public static function extend_auth_cookie_expiration_for_sso() {
- /**
- * Determines how long the auth cookie is valid for when a user logs in with SSO.
- *
- * @module sso
- *
- * @since 4.4.0
- * @since 6.1.0 Fixed a typo. Filter was previously jetpack_sso_auth_cookie_expirtation.
- *
- * @param int YEAR_IN_SECONDS
- */
- return (int) apply_filters( 'jetpack_sso_auth_cookie_expiration', YEAR_IN_SECONDS );
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\Helpers::extend_auth_cookie_expiration_for_sso' );
+
+ return Helpers::extend_auth_cookie_expiration_for_sso();
}
/**
@@ -282,29 +191,16 @@ public static function extend_auth_cookie_expiration_for_sso() {
*
* @since 4.6.0
*
+ * @deprecated $$next-version$$
+ *
* @param string $action SSO action being performed.
*
* @return bool Is SSO allowed for the current action?
*/
public static function display_sso_form_for_action( $action ) {
- /**
- * Allows plugins the ability to overwrite actions where the SSO form is allowed to be used.
- *
- * @module sso
- *
- * @since 4.6.0
- *
- * @param array $allowed_actions_for_sso
- */
- $allowed_actions_for_sso = (array) apply_filters(
- 'jetpack_sso_allowed_actions',
- array(
- 'login',
- 'jetpack-sso',
- 'jetpack_json_api_authorization',
- )
- );
- return in_array( $action, $allowed_actions_for_sso, true );
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\Helpers::display_sso_form_for_action' );
+
+ return Helpers::display_sso_form_for_action( $action );
}
/**
@@ -313,120 +209,53 @@ public static function display_sso_form_for_action( $action ) {
*
* @since 4.6.0
*
+ * @deprecated $$next-version$$
+ *
* @return array|bool
*/
public static function get_json_api_auth_environment() {
- if ( empty( $_COOKIE['jetpack_sso_original_request'] ) ) {
- return false;
- }
-
- $original_request = esc_url_raw( wp_unslash( $_COOKIE['jetpack_sso_original_request'] ) );
-
- $parsed_url = wp_parse_url( $original_request );
- if ( empty( $parsed_url ) || empty( $parsed_url['query'] ) ) {
- return false;
- }
-
- $args = array();
- wp_parse_str( $parsed_url['query'], $args );
-
- if ( empty( $args ) || empty( $args['action'] ) ) {
- return false;
- }
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\Helpers::get_json_api_auth_environment' );
- if ( 'jetpack_json_api_authorization' !== $args['action'] ) {
- return false;
- }
-
- return array_merge(
- $args,
- array( 'jetpack_json_api_original_query' => $original_request )
- );
+ return Helpers::get_json_api_auth_environment();
}
/**
* Check if the site has a custom login page URL, and return it.
* If default login page URL is used (`wp-login.php`), `null` will be returned.
*
+ * @deprecated $$next-version$$
+ *
* @return string|null
*/
public static function get_custom_login_url() {
- $login_url = wp_login_url();
-
- if ( str_ends_with( $login_url, 'wp-login.php' ) ) {
- // No custom URL found.
- return null;
- }
-
- $site_url = trailingslashit( site_url() );
-
- if ( ! str_starts_with( $login_url, $site_url ) ) {
- // Something went wrong, we can't properly extract the custom URL.
- return null;
- }
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\Helpers::get_custom_login_url' );
- // Extracting the "path" part of the URL, because we don't need the `site_url` part.
- return str_ireplace( $site_url, '', $login_url );
+ return Helpers::get_custom_login_url();
}
/**
* Clear the cookies that store the profile information for the last
* WPCOM user to connect.
+ *
+ * @deprecated $$next-version$$
*/
public static function clear_wpcom_profile_cookies() {
- if ( isset( $_COOKIE[ 'jetpack_sso_wpcom_name_' . COOKIEHASH ] ) ) {
- setcookie(
- 'jetpack_sso_wpcom_name_' . COOKIEHASH,
- ' ',
- time() - YEAR_IN_SECONDS,
- COOKIEPATH,
- COOKIE_DOMAIN,
- is_ssl(),
- true
- );
- }
-
- if ( isset( $_COOKIE[ 'jetpack_sso_wpcom_gravatar_' . COOKIEHASH ] ) ) {
- setcookie(
- 'jetpack_sso_wpcom_gravatar_' . COOKIEHASH,
- ' ',
- time() - YEAR_IN_SECONDS,
- COOKIEPATH,
- COOKIE_DOMAIN,
- is_ssl(),
- true
- );
- }
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\Helpers::clear_wpcom_profile_cookies' );
+
+ return Helpers::clear_wpcom_profile_cookies();
}
/**
* Remove an SSO connection for a user.
*
+ * @deprecated $$next-version$$
+ *
* @param int $user_id The local user id.
*/
public static function delete_connection_for_user( $user_id ) {
- $wpcom_user_id = get_user_meta( $user_id, 'wpcom_user_id', true );
- if ( ! $wpcom_user_id ) {
- return;
- }
-
- $xml = new Jetpack_IXR_Client(
- array(
- 'wpcom_user_id' => $user_id,
- )
- );
- $xml->query( 'jetpack.sso.removeUser', $wpcom_user_id );
-
- if ( $xml->isError() ) {
- return false;
- }
-
- // Clean up local data stored for SSO.
- delete_user_meta( $user_id, 'wpcom_user_id' );
- delete_user_meta( $user_id, 'wpcom_user_data' );
- self::clear_wpcom_profile_cookies();
-
- return $xml->getResponse();
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\Helpers::delete_connection_for_user' );
+
+ return Helpers::delete_connection_for_user( $user_id );
}
/**
@@ -434,14 +263,14 @@ public static function delete_connection_for_user( $user_id ) {
*
* @since 13.3
*
+ * @deprecated $$next-version$$
+ *
* @param int $user_id Local User information.
*/
public static function is_user_connected( $user_id = 0 ) {
- if ( ! $user_id ) {
- $user_id = get_current_user_id();
- }
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\Manager->is_user_connected' );
- return ( new Connection_Manager( 'jetpack' ) )->is_user_connected( $user_id );
+ return ( new Manager() )->is_user_connected( $user_id );
}
}
diff --git a/projects/plugins/jetpack/modules/sso/class.jetpack-sso-notices.php b/projects/plugins/jetpack/modules/sso/class.jetpack-sso-notices.php
index 57f9f9149a2d6..d027eacfcd529 100644
--- a/projects/plugins/jetpack/modules/sso/class.jetpack-sso-notices.php
+++ b/projects/plugins/jetpack/modules/sso/class.jetpack-sso-notices.php
@@ -2,16 +2,20 @@
/**
* A collection of helper functions used in the SSO module.
*
+ * @deprecated $$next-version$$ Use Automattic\Jetpack\Connection\Manager\SSO instead.
+ *
* @package automattic/jetpack
*/
-use Automattic\Jetpack\Redirect;
+use Automattic\Jetpack\Connection\SSO\Notices;
if ( ! class_exists( 'Jetpack_SSO_Notices' ) ) :
/**
* A collection of helper functions used in the SSO module.
*
+ * @deprecated $$next-version$$
+ *
* @since 4.4.0
*/
class Jetpack_SSO_Notices {
@@ -20,26 +24,16 @@ class Jetpack_SSO_Notices {
* the user's account on WordPress.com does not have two step enabled.
*
* @since 2.7
+ *
+ * @deprecated $$next-version$$
+ *
* @param string $message Error message.
* @return string
**/
public static function error_msg_enable_two_step( $message ) {
- $error = sprintf(
- wp_kses(
- /* translators: URL to settings page */
- __(
- 'Two-Step Authentication is required to access this site. Please visit your Security Settings to configure Two-step Authentication for your account.',
- 'jetpack'
- ),
- array( 'a' => array( 'href' => array() ) )
- ),
- Redirect::get_url( 'calypso-me-security-two-step' ),
- Redirect::get_url( 'wpcom-support-security-two-step-authentication' )
- );
-
- $message .= sprintf( '
%s
', $error );
-
- return $message;
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\Manager\\SSO\\Notices::error_msg_enable_two_step' );
+
+ return Notices::error_msg_enable_two_step( $message );
}
/**
@@ -47,25 +41,15 @@ public static function error_msg_enable_two_step( $message ) {
* is off and they already have an account with their email address on
* this site.
*
+ * @deprecated $$next-version$$
+ *
* @param string $message Error message.
* @return string
*/
public static function error_msg_email_already_exists( $message ) {
- $error = sprintf(
- wp_kses(
- /* translators: login URL */
- __(
- 'You already have an account on this site. Please sign in with your username and password and then connect to WordPress.com.',
- 'jetpack'
- ),
- array( 'a' => array( 'href' => array() ) )
- ),
- esc_url_raw( add_query_arg( 'jetpack-sso-show-default-form', '1', wp_login_url() ) )
- );
-
- $message .= sprintf( '
%s
', $error );
-
- return $message;
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\Manager\\SSO\\Notices::error_msg_email_already_exists' );
+
+ return Notices::error_msg_email_already_exists( $message );
}
/**
@@ -73,14 +57,16 @@ public static function error_msg_email_already_exists( $message ) {
*
* @since 4.3.2
*
+ * @deprecated $$next-version$$
+ *
* @param string $message Error Message.
*
* @return string
*/
public static function error_msg_identity_crisis( $message ) {
- $error = esc_html__( 'Logging in with WordPress.com is not currently available because this site is experiencing connection problems.', 'jetpack' );
- $message .= sprintf( '
%s
', $error );
- return $message;
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\Manager\\SSO\\Notices::error_msg_identity_crisis' );
+
+ return Notices::error_msg_identity_crisis( $message );
}
/**
@@ -89,17 +75,16 @@ public static function error_msg_identity_crisis( $message ) {
*
* @since 4.3.2
*
+ * @deprecated $$next-version$$
+ *
* @param string $message Error message.
*
* @return string
*/
public static function error_invalid_response_data( $message ) {
- $error = esc_html__(
- 'There was an error logging you in via WordPress.com, please try again or try logging in with your username and password.',
- 'jetpack'
- );
- $message .= sprintf( '
%s
', $error );
- return $message;
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\Manager\\SSO\\Notices::error_invalid_response_data' );
+
+ return Notices::error_invalid_response_data( $message );
}
/**
@@ -108,38 +93,32 @@ public static function error_invalid_response_data( $message ) {
*
* @since 4.3.2
*
+ * @deprecated $$next-version$$
+ *
* @param string $message Error message.
*
* @return string
*/
public static function error_unable_to_create_user( $message ) {
- $error = esc_html__(
- 'There was an error creating a user for you. Please contact the administrator of your site.',
- 'jetpack'
- );
- $message .= sprintf( '
%s
', $error );
- return $message;
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\Manager\\SSO\\Notices::error_unable_to_create_user' );
+
+ return Notices::error_unable_to_create_user( $message );
}
/**
* When the default login form is hidden, this method is called on the 'authenticate' filter with a priority of 30.
* This method disables the ability to submit the default login form.
*
+ * @deprecated $$next-version$$
+ *
* @param WP_User|WP_Error $user Either the user attempting to login or an existing authentication failure.
*
* @return WP_Error
*/
public static function disable_default_login_form( $user ) {
- if ( is_wp_error( $user ) ) {
- return $user;
- }
-
- /**
- * Since we're returning an error that will be shown as a red notice, let's remove the
- * informational "blue" notice.
- */
- remove_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'msg_login_by_jetpack' ) );
- return new WP_Error( 'jetpack_sso_required', self::get_sso_required_message() );
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\Manager\\SSO\\Notices::disable_default_login_form' );
+
+ return Notices::disable_default_login_form( $user );
}
/**
@@ -147,65 +126,45 @@ public static function disable_default_login_form( $user ) {
* login form in Settings > General > Secure Sign On
*
* @since 2.7
+ *
+ * @deprecated $$next-version$$
+ *
* @param string $message Error message.
*
* @return string
**/
public static function msg_login_by_jetpack( $message ) {
- $message .= sprintf( '
%s
', self::get_sso_required_message() );
- return $message;
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\Manager\\SSO\\Notices::msg_login_by_jetpack' );
+
+ return Notices::msg_login_by_jetpack( $message );
}
/**
* Get the message for SSO required.
*
+ * @deprecated $$next-version$$
+ *
* @return string
*/
public static function get_sso_required_message() {
- $msg = esc_html__(
- 'A WordPress.com account is required to access this site. Click the button below to sign in or create a free WordPress.com account.',
- 'jetpack'
- );
-
- /**
- * Filter the message displayed when the default WordPress login form is disabled.
- *
- * @module sso
- *
- * @since 2.8.0
- *
- * @param string $msg Disclaimer when default WordPress login form is disabled.
- */
- return apply_filters( 'jetpack_sso_disclaimer_message', $msg );
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\Manager\\SSO\\Notices::get_sso_required_message' );
+
+ return Notices::get_sso_required_message();
}
/**
* Message displayed when the user can not be found after approving the SSO process on WordPress.com
*
+ * @deprecated $$next-version$$
+ *
* @param string $message Error message.
*
* @return string
*/
public static function cant_find_user( $message ) {
- $error = __(
- "We couldn't find your account. If you already have an account, make sure you have connected to WordPress.com.",
- 'jetpack'
- );
-
- /**
- * Filters the "couldn't find your account" notice after an attempted SSO.
- *
- * @module sso
- *
- * @since 10.5.0
- *
- * @param string $error Error text.
- */
- $error = apply_filters( 'jetpack_sso_unknown_user_notice', $error );
-
- $message .= sprintf( '
- render_wpcom_invite_checkbox' );
}
/**
* Render a checkbox to differentiate if a user is external.
*
+ * @deprecated $$next-version$$
+ *
* @param string $type The type of new user form the hook follows.
*/
public function render_wpcom_external_user_checkbox( $type ) {
- // Only enable this feature on WordPress.com sites.
- if ( ! ( new Host() )->is_wpcom_platform() ) {
- return;
- }
-
- if ( $type === 'add-new-user' ) {
- ?>
-
-
-
-
-
-
-
-
-
-
- render_wpcom_external_user_checkbox' );
}
/**
* Render the custom email message form field for new user registration.
*
+ * @deprecated $$next-version$$
+ *
* @param string $type The type of new user form the hook follows.
*/
public function render_custom_email_message_form_field( $type ) {
- if ( $type === 'add-new-user' ) {
- $valid_nonce = isset( $_POST['_wpnonce_create-user'] )
- ? wp_verify_nonce( sanitize_key( $_POST['_wpnonce_create-user'] ), 'create-user' )
- : false;
- $custom_email_message = ( $valid_nonce && isset( $_POST['custom_email_message'] ) ) ? sanitize_text_field( wp_unslash( $_POST['custom_email_message'] ) ) : '';
- ?>
-
-
-
-
-
-
-
-
-
-
- render_custom_email_message_form_field' );
}
/**
@@ -817,236 +172,36 @@ public function render_custom_email_message_form_field( $type ) {
* It should be sent when SSO is disabled or when admins opt-out of WordPress.com invites intentionally.
* If the "Send User Notification" checkbox is checked, the core invitation email should be sent.
*
- * @param boolean $send_wp_email Whether the core invitation email should be sent.
+ * @deprecated $$next-version$$
*
- * @return boolean Indicating if the core invitation main should be sent.
+ * @param boolean $send_wp_email Whether the core invitation email should be sent.
*/
public function should_send_wp_mail_new_user( $send_wp_email ) {
- if ( ! isset( $_POST['invite_user_wpcom'] ) && isset( $_POST['send_user_notification'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
- return $send_wp_email;
- }
- return false;
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\User_Admin->should_send_wp_mail_new_user' );
}
/**
* Send user invitation to WordPress.com if user has no errors.
*
+ * @deprecated $$next-version$$
+ *
* @param WP_Error $errors The WP_Error object.
* @param bool $update Whether the user is being updated or not.
* @param stdClass $user The User object about to be created.
- * @return WP_Error The modified or not WP_Error object.
*/
public function send_wpcom_mail_user_invite( $errors, $update, $user ) {
- // Only admins should be able to invite new users.
- if ( ! current_user_can( 'create_users' ) ) {
- return $errors;
- }
-
- if ( $update ) {
- return $errors;
- }
-
- // check for a valid nonce.
- if (
- ! isset( $_POST['_wpnonce_create-user'] )
- || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce_create-user'] ), 'create-user' )
- ) {
- return $errors;
- }
-
- // Check if the user is being invited to WordPress.com.
- if ( ! isset( $_POST['invite_user_wpcom'] ) ) {
- return $errors;
- }
-
- // check if the custom email message is too long.
- if (
- ! empty( $_POST['custom_email_message'] )
- && strlen( sanitize_text_field( wp_unslash( $_POST['custom_email_message'] ) ) ) > 500
- ) {
- $errors->add(
- 'custom_email_message',
- wp_kses(
- __( 'Error: The custom message is too long. Please keep it under 500 characters.', 'jetpack' ),
- array(
- 'strong' => array(),
- )
- )
- );
- }
-
- $site_id = Manager::get_site_id( true );
- if ( ! $site_id ) {
- $errors->add(
- 'invalid_site_id',
- wp_kses(
- __( 'Error: Invalid site ID.', 'jetpack' ),
- array(
- 'strong' => array(),
- )
- )
- );
- }
-
- // Bail if there are any errors.
- if ( $errors->has_errors() ) {
- return $errors;
- }
-
- $new_user_request = array(
- 'email_or_username' => sanitize_email( $user->user_email ),
- 'role' => sanitize_key( $user->role ),
- );
-
- if (
- isset( $_POST['custom_email_message'] )
- && strlen( sanitize_text_field( wp_unslash( $_POST['custom_email_message'] ) ) > 0 )
- ) {
- $new_user_request['message'] = sanitize_text_field( wp_unslash( $_POST['custom_email_message'] ) );
- }
-
- if ( isset( $_POST['user_external_contractor'] ) ) {
- $new_user_request['is_external'] = true;
- }
-
- $response = Client::wpcom_json_api_request_as_user(
- sprintf(
- '/sites/%d/invites/new',
- (int) $site_id
- ),
- '2', // Api version
- array(
- 'method' => 'POST',
- ),
- array(
- 'invitees' => array( $new_user_request ),
- )
- );
-
- $event_name = 'sso_new_user_invite_sent';
- $custom_message_sent = isset( $new_user_request['message'] ) ? 'true' : 'false';
-
- if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
- $errors->add(
- 'invitation_not_sent',
- wp_kses(
- __( 'Error: The user invitation email could not be sent, the user account was not created.', 'jetpack' ),
- array(
- 'strong' => array(),
- )
- )
- );
- self::$tracking->record_user_event(
- $event_name,
- array(
- 'success' => 'false',
- 'error' => wp_remote_retrieve_body( $response ), // Get as much information as possible.
- )
- );
- } else {
- self::$tracking->record_user_event(
- $event_name,
- array(
- 'success' => 'true',
- 'custom_message_sent' => $custom_message_sent,
- )
- );
- }
-
- return $errors;
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\User_Admin->send_wpcom_mail_user_invite' );
}
/**
* Adds a column in the user admin table to display user connection status and actions.
*
- * @param array $columns User list table columns.
+ * @deprecated $$next-version$$
*
- * @return array
+ * @param array $columns User list table columns.
*/
public function jetpack_user_connected_th( $columns ) {
-
- $tooltip_string = esc_attr__( 'Jetpack SSO allows a seamless and secure experience on WordPress.com. Join millions of WordPress users who trust us to keep their accounts safe.', 'jetpack' );
-
- wp_enqueue_script(
- 'jetpack-sso-users',
- plugins_url( 'jetpack_vendor/automattic/jetpack-connection/src/sso/jetpack-sso-users.js', JETPACK__PLUGIN_FILE ),
- array(),
- JETPACK__VERSION,
- false
- );
-
- wp_add_inline_script(
- 'jetpack-sso-users',
- "var Jetpack_SSOTooltip = { 'tooltipString': '{$tooltip_string}' }",
- 'before'
- );
-
- $columns['user_jetpack'] = sprintf(
- '%2$s',
- $tooltip_string,
- esc_html__( 'SSO Status', 'jetpack' ),
- esc_attr__( 'Tooltip', 'jetpack' )
- );
- return $columns;
- }
-
- /**
- * Executed when our WP_User_Query instance is set, and we don't have cached invites.
- * This function uses the user emails and the 'are-users-invited' endpoint to build the cache.
- *
- * @return void
- */
- private static function rebuild_invite_cache() {
- $blog_id = Manager::get_site_id( true );
-
- if ( self::$cached_invites === null && self::$user_search !== null ) {
-
- self::$cached_invites = array();
-
- $results = self::$user_search->get_results();
-
- $user_emails = array_reduce(
- $results,
- function ( $current, $item ) {
- if ( ! Jetpack_SSO_Helpers::is_user_connected( $item->ID ) ) {
- $current[] = rawurlencode( $item->user_email );
- } else {
- self::$cached_invites[] = array(
- 'email_or_username' => $item->user_email,
- 'invited' => false,
- 'invite_code' => '',
- );
- }
- return $current;
- },
- array()
- );
-
- if ( ! empty( $user_emails ) ) {
- $url = '/sites/' . $blog_id . '/invites/are-users-invited';
-
- $response = Client::wpcom_json_api_request_as_user(
- $url,
- 'v2',
- array(
- 'method' => 'POST',
- ),
- array( 'users' => $user_emails ),
- 'wpcom'
- );
-
- if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
- $body = json_decode( $response['body'], true );
-
- // ensure array_merge happens with the right parameters
- if ( empty( $body ) ) {
- $body = array();
- }
-
- self::$cached_invites = array_merge( self::$cached_invites, $body );
- }
- }
- }
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\User_Admin->jetpack_user_connected_th' );
}
/**
@@ -1055,242 +210,45 @@ function ( $current, $item ) {
* @access private
* @static
*
- * @param string $email The user email.
+ * @deprecated $$next-version$$
*
- * @return array|void Returns the cached invite if found.
+ * @param string $email The user email.
*/
public static function get_pending_cached_wpcom_invite( $email ) {
- if ( self::$cached_invites === null ) {
- self::rebuild_invite_cache();
- }
-
- if ( ! empty( self::$cached_invites ) && is_array( self::$cached_invites ) ) {
- $index = array_search( $email, array_column( self::$cached_invites, 'email_or_username' ), true );
- if ( $index !== false ) {
- return self::$cached_invites[ $index ];
- }
- }
- }
-
- /**
- * Check if a given user is invited to the site.
- *
- * @access private
- * @static
- * @param int $user_id The user ID.
- *
- * @return string|false returns the user invite code if the user is invited, false otherwise.
- */
- private static function has_pending_wpcom_invite( $user_id ) {
- $blog_id = Manager::get_site_id( true );
- $user = get_user_by( 'id', $user_id );
- $cached_invite = self::get_pending_cached_wpcom_invite( $user->user_email );
-
- if ( $cached_invite ) {
- return $cached_invite['invite_code'];
- }
-
- $url = '/sites/' . $blog_id . '/invites/is-invited';
- $url = add_query_arg(
- array(
- 'email_or_username' => rawurlencode( $user->user_email ),
- ),
- $url
- );
- $response = Client::wpcom_json_api_request_as_user(
- $url,
- 'v2',
- array(),
- null,
- 'wpcom'
- );
-
- if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
- return false;
- }
-
- return json_decode( $response['body'], true )['invite_code'];
- }
-
- /**
- * Delete an external contributor from the site.
- *
- * @access private
- * @static
- * @param int $user_id The user ID.
- *
- * @return bool Returns true if the user was successfully deleted, false otherwise.
- */
- private static function delete_external_contributor( $user_id ) {
- $blog_id = Manager::get_site_id( true );
- $url = '/sites/' . $blog_id . '/external-contributors/remove';
- $response = Client::wpcom_json_api_request_as_user(
- $url,
- 'v2',
- array(
- 'method' => 'POST',
- ),
- array(
- 'user_id' => $user_id,
- ),
- 'wpcom'
- );
-
- if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
- return false;
- }
-
- return true;
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\User_Admin->get_pending_cached_wpcom_invite' );
}
/**
* Show Jetpack SSO user connection status.
*
+ * @deprecated $$next-version$$
+ *
* @param string $val HTML for the column.
* @param string $col User list table column.
* @param int $user_id User ID.
- *
- * @return string
*/
public function jetpack_show_connection_status( $val, $col, $user_id ) {
- if ( 'user_jetpack' === $col ) {
- if ( Jetpack_SSO_Helpers::is_user_connected( $user_id ) ) {
- $connection_html = sprintf(
- '%2$s',
- esc_attr__( 'This user is connected and can log-in to this site.', 'jetpack' ),
- esc_html__( 'Connected', 'jetpack' )
- );
- return $connection_html;
- } else {
- $has_pending_invite = self::has_pending_wpcom_invite( $user_id );
- if ( $has_pending_invite ) {
- $connection_html = sprintf(
- '%2$s',
- esc_attr__( 'This user didn’t accept the invitation to join this site yet.', 'jetpack' ),
- esc_html__( 'Pending invite', 'jetpack' )
- );
- return $connection_html;
- }
- $nonce = wp_create_nonce( 'jetpack-sso-invite-user' );
- $connection_html = sprintf(
- // Using formmethod and formaction because we can't nest forms and have to submit using the main form.
- '
- %2$s
-
- %3$s
-
- ',
- add_query_arg(
- array(
- 'user_id' => $user_id,
- 'invite_nonce' => $nonce,
- 'action' => 'jetpack_invite_user_to_wpcom',
- ),
- admin_url( 'admin-post.php' )
- ),
- esc_html__( 'Send invite', 'jetpack' ),
- esc_attr__( 'This user doesn’t have an SSO connection to WordPress.com. Invite them to the site to increase security and improve their experience.', 'jetpack' ),
- esc_attr__( 'Tooltip', 'jetpack' )
- );
- return $connection_html;
- }
- }
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\User_Admin->jetpack_show_connection_status' );
}
/**
* Creates error notices and redirects the user to the previous page.
*
+ * @deprecated $$next-version$$
+ *
* @param array $query_params - query parameters added to redirection URL.
*/
public function create_error_notice_and_redirect( $query_params ) {
- $ref = wp_get_referer();
- if ( empty( $ref ) ) {
- $ref = network_admin_url( 'users.php' );
- }
-
- $url = add_query_arg(
- $query_params,
- $ref
- );
- return wp_safe_redirect( $url );
+ _deprecated_function( __METHOD__, 'jetpack-$$next-version$$', 'Automattic\\Jetpack\\Connection\\SSO\\User_Admin->create_error_notice_and_redirect' );
}
/**
* Style the Jetpack user rows and columns.
+ *
+ * @deprecated $$next-version$$
*/
public function jetpack_user_table_styles() {
- ?>
-
- jetpack_user_table_styles' );
}
}
endif;
diff --git a/projects/plugins/jetpack/phpunit.xml.dist b/projects/plugins/jetpack/phpunit.xml.dist
index 68b4c81e47bce..099467c668424 100644
--- a/projects/plugins/jetpack/phpunit.xml.dist
+++ b/projects/plugins/jetpack/phpunit.xml.dist
@@ -51,9 +51,6 @@
tests/php/modules/sitemaps
-
- tests/php/modules/sso
- tests/php/modules/subscriptions
diff --git a/projects/plugins/jetpack/tests/php.multisite.xml b/projects/plugins/jetpack/tests/php.multisite.xml
index 2aaaf3ab7d12e..c0e91fdaf0656 100644
--- a/projects/plugins/jetpack/tests/php.multisite.xml
+++ b/projects/plugins/jetpack/tests/php.multisite.xml
@@ -43,9 +43,6 @@
php/modules/widgets
-
- php/modules/sso
- php/modules/subscriptions
diff --git a/projects/plugins/jetpack/tests/php/bootstrap.php b/projects/plugins/jetpack/tests/php/bootstrap.php
index b051c86565812..1ca37dc618a42 100644
--- a/projects/plugins/jetpack/tests/php/bootstrap.php
+++ b/projects/plugins/jetpack/tests/php/bootstrap.php
@@ -151,6 +151,11 @@ function jetpack_full_sync_immediately_off( $modules ) {
require __DIR__ . '/../../modules/shortcodes.php';
}
+// Load the sso module to test properly.
+if ( ! in_running_uninstall_group() ) {
+ require __DIR__ . '/../../modules/sso.php';
+}
+
// Load attachment helper methods.
require __DIR__ . '/attachment_test_case.php';
diff --git a/projects/plugins/jetpack/tests/php/general/test_class.jetpack.php b/projects/plugins/jetpack/tests/php/general/test_class.jetpack.php
index 48c597f541400..b99202f86a501 100644
--- a/projects/plugins/jetpack/tests/php/general/test_class.jetpack.php
+++ b/projects/plugins/jetpack/tests/php/general/test_class.jetpack.php
@@ -863,6 +863,7 @@ public function test_classic_xmlrpc_when_active_and_signed_with_user() {
'jetpack.subscriptions.subscribe',
'jetpack.updatePublicizeConnections',
'jetpack.getHeartbeatData',
+ 'jetpack.userDisconnect',
);
$this->assertXMLRPCMethodsComply( $required, $allowed, array_keys( $methods ) );
@@ -919,6 +920,7 @@ public function test_classic_xmlrpc_when_active_and_signed_with_user_with_edit()
'jetpack.subscriptions.subscribe',
'jetpack.updatePublicizeConnections',
'jetpack.getHeartbeatData',
+ 'jetpack.userDisconnect',
);
$this->assertXMLRPCMethodsComply( $required, $allowed, array_keys( $methods ) );
diff --git a/projects/plugins/jetpack/tests/php/modules/sso/test_class.jetpack-sso-helpers.php b/projects/plugins/jetpack/tests/php/modules/sso/test_class.jetpack-sso-helpers.php
deleted file mode 100644
index 070aca6b6dba2..0000000000000
--- a/projects/plugins/jetpack/tests/php/modules/sso/test_class.jetpack-sso-helpers.php
+++ /dev/null
@@ -1,407 +0,0 @@
-user_data = (object) array(
- 'ID' => 123456789,
- 'email' => 'ssouser@testautomattic.com',
- 'login' => 'ssouser',
- 'display_name' => 'ssouser',
- 'first_name' => 'sso',
- 'last_name' => 'user',
- 'url' => 'https://automattic.com',
- 'description' => 'A user to test SSO',
- );
- }
-
- /**
- * Return 1.
- *
- * @return int
- */
- public function return_one() {
- return 1;
- }
-
- /**
- * Test "sso_helpers_is_two_step_required_filter_true".
- */
- public function test_sso_helpers_is_two_step_required_filter_true() {
- add_filter( 'jetpack_sso_require_two_step', '__return_true' );
- $this->assertTrue( Jetpack_SSO_Helpers::is_two_step_required() );
- remove_filter( 'jetpack_sso_require_two_step', '__return_true' );
- }
-
- /**
- * Test "sso_helpers_is_two_step_required_filter_false".
- */
- public function test_sso_helpers_is_two_step_required_filter_false() {
- add_filter( 'jetpack_sso_require_two_step', '__return_false' );
- $this->assertFalse( Jetpack_SSO_Helpers::is_two_step_required() );
- remove_filter( 'jetpack_sso_require_two_step', '__return_false' );
- }
-
- /**
- * Test "sso_helpers_is_two_step_required_option_true".
- */
- public function test_sso_helpers_is_two_step_required_option_true() {
- update_option( 'jetpack_sso_require_two_step', true );
- $this->assertTrue( Jetpack_SSO_Helpers::is_two_step_required() );
- delete_option( 'jetpack_sso_require_two_step' );
- }
-
- /**
- * Test "sso_helpers_is_two_step_required_option_false".
- */
- public function test_sso_helpers_is_two_step_required_option_false() {
- update_option( 'jetpack_sso_require_two_step', false );
- $this->assertFalse( Jetpack_SSO_Helpers::is_two_step_required() );
- delete_option( 'jetpack_sso_require_two_step' );
- }
-
- /**
- * Test "sso_helpers_should_hide_login_form_filter_true".
- */
- public function test_sso_helpers_should_hide_login_form_filter_true() {
- add_filter( 'jetpack_remove_login_form', '__return_true' );
- $this->assertTrue( Jetpack_SSO_Helpers::should_hide_login_form() );
- remove_filter( 'jetpack_remove_login_form', '__return_true' );
- }
-
- /**
- * Test "sso_helpers_should_hide_login_form_filter_false".
- */
- public function test_sso_helpers_should_hide_login_form_filter_false() {
- add_filter( 'jetpack_remove_login_form', '__return_false' );
- $this->assertFalse( Jetpack_SSO_Helpers::should_hide_login_form() );
- remove_filter( 'jetpack_remove_login_form', '__return_false' );
- }
-
- /**
- * Test "sso_helpers_match_by_email_filter_true".
- */
- public function test_sso_helpers_match_by_email_filter_true() {
- add_filter( 'jetpack_sso_match_by_email', '__return_true' );
- $this->assertTrue( Jetpack_SSO_Helpers::match_by_email() );
- remove_filter( 'jetpack_sso_match_by_email', '__return_true' );
- }
-
- /**
- * Test "sso_helpers_match_by_email_filter_false".
- */
- public function test_sso_helpers_match_by_email_filter_false() {
- add_filter( 'jetpack_sso_match_by_email', '__return_false' );
- $this->assertFalse( Jetpack_SSO_Helpers::match_by_email() );
- remove_filter( 'jetpack_sso_match_by_email', '__return_false' );
- }
-
- /**
- * Test "sso_helpers_new_user_override_filter_true_returns_default_role".
- */
- public function test_sso_helpers_new_user_override_filter_true_returns_default_role() {
- add_role( 'foo', 'Foo' );
- update_option( 'default_role', 'foo' );
- add_filter( 'jetpack_sso_new_user_override', '__return_true' );
- $this->assertEquals( 'foo', Jetpack_SSO_Helpers::new_user_override() );
- remove_filter( 'jetpack_sso_new_user_override', '__return_true' );
- }
-
- /**
- * Test "sso_helpers_new_user_override_filter_false".
- */
- public function test_sso_helpers_new_user_override_filter_false() {
- add_filter( 'jetpack_sso_new_user_override', '__return_false' );
- $this->assertFalse( Jetpack_SSO_Helpers::new_user_override() );
- remove_filter( 'jetpack_sso_new_user_override', '__return_false' );
- }
-
- /**
- * Test "sso_helpers_new_user_override_filter_rolename".
- */
- public function test_sso_helpers_new_user_override_filter_rolename() {
- add_filter( 'jetpack_sso_new_user_override', array( $this, 'return_administrator' ) );
- $this->assertEquals( 'administrator', Jetpack_SSO_Helpers::new_user_override() );
- remove_filter( 'jetpack_sso_new_user_override', array( $this, 'return_administrator' ) );
- }
-
- /**
- * Test "sso_helpers_new_user_override_filter_bad_rolename_returns_default".
- */
- public function test_sso_helpers_new_user_override_filter_bad_rolename_returns_default() {
- add_role( 'foo', 'Foo' );
- update_option( 'default_role', 'foo' );
- add_filter( 'jetpack_sso_new_user_override', array( $this, 'return_foobarbaz' ) );
- $this->assertEquals( 'foo', Jetpack_SSO_Helpers::new_user_override() );
- remove_filter( 'jetpack_sso_new_user_override', array( $this, 'return_foobarbaz' ) );
- }
-
- /**
- * Test "sso_helpers_sso_bypass_default_login_form_filter_true".
- */
- public function test_sso_helpers_sso_bypass_default_login_form_filter_true() {
- add_filter( 'jetpack_sso_bypass_login_forward_wpcom', '__return_true' );
- $this->assertTrue( Jetpack_SSO_Helpers::bypass_login_forward_wpcom() );
- remove_filter( 'jetpack_sso_bypass_login_forward_wpcom', '__return_true' );
- }
-
- /**
- * Test "sso_helpers_sso_bypass_default_login_form_filter_false".
- */
- public function test_sso_helpers_sso_bypass_default_login_form_filter_false() {
- add_filter( 'jetpack_sso_bypass_login_forward_wpcom', '__return_false' );
- $this->assertFalse( Jetpack_SSO_Helpers::bypass_login_forward_wpcom() );
- remove_filter( 'jetpack_sso_bypass_login_forward_wpcom', '__return_false' );
- }
-
- /**
- * Test "sso_helpers_require_two_step_disabled".
- */
- public function test_sso_helpers_require_two_step_disabled() {
- add_filter( 'jetpack_sso_require_two_step', '__return_true' );
- $this->assertTrue( Jetpack_SSO_Helpers::is_require_two_step_checkbox_disabled() );
- remove_filter( 'jetpack_sso_require_two_step', '__return_true' );
- }
-
- /**
- * Test "sso_helpers_require_two_step_enabled".
- */
- public function test_sso_helpers_require_two_step_enabled() {
- $this->assertFalse( Jetpack_SSO_Helpers::is_require_two_step_checkbox_disabled() );
- }
-
- /**
- * Test "sso_helpers_match_by_email_disabled".
- */
- public function test_sso_helpers_match_by_email_disabled() {
- add_filter( 'jetpack_sso_match_by_email', '__return_true' );
- $this->assertTrue( Jetpack_SSO_Helpers::is_match_by_email_checkbox_disabled() );
- remove_filter( 'jetpack_sso_match_by_email', '__return_true' );
- }
-
- /**
- * Test "sso_helpers_match_by_email_enabled".
- */
- public function test_sso_helpers_match_by_email_enabled() {
- $this->assertFalse( Jetpack_SSO_Helpers::is_match_by_email_checkbox_disabled() );
- }
-
- /**
- * Test "allow_redirect_hosts_adds_default_hosts".
- */
- public function test_allow_redirect_hosts_adds_default_hosts() {
- $hosts = Jetpack_SSO_Helpers::allowed_redirect_hosts( array( 'test.com' ) );
- $this->assertIsArray( $hosts );
- $this->assertContains( 'test.com', $hosts );
- $this->assertContains( 'wordpress.com', $hosts );
- $this->assertContains( 'jetpack.wordpress.com', $hosts );
- }
-
- /**
- * Test "allowed_redirect_hosts_api_base_added".
- */
- public function test_allowed_redirect_hosts_api_base_added() {
- $hosts = Jetpack_SSO_Helpers::allowed_redirect_hosts(
- array( 'test.com' ),
- 'http://fakesite.com/jetpack.'
- );
- $this->assertIsArray( $hosts );
- $this->assertCount( 6, $hosts );
- $this->assertContains( 'fakesite.com', $hosts );
- }
-
- /**
- * Test "allowed_redirect_hosts_api_base_added_on_dev_version".
- */
- public function test_allowed_redirect_hosts_api_base_added_on_dev_version() {
- add_filter( 'jetpack_development_version', '__return_true' );
- $hosts = Jetpack_SSO_Helpers::allowed_redirect_hosts(
- array( 'test.com' ),
- 'http://fakesite.com/jetpack.'
- );
- $this->assertIsArray( $hosts );
- $this->assertCount( 6, $hosts );
- $this->assertContains( 'fakesite.com', $hosts );
- remove_filter( 'jetpack_development_version', '__return_true' );
- }
-
- /**
- * Test "generate_user_returns_user_when_username_not_exists".
- */
- public function test_generate_user_returns_user_when_username_not_exists() {
- $user = Jetpack_SSO_Helpers::generate_user( $this->user_data );
- $this->assertIsObject( $user );
- $this->assertInstanceOf( 'WP_User', $user );
-
- wp_delete_user( $user->ID );
- }
-
- /**
- * Test "generate_user_returns_user_if_username_exists_and_has_tries".
- */
- public function test_generate_user_returns_user_if_username_exists_and_has_tries() {
- add_filter( 'jetpack_sso_allowed_username_generate_retries', array( $this, 'return_one' ) );
- self::factory()->user->create( array( 'user_login' => $this->user_data->login ) );
-
- $user = Jetpack_SSO_Helpers::generate_user( $this->user_data );
-
- $this->assertIsObject( $user );
- $this->assertInstanceOf( 'WP_User', $user );
-
- // If the username contains the user's ID, we know the username was generated with our random algo.
- $this->assertStringContainsString( (string) $this->user_data->ID, $user->user_login );
-
- wp_delete_user( $user->ID );
- }
-
- /**
- * Test "generate_user_returns_false_when_no_more_tries_and_username_exists".
- */
- public function test_generate_user_returns_false_when_no_more_tries_and_username_exists() {
- add_filter( 'jetpack_sso_allowed_username_generate_retries', '__return_zero' );
- self::factory()->user->create( array( 'user_login' => $this->user_data->login ) );
- $this->assertFalse( Jetpack_SSO_Helpers::generate_user( $this->user_data ) );
- }
-
- /**
- * Test "generate_user_sets_user_role_when_provided".
- */
- public function test_generate_user_sets_user_role_when_provided() {
- $this->user_data->role = 'administrator';
- $user = Jetpack_SSO_Helpers::generate_user( $this->user_data );
- $this->assertContains( 'administrator', get_userdata( $user->ID )->roles );
- }
-
- /**
- * Test "extend_auth_cookie_casts_to_int".
- */
- public function test_extend_auth_cookie_casts_to_int() {
- add_filter( 'jetpack_sso_auth_cookie_expiration', array( $this, 'return_string_value' ) );
- $this->assertSame( (int) $this->return_string_value(), Jetpack_SSO_Helpers::extend_auth_cookie_expiration_for_sso() );
- remove_filter( 'jetpack_sso_auth_cookie_expiration', array( $this, 'return_string_value' ) );
- }
-
- /**
- * Test "extend_auth_cookie_default_value_greater_than_default".
- */
- public function test_extend_auth_cookie_default_value_greater_than_default() {
- $this->assertGreaterThan( 2 * DAY_IN_SECONDS, Jetpack_SSO_Helpers::extend_auth_cookie_expiration_for_sso() );
- }
-
- /**
- * Test "display_sso_form_for_action".
- */
- public function test_display_sso_form_for_action() {
- // Let's test the default cases.
- $this->assertTrue( Jetpack_SSO_Helpers::display_sso_form_for_action( 'login' ) );
- $this->assertTrue( Jetpack_SSO_Helpers::display_sso_form_for_action( 'jetpack_json_api_authorization' ) );
- $this->assertFalse( Jetpack_SSO_Helpers::display_sso_form_for_action( 'hello_world' ) );
-
- add_filter( 'jetpack_sso_allowed_actions', array( $this, 'allow_hello_world_login_action_for_sso' ) );
- $this->assertTrue( Jetpack_SSO_Helpers::display_sso_form_for_action( 'hello_world' ) );
- remove_filter( 'jetpack_sso_allowed_actions', array( $this, 'allow_hello_world_login_action_for_sso' ) );
- }
-
- /**
- * Test "get_json_api_auth_environment".
- */
- public function test_get_json_api_auth_environment() {
- // With no cookie returns false.
- $_COOKIE['jetpack_sso_original_request'] = '';
- $this->assertFalse( Jetpack_SSO_Helpers::get_json_api_auth_environment() );
-
- // With empty query, returns false.
- $_COOKIE['jetpack_sso_original_request'] = 'http://website.com';
- $this->assertFalse( Jetpack_SSO_Helpers::get_json_api_auth_environment() );
-
- // With empty no action query argument, returns false.
- $_COOKIE['jetpack_sso_original_request'] = 'http://website.com?hello=world';
- $this->assertFalse( Jetpack_SSO_Helpers::get_json_api_auth_environment() );
-
- // When action is not for JSON API auth, return false.
- $_COOKIE['jetpack_sso_original_request'] = 'http://website.com?action=loggedout';
- $this->assertFalse( Jetpack_SSO_Helpers::get_json_api_auth_environment() );
-
- // If we pass the other tests, then let's make sure we get the right information back.
- $original_request = 'http://website.com/wp-login.php?action=jetpack_json_api_authorization&token=my-token';
- $_COOKIE['jetpack_sso_original_request'] = $original_request;
- $environment = Jetpack_SSO_Helpers::get_json_api_auth_environment();
- $this->assertIsArray( $environment );
- $this->assertSame(
- array(
- 'action' => 'jetpack_json_api_authorization',
- 'token' => 'my-token',
- 'jetpack_json_api_original_query' => $original_request,
- ),
- $environment
- );
- }
-
- /**
- * Test the `get_custom_login_url()` helper.
- */
- public function test_get_custom_login_url() {
- $login_url_default = Jetpack_SSO_Helpers::get_custom_login_url();
-
- $custom_url_expected = 'test-login-url/';
-
- $custom_url_filter = function ( $login_url ) use ( $custom_url_expected ) {
- return str_replace( 'wp-login.php', $custom_url_expected, $login_url );
- };
- add_filter( 'login_url', $custom_url_filter );
- $login_url_custom = Jetpack_SSO_Helpers::get_custom_login_url();
-
- static::assertNull( $login_url_default );
- static::assertEquals( $custom_url_expected, $login_url_custom );
- }
-
- /**
- * Return string '1'.
- *
- * @return string
- */
- public function return_string_value() {
- return '1';
- }
-
- /**
- * Return "administrator".
- *
- * @return string
- */
- public function return_administrator() {
- return 'administrator';
- }
-
- /**
- * Return "foobarbaz".
- *
- * @return string
- */
- public function return_foobarbaz() {
- return 'foobarbaz';
- }
-
- /**
- * Add "hello_world" action.
- *
- * @param array $actions Actions.
- * @return array
- */
- public function allow_hello_world_login_action_for_sso( $actions ) {
- $actions[] = 'hello_world';
- return $actions;
- }
-}
diff --git a/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-callables.php b/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-callables.php
index 616435a87e5fb..bf8a49bfd02a3 100644
--- a/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-callables.php
+++ b/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-callables.php
@@ -2,6 +2,7 @@
use Automattic\Jetpack\Blocks;
use Automattic\Jetpack\Connection\Rest_Authentication as Connection_Rest_Authentication;
+use Automattic\Jetpack\Connection\SSO\Helpers;
use Automattic\Jetpack\Connection\Urls;
use Automattic\Jetpack\Constants;
use Automattic\Jetpack\Sync\Defaults;
@@ -116,11 +117,11 @@ public function test_sync_callable_whitelist() {
'post_type_features' => Functions::get_post_type_features(),
'rest_api_allowed_post_types' => Functions::rest_api_allowed_post_types(),
'rest_api_allowed_public_metadata' => Functions::rest_api_allowed_public_metadata(),
- 'sso_is_two_step_required' => Jetpack_SSO_Helpers::is_two_step_required(),
- 'sso_should_hide_login_form' => Jetpack_SSO_Helpers::should_hide_login_form(),
- 'sso_match_by_email' => Jetpack_SSO_Helpers::match_by_email(),
- 'sso_new_user_override' => Jetpack_SSO_Helpers::new_user_override(),
- 'sso_bypass_default_login_form' => Jetpack_SSO_Helpers::bypass_login_forward_wpcom(),
+ 'sso_is_two_step_required' => Helpers::is_two_step_required(),
+ 'sso_should_hide_login_form' => Helpers::should_hide_login_form(),
+ 'sso_match_by_email' => Helpers::match_by_email(),
+ 'sso_new_user_override' => Helpers::new_user_override(),
+ 'sso_bypass_default_login_form' => Helpers::bypass_login_forward_wpcom(),
'wp_version' => Functions::wp_version(),
'get_plugins' => Functions::get_plugins(),
'get_plugins_action_links' => Functions::get_plugins_action_links(),
diff --git a/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-users.php b/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-users.php
index 590f40be76815..cb4f828597bb3 100644
--- a/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-users.php
+++ b/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-users.php
@@ -1,5 +1,6 @@
get_invite_user_data() );
+ Utils::generate_user( $this->get_invite_user_data() );
$this->sender->do_sync();
Constants::clear_constants();
@@ -750,7 +751,7 @@ public function test_invite_user_sync_invite_event_false() {
// Fake it till we make it
Constants::set_constant( 'JETPACK_INVITE_ACCEPTED', false );
// We modify the input here to mimick the same call structure of the update user endpoint.
- Jetpack_SSO_Helpers::generate_user( $this->get_invite_user_data() );
+ Utils::generate_user( $this->get_invite_user_data() );
$this->sender->do_sync();
Constants::clear_constants();