[ 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 __ = wp.i18n.__,
  10          copiedNoticeTimeout;
  11  
  12  	function setActionState( $action, state ) {
  13          $action.children().addClass( 'hidden' );
  14          $action.children( '.' + state ).removeClass( 'hidden' );
  15      }
  16  
  17  	function clearResultsAfterRow( $requestRow ) {
  18          $requestRow.removeClass( 'has-request-results' );
  19  
  20          if ( $requestRow.next().hasClass( 'request-results' ) ) {
  21              $requestRow.next().remove();
  22          }
  23      }
  24  
  25  	function appendResultsAfterRow( $requestRow, classes, summaryMessage, additionalMessages ) {
  26          var itemList = '',
  27              resultRowClasses = 'request-results';
  28  
  29          clearResultsAfterRow( $requestRow );
  30  
  31          if ( additionalMessages.length ) {
  32              $.each( additionalMessages, function( index, value ) {
  33                  itemList = itemList + '<li>' + value + '</li>';
  34              });
  35              itemList = '<ul>' + itemList + '</ul>';
  36          }
  37  
  38          $requestRow.addClass( 'has-request-results' );
  39  
  40          if ( $requestRow.hasClass( 'status-request-confirmed' ) ) {
  41              resultRowClasses = resultRowClasses + ' status-request-confirmed';
  42          }
  43  
  44          if ( $requestRow.hasClass( 'status-request-failed' ) ) {
  45              resultRowClasses = resultRowClasses + ' status-request-failed';
  46          }
  47  
  48          $requestRow.after( function() {
  49              return '<tr class="' + resultRowClasses + '"><th colspan="5">' +
  50                  '<div class="notice inline notice-alt ' + classes + '">' +
  51                  '<p>' + summaryMessage + '</p>' +
  52                  itemList +
  53                  '</div>' +
  54                  '</td>' +
  55                  '</tr>';
  56          });
  57      }
  58  
  59      $( '.export-personal-data-handle' ).click( function( event ) {
  60          var $this          = $( this ),
  61              $action        = $this.parents( '.export-personal-data' ),
  62              $requestRow    = $this.parents( 'tr' ),
  63              $progress      = $requestRow.find( '.export-progress' ),
  64              $rowActions    = $this.parents( '.row-actions' ),
  65              requestID      = $action.data( 'request-id' ),
  66              nonce          = $action.data( 'nonce' ),
  67              exportersCount = $action.data( 'exporters-count' ),
  68              sendAsEmail    = $action.data( 'send-as-email' ) ? true : false;
  69  
  70          event.preventDefault();
  71          event.stopPropagation();
  72  
  73          $rowActions.addClass( 'processing' );
  74  
  75          $action.blur();
  76          clearResultsAfterRow( $requestRow );
  77          setExportProgress( 0 );
  78  
  79  		function onExportDoneSuccess( zipUrl ) {
  80              var summaryMessage = __( 'This user&#8217;s personal data export link was sent.' );
  81  
  82              if ( 'undefined' !== typeof zipUrl ) {
  83                  summaryMessage = __( 'This user&#8217;s personal data export file was downloaded.' );
  84              }
  85  
  86              setActionState( $action, 'export-personal-data-success' );
  87  
  88              appendResultsAfterRow( $requestRow, 'notice-success', summaryMessage, [] );
  89  
  90              if ( 'undefined' !== typeof zipUrl ) {
  91                  window.location = zipUrl;
  92              } else if ( ! sendAsEmail ) {
  93                  onExportFailure( __( 'No personal data export file was generated.' ) );
  94              }
  95  
  96              setTimeout( function() { $rowActions.removeClass( 'processing' ); }, 500 );
  97          }
  98  
  99  		function onExportFailure( errorMessage ) {
 100              var summaryMessage = __( 'An error occurred while attempting to export personal data.' );
 101  
 102              setActionState( $action, 'export-personal-data-failed' );
 103  
 104              if ( errorMessage ) {
 105                  appendResultsAfterRow( $requestRow, 'notice-error', summaryMessage, [ errorMessage ] );
 106              }
 107  
 108              setTimeout( function() { $rowActions.removeClass( 'processing' ); }, 500 );
 109          }
 110  
 111  		function setExportProgress( exporterIndex ) {
 112              var progress       = ( exportersCount > 0 ? exporterIndex / exportersCount : 0 ),
 113                  progressString = Math.round( progress * 100 ).toString() + '%';
 114  
 115              $progress.html( progressString );
 116          }
 117  
 118  		function doNextExport( exporterIndex, pageIndex ) {
 119              $.ajax(
 120                  {
 121                      url: window.ajaxurl,
 122                      data: {
 123                          action: 'wp-privacy-export-personal-data',
 124                          exporter: exporterIndex,
 125                          id: requestID,
 126                          page: pageIndex,
 127                          security: nonce,
 128                          sendAsEmail: sendAsEmail
 129                      },
 130                      method: 'post'
 131                  }
 132              ).done( function( response ) {
 133                  var responseData = response.data;
 134  
 135                  if ( ! response.success ) {
 136                      // e.g. invalid request ID.
 137                      setTimeout( function() { onExportFailure( response.data ); }, 500 );
 138                      return;
 139                  }
 140  
 141                  if ( ! responseData.done ) {
 142                      setTimeout( doNextExport( exporterIndex, pageIndex + 1 ) );
 143                  } else {
 144                      setExportProgress( exporterIndex );
 145                      if ( exporterIndex < exportersCount ) {
 146                          setTimeout( doNextExport( exporterIndex + 1, 1 ) );
 147                      } else {
 148                          setTimeout( function() { onExportDoneSuccess( responseData.url ); }, 500 );
 149                      }
 150                  }
 151              }).fail( function( jqxhr, textStatus, error ) {
 152                  // e.g. Nonce failure.
 153                  setTimeout( function() { onExportFailure( error ); }, 500 );
 154              });
 155          }
 156  
 157          // And now, let's begin.
 158          setActionState( $action, 'export-personal-data-processing' );
 159          doNextExport( 1, 1 );
 160      });
 161  
 162      $( '.remove-personal-data-handle' ).click( function( event ) {
 163          var $this         = $( this ),
 164              $action       = $this.parents( '.remove-personal-data' ),
 165              $requestRow   = $this.parents( 'tr' ),
 166              $progress     = $requestRow.find( '.erasure-progress' ),
 167              $rowActions   = $this.parents( '.row-actions' ),
 168              requestID     = $action.data( 'request-id' ),
 169              nonce         = $action.data( 'nonce' ),
 170              erasersCount  = $action.data( 'erasers-count' ),
 171              hasRemoved    = false,
 172              hasRetained   = false,
 173              messages      = [];
 174  
 175          event.preventDefault();
 176          event.stopPropagation();
 177  
 178          $rowActions.addClass( 'processing' );
 179  
 180          $action.blur();
 181          clearResultsAfterRow( $requestRow );
 182          setErasureProgress( 0 );
 183  
 184  		function onErasureDoneSuccess() {
 185              var summaryMessage = __( 'No personal data was found for this user.' ),
 186                  classes = 'notice-success';
 187  
 188              setActionState( $action, 'remove-personal-data-success' );
 189  
 190              if ( false === hasRemoved ) {
 191                  if ( false === hasRetained ) {
 192                      summaryMessage = __( 'No personal data was found for this user.' );
 193                  } else {
 194                      summaryMessage = __( 'Personal data was found for this user but was not erased.' );
 195                      classes = 'notice-warning';
 196                  }
 197              } else {
 198                  if ( false === hasRetained ) {
 199                      summaryMessage = __( 'All of the personal data found for this user was erased.' );
 200                  } else {
 201                      summaryMessage = __( 'Personal data was found for this user but some of the personal data found was not erased.' );
 202                      classes = 'notice-warning';
 203                  }
 204              }
 205              appendResultsAfterRow( $requestRow, classes, summaryMessage, messages );
 206  
 207              setTimeout( function() { $rowActions.removeClass( 'processing' ); }, 500 );
 208          }
 209  
 210  		function onErasureFailure() {
 211              var summaryMessage = __( 'An error occurred while attempting to find and erase personal data.' );
 212  
 213              setActionState( $action, 'remove-personal-data-failed' );
 214  
 215              appendResultsAfterRow( $requestRow, 'notice-error', summaryMessage, [] );
 216  
 217              setTimeout( function() { $rowActions.removeClass( 'processing' ); }, 500 );
 218          }
 219  
 220  		function setErasureProgress( eraserIndex ) {
 221              var progress       = ( erasersCount > 0 ? eraserIndex / erasersCount : 0 ),
 222                  progressString = Math.round( progress * 100 ).toString() + '%';
 223  
 224              $progress.html( progressString );
 225          }
 226  
 227  		function doNextErasure( eraserIndex, pageIndex ) {
 228              $.ajax({
 229                  url: window.ajaxurl,
 230                  data: {
 231                      action: 'wp-privacy-erase-personal-data',
 232                      eraser: eraserIndex,
 233                      id: requestID,
 234                      page: pageIndex,
 235                      security: nonce
 236                  },
 237                  method: 'post'
 238              }).done( function( response ) {
 239                  var responseData = response.data;
 240  
 241                  if ( ! response.success ) {
 242                      setTimeout( function() { onErasureFailure(); }, 500 );
 243                      return;
 244                  }
 245                  if ( responseData.items_removed ) {
 246                      hasRemoved = hasRemoved || responseData.items_removed;
 247                  }
 248                  if ( responseData.items_retained ) {
 249                      hasRetained = hasRetained || responseData.items_retained;
 250                  }
 251                  if ( responseData.messages ) {
 252                      messages = messages.concat( responseData.messages );
 253                  }
 254                  if ( ! responseData.done ) {
 255                      setTimeout( doNextErasure( eraserIndex, pageIndex + 1 ) );
 256                  } else {
 257                      setErasureProgress( eraserIndex );
 258                      if ( eraserIndex < erasersCount ) {
 259                          setTimeout( doNextErasure( eraserIndex + 1, 1 ) );
 260                      } else {
 261                          setTimeout( function() { onErasureDoneSuccess(); }, 500 );
 262                      }
 263                  }
 264              }).fail( function() {
 265                  setTimeout( function() { onErasureFailure(); }, 500 );
 266              });
 267          }
 268  
 269          // And now, let's begin.
 270          setActionState( $action, 'remove-personal-data-processing' );
 271  
 272          doNextErasure( 1, 1 );
 273      });
 274  
 275      // Privacy Policy page, copy action.
 276      $( document ).on( 'click', function( event ) {
 277          var $parent,
 278              $container,
 279              range,
 280              $target = $( event.target ),
 281              copiedNotice = $target.siblings( '.success' );
 282  
 283          clearTimeout( copiedNoticeTimeout );
 284  
 285          if ( $target.is( 'button.privacy-text-copy' ) ) {
 286              $parent = $target.parent().parent();
 287              $container = $parent.find( 'div.wp-suggested-text' );
 288  
 289              if ( ! $container.length ) {
 290                  $container = $parent.find( 'div.policy-text' );
 291              }
 292  
 293              if ( $container.length ) {
 294                  try {
 295                      var documentPosition = document.documentElement.scrollTop,
 296                          bodyPosition     = document.body.scrollTop;
 297  
 298                      // Setup copy.
 299                      window.getSelection().removeAllRanges();
 300  
 301                      // Hide tutorial content to remove from copied content.
 302                      range = document.createRange();
 303                      $container.addClass( 'hide-privacy-policy-tutorial' );
 304  
 305                      // Copy action.
 306                      range.selectNodeContents( $container[0] );
 307                      window.getSelection().addRange( range );
 308                      document.execCommand( 'copy' );
 309  
 310                      // Reset section.
 311                      $container.removeClass( 'hide-privacy-policy-tutorial' );
 312                      window.getSelection().removeAllRanges();
 313  
 314                      // Return scroll position - see #49540.
 315                      if ( documentPosition > 0 && documentPosition !== document.documentElement.scrollTop ) {
 316                          document.documentElement.scrollTop = documentPosition;
 317                      } else if ( bodyPosition > 0 && bodyPosition !== document.body.scrollTop ) {
 318                          document.body.scrollTop = bodyPosition;
 319                      }
 320  
 321                      // Display and speak notice to indicate action complete.
 322                      copiedNotice.addClass( 'visible' );
 323                      wp.a11y.speak( __( 'The section has been copied to your clipboard.' ) );
 324  
 325                      // Delay notice dismissal.
 326                      copiedNoticeTimeout = setTimeout( function() {
 327                          copiedNotice.removeClass( 'visible' );
 328                      }, 3000 );
 329                  } catch ( er ) {}
 330              }
 331          }
 332      });
 333  });


Generated: Sun Nov 29 01:00:04 2020 Cross-referenced by PHPXref 0.7.1