[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/js/ -> auth-app.js (source)

   1  /**
   2   * @output wp-admin/js/auth-app.js
   3   */
   4  
   5  /* global authApp */
   6  
   7  ( function( $, authApp ) {
   8      var $appNameField = $( '#app_name' ),
   9          $approveBtn = $( '#approve' ),
  10          $rejectBtn = $( '#reject' ),
  11          $form = $appNameField.closest( 'form' ),
  12          context = {
  13              userLogin: authApp.user_login,
  14              successUrl: authApp.success,
  15              rejectUrl: authApp.reject
  16          };
  17  
  18      $approveBtn.click( function( e ) {
  19          var name = $appNameField.val(),
  20              appId = $( 'input[name="app_id"]', $form ).val();
  21  
  22          e.preventDefault();
  23  
  24          if ( $approveBtn.prop( 'aria-disabled' ) ) {
  25              return;
  26          }
  27  
  28          if ( 0 === name.length ) {
  29              $appNameField.focus();
  30              return;
  31          }
  32  
  33          $approveBtn.prop( 'aria-disabled', true ).addClass( 'disabled' );
  34  
  35          var request = {
  36              name: name
  37          };
  38  
  39          if ( appId.length > 0 ) {
  40              request.app_id = appId;
  41          }
  42  
  43          /**
  44           * Filters the request data used to Authorize an Application Password request.
  45           *
  46           * @since 5.6.0
  47           *
  48           * @param {Object} request            The request data.
  49           * @param {Object} context            Context about the Application Password request.
  50           * @param {string} context.userLogin  The user's login username.
  51           * @param {string} context.successUrl The URL the user will be redirected to after approving the request.
  52           * @param {string} context.rejectUrl  The URL the user will be redirected to after rejecting the request.
  53           */
  54          request = wp.hooks.applyFilters( 'wp_application_passwords_approve_app_request', request, context );
  55  
  56          wp.apiRequest( {
  57              path: '/wp/v2/users/me/application-passwords',
  58              method: 'POST',
  59              data: request
  60          } ).done( function( response, textStatus, jqXHR ) {
  61  
  62              /**
  63               * Fires when an Authorize Application Password request has been successfully approved.
  64               *
  65               * @since 5.6.0
  66               *
  67               * @param {Object} response          The response from the REST API.
  68               * @param {string} response.password The newly created password.
  69               * @param {string} textStatus        The status of the request.
  70               * @param {jqXHR}  jqXHR             The underlying jqXHR object that made the request.
  71               */
  72              wp.hooks.doAction( 'wp_application_passwords_approve_app_request_success', response, textStatus, jqXHR );
  73  
  74              var raw = authApp.success,
  75                  url, message, $notice;
  76  
  77              if ( raw ) {
  78                  url = raw + ( -1 === raw.indexOf( '?' ) ? '?' : '&' ) +
  79                      'site_url=' + encodeURIComponent( authApp.site_url ) +
  80                      '&user_login=' + encodeURIComponent( authApp.user_login ) +
  81                      '&password=' + encodeURIComponent( response.password );
  82  
  83                  window.location = url;
  84              } else {
  85                  message = wp.i18n.sprintf(
  86                      /* translators: %s: Application name. */
  87                      '<label for="new-application-password-value">' + wp.i18n.__( 'Your new password for %s is:' ) + '</label>',
  88                      '<strong></strong>'
  89                  ) + ' <input id="new-application-password-value" type="text" class="code" readonly="readonly" value="" />';
  90                  $notice = $( '<div></div>' )
  91                      .attr( 'role', 'alert' )
  92                      .attr( 'tabindex', -1 )
  93                      .addClass( 'notice notice-success notice-alt' )
  94                      .append( $( '<p></p>' ).addClass( 'application-password-display' ).html( message ) )
  95                      .append( '<p>' + wp.i18n.__( 'Be sure to save this in a safe location. You will not be able to retrieve it.' ) + '</p>' );
  96  
  97                  // We're using .text() to write the variables to avoid any chance of XSS.
  98                  $( 'strong', $notice ).text( name );
  99                  $( 'input', $notice ).val( response.password );
 100  
 101                  $form.replaceWith( $notice );
 102                  $notice.focus();
 103              }
 104          } ).fail( function( jqXHR, textStatus, errorThrown ) {
 105              var errorMessage = errorThrown,
 106                  error = null;
 107  
 108              if ( jqXHR.responseJSON ) {
 109                  error = jqXHR.responseJSON;
 110  
 111                  if ( error.message ) {
 112                      errorMessage = error.message;
 113                  }
 114              }
 115  
 116              var $notice = $( '<div></div>' )
 117                  .attr( 'role', 'alert' )
 118                  .addClass( 'notice notice-error' )
 119                  .append( $( '<p></p>' ).text( errorMessage ) );
 120  
 121              $( 'h1' ).after( $notice );
 122  
 123              $approveBtn.removeProp( 'aria-disabled', false ).removeClass( 'disabled' );
 124  
 125              /**
 126               * Fires when an Authorize Application Password request encountered an error when trying to approve the request.
 127               *
 128               * @since 5.6.0
 129               *
 130               * @param {Object|null} error       The error from the REST API. May be null if the server did not send proper JSON.
 131               * @param {string}      textStatus  The status of the request.
 132               * @param {string}      errorThrown The error message associated with the response status code.
 133               * @param {jqXHR}       jqXHR       The underlying jqXHR object that made the request.
 134               */
 135              wp.hooks.doAction( 'wp_application_passwords_approve_app_request_success', error, textStatus, jqXHR );
 136          } );
 137      } );
 138  
 139      $rejectBtn.click( function( e ) {
 140          e.preventDefault();
 141  
 142          /**
 143           * Fires when an Authorize Application Password request has been rejected by the user.
 144           *
 145           * @since 5.6.0
 146           *
 147           * @param {Object} context            Context about the Application Password request.
 148           * @param {string} context.userLogin  The user's login username.
 149           * @param {string} context.successUrl The URL the user will be redirected to after approving the request.
 150           * @param {string} context.rejectUrl  The URL the user will be redirected to after rejecting the request.
 151           */
 152          wp.hooks.doAction( 'wp_application_passwords_reject_app', context );
 153  
 154          // @todo: Make a better way to do this so it feels like less of a semi-open redirect.
 155          window.location = authApp.reject;
 156      } );
 157  
 158      $form.on( 'submit', function( e ) {
 159          e.preventDefault();
 160      } );
 161  }( jQuery, authApp ) );


Generated: Mon Nov 30 01:00:03 2020 Cross-referenced by PHPXref 0.7.1