[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-content/plugins/akismet/_inc/ -> akismet.js (source)

   1  jQuery( function ( $ ) {
   2      var mshotRemovalTimer = null;
   3      var mshotSecondTryTimer = null
   4      var mshotThirdTryTimer = null
   5      
   6      var mshotEnabledLinkSelector = 'a[id^="author_comment_url"], tr.pingback td.column-author a:first-of-type, td.comment p a';
   7      
   8      $('.akismet-status').each(function () {
   9          var thisId = $(this).attr('commentid');
  10          $(this).prependTo('#comment-' + thisId + ' .column-comment');
  11      });
  12      $('.akismet-user-comment-count').each(function () {
  13          var thisId = $(this).attr('commentid');
  14          $(this).insertAfter('#comment-' + thisId + ' .author strong:first').show();
  15      });
  16  
  17      akismet_enable_comment_author_url_removal();
  18      
  19      $( '#the-comment-list' ).on( 'click', '.akismet_remove_url', function () {
  20          var thisId = $(this).attr('commentid');
  21          var data = {
  22              action: 'comment_author_deurl',
  23              _wpnonce: WPAkismet.comment_author_url_nonce,
  24              id: thisId
  25          };
  26          $.ajax({
  27              url: ajaxurl,
  28              type: 'POST',
  29              data: data,
  30              beforeSend: function () {
  31                  // Removes "x" link
  32                  $("a[commentid='"+ thisId +"']").hide();
  33                  // Show temp status
  34                  $("#author_comment_url_"+ thisId).html( $( '<span/>' ).text( WPAkismet.strings['Removing...'] ) );
  35              },
  36              success: function (response) {
  37                  if (response) {
  38                      // Show status/undo link
  39                      $("#author_comment_url_"+ thisId)
  40                          .attr('cid', thisId)
  41                          .addClass('akismet_undo_link_removal')
  42                          .html(
  43                              $( '<span/>' ).text( WPAkismet.strings['URL removed'] )
  44                          )
  45                          .append( ' ' )
  46                          .append(
  47                              $( '<span/>' )
  48                                  .text( WPAkismet.strings['(undo)'] )
  49                                  .addClass( 'akismet-span-link' )
  50                          );
  51                  }
  52              }
  53          });
  54  
  55          return false;
  56      }).on( 'click', '.akismet_undo_link_removal', function () {
  57          var thisId = $(this).attr('cid');
  58          var thisUrl = $(this).attr('href');
  59          var data = {
  60              action: 'comment_author_reurl',
  61              _wpnonce: WPAkismet.comment_author_url_nonce,
  62              id: thisId,
  63              url: thisUrl
  64          };
  65          $.ajax({
  66              url: ajaxurl,
  67              type: 'POST',
  68              data: data,
  69              beforeSend: function () {
  70                  // Show temp status
  71                  $("#author_comment_url_"+ thisId).html( $( '<span/>' ).text( WPAkismet.strings['Re-adding...'] ) );
  72              },
  73              success: function (response) {
  74                  if (response) {
  75                      // Add "x" link
  76                      $("a[commentid='"+ thisId +"']").show();
  77                      // Show link. Core strips leading http://, so let's do that too.
  78                      $("#author_comment_url_"+ thisId).removeClass('akismet_undo_link_removal').text( thisUrl.replace( /^http:\/\/(www\.)?/ig, '' ) );
  79                  }
  80              }
  81          });
  82  
  83          return false;
  84      });
  85  
  86      // Show a preview image of the hovered URL. Applies to author URLs and URLs inside the comments.
  87      $( '#the-comment-list' ).on( 'mouseover', mshotEnabledLinkSelector, function () {
  88          clearTimeout( mshotRemovalTimer );
  89  
  90          if ( $( '.akismet-mshot' ).length > 0 ) {
  91              if ( $( '.akismet-mshot:first' ).data( 'link' ) == this ) {
  92                  // The preview is already showing for this link.
  93                  return;
  94              }
  95              else {
  96                  // A new link is being hovered, so remove the old preview.
  97                  $( '.akismet-mshot' ).remove();
  98              }
  99          }
 100  
 101          clearTimeout( mshotSecondTryTimer );
 102          clearTimeout( mshotThirdTryTimer );
 103  
 104          var thisHref = $( this ).attr( 'href' );
 105  
 106          var mShot = $( '<div class="akismet-mshot mshot-container"><div class="mshot-arrow"></div><img src="' + akismet_mshot_url( thisHref ) + '" width="450" height="338" class="mshot-image" /></div>' );
 107          mShot.data( 'link', this );
 108  
 109          var offset = $( this ).offset();
 110  
 111          mShot.offset( {
 112              left : Math.min( $( window ).width() - 475, offset.left + $( this ).width() + 10 ), // Keep it on the screen if the link is near the edge of the window.
 113              top: offset.top + ( $( this ).height() / 2 ) - 101 // 101 = top offset of the arrow plus the top border thickness
 114          } );
 115  
 116          // These retries appear to be superfluous if .mshot-image has already loaded, but it's because mShots
 117          // can return a "Generating thumbnail..." image if it doesn't have a thumbnail ready, so we need
 118          // to retry to see if we can get the newly generated thumbnail.
 119          mshotSecondTryTimer = setTimeout( function () {
 120              mShot.find( '.mshot-image' ).attr( 'src', akismet_mshot_url( thisHref, 2 ) );
 121          }, 6000 );
 122  
 123          mshotThirdTryTimer = setTimeout( function () {
 124              mShot.find( '.mshot-image' ).attr( 'src', akismet_mshot_url( thisHref, 3 ) );
 125          }, 12000 );
 126  
 127          $( 'body' ).append( mShot );
 128      } ).on( 'mouseout', 'a[id^="author_comment_url"], tr.pingback td.column-author a:first-of-type, td.comment p a', function () {
 129          mshotRemovalTimer = setTimeout( function () {
 130              clearTimeout( mshotSecondTryTimer );
 131              clearTimeout( mshotThirdTryTimer );
 132  
 133              $( '.akismet-mshot' ).remove();
 134          }, 200 );
 135      } ).on( 'mouseover', 'tr', function () {
 136          // When the mouse hovers over a comment row, begin preloading mshots for any links in the comment or the comment author.
 137          var linksToPreloadMshotsFor = $( this ).find( mshotEnabledLinkSelector );
 138          
 139          linksToPreloadMshotsFor.each( function () {
 140              // Don't attempt to preload an mshot for a single link twice. Browser caching should cover this, but in case of
 141              // race conditions, save a flag locally when we've begun trying to preload one.
 142              if ( ! $( this ).data( 'akismet-mshot-preloaded' ) ) {
 143                  akismet_preload_mshot( $( this ).attr( 'href' ) );
 144                  $( this ).data( 'akismet-mshot-preloaded', true );
 145              }
 146          } );
 147      } );
 148  
 149      $( '.checkforspam' ).click( function( e ) {
 150          e.preventDefault();
 151  
 152          if ( $( this ).hasClass( 'button-disabled' ) ) {
 153              window.location.href = $( this ).data( 'success-url' ).replace( '__recheck_count__', 0 ).replace( '__spam_count__', 0 );
 154              return;
 155          }
 156  
 157          $('.checkforspam').addClass('button-disabled').addClass( 'checking' );
 158          $('.checkforspam-spinner').addClass( 'spinner' ).addClass( 'is-active' );
 159  
 160          // Update the label on the "Check for Spam" button to use the active "Checking for Spam" language.
 161          $( '.checkforspam .akismet-label' ).text( $( '.checkforspam' ).data( 'active-label' ) );
 162  
 163          akismet_check_for_spam(0, 100);
 164      });
 165  
 166      var spam_count = 0;
 167      var recheck_count = 0;
 168  
 169  	function akismet_check_for_spam(offset, limit) {
 170          var check_for_spam_buttons = $( '.checkforspam' );
 171          
 172          // We show the percentage complete down to one decimal point so even queues with 100k
 173          // pending comments will show some progress pretty quickly.
 174          var percentage_complete = Math.round( ( recheck_count / check_for_spam_buttons.data( 'pending-comment-count' ) ) * 1000 ) / 10;
 175          
 176          // Update the progress counter on the "Check for Spam" button.
 177          $( '.checkforspam-progress' ).text( check_for_spam_buttons.data( 'progress-label-format' ).replace( '%1$s', percentage_complete ) );
 178  
 179          $.post(
 180              ajaxurl,
 181              {
 182                  'action': 'akismet_recheck_queue',
 183                  'offset': offset,
 184                  'limit': limit
 185              },
 186              function(result) {
 187                  recheck_count += result.counts.processed;
 188                  spam_count += result.counts.spam;
 189                  
 190                  if (result.counts.processed < limit) {
 191                      window.location.href = check_for_spam_buttons.data( 'success-url' ).replace( '__recheck_count__', recheck_count ).replace( '__spam_count__', spam_count );
 192                  }
 193                  else {
 194                      // Account for comments that were caught as spam and moved out of the queue.
 195                      akismet_check_for_spam(offset + limit - result.counts.spam, limit);
 196                  }
 197              }
 198          );
 199      }
 200      
 201      if ( "start_recheck" in WPAkismet && WPAkismet.start_recheck ) {
 202          $( '.checkforspam' ).click();
 203      }
 204      
 205      if ( typeof MutationObserver !== 'undefined' ) {
 206          // Dynamically add the "X" next the the author URL links when a comment is quick-edited.
 207          var comment_list_container = document.getElementById( 'the-comment-list' );
 208  
 209          if ( comment_list_container ) {
 210              var observer = new MutationObserver( function ( mutations ) {
 211                  for ( var i = 0, _len = mutations.length; i < _len; i++ ) {
 212                      if ( mutations[i].addedNodes.length > 0 ) {
 213                          akismet_enable_comment_author_url_removal();
 214                          
 215                          // Once we know that we'll have to check for new author links, skip the rest of the mutations.
 216                          break;
 217                      }
 218                  }
 219              } );
 220              
 221              observer.observe( comment_list_container, { attributes: true, childList: true, characterData: true } );
 222          }
 223      }
 224  
 225  	function akismet_enable_comment_author_url_removal() {
 226          $( '#the-comment-list' )
 227              .find( 'tr.comment, tr[id ^= "comment-"]' )
 228              .find( '.column-author a[href^="http"]:first' ) // Ignore mailto: links, which would be the comment author's email.
 229              .each(function () {
 230                  if ( $( this ).parent().find( '.akismet_remove_url' ).length > 0 ) {
 231                      return;
 232                  }
 233              
 234              var linkHref = $(this).attr( 'href' );
 235          
 236              // Ignore any links to the current domain, which are diagnostic tools, like the IP address link
 237              // or any other links another plugin might add.
 238              var currentHostParts = document.location.href.split( '/' );
 239              var currentHost = currentHostParts[0] + '//' + currentHostParts[2] + '/';
 240          
 241              if ( linkHref.indexOf( currentHost ) != 0 ) {
 242                  var thisCommentId = $(this).parents('tr:first').attr('id').split("-");
 243  
 244                  $(this)
 245                      .attr("id", "author_comment_url_"+ thisCommentId[1])
 246                      .after(
 247                          $( '<a href="#" class="akismet_remove_url">x</a>' )
 248                              .attr( 'commentid', thisCommentId[1] )
 249                              .attr( 'title', WPAkismet.strings['Remove this URL'] )
 250                      );
 251              }
 252          });
 253      }
 254      
 255      /**
 256       * Generate an mShot URL if given a link URL.
 257       *
 258       * @param string linkUrl
 259       * @param int retry If retrying a request, the number of the retry.
 260       * @return string The mShot URL;
 261       */
 262  	function akismet_mshot_url( linkUrl, retry ) {
 263          var mshotUrl = '//s0.wordpress.com/mshots/v1/' + encodeURIComponent( linkUrl ) + '?w=900';
 264          
 265          if ( retry ) {
 266              mshotUrl += '&r=' + encodeURIComponent( retry );
 267          }
 268          
 269          return mshotUrl;
 270      }
 271      
 272      /**
 273       * Begin loading an mShot preview of a link.
 274       *
 275       * @param string linkUrl
 276       */
 277  	function akismet_preload_mshot( linkUrl ) {
 278          var img = new Image();
 279          img.src = akismet_mshot_url( linkUrl );
 280      }
 281  
 282      /**
 283       * Sets the comment form privacy notice display to hide when one clicks Core's dismiss button on the related admin notice.
 284       */
 285      $( '#akismet-privacy-notice-admin-notice' ).on( 'click', '.notice-dismiss', function(){
 286          $.ajax({
 287                          url: './options-general.php?page=akismet-key-config&akismet_comment_form_privacy_notice=hide',
 288          });
 289      });
 290  
 291      $( ".akismet-could-be-primary" ).each( function () {
 292          var form = $( this ).closest( 'form' );
 293  
 294          form.data( 'initial-state', form.serialize() );
 295  
 296          form.on( 'change keyup', function () {
 297              var self = $( this );
 298              var submit_button = self.find( ".akismet-could-be-primary" );
 299  
 300              if ( self.serialize() != self.data( 'initial-state' ) ) {
 301                  submit_button.addClass( "akismet-is-primary" );
 302              }
 303              else {
 304                  submit_button.removeClass( "akismet-is-primary" );
 305              }
 306          } );
 307      } );
 308  });


Generated: Tue Sep 17 01:00:03 2019 Cross-referenced by PHPXref 0.7.1