暫無描述

grunion-admin.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* global ajaxurl jetpack_empty_spam_button_parameters */
  2. jQuery( function ( $ ) {
  3. if ( typeof jetpack_empty_spam_button_parameters !== 'undefined' ) {
  4. // Create the "Empty Spam" button and add it above and below the list of spam feedbacks.
  5. var jetpack_empty_spam_feedbacks_button_container = $( '<div/>' ).addClass(
  6. 'jetpack-empty-spam-container'
  7. );
  8. var jetpack_empty_spam_feedbacks_button = $( '<a />' )
  9. .addClass( 'button-secondary' )
  10. .addClass( 'jetpack-empty-spam' )
  11. .attr( 'href', '#' )
  12. .attr( 'data-progress-label', jetpack_empty_spam_button_parameters.progress_label )
  13. .attr( 'data-success-url', jetpack_empty_spam_button_parameters.success_url )
  14. .attr( 'data-failure-url', jetpack_empty_spam_button_parameters.failure_url )
  15. .attr( 'data-spam-feedbacks-count', jetpack_empty_spam_button_parameters.spam_count )
  16. .attr( 'data-nonce', jetpack_empty_spam_button_parameters.nonce )
  17. .text( jetpack_empty_spam_button_parameters.label );
  18. jetpack_empty_spam_feedbacks_button_container.append( jetpack_empty_spam_feedbacks_button );
  19. var jetpack_empty_spam_feedbacks_spinner = $( '<span />' ).addClass(
  20. 'jetpack-empty-spam-spinner'
  21. );
  22. jetpack_empty_spam_feedbacks_button_container.append( jetpack_empty_spam_feedbacks_spinner );
  23. // Add the button both above and below the list of spam feedbacks.
  24. $( '.tablenav.top .actions, .tablenav.bottom .actions' )
  25. .not( '.bulkactions' )
  26. .append( jetpack_empty_spam_feedbacks_button_container );
  27. }
  28. $( document ).on( 'click', '#jetpack-check-feedback-spam:not(.button-disabled)', function ( e ) {
  29. e.preventDefault();
  30. $( '#jetpack-check-feedback-spam:not(.button-disabled)' ).addClass( 'button-disabled' );
  31. $( '.jetpack-check-feedback-spam-spinner' ).addClass( 'spinner' ).show();
  32. grunion_check_for_spam( 0, 100 );
  33. } );
  34. function grunion_check_for_spam( offset, limit ) {
  35. var nonceName = $( '#jetpack-check-feedback-spam' ).data( 'nonce-name' );
  36. var nonce = $( '#' + nonceName ).attr( 'value' );
  37. var failureUrl = $( '#jetpack-check-feedback-spam' ).data( 'failure-url' );
  38. var requestOptions = {
  39. action: 'grunion_recheck_queue',
  40. offset: offset,
  41. limit: limit,
  42. };
  43. requestOptions[ nonceName ] = nonce;
  44. $.post( ajaxurl, requestOptions )
  45. .fail( function ( result ) {
  46. // An error is only returned in the case of a missing nonce or invalid permissions, so we don't need the actual error message.
  47. window.location.href = failureUrl;
  48. return;
  49. } )
  50. .done( function ( result ) {
  51. if ( result.processed < limit ) {
  52. window.location.reload();
  53. } else {
  54. grunion_check_for_spam( offset + limit, limit );
  55. }
  56. } );
  57. }
  58. var initial_spam_count = 0;
  59. var deleted_spam_count = 0;
  60. $( document ).on( 'click', '.jetpack-empty-spam', function ( e ) {
  61. e.preventDefault();
  62. if ( $( this ).hasClass( 'button-disabled' ) ) {
  63. // An Emptying process is already underway or the button is otherwise disabled.
  64. return;
  65. }
  66. $( '.jetpack-empty-spam' ).addClass( 'button-disabled' ).addClass( 'emptying' );
  67. $( '.jetpack-empty-spam-spinner' ).addClass( 'spinner' ).addClass( 'is-active' );
  68. // Update the label on the "Empty Spam" button to use the active "Emptying Spam" language.
  69. $( '.jetpack-empty-spam' ).text(
  70. $( '.jetpack-empty-spam' ).data( 'progress-label' ).replace( '%1$s', '0' )
  71. );
  72. initial_spam_count = parseInt( $( this ).data( 'spam-feedbacks-count' ), 10 );
  73. grunion_delete_spam();
  74. } );
  75. function grunion_delete_spam() {
  76. var empty_spam_buttons = $( '.jetpack-empty-spam' );
  77. var nonce = empty_spam_buttons.data( 'nonce' );
  78. // We show the percentage complete down to one decimal point so even with 100k
  79. // spam feedbacks, it will show some progress pretty quickly.
  80. var percentage_complete = Math.round( ( deleted_spam_count / initial_spam_count ) * 1000 ) / 10;
  81. // Update the progress counter on the "Check for Spam" button.
  82. empty_spam_buttons.text(
  83. empty_spam_buttons.data( 'progress-label' ).replace( '%1$s', percentage_complete )
  84. );
  85. $.post( ajaxurl, {
  86. action: 'jetpack_delete_spam_feedbacks',
  87. nonce: nonce,
  88. } )
  89. .fail( function ( result ) {
  90. // An error is only returned in the case of a missing nonce or invalid permissions, so we don't need the actual error message.
  91. window.location.href = empty_spam_buttons.data( 'failure-url' );
  92. return;
  93. } )
  94. .done( function ( result ) {
  95. deleted_spam_count += result.data.counts.deleted;
  96. if ( result.data.counts.deleted < result.data.counts.limit ) {
  97. window.location.href = empty_spam_buttons.data( 'success-url' );
  98. } else {
  99. grunion_delete_spam();
  100. }
  101. } );
  102. }
  103. } );