[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7