No Description

class.jetpack-sso-notices.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. use Automattic\Jetpack\Redirect;
  3. if ( ! class_exists( 'Jetpack_SSO_Notices' ) ) :
  4. /**
  5. * A collection of helper functions used in the SSO module.
  6. *
  7. * @since 4.4.0
  8. */
  9. class Jetpack_SSO_Notices {
  10. /**
  11. * Error message displayed on the login form when two step is required and
  12. * the user's account on WordPress.com does not have two step enabled.
  13. *
  14. * @since 2.7
  15. * @param string $message
  16. * @return string
  17. **/
  18. public static function error_msg_enable_two_step( $message ) {
  19. $error = sprintf(
  20. wp_kses(
  21. __(
  22. 'Two-Step Authentication is required to access this site. Please visit your <a href="%1$s" rel="noopener noreferrer" target="_blank">Security Settings</a> to configure <a href="%2$s" rel="noopener noreferrer" target="_blank">Two-step Authentication</a> for your account.',
  23. 'jetpack'
  24. ),
  25. array( 'a' => array( 'href' => array() ) )
  26. ),
  27. Redirect::get_url( 'calypso-me-security-two-step' ),
  28. Redirect::get_url( 'wpcom-support-security-two-step-authentication' )
  29. );
  30. $message .= sprintf( '<p class="message" id="login_error">%s</p>', $error );
  31. return $message;
  32. }
  33. /**
  34. * Error message displayed when the user tries to SSO, but match by email
  35. * is off and they already have an account with their email address on
  36. * this site.
  37. *
  38. * @param string $message
  39. * @return string
  40. */
  41. public static function error_msg_email_already_exists( $message ) {
  42. $error = sprintf(
  43. wp_kses(
  44. __(
  45. 'You already have an account on this site. Please <a href="%1$s">sign in</a> with your username and password and then connect to WordPress.com.',
  46. 'jetpack'
  47. ),
  48. array( 'a' => array( 'href' => array() ) )
  49. ),
  50. esc_url_raw( add_query_arg( 'jetpack-sso-show-default-form', '1', wp_login_url() ) )
  51. );
  52. $message .= sprintf( '<p class="message" id="login_error">%s</p>', $error );
  53. return $message;
  54. }
  55. /**
  56. * Error message that is displayed when the current site is in an identity crisis and SSO can not be used.
  57. *
  58. * @since 4.3.2
  59. *
  60. * @param $message
  61. *
  62. * @return string
  63. */
  64. public static function error_msg_identity_crisis( $message ) {
  65. $error = esc_html__( 'Logging in with WordPress.com is not currently available because this site is experiencing connection problems.', 'jetpack' );
  66. $message .= sprintf( '<p class="message" id="login_error">%s</p>', $error );
  67. return $message;
  68. }
  69. /**
  70. * Error message that is displayed when we are not able to verify the SSO nonce due to an XML error or
  71. * failed validation. In either case, we prompt the user to try again or log in with username and password.
  72. *
  73. * @since 4.3.2
  74. *
  75. * @param $message
  76. *
  77. * @return string
  78. */
  79. public static function error_invalid_response_data( $message ) {
  80. $error = esc_html__(
  81. 'There was an error logging you in via WordPress.com, please try again or try logging in with your username and password.',
  82. 'jetpack'
  83. );
  84. $message .= sprintf( '<p class="message" id="login_error">%s</p>', $error );
  85. return $message;
  86. }
  87. /**
  88. * Error message that is displayed when we were not able to automatically create an account for a user
  89. * after a user has logged in via SSO. By default, this message is triggered after trying to create an account 5 times.
  90. *
  91. * @since 4.3.2
  92. *
  93. * @param $message
  94. *
  95. * @return string
  96. */
  97. public static function error_unable_to_create_user( $message ) {
  98. $error = esc_html__(
  99. 'There was an error creating a user for you. Please contact the administrator of your site.',
  100. 'jetpack'
  101. );
  102. $message .= sprintf( '<p class="message" id="login_error">%s</p>', $error );
  103. return $message;
  104. }
  105. /**
  106. * When the default login form is hidden, this method is called on the 'authenticate' filter with a priority of 30.
  107. * This method disables the ability to submit the default login form.
  108. *
  109. * @param $user
  110. *
  111. * @return WP_Error
  112. */
  113. public static function disable_default_login_form( $user ) {
  114. if ( is_wp_error( $user ) ) {
  115. return $user;
  116. }
  117. /**
  118. * Since we're returning an error that will be shown as a red notice, let's remove the
  119. * informational "blue" notice.
  120. */
  121. remove_filter( 'login_message', array( 'Jetpack_SSO_Notices', 'msg_login_by_jetpack' ) );
  122. return new WP_Error( 'jetpack_sso_required', self::get_sso_required_message() );
  123. }
  124. /**
  125. * Message displayed when the site admin has disabled the default WordPress
  126. * login form in Settings > General > Secure Sign On
  127. *
  128. * @since 2.7
  129. * @param string $message
  130. *
  131. * @return string
  132. **/
  133. public static function msg_login_by_jetpack( $message ) {
  134. $message .= sprintf( '<p class="message">%s</p>', self::get_sso_required_message() );
  135. return $message;
  136. }
  137. public static function get_sso_required_message() {
  138. $msg = esc_html__(
  139. 'A WordPress.com account is required to access this site. Click the button below to sign in or create a free WordPress.com account.',
  140. 'jetpack'
  141. );
  142. /**
  143. * Filter the message displayed when the default WordPress login form is disabled.
  144. *
  145. * @module sso
  146. *
  147. * @since 2.8.0
  148. *
  149. * @param string $msg Disclaimer when default WordPress login form is disabled.
  150. */
  151. return apply_filters( 'jetpack_sso_disclaimer_message', $msg );
  152. }
  153. /**
  154. * Message displayed when the user can not be found after approving the SSO process on WordPress.com
  155. *
  156. * @param string $message
  157. * @return string
  158. */
  159. public static function cant_find_user( $message ) {
  160. $error = esc_html__(
  161. "We couldn't find your account. If you already have an account, make sure you have connected to WordPress.com.",
  162. 'jetpack'
  163. );
  164. $message .= sprintf( '<p class="message" id="login_error">%s</p>', $error );
  165. return $message;
  166. }
  167. /**
  168. * Error message that is displayed when the current site is in an identity crisis and SSO can not be used.
  169. *
  170. * @since 4.4.0
  171. *
  172. * @param $message
  173. *
  174. * @return string
  175. */
  176. public static function sso_not_allowed_in_staging( $message ) {
  177. $error = esc_html__(
  178. 'Logging in with WordPress.com is disabled for sites that are in staging mode.',
  179. 'jetpack'
  180. );
  181. $message .= sprintf( '<p class="message">%s</p>', $error );
  182. return $message;
  183. }
  184. }
  185. endif;