class.jetpack-sso-notices.php 6.0 KB

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