[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/js/ -> privacy-tools.js (source)

   1  /**
   2   * Interactions used by the User Privacy tools in WordPress.
   3   *
   4   * @output wp-admin/js/privacy-tools.js
   5   */
   6  
   7  // Privacy request action handling.
   8  jQuery( document ).ready( function( $ ) {
   9      var strings = window.privacyToolsL10n || {};
  10  
  11  	function setActionState( $action, state ) {
  12          $action.children().addClass( 'hidden' );
  13          $action.children( '.' + state ).removeClass( 'hidden' );
  14      }
  15  
  16  	function clearResultsAfterRow( $requestRow ) {
  17          $requestRow.removeClass( 'has-request-results' );
  18  
  19          if ( $requestRow.next().hasClass( 'request-results' ) ) {
  20              $requestRow.next().remove();
  21          }
  22      }
  23  
  24  	function appendResultsAfterRow( $requestRow, classes, summaryMessage, additionalMessages ) {
  25          var itemList = '',
  26              resultRowClasses = 'request-results';
  27  
  28          clearResultsAfterRow( $requestRow );
  29  
  30          if ( additionalMessages.length ) {
  31              $.each( additionalMessages, function( index, value ) {
  32                  itemList = itemList + '<li>' + value + '</li>';
  33              });
  34              itemList = '<ul>' + itemList + '</ul>';
  35          }
  36  
  37          $requestRow.addClass( 'has-request-results' );
  38  
  39          if ( $requestRow.hasClass( 'status-request-confirmed' ) ) {
  40              resultRowClasses = resultRowClasses + ' status-request-confirmed';
  41          }
  42  
  43          if ( $requestRow.hasClass( 'status-request-failed' ) ) {
  44              resultRowClasses = resultRowClasses + ' status-request-failed';
  45          }
  46  
  47          $requestRow.after( function() {
  48              return '<tr class="' + resultRowClasses + '"><th colspan="5">' +
  49                  '<div class="notice inline notice-alt ' + classes + '">' +
  50                  '<p>' + summaryMessage + '</p>' +
  51                  itemList +
  52                  '</div>' +
  53                  '</td>' +
  54                  '</tr>';
  55          });
  56      }
  57  
  58      $( '.export-personal-data-handle' ).click( function( event ) {
  59          var $this          = $( this ),
  60              $action        = $this.parents( '.export-personal-data' ),
  61              $requestRow    = $this.parents( 'tr' ),
  62              $progress      = $requestRow.find( '.export-progress' ),
  63              requestID      = $action.data( 'request-id' ),
  64              nonce          = $action.data( 'nonce' ),
  65              exportersCount = $action.data( 'exporters-count' ),
  66              sendAsEmail    = $action.data( 'send-as-email' ) ? true : false;
  67  
  68          event.preventDefault();
  69          event.stopPropagation();
  70  
  71          $action.blur();
  72          clearResultsAfterRow( $requestRow );
  73          setExportProgress( 0 );
  74  
  75  		function onExportDoneSuccess( zipUrl ) {
  76              var summaryMessage = strings.emailSent;
  77  
  78              setActionState( $action, 'export-personal-data-success' );
  79  
  80              appendResultsAfterRow( $requestRow, 'notice-success', summaryMessage, [] );
  81              $this.hide();
  82  
  83              if ( 'undefined' !== typeof zipUrl ) {
  84                  window.location = zipUrl;
  85              } else if ( ! sendAsEmail ) {
  86                  onExportFailure( strings.noExportFile );
  87              }
  88          }
  89  
  90  		function onExportFailure( errorMessage ) {
  91              setActionState( $action, 'export-personal-data-failed' );
  92              if ( errorMessage ) {
  93                  appendResultsAfterRow( $requestRow, 'notice-error', strings.exportError, [ errorMessage ] );
  94              }
  95          }
  96  
  97  		function setExportProgress( exporterIndex ) {
  98              var progress       = ( exportersCount > 0 ? exporterIndex / exportersCount : 0 );
  99              var progressString = Math.round( progress * 100 ).toString() + '%';
 100              $progress.html( progressString );
 101          }
 102  
 103  		function doNextExport( exporterIndex, pageIndex ) {
 104              $.ajax(
 105                  {
 106                      url: window.ajaxurl,
 107                      data: {
 108                          action: 'wp-privacy-export-personal-data',
 109                          exporter: exporterIndex,
 110                          id: requestID,
 111                          page: pageIndex,
 112                          security: nonce,
 113                          sendAsEmail: sendAsEmail
 114                      },
 115                      method: 'post'
 116                  }
 117              ).done( function( response ) {
 118                  var responseData = response.data;
 119  
 120                  if ( ! response.success ) {
 121  
 122                      // e.g. invalid request ID.
 123                      onExportFailure( response.data );
 124                      return;
 125                  }
 126  
 127                  if ( ! responseData.done ) {
 128                      setTimeout( doNextExport( exporterIndex, pageIndex + 1 ) );
 129                  } else {
 130                      if ( exporterIndex < exportersCount ) {
 131                          setExportProgress( exporterIndex );
 132                          setTimeout( doNextExport( exporterIndex + 1, 1 ) );
 133                      } else {
 134                          onExportDoneSuccess( responseData.url );
 135                      }
 136                  }
 137              }).fail( function( jqxhr, textStatus, error ) {
 138  
 139                  // e.g. Nonce failure.
 140                  onExportFailure( error );
 141              });
 142          }
 143  
 144          // And now, let's begin.
 145          setActionState( $action, 'export-personal-data-processing' );
 146          doNextExport( 1, 1 );
 147      });
 148  
 149      $( '.remove-personal-data-handle' ).click( function( event ) {
 150          var $this         = $( this ),
 151              $action       = $this.parents( '.remove-personal-data' ),
 152              $requestRow   = $this.parents( 'tr' ),
 153              $progress     = $requestRow.find( '.erasure-progress' ),
 154              requestID     = $action.data( 'request-id' ),
 155              nonce         = $action.data( 'nonce' ),
 156              erasersCount  = $action.data( 'erasers-count' ),
 157              hasRemoved    = false,
 158              hasRetained   = false,
 159              messages      = [];
 160  
 161          event.stopPropagation();
 162  
 163          $action.blur();
 164          clearResultsAfterRow( $requestRow );
 165          setErasureProgress( 0 );
 166  
 167  		function onErasureDoneSuccess() {
 168              var summaryMessage = strings.noDataFound;
 169              var classes = 'notice-success';
 170  
 171              setActionState( $action, 'remove-personal-data-success' );
 172  
 173              if ( false === hasRemoved ) {
 174                  if ( false === hasRetained ) {
 175                      summaryMessage = strings.noDataFound;
 176                  } else {
 177                      summaryMessage = strings.noneRemoved;
 178                      classes = 'notice-warning';
 179                  }
 180              } else {
 181                  if ( false === hasRetained ) {
 182                      summaryMessage = strings.foundAndRemoved;
 183                  } else {
 184                      summaryMessage = strings.someNotRemoved;
 185                      classes = 'notice-warning';
 186                  }
 187              }
 188              appendResultsAfterRow( $requestRow, classes, summaryMessage, messages );
 189              $this.hide();
 190          }
 191  
 192  		function onErasureFailure() {
 193              setActionState( $action, 'remove-personal-data-failed' );
 194              appendResultsAfterRow( $requestRow, 'notice-error', strings.removalError, [] );
 195          }
 196  
 197  		function setErasureProgress( eraserIndex ) {
 198              var progress       = ( erasersCount > 0 ? eraserIndex / erasersCount : 0 );
 199              var progressString = Math.round( progress * 100 ).toString() + '%';
 200              $progress.html( progressString );
 201          }
 202  
 203  		function doNextErasure( eraserIndex, pageIndex ) {
 204              $.ajax({
 205                  url: window.ajaxurl,
 206                  data: {
 207                      action: 'wp-privacy-erase-personal-data',
 208                      eraser: eraserIndex,
 209                      id: requestID,
 210                      page: pageIndex,
 211                      security: nonce
 212                  },
 213                  method: 'post'
 214              }).done( function( response ) {
 215                  var responseData = response.data;
 216  
 217                  if ( ! response.success ) {
 218                      onErasureFailure();
 219                      return;
 220                  }
 221                  if ( responseData.items_removed ) {
 222                      hasRemoved = hasRemoved || responseData.items_removed;
 223                  }
 224                  if ( responseData.items_retained ) {
 225                      hasRetained = hasRetained || responseData.items_retained;
 226                  }
 227                  if ( responseData.messages ) {
 228                      messages = messages.concat( responseData.messages );
 229                  }
 230                  if ( ! responseData.done ) {
 231                      setTimeout( doNextErasure( eraserIndex, pageIndex + 1 ) );
 232                  } else {
 233                      if ( eraserIndex < erasersCount ) {
 234                          setErasureProgress( eraserIndex );
 235                          setTimeout( doNextErasure( eraserIndex + 1, 1 ) );
 236                      } else {
 237                          onErasureDoneSuccess();
 238                      }
 239                  }
 240              }).fail( function() {
 241                  onErasureFailure();
 242              });
 243          }
 244  
 245          // And now, let's begin.
 246          setActionState( $action, 'remove-personal-data-processing' );
 247  
 248          doNextErasure( 1, 1 );
 249      });
 250  
 251      // Privacy policy page, copy button.
 252      $( document ).on( 'click', function( event ) {
 253          var $target = $( event.target );
 254          var $parent, $container, range;
 255  
 256          if ( $target.is( 'button.privacy-text-copy' ) ) {
 257              $parent = $target.parent().parent();
 258              $container = $parent.find( 'div.wp-suggested-text' );
 259  
 260              if ( ! $container.length ) {
 261                  $container = $parent.find( 'div.policy-text' );
 262              }
 263  
 264              if ( $container.length ) {
 265                  try {
 266                      window.getSelection().removeAllRanges();
 267                      range = document.createRange();
 268                      $container.addClass( 'hide-privacy-policy-tutorial' );
 269  
 270                      range.selectNodeContents( $container[0] );
 271                      window.getSelection().addRange( range );
 272                      document.execCommand( 'copy' );
 273  
 274                      $container.removeClass( 'hide-privacy-policy-tutorial' );
 275                      window.getSelection().removeAllRanges();
 276                  } catch ( er ) {}
 277              }
 278          }
 279      });
 280  });
 281  


Generated: Sun Feb 23 01:00:03 2020 Cross-referenced by PHPXref 0.7.1