[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/js/ -> application-passwords.js (source)

   1  /**
   2   * @output wp-admin/js/application-passwords.js
   3   */
   4  
   5  ( function( $ ) {
   6      var $appPassSection = $( '#application-passwords-section' ),
   7          $newAppPassForm = $appPassSection.find( '.create-application-password' ),
   8          $newAppPassField = $newAppPassForm.find( '.input' ),
   9          $newAppPassButton = $newAppPassForm.find( '.button' ),
  10          $appPassTwrapper = $appPassSection.find( '.application-passwords-list-table-wrapper' ),
  11          $appPassTbody = $appPassSection.find( 'tbody' ),
  12          $appPassTrNoItems = $appPassTbody.find( '.no-items' ),
  13          $removeAllBtn = $( '#revoke-all-application-passwords' ),
  14          tmplNewAppPass = wp.template( 'new-application-password' ),
  15          tmplAppPassRow = wp.template( 'application-password-row' ),
  16          userId = $( '#user_id' ).val();
  17  
  18      $newAppPassButton.click( function( e ) {
  19          e.preventDefault();
  20  
  21          if ( $newAppPassButton.prop( 'aria-disabled' ) ) {
  22              return;
  23          }
  24  
  25          var name = $newAppPassField.val();
  26  
  27          if ( 0 === name.length ) {
  28              $newAppPassField.focus();
  29              return;
  30          }
  31  
  32          clearNotices();
  33          $newAppPassButton.prop( 'aria-disabled', true ).addClass( 'disabled' );
  34  
  35          var request = {
  36              name: name
  37          };
  38  
  39          /**
  40           * Filters the request data used to create a new Application Password.
  41           *
  42           * @since 5.6.0
  43           *
  44           * @param {Object} request The request data.
  45           * @param {number} userId  The id of the user the password is added for.
  46           */
  47          request = wp.hooks.applyFilters( 'wp_application_passwords_new_password_request', request, userId );
  48  
  49          wp.apiRequest( {
  50              path: '/wp/v2/users/' + userId + '/application-passwords',
  51              method: 'POST',
  52              data: request
  53          } ).always( function() {
  54              $newAppPassButton.removeProp( 'aria-disabled' ).removeClass( 'disabled' );
  55          } ).done( function( response ) {
  56              $newAppPassField.val( '' );
  57              $newAppPassButton.prop( 'disabled', false );
  58  
  59              $newAppPassForm.after( tmplNewAppPass( {
  60                  name: name,
  61                  password: response.password
  62              } ) );
  63              $( '.new-application-password-notice' ).focus();
  64  
  65              $appPassTbody.prepend( tmplAppPassRow( response ) );
  66  
  67              $appPassTwrapper.show();
  68              $appPassTrNoItems.remove();
  69  
  70              /**
  71               * Fires after an application password has been successfully created.
  72               *
  73               * @since 5.6.0
  74               *
  75               * @param {Object} response The response data from the REST API.
  76               * @param {Object} request  The request data used to create the password.
  77               */
  78              wp.hooks.doAction( 'wp_application_passwords_created_password', response, request );
  79          } ).fail( handleErrorResponse );
  80      } );
  81  
  82      $appPassTbody.on( 'click', '.delete', function( e ) {
  83          e.preventDefault();
  84  
  85          if ( ! window.confirm( wp.i18n.__( 'Are you sure you want to revoke this password? This action cannot be undone.' ) ) ) {
  86              return;
  87          }
  88  
  89          var $submitButton = $( this ),
  90              $tr = $submitButton.closest( 'tr' ),
  91              uuid = $tr.data( 'uuid' );
  92  
  93          clearNotices();
  94          $submitButton.prop( 'disabled', true );
  95  
  96          wp.apiRequest( {
  97              path: '/wp/v2/users/' + userId + '/application-passwords/' + uuid,
  98              method: 'DELETE'
  99          } ).always( function() {
 100              $submitButton.prop( 'disabled', false );
 101          } ).done( function( response ) {
 102              if ( response.deleted ) {
 103                  if ( 0 === $tr.siblings().length ) {
 104                      $appPassTwrapper.hide();
 105                  }
 106                  $tr.remove();
 107  
 108                  addNotice( wp.i18n.__( 'Application password revoked.' ), 'success' ).focus();
 109              }
 110          } ).fail( handleErrorResponse );
 111      } );
 112  
 113      $removeAllBtn.on( 'click', function( e ) {
 114          e.preventDefault();
 115  
 116          if ( ! window.confirm( wp.i18n.__( 'Are you sure you want to revoke all passwords? This action cannot be undone.' ) ) ) {
 117              return;
 118          }
 119  
 120          var $submitButton = $( this );
 121  
 122          clearNotices();
 123          $submitButton.prop( 'disabled', true );
 124  
 125          wp.apiRequest( {
 126              path: '/wp/v2/users/' + userId + '/application-passwords',
 127              method: 'DELETE'
 128          } ).always( function() {
 129              $submitButton.prop( 'disabled', false );
 130          } ).done( function( response ) {
 131              if ( response.deleted ) {
 132                  $appPassTbody.children().remove();
 133                  $appPassSection.children( '.new-application-password' ).remove();
 134                  $appPassTwrapper.hide();
 135  
 136                  addNotice( wp.i18n.__( 'All application passwords revoked.' ), 'success' ).focus();
 137              }
 138          } ).fail( handleErrorResponse );
 139      } );
 140  
 141      $appPassSection.on( 'click', '.notice-dismiss', function( e ) {
 142          e.preventDefault();
 143          var $el = $( this ).parent();
 144          $el.removeAttr( 'role' );
 145          $el.fadeTo( 100, 0, function () {
 146              $el.slideUp( 100, function () {
 147                  $el.remove();
 148                  $newAppPassField.focus();
 149              } );
 150          } );
 151      } );
 152  
 153      // If there are no items, don't display the table yet.  If there are, show it.
 154      if ( 0 === $appPassTbody.children( 'tr' ).not( $appPassTrNoItems ).length ) {
 155          $appPassTwrapper.hide();
 156      }
 157  
 158      /**
 159       * Handles an error response from the REST API.
 160       *
 161       * @since 5.6.0
 162       *
 163       * @param {jqXHR} xhr The XHR object from the ajax call.
 164       * @param {string} textStatus The string categorizing the ajax request's status.
 165       * @param {string} errorThrown The HTTP status error text.
 166       */
 167  	function handleErrorResponse( xhr, textStatus, errorThrown ) {
 168          var errorMessage = errorThrown;
 169  
 170          if ( xhr.responseJSON && xhr.responseJSON.message ) {
 171              errorMessage = xhr.responseJSON.message;
 172          }
 173  
 174          addNotice( errorMessage, 'error' );
 175      }
 176  
 177      /**
 178       * Displays a message in the Application Passwords section.
 179       *
 180       * @since 5.6.0
 181       *
 182       * @param {string} message The message to display.
 183       * @param {string} type    The notice type. Either 'success' or 'error'.
 184       * @returns {jQuery} The notice element.
 185       */
 186  	function addNotice( message, type ) {
 187          var $notice = $( '<div></div>' )
 188              .attr( 'role', 'alert' )
 189              .attr( 'tabindex', '-1' )
 190              .addClass( 'is-dismissible notice notice-' + type )
 191              .append( $( '<p></p>' ).text( message ) )
 192              .append(
 193                  $( '<button></button>' )
 194                      .attr( 'type', 'button' )
 195                      .addClass( 'notice-dismiss' )
 196                      .append( $( '<span></span>' ).addClass( 'screen-reader-text' ).text( wp.i18n.__( 'Dismiss this notice.' ) ) )
 197              );
 198  
 199          $newAppPassForm.after( $notice );
 200  
 201          return $notice;
 202      }
 203  
 204      /**
 205       * Clears notice messages from the Application Passwords section.
 206       *
 207       * @since 5.6.0
 208       */
 209  	function clearNotices() {
 210          $( '.notice', $appPassSection ).remove();
 211      }
 212  }( jQuery ) );


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