[ 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' ).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      });
 169      $( '.checkforspam.enable-on-load' ).removeClass( 'button-disabled' );
 170  
 171      var spam_count = 0;
 172      var recheck_count = 0;
 173  
 174  	function akismet_check_for_spam(offset, limit) {
 175          var check_for_spam_buttons = $( '.checkforspam' );
 176          
 177          var nonce = check_for_spam_buttons.data( 'nonce' );
 178          
 179          // We show the percentage complete down to one decimal point so even queues with 100k
 180          // pending comments will show some progress pretty quickly.
 181          var percentage_complete = Math.round( ( recheck_count / check_for_spam_buttons.data( 'pending-comment-count' ) ) * 1000 ) / 10;
 182          
 183          // Update the progress counter on the "Check for Spam" button.
 184          $( '.checkforspam' ).text( check_for_spam_buttons.data( 'progress-label' ).replace( '%1$s', percentage_complete ) );
 185  
 186          $.post(
 187              ajaxurl,
 188              {
 189                  'action': 'akismet_recheck_queue',
 190                  'offset': offset,
 191                  'limit': limit,
 192                  'nonce': nonce
 193              },
 194              function(result) {
 195                  if ( 'error' in result ) {
 196                      // An error is only returned in the case of a missing nonce, so we don't need the actual error message.
 197                      window.location.href = check_for_spam_buttons.data( 'failure-url' );
 198                      return;
 199                  }
 200                  
 201                  recheck_count += result.counts.processed;
 202                  spam_count += result.counts.spam;
 203                  
 204                  if (result.counts.processed < limit) {
 205                      window.location.href = check_for_spam_buttons.data( 'success-url' ).replace( '__recheck_count__', recheck_count ).replace( '__spam_count__', spam_count );
 206                  }
 207                  else {
 208                      // Account for comments that were caught as spam and moved out of the queue.
 209                      akismet_check_for_spam(offset + limit - result.counts.spam, limit);
 210                  }
 211              }
 212          );
 213      }
 214      
 215      if ( "start_recheck" in WPAkismet && WPAkismet.start_recheck ) {
 216          $( '.checkforspam' ).click();
 217      }
 218      
 219      if ( typeof MutationObserver !== 'undefined' ) {
 220          // Dynamically add the "X" next the the author URL links when a comment is quick-edited.
 221          var comment_list_container = document.getElementById( 'the-comment-list' );
 222  
 223          if ( comment_list_container ) {
 224              var observer = new MutationObserver( function ( mutations ) {
 225                  for ( var i = 0, _len = mutations.length; i < _len; i++ ) {
 226                      if ( mutations[i].addedNodes.length > 0 ) {
 227                          akismet_enable_comment_author_url_removal();
 228                          
 229                          // Once we know that we'll have to check for new author links, skip the rest of the mutations.
 230                          break;
 231                      }
 232                  }
 233              } );
 234              
 235              observer.observe( comment_list_container, { attributes: true, childList: true, characterData: true } );
 236          }
 237      }
 238  
 239  	function akismet_enable_comment_author_url_removal() {
 240          $( '#the-comment-list' )
 241              .find( 'tr.comment, tr[id ^= "comment-"]' )
 242              .find( '.column-author a[href^="http"]:first' ) // Ignore mailto: links, which would be the comment author's email.
 243              .each(function () {
 244                  if ( $( this ).parent().find( '.akismet_remove_url' ).length > 0 ) {
 245                      return;
 246                  }
 247              
 248              var linkHref = $(this).attr( 'href' );
 249          
 250              // Ignore any links to the current domain, which are diagnostic tools, like the IP address link
 251              // or any other links another plugin might add.
 252              var currentHostParts = document.location.href.split( '/' );
 253              var currentHost = currentHostParts[0] + '//' + currentHostParts[2] + '/';
 254          
 255              if ( linkHref.indexOf( currentHost ) != 0 ) {
 256                  var thisCommentId = $(this).parents('tr:first').attr('id').split("-");
 257  
 258                  $(this)
 259                      .attr("id", "author_comment_url_"+ thisCommentId[1])
 260                      .after(
 261                          $( '<a href="#" class="akismet_remove_url">x</a>' )
 262                              .attr( 'commentid', thisCommentId[1] )
 263                              .attr( 'title', WPAkismet.strings['Remove this URL'] )
 264                      );
 265              }
 266          });
 267      }
 268      
 269      /**
 270       * Generate an mShot URL if given a link URL.
 271       *
 272       * @param string linkUrl
 273       * @param int retry If retrying a request, the number of the retry.
 274       * @return string The mShot URL;
 275       */
 276  	function akismet_mshot_url( linkUrl, retry ) {
 277          var mshotUrl = '//s0.wordpress.com/mshots/v1/' + encodeURIComponent( linkUrl ) + '?w=900';
 278          
 279          if ( retry ) {
 280              mshotUrl += '&r=' + encodeURIComponent( retry );
 281          }
 282          
 283          return mshotUrl;
 284      }
 285      
 286      /**
 287       * Begin loading an mShot preview of a link.
 288       *
 289       * @param string linkUrl
 290       */
 291  	function akismet_preload_mshot( linkUrl ) {
 292          var img = new Image();
 293          img.src = akismet_mshot_url( linkUrl );
 294      }
 295  
 296      $( '.akismet-could-be-primary' ).each( function () {
 297          var form = $( this ).closest( 'form' );
 298  
 299          form.data( 'initial-state', form.serialize() );
 300  
 301          form.on( 'change keyup', function () {
 302              var self = $( this );
 303              var submit_button = self.find( '.akismet-could-be-primary' );
 304  
 305              if ( self.serialize() != self.data( 'initial-state' ) ) {
 306                  submit_button.addClass( 'akismet-is-primary' );
 307              }
 308              else {
 309                  submit_button.removeClass( 'akismet-is-primary' );
 310              }
 311          } );
 312      } );
 313  
 314      /**
 315       * Shows the Enter API key form
 316       */
 317      $( '.akismet-enter-api-key-box a' ).on( 'click', function ( e ) {
 318          e.preventDefault();
 319  
 320          var div = $( '.enter-api-key' );
 321          div.show( 500 );
 322          div.find( 'input[name=key]' ).focus();
 323  
 324          $( this ).hide();
 325      } );
 326  
 327      /**
 328       * Hides the Connect with Jetpack form | Shows the Activate Akismet Account form
 329       */
 330      $( 'a.toggle-ak-connect' ).on( 'click', function ( e ) {
 331          e.preventDefault();
 332  
 333          $( '.akismet-ak-connect' ).slideToggle('slow');
 334          $( 'a.toggle-ak-connect' ).hide();
 335          $( '.akismet-jp-connect' ).hide();
 336          $( 'a.toggle-jp-connect' ).show();
 337      } );
 338  
 339      /**
 340       * Shows the Connect with Jetpack form | Hides the Activate Akismet Account form
 341       */
 342      $( 'a.toggle-jp-connect' ).on( 'click', function ( e ) {
 343          e.preventDefault();
 344  
 345          $( '.akismet-jp-connect' ).slideToggle('slow');
 346          $( 'a.toggle-jp-connect' ).hide();
 347          $( '.akismet-ak-connect' ).hide();
 348          $( 'a.toggle-ak-connect' ).show();
 349      } );
 350  });


Generated: Fri Aug 7 01:00:03 2020 Cross-referenced by PHPXref 0.7.1