[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/ -> authorize-application.php (source)

   1  <?php
   2  /**
   3   * Authorize Application Screen
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /** WordPress Administration Bootstrap */
  10  require_once  __DIR__ . '/admin.php';
  11  
  12  $error        = null;
  13  $new_password = '';
  14  
  15  // This is the no-js fallback script. Generally this will all be handled by `auth-app.js`.
  16  if ( isset( $_POST['action'] ) && 'authorize_application_password' === $_POST['action'] ) {
  17      check_admin_referer( 'authorize_application_password' );
  18  
  19      $success_url = $_POST['success_url'];
  20      $reject_url  = $_POST['reject_url'];
  21      $app_name    = $_POST['app_name'];
  22      $app_id      = $_POST['app_id'];
  23      $redirect    = '';
  24  
  25      if ( isset( $_POST['reject'] ) ) {
  26          if ( $reject_url ) {
  27              $redirect = $reject_url;
  28          } else {
  29              $redirect = admin_url();
  30          }
  31      } elseif ( isset( $_POST['approve'] ) ) {
  32          $created = WP_Application_Passwords::create_new_application_password(
  33              get_current_user_id(),
  34              array(
  35                  'name'   => $app_name,
  36                  'app_id' => $app_id,
  37              )
  38          );
  39  
  40          if ( is_wp_error( $created ) ) {
  41              $error = $created;
  42          } else {
  43              list( $new_password ) = $created;
  44  
  45              if ( $success_url ) {
  46                  $redirect = add_query_arg(
  47                      array(
  48                          'site_url'   => urlencode( site_url() ),
  49                          'user_login' => urlencode( wp_get_current_user()->user_login ),
  50                          'password'   => urlencode( $new_password ),
  51                      ),
  52                      $success_url
  53                  );
  54              }
  55          }
  56      }
  57  
  58      if ( $redirect ) {
  59          // Explicitly not using wp_safe_redirect b/c sends to arbitrary domain.
  60          wp_redirect( $redirect );
  61          exit;
  62      }
  63  }
  64  
  65  // Used in the HTML title tag.
  66  $title = __( 'Authorize Application' );
  67  
  68  $app_name    = ! empty( $_REQUEST['app_name'] ) ? $_REQUEST['app_name'] : '';
  69  $app_id      = ! empty( $_REQUEST['app_id'] ) ? $_REQUEST['app_id'] : '';
  70  $success_url = ! empty( $_REQUEST['success_url'] ) ? $_REQUEST['success_url'] : null;
  71  
  72  if ( ! empty( $_REQUEST['reject_url'] ) ) {
  73      $reject_url = $_REQUEST['reject_url'];
  74  } elseif ( $success_url ) {
  75      $reject_url = add_query_arg( 'success', 'false', $success_url );
  76  } else {
  77      $reject_url = null;
  78  }
  79  
  80  $user = wp_get_current_user();
  81  
  82  $request  = compact( 'app_name', 'app_id', 'success_url', 'reject_url' );
  83  $is_valid = wp_is_authorize_application_password_request_valid( $request, $user );
  84  
  85  if ( is_wp_error( $is_valid ) ) {
  86      wp_die(
  87          __( 'The Authorize Application request is not allowed.' ) . ' ' . implode( ' ', $is_valid->get_error_messages() ),
  88          __( 'Cannot Authorize Application' )
  89      );
  90  }
  91  
  92  if ( wp_is_site_protected_by_basic_auth( 'front' ) ) {
  93      wp_die(
  94          __( 'Your website appears to use Basic Authentication, which is not currently compatible with application passwords.' ),
  95          __( 'Cannot Authorize Application' ),
  96          array(
  97              'response'  => 501,
  98              'link_text' => __( 'Go Back' ),
  99              'link_url'  => $reject_url ? add_query_arg( 'error', 'disabled', $reject_url ) : admin_url(),
 100          )
 101      );
 102  }
 103  
 104  if ( ! wp_is_application_passwords_available_for_user( $user ) ) {
 105      if ( wp_is_application_passwords_available() ) {
 106          $message = __( 'Application passwords are not available for your account. Please contact the site administrator for assistance.' );
 107      } else {
 108          $message = __( 'Application passwords are not available.' );
 109      }
 110  
 111      wp_die(
 112          $message,
 113          __( 'Cannot Authorize Application' ),
 114          array(
 115              'response'  => 501,
 116              'link_text' => __( 'Go Back' ),
 117              'link_url'  => $reject_url ? add_query_arg( 'error', 'disabled', $reject_url ) : admin_url(),
 118          )
 119      );
 120  }
 121  
 122  wp_enqueue_script( 'auth-app' );
 123  wp_localize_script(
 124      'auth-app',
 125      'authApp',
 126      array(
 127          'site_url'   => site_url(),
 128          'user_login' => $user->user_login,
 129          'success'    => $success_url,
 130          'reject'     => $reject_url ? $reject_url : admin_url(),
 131      )
 132  );
 133  
 134  require_once ABSPATH . 'wp-admin/admin-header.php';
 135  
 136  ?>
 137  <div class="wrap">
 138      <h1><?php echo esc_html( $title ); ?></h1>
 139  
 140      <?php if ( is_wp_error( $error ) ) : ?>
 141          <div class="notice notice-error"><p><?php echo $error->get_error_message(); ?></p></div>
 142      <?php endif; ?>
 143  
 144      <div class="card auth-app-card">
 145          <h2 class="title"><?php _e( 'An application would like to connect to your account.' ); ?></h2>
 146          <?php if ( $app_name ) : ?>
 147              <p>
 148                  <?php
 149                  printf(
 150                      /* translators: %s: Application name. */
 151                      __( 'Would you like to give the application identifying itself as %s access to your account? You should only do this if you trust the application in question.' ),
 152                      '<strong>' . esc_html( $app_name ) . '</strong>'
 153                  );
 154                  ?>
 155              </p>
 156          <?php else : ?>
 157              <p><?php _e( 'Would you like to give this application access to your account? You should only do this if you trust the application in question.' ); ?></p>
 158          <?php endif; ?>
 159  
 160          <?php
 161          if ( is_multisite() ) {
 162              $blogs       = get_blogs_of_user( $user->ID, true );
 163              $blogs_count = count( $blogs );
 164              if ( $blogs_count > 1 ) {
 165                  ?>
 166                  <p>
 167                      <?php
 168                      printf(
 169                          /* translators: 1: URL to my-sites.php, 2: Number of sites the user has. */
 170                          _n(
 171                              'This will grant access to <a href="%1$s">the %2$s site in this installation that you have permissions on</a>.',
 172                              'This will grant access to <a href="%1$s">all %2$s sites in this installation that you have permissions on</a>.',
 173                              $blogs_count
 174                          ),
 175                          admin_url( 'my-sites.php' ),
 176                          number_format_i18n( $blogs_count )
 177                      );
 178                      ?>
 179                  </p>
 180                  <?php
 181              }
 182          }
 183          ?>
 184  
 185          <?php if ( $new_password ) : ?>
 186              <div class="notice notice-success notice-alt below-h2">
 187                  <p class="application-password-display">
 188                      <label for="new-application-password-value">
 189                          <?php
 190                          printf(
 191                              /* translators: %s: Application name. */
 192                              esc_html__( 'Your new password for %s is:' ),
 193                              '<strong>' . esc_html( $app_name ) . '</strong>'
 194                          );
 195                          ?>
 196                      </label>
 197                      <input id="new-application-password-value" type="text" class="code" readonly="readonly" value="<?php esc_attr( WP_Application_Passwords::chunk_password( $new_password ) ); ?>" />
 198                  </p>
 199                  <p><?php _e( 'Be sure to save this in a safe location. You will not be able to retrieve it.' ); ?></p>
 200              </div>
 201  
 202              <?php
 203              /**
 204               * Fires in the Authorize Application Password new password section in the no-JS version.
 205               *
 206               * In most cases, this should be used in combination with the {@see 'wp_application_passwords_approve_app_request_success'}
 207               * action to ensure that both the JS and no-JS variants are handled.
 208               *
 209               * @since 5.6.0
 210               * @since 5.6.1 Corrected action name and signature.
 211               *
 212               * @param string  $new_password The newly generated application password.
 213               * @param array   $request      The array of request data. All arguments are optional and may be empty.
 214               * @param WP_User $user         The user authorizing the application.
 215               */
 216              do_action( 'wp_authorize_application_password_form_approved_no_js', $new_password, $request, $user );
 217              ?>
 218          <?php else : ?>
 219              <form action="<?php echo esc_url( admin_url( 'authorize-application.php' ) ); ?>" method="post" class="form-wrap">
 220                  <?php wp_nonce_field( 'authorize_application_password' ); ?>
 221                  <input type="hidden" name="action" value="authorize_application_password" />
 222                  <input type="hidden" name="app_id" value="<?php echo esc_attr( $app_id ); ?>" />
 223                  <input type="hidden" name="success_url" value="<?php echo esc_url( $success_url ); ?>" />
 224                  <input type="hidden" name="reject_url" value="<?php echo esc_url( $reject_url ); ?>" />
 225  
 226                  <div class="form-field">
 227                      <label for="app_name"><?php _e( 'New Application Password Name' ); ?></label>
 228                      <input type="text" id="app_name" name="app_name" value="<?php echo esc_attr( $app_name ); ?>" placeholder="<?php esc_attr_e( 'WordPress App on My Phone' ); ?>" required />
 229                  </div>
 230  
 231                  <?php
 232                  /**
 233                   * Fires in the Authorize Application Password form before the submit buttons.
 234                   *
 235                   * @since 5.6.0
 236                   *
 237                   * @param array   $request {
 238                   *     The array of request data. All arguments are optional and may be empty.
 239                   *
 240                   *     @type string $app_name    The suggested name of the application.
 241                   *     @type string $success_url The url the user will be redirected to after approving the application.
 242                   *     @type string $reject_url  The url the user will be redirected to after rejecting the application.
 243                   * }
 244                   * @param WP_User $user The user authorizing the application.
 245                   */
 246                  do_action( 'wp_authorize_application_password_form', $request, $user );
 247                  ?>
 248  
 249                  <?php
 250                  submit_button(
 251                      __( 'Yes, I approve of this connection' ),
 252                      'primary',
 253                      'approve',
 254                      false,
 255                      array(
 256                          'aria-describedby' => 'description-approve',
 257                      )
 258                  );
 259                  ?>
 260                  <p class="description" id="description-approve">
 261                      <?php
 262                      if ( $success_url ) {
 263                          printf(
 264                              /* translators: %s: The URL the user is being redirected to. */
 265                              __( 'You will be sent to %s' ),
 266                              '<strong><code>' . esc_html(
 267                                  add_query_arg(
 268                                      array(
 269                                          'site_url'   => site_url(),
 270                                          'user_login' => $user->user_login,
 271                                          'password'   => '[------]',
 272                                      ),
 273                                      $success_url
 274                                  )
 275                              ) . '</code></strong>'
 276                          );
 277                      } else {
 278                          _e( 'You will be given a password to manually enter into the application in question.' );
 279                      }
 280                      ?>
 281                  </p>
 282  
 283                  <?php
 284                  submit_button(
 285                      __( 'No, I do not approve of this connection' ),
 286                      'secondary',
 287                      'reject',
 288                      false,
 289                      array(
 290                          'aria-describedby' => 'description-reject',
 291                      )
 292                  );
 293                  ?>
 294                  <p class="description" id="description-reject">
 295                      <?php
 296                      if ( $reject_url ) {
 297                          printf(
 298                              /* translators: %s: The URL the user is being redirected to. */
 299                              __( 'You will be sent to %s' ),
 300                              '<strong><code>' . esc_html( $reject_url ) . '</code></strong>'
 301                          );
 302                      } else {
 303                          _e( 'You will be returned to the WordPress Dashboard, and no changes will be made.' );
 304                      }
 305                      ?>
 306                  </p>
 307              </form>
 308          <?php endif; ?>
 309      </div>
 310  </div>
 311  <?php
 312  
 313  require_once ABSPATH . 'wp-admin/admin-footer.php';


Generated: Sun Sep 26 01:00:08 2021 Cross-referenced by PHPXref 0.7.1