[ 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      if ( "enable_mshots" in WPAkismet && WPAkismet.enable_mshots ) {
  88          $( '#the-comment-list' ).on( 'mouseover', mshotEnabledLinkSelector, function () {
  89              clearTimeout( mshotRemovalTimer );
  90  
  91              if ( $( '.akismet-mshot' ).length > 0 ) {
  92                  if ( $( '.akismet-mshot:first' ).data( 'link' ) == this ) {
  93                      // The preview is already showing for this link.
  94                      return;
  95                  }
  96                  else {
  97                      // A new link is being hovered, so remove the old preview.
  98                      $( '.akismet-mshot' ).remove();
  99                  }
 100              }
 101  
 102              clearTimeout( mshotSecondTryTimer );
 103              clearTimeout( mshotThirdTryTimer );
 104  
 105              var thisHref = $( this ).attr( 'href' );
 106  
 107              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>' );
 108              mShot.data( 'link', this );
 109  
 110              var offset = $( this ).offset();
 111  
 112              mShot.offset( {
 113                  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.
 114                  top: offset.top + ( $( this ).height() / 2 ) - 101 // 101 = top offset of the arrow plus the top border thickness
 115              } );
 116  
 117              // These retries appear to be superfluous if .mshot-image has already loaded, but it's because mShots
 118              // can return a "Generating thumbnail..." image if it doesn't have a thumbnail ready, so we need
 119              // to retry to see if we can get the newly generated thumbnail.
 120              mshotSecondTryTimer = setTimeout( function () {
 121                  mShot.find( '.mshot-image' ).attr( 'src', akismet_mshot_url( thisHref, 2 ) );
 122              }, 6000 );
 123  
 124              mshotThirdTryTimer = setTimeout( function () {
 125                  mShot.find( '.mshot-image' ).attr( 'src', akismet_mshot_url( thisHref, 3 ) );
 126              }, 12000 );
 127  
 128              $( 'body' ).append( mShot );
 129          } ).on( 'mouseout', 'a[id^="author_comment_url"], tr.pingback td.column-author a:first-of-type, td.comment p a', function () {
 130              mshotRemovalTimer = setTimeout( function () {
 131                  clearTimeout( mshotSecondTryTimer );
 132                  clearTimeout( mshotThirdTryTimer );
 133  
 134                  $( '.akismet-mshot' ).remove();
 135              }, 200 );
 136          } ).on( 'mouseover', 'tr', function () {
 137              // When the mouse hovers over a comment row, begin preloading mshots for any links in the comment or the comment author.
 138              var linksToPreloadMshotsFor = $( this ).find( mshotEnabledLinkSelector );
 139              
 140              linksToPreloadMshotsFor.each( function () {
 141                  // Don't attempt to preload an mshot for a single link twice. Browser caching should cover this, but in case of
 142                  // race conditions, save a flag locally when we've begun trying to preload one.
 143                  if ( ! $( this ).data( 'akismet-mshot-preloaded' ) ) {
 144                      akismet_preload_mshot( $( this ).attr( 'href' ) );
 145                      $( this ).data( 'akismet-mshot-preloaded', true );
 146                  }
 147              } );
 148          } );
 149      }
 150  
 151      $( '.checkforspam.enable-on-load' ).on( 'click', function( e ) {
 152          if ( $( this ).hasClass( 'ajax-disabled' ) ) {
 153              // Akismet hasn't been configured yet. Allow the user to proceed to the button's link.
 154              return;
 155          }
 156  
 157          e.preventDefault();
 158  
 159          if ( $( this ).hasClass( 'button-disabled' ) ) {
 160              window.location.href = $( this ).data( 'success-url' ).replace( '__recheck_count__', 0 ).replace( '__spam_count__', 0 );
 161              return;
 162          }
 163  
 164          $('.checkforspam').addClass('button-disabled').addClass( 'checking' );
 165          $('.checkforspam-spinner').addClass( 'spinner' ).addClass( 'is-active' );
 166  
 167          akismet_check_for_spam(0, 100);
 168      }).removeClass( 'button-disabled' );
 169  
 170      var spam_count = 0;
 171      var recheck_count = 0;
 172  
 173  	function akismet_check_for_spam(offset, limit) {
 174          var check_for_spam_buttons = $( '.checkforspam' );
 175          
 176          var nonce = check_for_spam_buttons.data( 'nonce' );
 177          
 178          // We show the percentage complete down to one decimal point so even queues with 100k
 179          // pending comments will show some progress pretty quickly.
 180          var percentage_complete = Math.round( ( recheck_count / check_for_spam_buttons.data( 'pending-comment-count' ) ) * 1000 ) / 10;
 181          
 182          // Update the progress counter on the "Check for Spam" button.
 183          $( '.checkforspam' ).text( check_for_spam_buttons.data( 'progress-label' ).replace( '%1$s', percentage_complete ) );
 184  
 185          $.post(
 186              ajaxurl,
 187              {
 188                  'action': 'akismet_recheck_queue',
 189                  'offset': offset,
 190                  'limit': limit,
 191                  'nonce': nonce
 192              },
 193              function(result) {
 194                  if ( 'error' in result ) {
 195                      // An error is only returned in the case of a missing nonce, so we don't need the actual error message.
 196                      window.location.href = check_for_spam_buttons.data( 'failure-url' );
 197                      return;
 198                  }
 199                  
 200                  recheck_count += result.counts.processed;
 201                  spam_count += result.counts.spam;
 202                  
 203                  if (result.counts.processed < limit) {
 204                      window.location.href = check_for_spam_buttons.data( 'success-url' ).replace( '__recheck_count__', recheck_count ).replace( '__spam_count__', spam_count );
 205                  }
 206                  else {
 207                      // Account for comments that were caught as spam and moved out of the queue.
 208                      akismet_check_for_spam(offset + limit - result.counts.spam, limit);
 209                  }
 210              }
 211          );
 212      }
 213      
 214      if ( "start_recheck" in WPAkismet && WPAkismet.start_recheck ) {
 215          $( '.checkforspam' ).click();
 216      }
 217      
 218      if ( typeof MutationObserver !== 'undefined' ) {
 219          // Dynamically add the "X" next the the author URL links when a comment is quick-edited.
 220          var comment_list_container = document.getElementById( 'the-comment-list' );
 221  
 222          if ( comment_list_container ) {
 223              var observer = new MutationObserver( function ( mutations ) {
 224                  for ( var i = 0, _len = mutations.length; i < _len; i++ ) {
 225                      if ( mutations[i].addedNodes.length > 0 ) {
 226                          akismet_enable_comment_author_url_removal();
 227                          
 228                          // Once we know that we'll have to check for new author links, skip the rest of the mutations.
 229                          break;
 230                      }
 231                  }
 232              } );
 233              
 234              observer.observe( comment_list_container, { attributes: true, childList: true, characterData: true } );
 235          }
 236      }
 237  
 238  	function akismet_enable_comment_author_url_removal() {
 239          $( '#the-comment-list' )
 240              .find( 'tr.comment, tr[id ^= "comment-"]' )
 241              .find( '.column-author a[href^="http"]:first' ) // Ignore mailto: links, which would be the comment author's email.
 242              .each(function () {
 243                  if ( $( this ).parent().find( '.akismet_remove_url' ).length > 0 ) {
 244                      return;
 245                  }
 246              
 247              var linkHref = $(this).attr( 'href' );
 248          
 249              // Ignore any links to the current domain, which are diagnostic tools, like the IP address link
 250              // or any other links another plugin might add.
 251              var currentHostParts = document.location.href.split( '/' );
 252              var currentHost = currentHostParts[0] + '//' + currentHostParts[2] + '/';
 253          
 254              if ( linkHref.indexOf( currentHost ) != 0 ) {
 255                  var thisCommentId = $(this).parents('tr:first').attr('id').split("-");
 256  
 257                  $(this)
 258                      .attr("id", "author_comment_url_"+ thisCommentId[1])
 259                      .after(
 260                          $( '<a href="#" class="akismet_remove_url">x</a>' )
 261                              .attr( 'commentid', thisCommentId[1] )
 262                              .attr( 'title', WPAkismet.strings['Remove this URL'] )
 263                      );
 264              }
 265          });
 266      }
 267      
 268      /**
 269       * Generate an mShot URL if given a link URL.
 270       *
 271       * @param string linkUrl
 272       * @param int retry If retrying a request, the number of the retry.
 273       * @return string The mShot URL;
 274       */
 275  	function akismet_mshot_url( linkUrl, retry ) {
 276          var mshotUrl = '//s0.wordpress.com/mshots/v1/' + encodeURIComponent( linkUrl ) + '?w=900';
 277          
 278          if ( retry ) {
 279              mshotUrl += '&r=' + encodeURIComponent( retry );
 280          }
 281          
 282          return mshotUrl;
 283      }
 284      
 285      /**
 286       * Begin loading an mShot preview of a link.
 287       *
 288       * @param string linkUrl
 289       */
 290  	function akismet_preload_mshot( linkUrl ) {
 291          var img = new Image();
 292          img.src = akismet_mshot_url( linkUrl );
 293      }
 294  
 295      $( '.akismet-could-be-primary' ).each( function () {
 296          var form = $( this ).closest( 'form' );
 297  
 298          form.data( 'initial-state', form.serialize() );
 299  
 300          form.on( 'change keyup', function () {
 301              var self = $( this );
 302              var submit_button = self.find( '.akismet-could-be-primary' );
 303  
 304              if ( self.serialize() != self.data( 'initial-state' ) ) {
 305                  submit_button.addClass( 'akismet-is-primary' );
 306              }
 307              else {
 308                  submit_button.removeClass( 'akismet-is-primary' );
 309              }
 310          } );
 311      } );
 312  
 313      /**
 314       * Shows the Enter API key form
 315       */
 316      $( '.akismet-enter-api-key-box a' ).on( 'click', function ( e ) {
 317          e.preventDefault();
 318  
 319          var div = $( '.enter-api-key' );
 320          div.show( 500 );
 321          div.find( 'input[name=key]' ).focus();
 322  
 323          $( this ).hide();
 324      } );
 325  
 326      /**
 327       * Hides the Connect with Jetpack form | Shows the Activate Akismet Account form
 328       */
 329      $( 'a.toggle-ak-connect' ).on( 'click', function ( e ) {
 330          e.preventDefault();
 331  
 332          $( '.akismet-ak-connect' ).slideToggle('slow');
 333          $( 'a.toggle-ak-connect' ).hide();
 334          $( '.akismet-jp-connect' ).hide();
 335          $( 'a.toggle-jp-connect' ).show();
 336      } );
 337  
 338      /**
 339       * Shows the Connect with Jetpack form | Hides the Activate Akismet Account form
 340       */
 341      $( 'a.toggle-jp-connect' ).on( 'click', function ( e ) {
 342          e.preventDefault();
 343  
 344          $( '.akismet-jp-connect' ).slideToggle('slow');
 345          $( 'a.toggle-jp-connect' ).hide();
 346          $( '.akismet-ak-connect' ).hide();
 347          $( 'a.toggle-ak-connect' ).show();
 348      } );
 349  });


Generated: Mon May 17 01:00:05 2021 Cross-referenced by PHPXref 0.7.1