Plugin Directory

Changeset 714049


Ignore:
Timestamp:
05/16/2013 07:49:52 PM (13 years ago)
Author:
matthewbe
Message:
  • using jquery to fix position related bugs
  • spawn animation
  • spiders escape in a zigzag when approaching
  • moved JS code in external file
Location:
spider-invasion/trunk
Files:
3 added
4 edited

Legend:

Unmodified
Added
Removed
  • spider-invasion/trunk/readme.txt

    r713379 r714049  
    1818
    1919= Note that your post should be at least 1 year old for spiders to appear. =
    20 Moreover, these spiders are kind of frightened and unadventurous, hence they may not show up all the time.
     20Moreover, these spiders are kind of frightened and unadventurous hence they may not show up all the time and if you're approaching them too close, they will frighten away.
    2121
    2222== Installation ==
     
    2727
    2828== Changelog ==
     29
     30= 0.4 =
     31* using jquery to fix position related bugs
     32* spawn animation
     33* spiders escape in a zigzag when approaching
     34* moved JS code in external file
    2935
    3036= 0.3 =
  • spider-invasion/trunk/spider-invasion.php

    r713304 r714049  
    22/*
    33Plugin Name: Spider Invasion!
    4 Version: 0.3
     4Version: 0.4
    55Plugin URI: http://www.mendoweb.be/blog/wordpress-plugin-spider-invasion/
    66Description: Spiders invade your oldest posts. The older the post, the more spiders you get.
     
    2323*/
    2424
    25 function date_diff_year($d1, $d2) {
     25function includeJQuery() {
     26    if ( !is_admin() ) {
     27        wp_enqueue_script( 'jquery' );
     28    }
     29}
     30add_action( 'init', 'includeJQuery' );
     31
     32function get_date_diff($d1, $d2) {
    2633    /*
    2734    // only from PHP 5.3 and later versions
     
    2936    $datetime_d1 = date_create( $d1 );
    3037    $interval = date_diff( $datetime_d2, $datetime_d1 );
    31     return (int) $interval->format( '%y' );
     38    return array(
     39        'y' => (int) $interval->format( '%y' ),
     40        'm' => (int) $interval->format( '%m' ),
     41        'd' => (int) $interval->format( '%d' )
     42    );
    3243    */
    3344    // compatible with PHP 5.2 and lower
    3445    $diff = abs( strtotime( $d2 ) - strtotime( $d1 ) );
    35     return floor( $diff / (365*60*60*24) );
     46    $years = floor($diff / (365*60*60*24));
     47    $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
     48    $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
     49    return array(
     50        'y' => $years,
     51        'm' => $months,
     52        'd' => $days
     53    );
    3654}
    3755
     
    4159    if( empty( $date_post ) ) return;
    4260   
    43     $nb_spiders = date_diff_year( $date_post, date( 'Ymd' ) );
    44     if( $nb_spiders == 0 ) return;
    45    
    46     $invade_content = FALSE;
    47     $max_density = 800*800;
    48     $min_density = 1800*1800;
    49     $plugin_images_dir = plugins_url( 'images' , __FILE__ );
     61    $date_diff = get_date_diff( $date_post, date( 'Ymd' ) );
     62    if( $date_diff['y']*12 + $date_diff['m'] < 8 ) return;
     63    $nb_spiders = $date_diff['y'];
    5064    $compressJS = TRUE;
    5165   
    52     echo '<!-- start spider invasion -->';
    53     if($compressJS) {
    54         $str = <<<JS
    55 <script>
    56 function getRandomPosition(e){var t=document.body.offsetHeight-e.clientHeight;var n=document.body.offsetWidth-e.clientWidth;var r=Math.floor(Math.random()*t);var i=Math.floor(Math.random()*n);return[r,i]}
    57 function spawnSpider(){var e="$plugin_images_dir/cute-spider-"+(Math.floor(Math.random()*3)+1)+".png";var t=document.createElement("img");t.setAttribute("style","opacity:0.5;position:absolute;");t.setAttribute("src",e);document.body.appendChild(t);var n=getRandomPosition(t);var r="$invade_content";if(r===""){var i=document.getElementById("content");if(i!=null&&n[1]>i.offsetLeft-t.clientHeight&&n[1]<i.offsetLeft+i.offsetWidth&&n[0]>i.offsetTop&&n[0]<i.offsetTop+i.offsetHeight){t.setAttribute("style","display:none;")}}t.style.top=n[0]+"px";t.style.left=n[1]+"px"}
    58 window.onload=function(){var e=document.body.offsetHeight*document.body.offsetWidth;var t=Math.floor(e/$max_density);var n=Math.floor(e/$min_density);var r=$nb_spiders>t?t<1?1:t:Math.floor($nb_spiders);r=$nb_spiders<n?n:$nb_spiders;for(i=0;i<r;++i)spawnSpider()}
    59 </script>
    60 JS;
    61     }
    62     else {
    63         $str = <<<JS
    64 <script>
    65 function getRandomPosition(element) {
    66     var x = document.body.offsetHeight-element.clientHeight;
    67     var y = document.body.offsetWidth-element.clientWidth;
    68     var randomX = Math.floor(Math.random()*x);
    69     var randomY = Math.floor(Math.random()*y);
    70     return [randomX,randomY];
    71 }
    72 function getRandomPosition(element) {
    73     var x = document.body.offsetHeight-element.clientHeight;
    74     var y = document.body.offsetWidth-element.clientWidth;
    75     var randomX = Math.floor(Math.random()*x);
    76     var randomY = Math.floor(Math.random()*y);
    77     return [randomX,randomY];
    78 }
    79 function spawnSpider() {
    80     var src = '$plugin_images_dir/cute-spider-' + (Math.floor(Math.random() * 3) + 1) + '.png';
    81     var img = document.createElement('img');
    82     img.setAttribute("style", "opacity:0.5;position:absolute;");
    83     img.setAttribute("src", src);
    84     document.body.appendChild(img);
    85     var xy = getRandomPosition(img);
    86     var invadeContent = '$invade_content';
    87     if(invadeContent === '') {
    88         var content = document.getElementById('content');
    89         if(
    90             content != null &&
    91             xy[1] > (content.offsetLeft - img.clientHeight) && xy[1] < (content.offsetLeft + content.offsetWidth) &&
    92             xy[0] > content.offsetTop && xy[0] < (content.offsetTop + content.offsetHeight)
    93         ) {
    94             img.setAttribute("style", "display:none;");
    95         }
    96     }
    97     img.style.top = xy[0] + 'px';
    98     img.style.left = xy[1] + 'px';
    99 }
    100 window.onload = function() {
    101     var surface = document.body.offsetHeight*document.body.offsetWidth;
    102     var densityMax = Math.floor(surface/$max_density);
    103     var densityMin = Math.floor(surface/$min_density);
    104     var nbSpiders = $nb_spiders > densityMax ? (densityMax < 1 ? 1 : densityMax) : Math.floor($nb_spiders);
    105     nbSpiders = $nb_spiders < densityMin ? densityMin : $nb_spiders;
    106     for(i=0;i<nbSpiders;++i) spawnSpider();
    107 }
    108 </script>
    109 JS;
    110     }
    111     echo $str;
    112     echo '<!-- end spider invasion -->
    113 ';
     66    wp_enqueue_script( 'spider-invasion', plugins_url( 'js' , __FILE__ ) . '/spider-invasion' . ($compressJS ? '.min' : '') . '.js' );
     67    $params = array(
     68        'max_density' => 800*800,
     69        'min_density' => 1800*1800,
     70        'invade_content' => FALSE,
     71        'images_dir' => plugins_url( 'images' , __FILE__ ),
     72        'nb_spiders' => $nb_spiders,
     73    );
     74    wp_localize_script( 'spider-invasion', 'param', $params );
    11475}
    11576add_action('wp_head', 'spider_invasion_js');
Note: See TracChangeset for help on using the changeset viewer.