[ 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  $title = __( 'Authorize Application' );
  66  
  67  $app_name    = ! empty( $_REQUEST['app_name'] ) ? $_REQUEST['app_name'] : '';
  68  $app_id      = ! empty( $_REQUEST['app_id'] ) ? $_REQUEST['app_id'] : '';
  69  $success_url = ! empty( $_REQUEST['success_url'] ) ? $_REQUEST['success_url'] : null;
  70  
  71  if ( ! empty( $_REQUEST['reject_url'] ) ) {
  72      $reject_url = $_REQUEST['reject_url'];
  73  } elseif ( $success_url ) {
  74      $reject_url = add_query_arg( 'success', 'false', $success_url );
  75  } else {
  76      $reject_url = null;
  77  }
  78  
  79  $user = wp_get_current_user();
  80  
  81  $request  = compact( 'app_name', 'app_id', 'success_url', 'reject_url' );
  82  $is_valid = wp_is_authorize_application_password_request_valid( $request, $user );
  83  
  84  if ( is_wp_error( $is_valid ) ) {
  85      wp_die(
  86          __( 'The Authorize Application request is not allowed.' ) . ' ' . implode( ' ', $is_valid->get_error_messages() ),
  87          __( 'Cannot Authorize Application' )
  88      );
  89  }
  90  
  91  if ( ! wp_is_application_passwords_available_for_user( $user ) ) {
  92      if ( wp_is_application_passwords_available() ) {
  93          $message = __( 'Application passwords are not available for your account. Please contact the site administrator for assistance.' );
  94      } else {
  95          $message = __( 'Application passwords are not available.' );
  96      }
  97  
  98      wp_die(
  99          $message,
 100          __( 'Cannot Authorize Application' ),
 101          array(
 102              'response'  => 501,
 103              'link_text' => __( 'Go Back' ),
 104              'link_url'  => $reject_url ? add_query_arg( 'error', 'disabled', $reject_url ) : admin_url(),
 105          )
 106      );
 107  }
 108  
 109  wp_enqueue_script( 'auth-app' );
 110  wp_localize_script(
 111      'auth-app',
 112      'authApp',
 113      array(
 114          'site_url'   => site_url(),
 115          'user_login' => $user->user_login,
 116          'success'    => $success_url,
 117          'reject'     => $reject_url ? $reject_url : admin_url(),
 118      )
 119  );
 120  
 121  require_once ABSPATH . 'wp-admin/admin-header.php';
 122  
 123  ?>
 124  <div class="wrap">
 125      <h1><?php echo esc_html( $title ); ?></h1>
 126  
 127      <?php if ( is_wp_error( $error ) ) : ?>
 128          <div class="notice notice-error"><p><?php echo $error->get_error_message(); ?></p></div>
 129      <?php endif; ?>
 130  
 131      <div class="card auth-app-card">
 132          <h2 class="title"><?php __( 'An application would like to connect to your account.' ); ?></h2>
 133          <?php if ( $app_name ) : ?>
 134              <p>
 135                  <?php
 136                  printf(
 137                      /* translators: %s: Application name. */
 138                      __( 'Would you like to give the application identifying itself as %s access to your account? You should only do this if you trust the app in question.' ),
 139                      '<strong>' . esc_html( $app_name ) . '</strong>'
 140                  );
 141                  ?>
 142              </p>
 143          <?php else : ?>
 144              <p><?php _e( 'Would you like to give this application access to your account? You should only do this if you trust the app in question.' ); ?></p>
 145          <?php endif; ?>
 146  
 147          <?php
 148          if ( is_multisite() ) {
 149              $blogs       = get_blogs_of_user( $user->ID, true );
 150              $blogs_count = count( $blogs );
 151              if ( $blogs_count > 1 ) {
 152                  ?>
 153                  <p>
 154                      <?php
 155                      printf(
 156                          /* translators: 1: URL to my-sites.php, 2: Number of blogs the user has. */
 157                          _n(
 158                              'This will grant access to <a href="%1$s">the %2$s blog in this installation that you have permissions on</a>.',
 159                              'This will grant access to <a href="%1$s">all %2$s blogs in this installation that you have permissions on</a>.',
 160                              $blogs_count
 161                          ),
 162                          admin_url( 'my-sites.php' ),
 163                          number_format_i18n( $blogs_count )
 164                      );
 165                      ?>
 166                  </p>
 167                  <?php
 168              }
 169          }
 170          ?>
 171  
 172          <?php if ( $new_password ) : ?>
 173              <div class="notice notice-success notice-alt below-h2">
 174                  <p class="application-password-display">
 175                      <label for="new-application-password-value">
 176                          <?php
 177                          printf(
 178                              /* translators: %s: Application name. */
 179                              esc_html__( 'Your new password for %s is:' ),
 180                              '<strong>' . esc_html( $app_name ) . '</strong>'
 181                          );
 182                          ?>
 183                      </label>
 184                      <input id="new-application-password-value" type="text" class="code" readonly="readonly" value="<?php esc_attr( WP_Application_Passwords::chunk_password( $new_password ) ); ?>" />
 185                  </p>
 186                  <p><?php _e( 'Be sure to save this in a safe location. You will not be able to retrieve it.' ); ?></p>
 187              </div>
 188  
 189              <?php
 190              /**
 191               * Fires in the Authorize Application Password new password section.
 192               *
 193               * @since 5.6.0
 194               *
 195               * @param string  $new_password The newly generated application password.
 196               * @param array   $request      The array of request data. All arguments are optional and may be empty.
 197               * @param WP_User $user         The user authorizing the application.
 198               */
 199              do_action( 'wp_authorize_application_password_form', $request, $user );
 200              ?>
 201          <?php else : ?>
 202              <form action="<?php echo esc_url( admin_url( 'authorize-application.php' ) ); ?>" method="post" class="form-wrap">
 203                  <?php wp_nonce_field( 'authorize_application_password' ); ?>
 204                  <input type="hidden" name="action" value="authorize_application_password" />
 205                  <input type="hidden" name="app_id" value="<?php echo esc_attr( $app_id ); ?>" />
 206                  <input type="hidden" name="success_url" value="<?php echo esc_url( $success_url ); ?>" />
 207                  <input type="hidden" name="reject_url" value="<?php echo esc_url( $reject_url ); ?>" />
 208  
 209                  <div class="form-field">
 210                      <label for="app_name"><?php _e( 'New Application Password Name' ); ?></label>
 211                      <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 />
 212                  </div>
 213  
 214                  <?php
 215                  /**
 216                   * Fires in the Authorize Application Password form before the submit buttons.
 217                   *
 218                   * @since 5.6.0
 219                   *
 220                   * @param array   $request {
 221                   *     The array of request data. All arguments are optional and may be empty.
 222                   *
 223                   *     @type string $app_name    The suggested name of the application.
 224                   *     @type string $success_url The url the user will be redirected to after approving the application.
 225                   *     @type string $reject_url  The url the user will be redirected to after rejecting the application.
 226                   * }
 227                   * @param WP_User $user The user authorizing the application.
 228                   */
 229                  do_action( 'wp_authorize_application_password_form', $request, $user );
 230                  ?>
 231  
 232                  <?php
 233                  submit_button(
 234                      __( 'Yes, I approve of this connection.' ),
 235                      'primary',
 236                      'approve',
 237                      false,
 238                      array(
 239                          'aria-describedby' => 'description-approve',
 240                      )
 241                  );
 242                  ?>
 243                  <p class="description" id="description-approve">
 244                      <?php
 245                      if ( $success_url ) {
 246                          printf(
 247                              /* translators: %s: The URL the user is being redirected to. */
 248                              __( 'You will be sent to %s' ),
 249                              '<strong><kbd>' . esc_html(
 250                                  add_query_arg(
 251                                      array(
 252                                          'site_url'   => site_url(),
 253                                          'user_login' => $user->user_login,
 254                                          'password'   => '[------]',
 255                                      ),
 256                                      $success_url
 257                                  )
 258                              ) . '</kbd></strong>'
 259                          );
 260                      } else {
 261                          _e( 'You will be given a password to manually enter into the application in question.' );
 262                      }
 263                      ?>
 264                  </p>
 265  
 266                  <?php
 267                  submit_button(
 268                      __( 'No, I do not approve of this connection.' ),
 269                      'secondary',
 270                      'reject',
 271                      false,
 272                      array(
 273                          'aria-describedby' => 'description-reject',
 274                      )
 275                  );
 276                  ?>
 277                  <p class="description" id="description-reject">
 278                      <?php
 279                      if ( $reject_url ) {
 280                          printf(
 281                              /* translators: %s: The URL the user is being redirected to. */
 282                              __( 'You will be sent to %s' ),
 283                              '<strong><kbd>' . esc_html( $reject_url ) . '</kbd></strong>'
 284                          );
 285                      } else {
 286                          _e( 'You will be returned to the WordPress Dashboard, and no changes will be made.' );
 287                      }
 288                      ?>
 289                  </p>
 290              </form>
 291          <?php endif; ?>
 292      </div>
 293  </div>
 294  <?php
 295  
 296  require_once ABSPATH . 'wp-admin/admin-footer.php';


Generated: Wed Dec 2 01:00:04 2020 Cross-referenced by PHPXref 0.7.1