[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7