home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress2 / wp-content / plugins / akismet / _inc / akismet.js < prev    next >
Encoding:
JavaScript  |  2017-08-15  |  9.3 KB  |  276 lines

  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:not(.button-disabled)').click( function(e) {
  150.         e.preventDefault();
  151.  
  152.         $('.checkforspam:not(.button-disabled)').addClass('button-disabled');
  153.         $('.checkforspam-spinner').addClass( 'spinner' ).addClass( 'is-active' );
  154.  
  155.         // Update the label on the "Check for Spam" button to use the active "Checking for Spam" language.
  156.         $( '.checkforspam .akismet-label' ).text( $( '.checkforspam' ).data( 'active-label' ) );
  157.  
  158.         akismet_check_for_spam(0, 100);
  159.     });
  160.  
  161.     var spam_count = 0;
  162.     var recheck_count = 0;
  163.  
  164.     function akismet_check_for_spam(offset, limit) {
  165.         var check_for_spam_buttons = $( '.checkforspam' );
  166.         
  167.         // We show the percentage complete down to one decimal point so even queues with 100k
  168.         // pending comments will show some progress pretty quickly.
  169.         var percentage_complete = Math.round( ( recheck_count / check_for_spam_buttons.data( 'pending-comment-count' ) ) * 1000 ) / 10;
  170.         
  171.         // Update the progress counter on the "Check for Spam" button.
  172.         $( '.checkforspam-progress' ).text( check_for_spam_buttons.data( 'progress-label-format' ).replace( '%1$s', percentage_complete ) );
  173.  
  174.         $.post(
  175.             ajaxurl,
  176.             {
  177.                 'action': 'akismet_recheck_queue',
  178.                 'offset': offset,
  179.                 'limit': limit
  180.             },
  181.             function(result) {
  182.                 recheck_count += result.counts.processed;
  183.                 spam_count += result.counts.spam;
  184.                 
  185.                 if (result.counts.processed < limit) {
  186.                     window.location.href = check_for_spam_buttons.data( 'success-url' ).replace( '__recheck_count__', recheck_count ).replace( '__spam_count__', spam_count );
  187.                 }
  188.                 else {
  189.                     // Account for comments that were caught as spam and moved out of the queue.
  190.                     akismet_check_for_spam(offset + limit - result.counts.spam, limit);
  191.                 }
  192.             }
  193.         );
  194.     }
  195.     
  196.     if ( "start_recheck" in WPAkismet && WPAkismet.start_recheck ) {
  197.         $( '.checkforspam' ).click();
  198.     }
  199.     
  200.     if ( typeof MutationObserver !== 'undefined' ) {
  201.         // Dynamically add the "X" next the the author URL links when a comment is quick-edited.
  202.         var comment_list_container = document.getElementById( 'the-comment-list' );
  203.  
  204.         if ( comment_list_container ) {
  205.             var observer = new MutationObserver( function ( mutations ) {
  206.                 for ( var i = 0, _len = mutations.length; i < _len; i++ ) {
  207.                     if ( mutations[i].addedNodes.length > 0 ) {
  208.                         akismet_enable_comment_author_url_removal();
  209.                         
  210.                         // Once we know that we'll have to check for new author links, skip the rest of the mutations.
  211.                         break;
  212.                     }
  213.                 }
  214.             } );
  215.             
  216.             observer.observe( comment_list_container, { attributes: true, childList: true, characterData: true } );
  217.         }
  218.     }
  219.  
  220.     function akismet_enable_comment_author_url_removal() {
  221.         $( '#the-comment-list' )
  222.             .find( 'tr.comment, tr[id ^= "comment-"]' )
  223.             .find( '.column-author a[href^="http"]:first' ) // Ignore mailto: links, which would be the comment author's email.
  224.             .each(function () {
  225.                 if ( $( this ).parent().find( '.akismet_remove_url' ).length > 0 ) {
  226.                     return;
  227.                 }
  228.             
  229.             var linkHref = $(this).attr( 'href' );
  230.         
  231.             // Ignore any links to the current domain, which are diagnostic tools, like the IP address link
  232.             // or any other links another plugin might add.
  233.             var currentHostParts = document.location.href.split( '/' );
  234.             var currentHost = currentHostParts[0] + '//' + currentHostParts[2] + '/';
  235.         
  236.             if ( linkHref.indexOf( currentHost ) != 0 ) {
  237.                 var thisCommentId = $(this).parents('tr:first').attr('id').split("-");
  238.  
  239.                 $(this)
  240.                     .attr("id", "author_comment_url_"+ thisCommentId[1])
  241.                     .after(
  242.                         $( '<a href="#" class="akismet_remove_url">x</a>' )
  243.                             .attr( 'commentid', thisCommentId[1] )
  244.                             .attr( 'title', WPAkismet.strings['Remove this URL'] )
  245.                     );
  246.             }
  247.         });
  248.     }
  249.     
  250.     /**
  251.      * Generate an mShot URL if given a link URL.
  252.      *
  253.      * @param string linkUrl
  254.      * @param int retry If retrying a request, the number of the retry.
  255.      * @return string The mShot URL;
  256.      */
  257.     function akismet_mshot_url( linkUrl, retry ) {
  258.         var mshotUrl = '//s0.wordpress.com/mshots/v1/' + encodeURIComponent( linkUrl ) + '?w=900';
  259.         
  260.         if ( retry ) {
  261.             mshotUrl += '&r=' + encodeURIComponent( retry );
  262.         }
  263.         
  264.         return mshotUrl;
  265.     }
  266.     
  267.     /**
  268.      * Begin loading an mShot preview of a link.
  269.      *
  270.      * @param string linkUrl
  271.      */
  272.     function akismet_preload_mshot( linkUrl ) {
  273.         var img = new Image();
  274.         img.src = akismet_mshot_url( linkUrl );
  275.     }
  276. });