Plugin Directory

Changeset 747009


Ignore:
Timestamp:
07/27/2013 08:20:25 AM (13 years ago)
Author:
toxicToad
Message:

By popular request, added non resetting continuous background counter.
Various code cleanups

Location:
jellyfish-counter-widget/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • jellyfish-counter-widget/trunk/jellyfish-counter-widget.php

    r651892 r747009  
    1 <?php 
     1<?php
    22/*
    33  Plugin Name: Jellyfish Counter Widget
     
    55  Description: Creates a widget with an odometer style counter that displays either a static number or animates up to a predefined value.
    66  Author: Rob Miller
    7   Version: 0.6
     7  Version: 0.8
    88  Author URI: http://strawberryjellyfish.com/
     9 */
     10
     11/*
     12  This program is free software; you can redistribute it and/or modify
     13  it under the terms of the GNU General Public License as published by
     14  the Free Software Foundation; either version 2 of the License, or
     15  (at your option) any later version.
     16
     17  This program is distributed in the hope that it will be useful,
     18  but WITHOUT ANY WARRANTY; without even the implied warranty of
     19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     20  GNU General Public License for more details.
     21
     22  You should have received a copy of the GNU General Public License
     23  along with this program; if not, write to the Free Software
     24  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
     25  Online: http://www.gnu.org/licenses/gpl.txt
    926 */
    1027?>
    1128<?php
    12 // Register function to be called when widget initialization occurs
    13 
    14 add_action( 'widgets_init', 'jellyfish_cw_create_widgets' );
    15 
    16 // Create new widget
     29add_action('widgets_init', 'jellyfish_cw_create_widgets');
     30
    1731function jellyfish_cw_create_widgets() {
    18     register_widget( 'Jellyfish_Counter_Widget' );
     32    register_widget('Jellyfish_Counter_Widget');
    1933}
    2034
    21 // Widget implementation class
     35// Counter Widget class
    2236class Jellyfish_Counter_Widget extends WP_Widget {
    23    
    24     // Constructor function
     37
    2538    function __construct() {
    26         // Widget creation function
    27     parent::__construct(
    28                 'counter_widget',
    29                 'Counter Widget',
    30                 array( 'description' => 'Show an odometer style counter' )
     39        parent::__construct(
     40                'counter_widget', 'Counter Widget', array('description' => 'Show an odometer style counter')
    3141        );
    3242    }
    3343
    34     // Code to render options form
    35     function form( $instance ) {
    36        
    37                 // Retrieve previous values from instance
     44    // options form
     45    function form($instance) {
     46        // Retrieve previous values from instance
    3847        // or set default values if not present
    39     $show_title = ( !empty( $instance['show_title'] ) ?
    40                   $instance['show_title'] : 'true' );
    41    
    42         $widget_title = ( !empty( $instance['widget_title'] ) ?
    43                     esc_attr( $instance['widget_title'] ) :
    44                             'Counter' );
    45        
    46         $display_tenths = ( !empty( $instance['display_tenths'] ) ?
    47                       $instance['display_tenths'] : 'true' );
    48        
    49         $number_of_digits = ( !empty( $instance['number_of_digits'] ) ?
    50                     esc_attr( $instance['number_of_digits'] ) :
    51                             '6' );
    52        
    53         $digit_height = ( !empty( $instance['digit_height'] ) ?
    54                     esc_attr( $instance['digit_height'] ) :
    55                             '40' );
    56        
    57         $digit_width = ( !empty( $instance['digit_width'] ) ?
    58                     esc_attr( $instance['digit_width'] ) :
    59                             '30' );
    60        
    61         $digit_padding = ( !empty( $instance['digit_padding'] ) ?
    62                     esc_attr( $instance['digit_padding'] ) :
    63                             '0' );
    64        
    65         $digit_bustedness = ( !empty( $instance['digit_bustedness'] ) ?
    66                     esc_attr( $instance['digit_bustedness'] ) :
    67                             '2' );
    68        
    69         $digit_style = ( !empty( $instance['digit_style'] ) ?
    70                     esc_attr( $instance['digit_style'] ) :
    71                             'font-family: Courier New, Courier, monospace; font-weight: 900;' );
    72        
    73         $start_value = ( !empty( $instance['start_value'] ) ?
    74                     esc_attr( $instance['start_value'] ) :
    75                             '0' );
    76        
    77         $end_value = ( !empty( $instance['end_value'] ) ?
    78                     esc_attr( $instance['end_value'] ) :
    79                             '100' );
    80        $animate_speed = ( !empty( $instance['animate_speed'] ) ?
    81                     esc_attr( $instance['animate_speed'] ) :
    82                             '50' );
    83                
    84     ?>
    85     <p>
    86                    <label for="<?php echo
    87                         $this->get_field_id( 'widget_title' ); ?>">
    88             <?php echo 'Title:'; ?>         
    89             <input type="text"
    90                     id="<?php echo $this->get_field_id( 'widget_title' ); ?>"
    91                     name="<?php echo $this->get_field_name( 'widget_title' ); ?>"
    92                     value="<?php echo $widget_title; ?>" />         
    93                     </label>
    94         </p>
    95                 <p>
    96                     <label for="<?php echo
    97                         $this->get_field_id( 'show_title' ); ?>">
    98             <?php echo 'Show Title'; ?>         
    99             <select id="<?php echo $this->get_field_id( 'show_title' ); ?>"
    100                     name="<?php echo $this->get_field_name( 'show_title' ); ?>">
    101                 <option value="true"
    102                     <?php selected( $show_title, 'true' ); ?>>
    103                     Yes</option>
    104                 <option value="false"
    105                     <?php selected( $show_title, 'false' ); ?>>
    106                     No</option>
    107             </select>
    108                     </label>
    109         </p>
    110         <p>
    111                     <label for="<?php echo
    112                         $this->get_field_id( 'number_of_digits' ); ?>">
    113             <?php echo 'Number of Digits:'; ?>         
    114             <input type="text"
    115                     id="<?php echo $this->get_field_id( 'number_of_digits' ); ?>"
    116                     name="<?php echo $this->get_field_name( 'number_of_digits' ); ?>"
    117                     value="<?php echo $number_of_digits; ?>" />         
    118                     </label>
    119         </p>
    120         <p>
    121                     <label for="<?php echo
    122                         $this->get_field_id( 'display_tenths' ); ?>">
    123             <?php echo 'Display Tenths'; ?>         
    124             <select id="<?php echo $this->get_field_id( 'display_tenths' ); ?>"
    125                     name="<?php echo $this->get_field_name( 'display_tenths' ); ?>">
    126                 <option value="true"
    127                     <?php selected( $display_tenths, 'true' ); ?>>
    128                     Yes</option>
    129                 <option value="false"
    130                     <?php selected( $display_tenths, 'false' ); ?>>
    131                     No</option>
    132             </select>
    133                     </label>
    134         </p>
    135         <p>
    136                     <label for="<?php echo
    137                         $this->get_field_id( 'start_value' ); ?>">
    138             <?php echo 'Start Value:'; ?>           
    139             <input type="text"
    140                     id="<?php echo $this->get_field_id( 'start_value' ); ?>"
    141                     name="<?php echo $this->get_field_name( 'start_value' ); ?>"
    142                     value="<?php echo $start_value; ?>" />         
    143                     </label>
    144         </p>
    145                 <p>
    146                     <label for="<?php echo
    147                         $this->get_field_id( 'end_value' ); ?>">
    148             <?php echo 'End Value:'; ?>         
    149             <input type="text"
    150                     id="<?php echo $this->get_field_id( 'end_value' ); ?>"
    151                     name="<?php echo $this->get_field_name( 'end_value' ); ?>"
    152                     value="<?php echo $end_value; ?>" />           
    153                     </label>
    154         </p>
    155                <p>
    156                     <label for="<?php echo
    157                         $this->get_field_id( 'animate_speed' ); ?>">
    158             <?php echo 'Animation Speed (1-100):'; ?>           
    159             <input type="text"
    160                     id="<?php echo $this->get_field_id( 'animate_speed' ); ?>"
    161                     name="<?php echo $this->get_field_name( 'animate_speed' ); ?>"
    162                     value="<?php echo $animate_speed; ?>" />           
    163                     </label>
    164         </p>
    165         <p>
    166                     <label for="<?php echo
    167                         $this->get_field_id( 'digit_height' ); ?>">
    168             <?php echo 'Digit Height:'; ?>         
    169             <input type="text"
    170                     id="<?php echo $this->get_field_id( 'digit_height' ); ?>"
    171                     name="<?php echo $this->get_field_name( 'digit_height' ); ?>"
    172                     value="<?php echo $digit_height; ?>" />         
    173                     </label>
    174         </p>       
    175                 <p>
    176                     <label for="<?php echo
    177                         $this->get_field_id( 'digit_width' ); ?>">
    178             <?php echo 'Digit Width:'; ?>           
    179             <input type="text"
    180                     id="<?php echo $this->get_field_id( 'digit_width' ); ?>"
    181                     name="<?php echo $this->get_field_name( 'digit_width' ); ?>"
    182                     value="<?php echo $digit_width; ?>" />         
    183                     </label>
    184         </p>       
    185                 <p>
    186                     <label for="<?php echo
    187                         $this->get_field_id( 'digit_padding' ); ?>">
    188             <?php echo 'Padding:'; ?>           
    189             <input type="text"
    190                     id="<?php echo $this->get_field_id( 'digit_padding' ); ?>"
    191                     name="<?php echo $this->get_field_name( 'digit_padding' ); ?>"
    192                     value="<?php echo $digit_padding; ?>" />           
    193                     </label>
    194         </p>       
    195                 <p>
    196                     <label for="<?php echo
    197                         $this->get_field_id( 'digit_bustedness' ); ?>">
    198             <?php echo 'Bustedness:'; ?>           
    199             <input type="text"
    200                     id="<?php echo $this->get_field_id( 'digit_bustedness' ); ?>"
    201                     name="<?php echo $this->get_field_name( 'digit_bustedness' ); ?>"
    202                     value="<?php echo $digit_bustedness; ?>" />         
    203                     </label>
    204         </p>       
    205                 <p>
    206                     <label for="<?php echo
    207                         $this->get_field_id( 'digit_style' ); ?>">
    208             <?php echo 'Digit Style:'; ?>           
    209             <input type="text"
    210                     id="<?php echo $this->get_field_id( 'digit_style' ); ?>"
    211                     name="<?php echo $this->get_field_name( 'digit_style' ); ?>"
    212                     value="<?php echo $digit_style; ?>" />         
    213                     </label>
    214         </p>
    215 <?php
    216        
     48        $show_title = (!empty($instance['show_title']) ? $instance['show_title'] : 'true' );
     49        $widget_title = (!empty($instance['widget_title']) ? esc_attr($instance['widget_title']) : 'Counter' );
     50        $display_tenths = (!empty($instance['display_tenths']) ? $instance['display_tenths'] : 'true' );
     51        $number_of_digits = (!empty($instance['number_of_digits']) ? esc_attr($instance['number_of_digits']) : '6' );
     52        $digit_height = (!empty($instance['digit_height']) ? esc_attr($instance['digit_height']) : '40' );
     53        $digit_width = (!empty($instance['digit_width']) ? esc_attr($instance['digit_width']) : '30' );
     54        $digit_padding = (!empty($instance['digit_padding']) ? esc_attr($instance['digit_padding']) : '0' );
     55        $digit_bustedness = (!empty($instance['digit_bustedness']) ? esc_attr($instance['digit_bustedness']) : '2' );
     56        $digit_style = (!empty($instance['digit_style']) ? esc_attr($instance['digit_style']) : 'font-family: Courier New, Courier, monospace; font-weight: 900;' );
     57        $start_value = (!empty($instance['start_value']) ? esc_attr($instance['start_value']) : '0' );
     58        $end_value = (!empty($instance['end_value']) ? esc_attr($instance['end_value']) : '100' );
     59        $animate_speed = (!empty($instance['animate_speed']) ? esc_attr($instance['animate_speed']) : '50' );
     60        $persist = (!empty($instance['persist']) ? esc_attr($instance['persist']) : 'false' );
     61        $persist_interval = (!empty($instance['persist_interval']) ? esc_attr($instance['persist_interval']) : '1' );
     62        $init_timestamp = (!empty($instance['init_timestamp']) ? esc_attr($instance['init_timestamp']) : time() );
     63        ?>
     64        <p>
     65            <label for="<?php echo $this->get_field_id('widget_title'); ?>">
     66                <?php echo 'Title:'; ?>         
     67                <input type="text"
     68                       id="<?php echo $this->get_field_id('widget_title'); ?>"
     69                       name="<?php echo $this->get_field_name('widget_title'); ?>"
     70                       value="<?php echo $widget_title; ?>" />         
     71            </label>
     72        </p>
     73        <p>
     74            <label for="<?php echo $this->get_field_id('show_title'); ?>">
     75                <?php echo 'Show Title'; ?>         
     76                <select id="<?php echo $this->get_field_id('show_title'); ?>"
     77                        name="<?php echo $this->get_field_name('show_title'); ?>">
     78                    <option value="true"
     79                            <?php selected($show_title, 'true'); ?>>
     80                        Yes</option>
     81                    <option value="false"
     82                            <?php selected($show_title, 'false'); ?>>
     83                        No</option>
     84                </select>
     85            </label>
     86        </p>
     87        <p>
     88            <label for="<?php echo $this->get_field_id('start_value'); ?>">
     89                <?php echo 'Start Value:'; ?>           
     90                <input type="text"
     91                       id="<?php echo $this->get_field_id('start_value'); ?>"
     92                       name="<?php echo $this->get_field_name('start_value'); ?>"
     93                       value="<?php echo $start_value; ?>" />           
     94            </label>
     95        </p>
     96        <p>
     97            <label for="<?php echo $this->get_field_id('end_value'); ?>">
     98                <?php echo 'End Value:'; ?>         
     99                <input type="text"
     100                       id="<?php echo $this->get_field_id('end_value'); ?>"
     101                       name="<?php echo $this->get_field_name('end_value'); ?>"
     102                       value="<?php echo $end_value; ?>" />         
     103            </label>
     104        </p>
     105        <p>
     106            <label for="<?php echo $this->get_field_id('persist'); ?>">
     107                <?php echo 'Continuous Counter'; ?>         
     108                <select id="<?php echo $this->get_field_id('persist'); ?>"
     109                        name="<?php echo $this->get_field_name('persist'); ?>">
     110                    <option value="true"
     111                            <?php selected($persist, 'true'); ?>>
     112                        Yes</option>
     113                    <option value="false"
     114                            <?php selected($persist, 'false'); ?>>
     115                        No</option>
     116                </select>
     117            </label>
     118        </p>
     119        <p>
     120            <label for="<?php echo $this->get_field_id('persist_interval'); ?>">
     121                <?php echo 'Continuous Interval (seconds):'; ?>         
     122                <input type="text"
     123                       id="<?php echo $this->get_field_id('persist_interval'); ?>"
     124                       name="<?php echo $this->get_field_name('persist_interval'); ?>"
     125                       value="<?php echo $persist_interval; ?>" />         
     126            </label>
     127        </p>
     128        <p>
     129            <label for="<?php echo $this->get_field_id('number_of_digits'); ?>">
     130                <?php echo 'Number of Digits:'; ?>         
     131                <input type="text"
     132                       id="<?php echo $this->get_field_id('number_of_digits'); ?>"
     133                       name="<?php echo $this->get_field_name('number_of_digits'); ?>"
     134                       value="<?php echo $number_of_digits; ?>" />         
     135            </label>
     136        </p>
     137        <p>
     138            <label for="<?php echo $this->get_field_id('display_tenths'); ?>">
     139                <?php echo 'Display Tenths'; ?>         
     140                <select id="<?php echo $this->get_field_id('display_tenths'); ?>"
     141                        name="<?php echo $this->get_field_name('display_tenths'); ?>">
     142                    <option value="true"
     143                            <?php selected($display_tenths, 'true'); ?>>
     144                        Yes</option>
     145                    <option value="false"
     146                            <?php selected($display_tenths, 'false'); ?>>
     147                        No</option>
     148                </select>
     149            </label>
     150        </p>
     151        <p>
     152            <label for="<?php echo $this->get_field_id('animate_speed'); ?>">
     153                <?php echo 'Animation Speed (1-100):'; ?>           
     154                <input type="text"
     155                       id="<?php echo $this->get_field_id('animate_speed'); ?>"
     156                       name="<?php echo $this->get_field_name('animate_speed'); ?>"
     157                       value="<?php echo $animate_speed; ?>" />         
     158            </label>
     159        </p>
     160        <p>
     161            <label for="<?php echo $this->get_field_id('digit_height'); ?>">
     162                <?php echo 'Digit Height:'; ?>         
     163                <input type="text"
     164                       id="<?php echo $this->get_field_id('digit_height'); ?>"
     165                       name="<?php echo $this->get_field_name('digit_height'); ?>"
     166                       value="<?php echo $digit_height; ?>" />         
     167            </label>
     168        </p>       
     169        <p>
     170            <label for="<?php echo $this->get_field_id('digit_width'); ?>">
     171                <?php echo 'Digit Width:'; ?>           
     172                <input type="text"
     173                       id="<?php echo $this->get_field_id('digit_width'); ?>"
     174                       name="<?php echo $this->get_field_name('digit_width'); ?>"
     175                       value="<?php echo $digit_width; ?>" />           
     176            </label>
     177        </p>       
     178        <p>
     179            <label for="<?php echo $this->get_field_id('digit_padding'); ?>">
     180                <?php echo 'Padding:'; ?>           
     181                <input type="text"
     182                       id="<?php echo $this->get_field_id('digit_padding'); ?>"
     183                       name="<?php echo $this->get_field_name('digit_padding'); ?>"
     184                       value="<?php echo $digit_padding; ?>" />         
     185            </label>
     186        </p>       
     187        <p>
     188            <label for="<?php echo $this->get_field_id('digit_bustedness'); ?>">
     189                <?php echo 'Bustedness:'; ?>           
     190                <input type="text"
     191                       id="<?php echo $this->get_field_id('digit_bustedness'); ?>"
     192                       name="<?php echo $this->get_field_name('digit_bustedness'); ?>"
     193                       value="<?php echo $digit_bustedness; ?>" />         
     194            </label>
     195        </p>       
     196        <p>
     197            <label for="<?php echo $this->get_field_id('digit_style'); ?>">
     198                <?php echo 'Digit Style:'; ?>           
     199                <input type="text"
     200                       id="<?php echo $this->get_field_id('digit_style'); ?>"
     201                       name="<?php echo $this->get_field_name('digit_style'); ?>"
     202                       value="<?php echo $digit_style; ?>" />           
     203            </label>
     204        </p>
     205        <?php
    217206    }
    218207
    219 
    220     function update( $new_instance, $old_instance ) {
    221         $instance = $old_instance;
    222                 // validate inputs
    223                
    224         // Only numeric values
    225                 if ( is_numeric ( $new_instance['number_of_digits'] ) ) {
    226                     $instance['number_of_digits'] = intval( $new_instance['number_of_digits'] );
    227                 } else {
    228                     $instance['number_of_digits'] = $instance['number_of_digits'];
    229                 }
    230 
    231                 if ( is_numeric ( $new_instance['digit_height'] ) ) {
    232                     $instance['digit_height'] = intval( $new_instance['digit_height'] );
    233                 } else {
    234                     $instance['digit_height'] = $instance['digit_height'];
    235                 }
    236 
    237                 if ( is_numeric ( $new_instance['digit_width'] ) ) {
    238                     $instance['digit_width'] = intval( $new_instance['digit_width'] );
    239                 } else {
    240                     $instance['digit_width'] = $instance['digit_width'];
    241                 }
    242                
    243                 if ( is_numeric ( $new_instance['digit_padding'] ) ) {
    244                     $instance['digit_padding'] = intval( $new_instance['digit_padding'] );
    245                 } else {
    246                     $instance['digit_padding'] = $instance['digit_padding'];
    247                 }
    248 
    249                 if ( is_numeric ( $new_instance['digit_bustedness'] ) ) {
    250                     $instance['digit_bustedness'] = intval( $new_instance['digit_bustedness'] );
    251                 } else {
    252                     $instance['digit_bustedness'] = $instance['digit_bustedness'];
    253                 }
    254 
    255                 if ( is_numeric ( $new_instance['start_value'] ) ) {
    256                     $instance['start_value'] = floatval( $new_instance['start_value'] );
    257                 } else {
    258                     $instance['start_value'] = $instance['start_value'];
    259                 }
    260 
    261                 if ( is_numeric ( $new_instance['end_value'] ) ) {
    262                     $instance['end_value'] = floatval( $new_instance['end_value'] );
    263                 } else {
    264                     $instance['end_value'] = $instance['end_value'];
    265                 }
    266                 if ( is_numeric ( $new_instance['animate_speed'] ) ) {
    267                     $instance['animate_speed'] = intval( $new_instance['animate_speed'] );
    268                     if ($instance['animate_speed'] >100) {
    269                         $instance['animate_speed']=100;
    270                     }
    271                 } else {
    272                     $instance['end_value'] = $instance['end_value'];
    273                 }
    274                 // string values
    275         $instance['digit_style'] =
    276             strip_tags( $new_instance['digit_style'] );
    277 
    278                 $instance['widget_title'] =
    279             strip_tags( $new_instance['widget_title'] );
    280                 // boolean values
    281         $instance['show_title'] =
    282             strip_tags( $new_instance['show_title'] );
    283 
    284                 $instance['display_tenths'] =
    285             strip_tags( $new_instance['display_tenths'] );
    286 
    287         return $instance;
     208    function update($new_instance, $old_instance) {
     209        $instance = $old_instance;
     210        // validate inputs
     211        // Only numeric values
     212        if (is_numeric($new_instance['number_of_digits'])) {
     213            $instance['number_of_digits'] = intval($new_instance['number_of_digits']);
     214        } else {
     215            $instance['number_of_digits'] = $instance['number_of_digits'];
     216        }
     217
     218        if (is_numeric($new_instance['digit_height'])) {
     219            $instance['digit_height'] = intval($new_instance['digit_height']);
     220        } else {
     221            $instance['digit_height'] = $instance['digit_height'];
     222        }
     223
     224        if (is_numeric($new_instance['digit_width'])) {
     225            $instance['digit_width'] = intval($new_instance['digit_width']);
     226        } else {
     227            $instance['digit_width'] = $instance['digit_width'];
     228        }
     229
     230        if (is_numeric($new_instance['digit_padding'])) {
     231            $instance['digit_padding'] = intval($new_instance['digit_padding']);
     232        } else {
     233            $instance['digit_padding'] = $instance['digit_padding'];
     234        }
     235
     236        if (is_numeric($new_instance['digit_bustedness'])) {
     237            $instance['digit_bustedness'] = intval($new_instance['digit_bustedness']);
     238        } else {
     239            $instance['digit_bustedness'] = $instance['digit_bustedness'];
     240        }
     241
     242        if (is_numeric($new_instance['start_value'])) {
     243            $instance['start_value'] = floatval($new_instance['start_value']);
     244        } else {
     245            $instance['start_value'] = $instance['start_value'];
     246        }
     247
     248        if (is_numeric($new_instance['end_value'])) {
     249            $instance['end_value'] = floatval($new_instance['end_value']);
     250        } else {
     251            $instance['end_value'] = $instance['end_value'];
     252        }
     253        if (is_numeric($new_instance['animate_speed'])) {
     254            $instance['animate_speed'] = intval($new_instance['animate_speed']);
     255            if ($instance['animate_speed'] > 100) {
     256                $instance['animate_speed'] = 100;
     257            }
     258        }
     259        if (is_numeric($new_instance['persist_interval'])) {
     260            $instance['persist_interval'] = intval($new_instance['persist_interval']);
     261        }
     262        // string values
     263        $instance['digit_style'] =
     264                strip_tags($new_instance['digit_style']);
     265
     266        $instance['widget_title'] =
     267                strip_tags($new_instance['widget_title']);
     268        // boolean values
     269        $instance['show_title'] =
     270                strip_tags($new_instance['show_title']);
     271
     272        $instance['persist'] =
     273                strip_tags($new_instance['persist']);
     274
     275        $instance['display_tenths'] =
     276                strip_tags($new_instance['display_tenths']);
     277        $instance['init_timestamp'] = time();
     278
     279        return $instance;
    288280    }
    289281
    290     function widget( $args, $instance ) {
    291             // queue javascript if widget is used
    292     if ( is_active_widget(false, false, $this->id_base) )
    293             wp_enqueue_script('odometer', plugins_url( 'js/odometer.js', __FILE__ ), array('jquery'), '', true );
     282    function widget($args, $instance) {
     283        // queue javascript if widget is used
     284        if (is_active_widget(false, false, $this->id_base))
     285            wp_enqueue_script('odometer', plugins_url('js/odometer.js', __FILE__), array('jquery'), '', true);
    294286
    295287        // Extract members of args array as individual variables
    296         extract( $args );
     288        extract($args);
    297289        $widget_title = $instance['widget_title'];
    298290        $show_title = $instance['show_title'];
     
    307299        $digit_bustedness = $instance['digit_bustedness'];
    308300        $digit_style = $instance['digit_style'];
     301
     302        //these were added at v0.7 and may not have defaults for existing widgets
     303        //so we'll add some defaults here to avoid any undefined indexes
     304
     305        $persist = (!empty($instance['persist']) ?
     306                        esc_attr($instance['persist']) :
     307                        'false' );
     308
     309        $persist_interval = (!empty($instance['persist_interval']) ?
     310                        esc_attr($instance['persist_interval']) :
     311                        '1' );
     312
     313        $init_timestamp = (!empty($instance['init_timestamp']) ?
     314                        esc_attr($instance['init_timestamp']) :
     315                        time() );
     316
     317        if ($persist == 'true') {
     318            $start_value = $start_value + round((time() - $init_timestamp) / $persist_interval);
     319            if ($start_value > $end_value) {
     320                $start_value = $end_value;
     321            }
     322            $animate_speed = 100;
     323            $display_tenths = 0;
     324        } else {
     325            $persist_interval = 1;
     326        }
    309327        // Display widget title
    310328        echo $before_widget;
    311329        if ($show_title == 'true') {
    312330            echo $before_title;
    313             echo apply_filters( 'widget_title', $widget_title);
     331            echo apply_filters('widget_title', $widget_title);
    314332            echo $after_title;
    315333        }
    316        
     334
    317335        // output counter div
    318         echo '<div id="odometer-'.$args['widget_id'] .'" class="odometer"></div>';
     336        echo '<div id="odometer-' . $args['widget_id'] . '" class="odometer-widget"></div>';
    319337        // output javascript
    320338        echo "<script type='text/javascript'>
    321339                jQuery(document).ready(function() {
    322                         var waitTime = 100 - $animate_speed;
     340                        var waitTime = (100 - $animate_speed);
    323341                        var counterStartValue = $start_value;
    324342                        var counterEndValue = $end_value;
    325343                        var counterNow = $start_value;
    326                         var div = document.getElementById('odometer-".$args['widget_id'] ."');
     344                        var wholeNumber = 0;
     345                        var persist = $persist;
     346                        var div = document.getElementById('odometer-" . $args['widget_id'] . "');
    327347                        var myOdometer = new Odometer(div, {
    328348                                       digits: $number_of_digits,
     
    335355                                       });
    336356                       
     357
    337358                         function updateOdometer() {
    338                 counterNow=counterNow+0.005;
     359                            if (persist == true) {                           
     360                                counterNow=counterNow+0.15;
     361                                wholeNumber=wholeNumber+0.15;
     362                                if (wholeNumber >= 1) {
     363                                    wholeNumber = 0;
     364                                    waitTime = ($persist_interval * 1000);
     365                                } else {
     366                                    waitTime = 1;
     367                                }
     368                            } else {
     369                                 counterNow=counterNow+0.01;
     370                                 waitTime = (100 - $animate_speed);
     371                            }
    339372                                if (counterNow < counterEndValue) {
    340373                                    myOdometer.set(counterNow);
     
    354387                );
    355388    </script>";
    356        
     389
    357390        // finish off widget
    358391        echo $after_widget;
    359     }   
     392    }
     393
    360394}
    361395?>
  • jellyfish-counter-widget/trunk/js/odometer.js

    r651892 r747009  
    9898
    9999    var odometerDiv = document.createElement("div")
    100     odometerDiv.setAttribute("id","odometer");
     100    //odometerDiv.setAttribute("id","odometer");
    101101    // set container height and width based on the digit size so we can style
    102102    // the container for borders etc.
  • jellyfish-counter-widget/trunk/readme.txt

    r651894 r747009  
    77Requires at least: 3.0
    88Tested up to: 3.5
    9 Stable tag: 0.6
     9Stable tag: 0.8
    1010License: GPLv2 or later
    1111License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1717This plugin adds a widget to your WordPress web site that displays a static or
    1818animated odometer style counter that can display a set value or can animate
    19 between a starting and ending value.
     19between a starting and ending value. The counter can now have a continuous and
     20non resetting operation where it increments over time in the background until
     21the goal is reached.
    2022
    2123A great visual effect for travel blogs or any website that wants to display a
     
    2527totals and appearance.
    2628
    27 The counters are created via css and require no external graphics files.
     29The counters are created via css and javascript and require no external graphics
     30files.
    2831
    2932
     
    4447
    4548Add a counter widget to your sidebar and adjust the settings to suit your
    46 requirements. If you specify different start and end values the counter will
    47 count upwards when the page loads until it reaches the ending value.
     49requirements.
    4850
    49 If you just want a static counter make the start and end the same value or make
    50 the end value lower than the starting value.
     51There are three basic modes of operation:
    5152
    52 You can configure the digit height, width and font as well as animation
    53 speed and "bustedness" (misalignment of the digits).
     53* Static - If you only give a Start Value and no End Value the counter will display
     54a static number (useful if you just want to show a total and update it manually
     55as necessary)
     56
     57* Animated – If you supply both start value and end value in the widget, when it
     58is displayed the counter will increment upwards until it reaches the end value.
     59Speed of the count is controlled by the Animation Speed option. Note, this counter
     60has no memory, it will reset when a page is reloaded or changed. Good for visual
     61effect where start and end values are very close together.
     62
     63* Continuous – If you want to count over a long period of time and need your
     64counter to continue to count irrespective of page loads then just select the
     65continuous option in the widget. Then select the interval between the counter
     66increments, in seconds. As soon as you save the widget the counter will “start”
     67and will continue to tick away even if nobody is viewing your blog. You can of
     68course still use the start values and end values in this mode however animation
     69speed and display tenths have no effect.
     70
     71You can also configure the digit height, width and font as well as animation
     72speed (animated mode only)  and "bustedness" (misalignment of the digits).
    5473
    5574In the "Digit Style" setting you can specify a font or font style, this must be
     
    6887== Frequently Asked Questions ==
    6988
    70 None yet
    71 
    7289== Changelog ==
    7390=0.6=
    7491* initial release
    7592
     93=0.8=
     94* by request, added  a non resetting continuous counter feature that can increment
     95every set number of seconds until it reaches its goal
     96
    7697== Upgrade Notice ==
    7798
     99Existing counter widgets may need the animation speed adjusting as the timing
     100method has changed slightly making the counter animate slightly faster on
     101some browsers.
     102
    78103== Screenshots ==
Note: See TracChangeset for help on using the changeset viewer.