[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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              $rowActions    = $this.parents( '.row-actions' ),
  64              requestID      = $action.data( 'request-id' ),
  65              nonce          = $action.data( 'nonce' ),
  66              exportersCount = $action.data( 'exporters-count' ),
  67              sendAsEmail    = $action.data( 'send-as-email' ) ? true : false;
  68  
  69          event.preventDefault();
  70          event.stopPropagation();
  71  
  72          $rowActions.addClass( 'processing' );
  73  
  74          $action.blur();
  75          clearResultsAfterRow( $requestRow );
  76          setExportProgress( 0 );
  77  
  78  		function onExportDoneSuccess( zipUrl ) {
  79              var summaryMessage = strings.emailSent;
  80  
  81              setActionState( $action, 'export-personal-data-success' );
  82  
  83              appendResultsAfterRow( $requestRow, 'notice-success', summaryMessage, [] );
  84  
  85              if ( 'undefined' !== typeof zipUrl ) {
  86                  window.location = zipUrl;
  87              } else if ( ! sendAsEmail ) {
  88                  onExportFailure( strings.noExportFile );
  89              }
  90  
  91              setTimeout( function(){ $rowActions.removeClass( 'processing' ); }, 500 );
  92          }
  93  
  94  		function onExportFailure( errorMessage ) {
  95              setActionState( $action, 'export-personal-data-failed' );
  96              if ( errorMessage ) {
  97                  appendResultsAfterRow( $requestRow, 'notice-error', strings.exportError, [ errorMessage ] );
  98              }
  99  
 100              setTimeout( function(){ $rowActions.removeClass( 'processing' ); }, 500 );
 101          }
 102  
 103  		function setExportProgress( exporterIndex ) {
 104              var progress       = ( exportersCount > 0 ? exporterIndex / exportersCount : 0 );
 105              var progressString = Math.round( progress * 100 ).toString() + '%';
 106              $progress.html( progressString );
 107          }
 108  
 109  		function doNextExport( exporterIndex, pageIndex ) {
 110              $.ajax(
 111                  {
 112                      url: window.ajaxurl,
 113                      data: {
 114                          action: 'wp-privacy-export-personal-data',
 115                          exporter: exporterIndex,
 116                          id: requestID,
 117                          page: pageIndex,
 118                          security: nonce,
 119                          sendAsEmail: sendAsEmail
 120                      },
 121                      method: 'post'
 122                  }
 123              ).done( function( response ) {
 124                  var responseData = response.data;
 125  
 126                  if ( ! response.success ) {
 127                      // e.g. invalid request ID.
 128                      setTimeout( function(){ onExportFailure( response.data ); }, 500 );
 129                      return;
 130                  }
 131  
 132                  if ( ! responseData.done ) {
 133                      setTimeout( doNextExport( exporterIndex, pageIndex + 1 ) );
 134                  } else {
 135                      setExportProgress( exporterIndex );
 136                      if ( exporterIndex < exportersCount ) {
 137                          setTimeout( doNextExport( exporterIndex + 1, 1 ) );
 138                      } else {
 139                          setTimeout( function(){ onExportDoneSuccess( responseData.url ); }, 500 );
 140                      }
 141                  }
 142              }).fail( function( jqxhr, textStatus, error ) {
 143                  // e.g. Nonce failure.
 144                  setTimeout( function(){ onExportFailure( error ); }, 500 );
 145              });
 146          }
 147  
 148          // And now, let's begin.
 149          setActionState( $action, 'export-personal-data-processing' );
 150          doNextExport( 1, 1 );
 151      });
 152  
 153      $( '.remove-personal-data-handle' ).click( function( event ) {
 154          var $this         = $( this ),
 155              $action       = $this.parents( '.remove-personal-data' ),
 156              $requestRow   = $this.parents( 'tr' ),
 157              $progress     = $requestRow.find( '.erasure-progress' ),
 158              $rowActions   = $this.parents( '.row-actions' ),
 159              requestID     = $action.data( 'request-id' ),
 160              nonce         = $action.data( 'nonce' ),
 161              erasersCount  = $action.data( 'erasers-count' ),
 162              hasRemoved    = false,
 163              hasRetained   = false,
 164              messages      = [];
 165  
 166          event.preventDefault();
 167          event.stopPropagation();
 168  
 169          $rowActions.addClass( 'processing' );
 170  
 171          $action.blur();
 172          clearResultsAfterRow( $requestRow );
 173          setErasureProgress( 0 );
 174  
 175  		function onErasureDoneSuccess() {
 176              var summaryMessage = strings.noDataFound;
 177              var classes = 'notice-success';
 178  
 179              setActionState( $action, 'remove-personal-data-success' );
 180  
 181              if ( false === hasRemoved ) {
 182                  if ( false === hasRetained ) {
 183                      summaryMessage = strings.noDataFound;
 184                  } else {
 185                      summaryMessage = strings.noneRemoved;
 186                      classes = 'notice-warning';
 187                  }
 188              } else {
 189                  if ( false === hasRetained ) {
 190                      summaryMessage = strings.foundAndRemoved;
 191                  } else {
 192                      summaryMessage = strings.someNotRemoved;
 193                      classes = 'notice-warning';
 194                  }
 195              }
 196              appendResultsAfterRow( $requestRow, classes, summaryMessage, messages );
 197  
 198              setTimeout( function(){ $rowActions.removeClass( 'processing' ); }, 500 );
 199          }
 200  
 201  		function onErasureFailure() {
 202              setActionState( $action, 'remove-personal-data-failed' );
 203              appendResultsAfterRow( $requestRow, 'notice-error', strings.removalError, [] );
 204  
 205              setTimeout( function(){ $rowActions.removeClass( 'processing' ); }, 500 );
 206          }
 207  
 208  		function setErasureProgress( eraserIndex ) {
 209              var progress       = ( erasersCount > 0 ? eraserIndex / erasersCount : 0 );
 210              var progressString = Math.round( progress * 100 ).toString() + '%';
 211              $progress.html( progressString );
 212          }
 213  
 214  		function doNextErasure( eraserIndex, pageIndex ) {
 215              $.ajax({
 216                  url: window.ajaxurl,
 217                  data: {
 218                      action: 'wp-privacy-erase-personal-data',
 219                      eraser: eraserIndex,
 220                      id: requestID,
 221                      page: pageIndex,
 222                      security: nonce
 223                  },
 224                  method: 'post'
 225              }).done( function( response ) {
 226                  var responseData = response.data;
 227  
 228                  if ( ! response.success ) {
 229                      setTimeout( function(){ onErasureFailure(); }, 500 );
 230                      return;
 231                  }
 232                  if ( responseData.items_removed ) {
 233                      hasRemoved = hasRemoved || responseData.items_removed;
 234                  }
 235                  if ( responseData.items_retained ) {
 236                      hasRetained = hasRetained || responseData.items_retained;
 237                  }
 238                  if ( responseData.messages ) {
 239                      messages = messages.concat( responseData.messages );
 240                  }
 241                  if ( ! responseData.done ) {
 242                      setTimeout( doNextErasure( eraserIndex, pageIndex + 1 ) );
 243                  } else {
 244                      setErasureProgress( eraserIndex );
 245                      if ( eraserIndex < erasersCount ) {
 246                          setTimeout( doNextErasure( eraserIndex + 1, 1 ) );
 247                      } else {
 248                          setTimeout( function(){ onErasureDoneSuccess(); }, 500 );
 249                      }
 250                  }
 251              }).fail( function() {
 252                  setTimeout( function(){ onErasureFailure(); }, 500 );
 253              });
 254          }
 255  
 256          // And now, let's begin.
 257          setActionState( $action, 'remove-personal-data-processing' );
 258  
 259          doNextErasure( 1, 1 );
 260      });
 261  
 262      // Privacy Policy page, copy action.
 263      $( document ).on( 'click', function( event ) {
 264          var $parent,
 265              $container,
 266              range,
 267              __ = wp.i18n.__,
 268              $target = $( event.target ),
 269              copiedNotice = $target.siblings( '.success' );
 270  
 271          if ( $target.is( 'button.privacy-text-copy' ) ) {
 272              $parent = $target.parent().parent();
 273              $container = $parent.find( 'div.wp-suggested-text' );
 274  
 275              if ( ! $container.length ) {
 276                  $container = $parent.find( 'div.policy-text' );
 277              }
 278  
 279              if ( $container.length ) {
 280                  try {
 281                      var documentPosition = document.documentElement.scrollTop,
 282                          bodyPosition     = document.body.scrollTop;
 283  
 284                      // Setup copy.
 285                      window.getSelection().removeAllRanges();
 286  
 287                      // Hide tutorial content to remove from copied content.
 288                      range = document.createRange();
 289                      $container.addClass( 'hide-privacy-policy-tutorial' );
 290  
 291                      // Copy action.
 292                      range.selectNodeContents( $container[0] );
 293                      window.getSelection().addRange( range );
 294                      document.execCommand( 'copy' );
 295  
 296                      // Reset section.
 297                      $container.removeClass( 'hide-privacy-policy-tutorial' );
 298                      window.getSelection().removeAllRanges();
 299  
 300                      // Return scroll position - see #49540.
 301                      if ( documentPosition > 0 && documentPosition !== document.documentElement.scrollTop ) {
 302                          document.documentElement.scrollTop = documentPosition;
 303                      } else if ( bodyPosition > 0 && bodyPosition !== document.body.scrollTop ) {
 304                          document.body.scrollTop = bodyPosition;
 305                      }
 306  
 307                      // Display and speak notice to indicate action complete.
 308                      copiedNotice.addClass( 'visible' );
 309                      wp.a11y.speak( __( 'The section has been copied to your clipboard.' ) );
 310  
 311                      // Delay notice dismissal.
 312                      setTimeout( function(){ copiedNotice.removeClass( 'visible' ); }, 3000 );
 313                  } catch ( er ) {}
 314              }
 315          }
 316      });
 317  });
 318  


Generated : Wed Jun 3 08:20:02 2020 Cross-referenced by PHPXref