[ 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              requestID      = $action.data( 'request-id' ),
  63              nonce          = $action.data( 'nonce' ),
  64              exportersCount = $action.data( 'exporters-count' ),
  65              sendAsEmail    = $action.data( 'send-as-email' ) ? true : false;
  66  
  67          event.preventDefault();
  68          event.stopPropagation();
  69  
  70          $action.blur();
  71          clearResultsAfterRow( $requestRow );
  72  
  73  		function onExportDoneSuccess( zipUrl ) {
  74              setActionState( $action, 'export-personal-data-success' );
  75              if ( 'undefined' !== typeof zipUrl ) {
  76                  window.location = zipUrl;
  77              } else if ( ! sendAsEmail ) {
  78                  onExportFailure( strings.noExportFile );
  79              }
  80          }
  81  
  82  		function onExportFailure( errorMessage ) {
  83              setActionState( $action, 'export-personal-data-failed' );
  84              if ( errorMessage ) {
  85                  appendResultsAfterRow( $requestRow, 'notice-error', strings.exportError, [ errorMessage ] );
  86              }
  87          }
  88  
  89  		function doNextExport( exporterIndex, pageIndex ) {
  90              $.ajax(
  91                  {
  92                      url: window.ajaxurl,
  93                      data: {
  94                          action: 'wp-privacy-export-personal-data',
  95                          exporter: exporterIndex,
  96                          id: requestID,
  97                          page: pageIndex,
  98                          security: nonce,
  99                          sendAsEmail: sendAsEmail
 100                      },
 101                      method: 'post'
 102                  }
 103              ).done( function( response ) {
 104                  var responseData = response.data;
 105  
 106                  if ( ! response.success ) {
 107  
 108                      // e.g. invalid request ID
 109                      onExportFailure( response.data );
 110                      return;
 111                  }
 112  
 113                  if ( ! responseData.done ) {
 114                      setTimeout( doNextExport( exporterIndex, pageIndex + 1 ) );
 115                  } else {
 116                      if ( exporterIndex < exportersCount ) {
 117                          setTimeout( doNextExport( exporterIndex + 1, 1 ) );
 118                      } else {
 119                          onExportDoneSuccess( responseData.url );
 120                      }
 121                  }
 122              }).fail( function( jqxhr, textStatus, error ) {
 123  
 124                  // e.g. Nonce failure
 125                  onExportFailure( error );
 126              });
 127          }
 128  
 129          // And now, let's begin
 130          setActionState( $action, 'export-personal-data-processing' );
 131          doNextExport( 1, 1 );
 132      });
 133  
 134      $( '.remove-personal-data-handle' ).click( function( event ) {
 135          var $this         = $( this ),
 136              $action       = $this.parents( '.remove-personal-data' ),
 137              $requestRow   = $this.parents( 'tr' ),
 138              requestID     = $action.data( 'request-id' ),
 139              nonce         = $action.data( 'nonce' ),
 140              erasersCount  = $action.data( 'erasers-count' ),
 141              hasRemoved    = false,
 142              hasRetained   = false,
 143              messages      = [];
 144  
 145          event.stopPropagation();
 146  
 147          $action.blur();
 148          clearResultsAfterRow( $requestRow );
 149  
 150  		function onErasureDoneSuccess() {
 151              var summaryMessage = strings.noDataFound;
 152              var classes = 'notice-success';
 153  
 154              setActionState( $action, 'remove-personal-data-idle' );
 155  
 156              if ( false === hasRemoved ) {
 157                  if ( false === hasRetained ) {
 158                      summaryMessage = strings.noDataFound;
 159                  } else {
 160                      summaryMessage = strings.noneRemoved;
 161                      classes = 'notice-warning';
 162                  }
 163              } else {
 164                  if ( false === hasRetained ) {
 165                      summaryMessage = strings.foundAndRemoved;
 166                  } else {
 167                      summaryMessage = strings.someNotRemoved;
 168                      classes = 'notice-warning';
 169                  }
 170              }
 171              appendResultsAfterRow( $requestRow, 'notice-success', summaryMessage, messages );
 172          }
 173  
 174  		function onErasureFailure() {
 175              setActionState( $action, 'remove-personal-data-failed' );
 176              appendResultsAfterRow( $requestRow, 'notice-error', strings.removalError, [] );
 177          }
 178  
 179  		function doNextErasure( eraserIndex, pageIndex ) {
 180              $.ajax({
 181                  url: window.ajaxurl,
 182                  data: {
 183                      action: 'wp-privacy-erase-personal-data',
 184                      eraser: eraserIndex,
 185                      id: requestID,
 186                      page: pageIndex,
 187                      security: nonce
 188                  },
 189                  method: 'post'
 190              }).done( function( response ) {
 191                  var responseData = response.data;
 192  
 193                  if ( ! response.success ) {
 194                      onErasureFailure();
 195                      return;
 196                  }
 197                  if ( responseData.items_removed ) {
 198                      hasRemoved = hasRemoved || responseData.items_removed;
 199                  }
 200                  if ( responseData.items_retained ) {
 201                      hasRetained = hasRetained || responseData.items_retained;
 202                  }
 203                  if ( responseData.messages ) {
 204                      messages = messages.concat( responseData.messages );
 205                  }
 206                  if ( ! responseData.done ) {
 207                      setTimeout( doNextErasure( eraserIndex, pageIndex + 1 ) );
 208                  } else {
 209                      if ( eraserIndex < erasersCount ) {
 210                          setTimeout( doNextErasure( eraserIndex + 1, 1 ) );
 211                      } else {
 212                          onErasureDoneSuccess();
 213                      }
 214                  }
 215              }).fail( function() {
 216                  onErasureFailure();
 217              });
 218          }
 219  
 220          // And now, let's begin
 221          setActionState( $action, 'remove-personal-data-processing' );
 222  
 223          doNextErasure( 1, 1 );
 224      });
 225  
 226      // Privacy policy page, copy button.
 227      $( document ).on( 'click', function( event ) {
 228          var $target = $( event.target );
 229          var $parent, $container, range;
 230  
 231          if ( $target.is( 'button.privacy-text-copy' ) ) {
 232              $parent = $target.parent().parent();
 233              $container = $parent.find( 'div.wp-suggested-text' );
 234  
 235              if ( ! $container.length ) {
 236                  $container = $parent.find( 'div.policy-text' );
 237              }
 238  
 239              if ( $container.length ) {
 240                  try {
 241                      window.getSelection().removeAllRanges();
 242                      range = document.createRange();
 243                      $container.addClass( 'hide-privacy-policy-tutorial' );
 244  
 245                      range.selectNodeContents( $container[0] );
 246                      window.getSelection().addRange( range );
 247                      document.execCommand( 'copy' );
 248  
 249                      $container.removeClass( 'hide-privacy-policy-tutorial' );
 250                      window.getSelection().removeAllRanges();
 251                  } catch ( er ) {}
 252              }
 253          }
 254      });
 255  });
 256  


Generated: Mon Sep 23 01:00:03 2019 Cross-referenced by PHPXref 0.7.1