WordPress.org

Plugin Directory

Changeset 326255


Ignore:
Timestamp:
12/27/10 01:25:43 (3 years ago)
Author:
vhauri
Message:

Updating to version 2.0 and tagging 1.4

Location:
tagline-rotator
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • tagline-rotator/trunk/readme.txt

    r260504 r326255  
    6060Version 1.3 - 7-4-2010 - DO NOT DOWNLOAD: fixed an issue with taglines not displaying in headers, but included tagline from Settings->General by mistake. Use 1.4 instead. 
    6161Version 1.4 - 7-5-2010 - Fixed me shutting my brain off on version 1.3. Displays tagline in header, but does not duplicate taglines. 
     62Version 2.0 - 12-26-2010 - Changed plugin to class-based structure and cleaned up a lot of the logic. Removed all MySQL queries and switched to using options for storing taglines. Added an upgrade function to pull old taglines from table-based versions of the plugin that is called on activation. 
  • tagline-rotator/trunk/tagline-rotator.php

    r260504 r326255  
    55Plugin URI: http://neverblog.net/tagline-rotator-plugin-for-wordpress 
    66Description: Displays a random tagline from a database list. You can manage taglines through Settings->Tagline Rotator. 
    7 Version: 1.4 
     7Version: 2.0  
    88Author: Vasken Hauri 
    99Author URI: http://neverblog.net 
     
    2929*/ 
    3030 
     31if( !class_exists('WP_Tagline_Rotator') ){ 
     32    class WP_Tagline_Rotator{ 
    3133 
    32 //new in version 1.1 to call the upgrade function 
    33 register_activation_hook(__FILE__,'tagline_upgrade_check'); 
     34        public function __construct(){ 
     35            if(is_admin()) 
     36                add_action('admin_menu', array($this, 'add_pages')); 
     37            else 
     38                add_filter('bloginfo', array($this, 'filter_bloginfo'),1,2); 
     39             
     40            register_activation_hook(__FILE__,array('WP_Tagline_Rotator', 'tagline_upgrade_check')); 
     41        } 
     42     
     43        public static function add_pages() { 
     44                // Add a new submenu under Options: 
     45                add_options_page('Tagline Rotator', 'Tagline Rotator', 8, 'taglineoptions', array('WP_Tagline_Rotator', 'tagline_options_page')); 
     46        } 
    3447 
    35 // set the option for the tables to false until created 
    36 add_option('tagline_tables','false'); 
     48        public static function tagline_upgrade_check(){ 
     49            global $wpdb; 
     50            $old_taglines = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "tagline_rotator" ); 
     51            $taglines = get_option('tagline_rotator_taglines'); 
     52             
     53            foreach($old_taglines as $old_tagline){ 
     54                $taglines[] = $old_tagline->random_tagline;      
     55            } 
    3756 
    38 // check to make sure the tables exist 
    39 $tagline_set = get_option('tagline_tables'); 
     57            update_option('tagline_rotator_taglines', $taglines); 
     58        } 
    4059 
    41 global $original_tagline; 
    42 $original_tagline = get_option('blogdescription'); 
     60        public static function tagline_options_page() { 
     61            $taglines = get_option('tagline_rotator_taglines'); 
     62             
     63            //loop through the checkboxes to see what to delete 
     64            if(isset($_POST['box'])){ 
     65                $box = $_POST['box']; 
     66                foreach ($box as $x){ 
     67                    unset($taglines[$x]); 
     68                }    
     69            }   
    4370 
    44 if(!is_admin()){ 
    45     filter_bloginfo(); 
    46 } 
     71            if(isset($_POST['text'])){ 
     72                $text = $_POST['text']; 
     73                foreach ($text as $key => $tagline){ 
     74                    if($tagline !== '') 
     75                        $taglines[$key] = $tagline; 
     76                    } 
     77                } 
     78             
     79            // check for new tagline and insert if found 
     80            if(isset($_POST['new_tagline']) && $_POST['new_tagline'] !== ''){ 
     81                $new_tagline = $_POST['new_tagline']; 
     82                $taglines[] = $new_tagline; 
     83            } 
     84             
     85            update_option('tagline_rotator_taglines', $taglines); 
    4786 
    48 if($tagline_set != 'true') 
    49 { 
    50 update_option('tagline_tables','false'); 
    51 } 
     87            ?> 
    5288 
    53 // create the tables 
    54 createtables(); 
     89            <!-- begin options page form --> 
     90            <div class="wrap"> 
     91            <h2>Tagline Rotator Options</h2> 
     92            <p><strong>PLEASE NOTE:</strong> You must click 'Save Changes' in order to permanently add or delete a tagline.</p> 
     93            <form method="post"> 
     94            <hr> 
     95            <h3>Add New Taglines</h3> 
     96            <table class="form-table"> 
     97            <tr valign="top"> 
     98            <th scope="row">Add a new tagline</th> 
     99            <td><input type="text" name="new_tagline" size="80" /></td></tr> 
     100            </table> 
    55101 
    56 // Hook for adding admin menus 
    57 add_action('admin_menu', 'tagline_add_pages'); 
     102            <p class="submit"> 
     103            <input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" /> 
     104            </p> 
     105            <hr> 
     106            <h3>Delete Taglines</h3> 
     107            <table class="form-table"> 
     108            <?php wp_nonce_field('update-options'); ?> 
    58109 
    59 // action function for above hook 
    60 function tagline_add_pages() { 
    61     // Add a new submenu under Options: 
    62     add_options_page('Tagline Rotator', 'Tagline Rotator', 8, 'taglineoptions', 'tagline_options_page'); 
    63 } 
     110            <?php 
     111            foreach($taglines as $key => $tagline){ 
     112            ?> 
    64113 
     114            <tr valign="top"> 
     115            <th scope="row">Delete this tagline</th> 
     116            <?php $sanitized_tagline = preg_replace('/"/','&quot;',$tagline); ?> 
     117            <td><input type="checkbox" name="box[]" value="<?php echo $key;?>" /><input type="text" size="80" name="text[]" value="<?php echo $sanitized_tagline;?>" /><input type="hidden" name="vhtr_ids[]" value="<?php echo $key;?>" /></td></tr> 
    65118 
    66 // tagline_options_page() displays the page content for the Tagline Rotator submenu 
    67 function tagline_options_page() { 
     119            <?php 
     120            } 
     121            ?> 
     122            </table> 
     123            <br> 
     124            <p class="submit"> 
     125            <input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" /> 
     126            </p> 
     127            <?php 
     128        } 
    68129 
    69     //new in version 1.1. sets the database value to the wordpress database prefix 
    70     global $wpdb; 
    71     $table_name = $wpdb->prefix . "tagline_rotator"; 
    72  
    73 //loop through the checkboxes to see what to delete 
    74 if(isset($_POST['box'])) 
    75 { 
    76     $box = $_POST['box']; 
    77  
    78     foreach ($box as $x)  
    79     { 
    80  
    81         $query = "DELETE FROM " . $table_name ." WHERE id='" . $x . "' LIMIT 1"; 
    82         $result = mysql_query($query) or die('Query did not work'); 
    83    
    84     } 
    85      
    86  
    87  
    88 if(isset($_POST['text'])) 
    89             { 
    90             $text = $_POST['text']; 
    91             $id_numbers = $_POST['vhtr_ids'];        
    92             foreach ($text as $key => $y) 
    93                 { 
    94                     $query = "UPDATE " . $table_name . " SET random_tagline='" . $y . "' WHERE id='" . $id_numbers[$key] . "' LIMIT 1"; 
    95                     $result = mysql_query($query) or die ('Could not update tagline');                                   
     130        public static function filter_bloginfo($output = '', $show){ 
     131            if($show == 'description'){ 
     132                $taglines = get_option('tagline_rotator_taglines'); 
     133                if($taglines !== FALSE){ 
     134                    $tagline_counter = count($taglines); 
     135                    $key = rand(0, $tagline_counter - 1);    
     136                    $output = $taglines[$key]; 
    96137                } 
    97138            } 
     139            return $output; 
     140        }    
    98141 
    99 // check for new tagline and insert if found 
    100 if(isset($_POST['new_tagline'])) 
    101 { 
    102     //new in version 1.1. sets the database value to the wordpress database prefix 
    103     global $wpdb; 
    104     $table_name = $wpdb->prefix . "tagline_rotator"; 
     142    } 
     143}//end WP_Tagline_Rotator class 
    105144 
    106     $new_tagline = $_POST['new_tagline']; 
    107  
    108     $query = "INSERT INTO " . $table_name . " SET random_tagline=\"" . $new_tagline . "\""; 
    109     mysql_query($query) or die ('Unable to add tagline.'); 
    110  
     145if( class_exists('WP_Tagline_Rotator')){ 
     146    new WP_Tagline_Rotator; 
    111147} 
    112  
    113 ?> 
    114  
    115 <!-- begin options page form --> 
    116 <div class="wrap"> 
    117 <h2>Tagline Rotator Options</h2> 
    118 <p><strong>PLEASE NOTE:</strong> You must click 'Save Changes' in order to permanently add or delete a tagline.</p> 
    119 <form method="post"> 
    120 <hr> 
    121 <h3>Add New Taglines</h3> 
    122 <table class="form-table"> 
    123 <tr valign="top"> 
    124 <th scope="row">Add a new tagline</th> 
    125 <td><input type="text" name="new_tagline" size="80" /></td></tr> 
    126 </table> 
    127  
    128 <p class="submit"> 
    129 <input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" /> 
    130 </p> 
    131 <hr> 
    132 <h3>Delete Taglines</h3> 
    133 <table class="form-table"> 
    134 <?php wp_nonce_field('update-options'); ?> 
    135  
    136 <?php 
    137     //new in version 1.1. sets the database value to the wordpress database prefix 
    138     global $wpdb; 
    139     $table_name = $wpdb->prefix . "tagline_rotator"; 
    140  
    141 // delete any empty rows in the form 
    142 $query = "DELETE FROM " . $table_name . " WHERE random_tagline = ''"; 
    143 mysql_query($query); 
    144  
    145 // and select the non-empty ones to populate our checkboxes 
    146 $query = "SELECT random_tagline,id FROM " . $table_name . " WHERE random_tagline!=''"; 
    147 $result = mysql_query($query); 
    148 while($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
    149 { 
    150 ?> 
    151  
    152 <tr valign="top"> 
    153 <th scope="row">Delete this tagline</th> 
    154 <?php $sanitized_tagline = preg_replace('/"/','&quot;',$row['random_tagline']); ?> 
    155 <td><input type="checkbox" name="box[]" value="<?php echo $row['id'];?>" /><input type="text" size="80" name="text[]" value="<?php echo $sanitized_tagline;?>" /><input type="hidden" name="vhtr_ids[]" value="<?php echo $row['id'];?>" /></td></tr> 
    156  
    157 <?php 
    158 } 
    159 ?> 
    160 </table> 
    161 <br> 
    162 <p class="submit"> 
    163 <input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" /> 
    164 </p> 
    165 <?php 
    166 } 
    167  
    168 // add a filter to replace the default bloginfo function with our own 
    169  
    170 function filter_bloginfo(){ 
    171           add_filter('option_blogdescription','mybloginfo',1,2); 
    172  
    173 } 
    174  
    175  
    176  
    177 // main function to create tables 
    178 function createtables () 
    179 { 
    180  
    181     //new in version 1.1. sets the database value to the wordpress database prefix 
    182     global $wpdb; 
    183     $table_name = $wpdb->prefix . "tagline_rotator"; 
    184      
    185     $tagline_tables = get_option('tagline_tables'); 
    186  
    187  
    188     if($tagline_tables == 'false') 
    189     { 
    190         $query = "CREATE TABLE " . $table_name . " (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,random_tagline VARCHAR( 200 ) NOT NULL)"; 
    191         mysql_query($query) or die ('Could not create tagline table'); 
    192         update_option('tagline_tables','true'); 
    193     } 
    194  
    195 } 
    196  
    197 // edited bloginfo function. pay special attention to description case 
    198 function mybloginfo($output = '') { 
    199     //new in version 1.1. sets the database value to the wordpress database prefix 
    200     global $wpdb, $original_tagline; 
    201     $table_name = $wpdb->prefix . "tagline_rotator"; 
    202  
    203     $query = "SELECT random_tagline FROM " . $table_name . " ORDER BY rand() limit 1"; 
    204     $result = mysql_query($query); 
    205         while($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
    206     { 
    207         $random_tagline = $row['random_tagline']; 
    208     } 
    209     if($original_tagline == $output){ 
    210         return $random_tagline; 
    211     }else{   
    212         return $random_tagline . $output; 
    213     } 
    214 }    
    215  
    216 //new to version 1.1 function to add version info and check if a table needs to be renamed 
    217  
    218 function tagline_upgrade_check(){ 
    219  
    220     //new in version 1.1. sets the database value to the wordpress database prefix 
    221     global $wpdb; 
    222     $table_name = $wpdb->prefix . "tagline_rotator"; 
    223  
    224     $installed_ver = get_option( "tagline_db_version" ); 
    225  
    226     if( $installed_ver != "1.1" ) { 
    227          
    228         if($wpdb->get_var("SHOW TABLES LIKE 'wp_tagline_rotator'") != $table_name) { 
    229              
    230             $query = "RENAME TABLE wp_tagline_rotator TO " . $table_name; 
    231             mysql_query($query) or die('The query failed'); 
    232         } 
    233  
    234         add_option("tagline_db_version", "1.1"); 
    235  
    236    } 
    237 } 
    238 ?> 
Note: See TracChangeset for help on using the changeset viewer.