Plugin Directory

Changeset 5076


Ignore:
Timestamp:
02/11/2006 02:55:57 AM (21 years ago)
Author:
kccricket
Message:
  • Switching to the_content for autoprint.
  • Syncing some of 3.0's improvements with the old branch so that WP1.5 users aren't completely in the cold.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • cricket-moods/branches/2.x/cricket-moods.php

    r4191 r5076  
    1010
    1111/**
    12 Copyright (c) 2005 Keith Constable
     12Copyright (c) 2006 Keith Constable
    1313
    1414Permission is hereby granted, free of charge, to any person obtaining a copy of
    … …  
    3434 ************************************************** !! **/
    3535
     36define('CM_VERSION', '2.1');
    3637// The name of the option key that contains the available moods.
    3738define('CM_OPTION_MOODS', 'cricketmoods_moods');
    … …  
    4344define('CM_OPTION_AUTOPRINT', 'cricketmoods_autoprint');
    4445
    45 /* Removing presentation options.  I need to think this through.
    46 define('CM_OPTION_BEFORE', 'cricketmoods_before');
    47 define('CM_OPTION_SEPARATOR', 'cricketmoods_separator');
    48 define('CM_OPTION_AFTER', 'cricketmoods_after');
    49 */
     46define('CM_OPTION_VERSION', 'cricketmoods_version');
    5047
    5148define('CM_IMAGE_DIR', get_option(CM_OPTION_DIR) );
    5249define('CM_META_KEY', 'mood');
    5350
    54 
    55 // These are used for writing various debug information to a file.  They are
    56 // currently unused.  So don't bother uncommenting them.
    57 //define('CM_DEBUG_FILE', $_SERVER['DOCUMENT_ROOT'].'/wp-content/plugins/cricket-moods/debug.txt');
    58 //$cm_debug = fopen(CM_DEBUG_FILE, 'a');
    5951
    6052
    … …  
    7264called from within The Loop.
    7365*/
    74 function cm_the_moods($separator=' & ', $before = null, $after = null) {
    75         global $wpdb;
     66function cm_the_moods($separator=' & ', $before = null, $after = null, $return = false) {
     67        global $post_ID, $wpdb;
    7668
    7769        // Get the moods for the current post.
    78         $post_moods = get_post_custom_values(CM_META_KEY);
     70        $post_moods = get_post_meta($post_ID, CM_META_KEY);
    7971
    8072        if( !empty($post_moods) ) {
    … …  
    8476                $mood_list = cm_process_moods();
    8577
     78                $output = '';
     79
    8680                foreach( $post_moods as $i => $mood_id ) {
    87                         $mood_name = wptexturize($mood_list[$mood_id]['mood_name']);
    8881                        if( $i == 0 ) {
    89                                 echo $before;
    90                         }
    91 
    92                         // Only print the img tag if the mood has an associated image.
    93                         if( !empty( $mood_list[$mood_id]['mood_image'] ) ) {
    94                                 echo '<img src="http://'. $_SERVER['HTTP_HOST'] . CM_IMAGE_DIR . wptexturize($mood_list[$mood_id]['mood_image']) .'" alt="'. $mood_name .' emoticon" /> ';
    95                         }
    96 
    97                         echo $mood_name;
     82                                $output .= $before;
     83                        }
     84
     85                        // Failsafe in case that mood ID doesn't actually exist.
     86                        if( !empty($mood_list[$mood_id]) ) {
     87                                $mood_name = wptexturize($mood_list[$mood_id]['mood_name']);
     88
     89                                // Only print the img tag if the mood has an associated image.
     90                                if( !empty( $mood_list[$mood_id]['mood_image'] ) ) {
     91                                        $output .= '<img src="'. CM_IMAGE_DIR . wptexturize($mood_list[$mood_id]['mood_image']) .'" alt="'. $mood_name .' emoticon" /> ';
     92                                }
     93
     94                                $output .= $mood_name;
     95
     96                                if( $i != $count ) {
     97                                        $output .= $separator;
     98                                }
     99                        }
    98100
    99101                        // Determine if this is the last mood.
    100                         if( $i != $count ) {
    101                                 echo $separator;
    102                         } else {
    103                                 echo $after;
    104                         }
     102                        if( $i == $count ) {
     103                                $output .= $after;
     104                        }
     105                }
     106
     107                if ($return) {
     108                        return $output;
     109                } else {
     110                        echo $output;
    105111                }
    106112        }
    … …  
    203209
    204210        // If no $mood, pull from $_POST.
    205         if(!$moods) {
     211        if( !isset($moods) ) {
    206212                $moods = cm_get_posted_moods();
    207213        }
    … …  
    240246} // cm_update_moods
    241247
    242 
    243 // Update the moods whenever a post is saved or edited.
    244 add_action('save_post', 'cm_update_moods');
    245 add_action('edit_post', 'cm_update_moods');
    246 
    247 
    248 
    249 /**
    250 cm_update_option
    251 
    252 A slight modification of WP's update_option().
    253 Sometimes, you *don't* want to trim.
    254 */
    255 /* Removing presentation options.
    256 function cm_update_option($option_name, $newvalue) {
    257         global $wpdb, $cache_settings;
    258         if ( is_array($newvalue) || is_object($newvalue) )
    259                 $newvalue = serialize($newvalue);
    260 
    261 //      $newvalue = trim($newvalue); // I can't think of any situation we wouldn't want to trim
    262         // kccricket says: Good for you, I can.
    263 
    264     // If the new and old values are the same, no need to update.
    265     if ($newvalue == get_option($option_name)) {
    266         return true;
    267     }
    268 
    269         // If it's not there add it
    270         if ( !$wpdb->get_var("SELECT option_name FROM $wpdb->options WHERE option_name = '$option_name'") )
    271                 add_option($option_name);
    272 
    273         $newvalue = $wpdb->escape($newvalue);
    274         $wpdb->query("UPDATE $wpdb->options SET option_value = '$newvalue' WHERE option_name = '$option_name'");
    275         $cache_settings = get_alloptions(); // Re cache settings
    276         return true;
    277 }
    278 */
     248add_action('save_post', 'cm_update_post_moods');
     249add_action('edit_post', 'cm_update_post_moods');
    279250
    280251
    … …  
    335306
    336307/**
     308cm_mood_sort
     309
     310**/
     311function cm_mood_sort( $row1,$row2 ) {
     312        if ( $first = strnatcasecmp($row1['mood_name'], $row2['mood_name']) ) {
     313                return $first;
     314        } else {
     315                return strnatcasecmp($row1['mood_image'], $row2['mood_image']);
     316        }
     317}
     318
     319
     320
     321/**
    337322cm_auto_moods
    338323
    339324Used if the AutoPrint option is enabled.
    340325*/
    341 function cm_auto_moods($time) {
    342         echo $time;
    343         cm_the_moods(' &amp; ', '<br/>Current Mood: ');
     326function cm_auto_moods($content) {
     327        return cm_the_moods(' &amp; ', '<p class="moods">Current Mood: ', '</p>', true) . $content;
    344328}
    345329
    … …  
    348332// is_admin() didn't work here...
    349333if ( strpos($_SERVER['PHP_SELF'], 'wp-admin/') === false && get_option(CM_OPTION_AUTOPRINT) == "on" ) {
    350         add_filter('the_time', 'cm_auto_moods');
     334        add_filter('the_content', 'cm_auto_moods');
    351335}
    352336
    … …  
    472456
    473457<?php
     458
     459        $mood_list = cm_process_moods();
     460
    474461        // If the user pushed the update button.
    475462        if ( isset($_POST['cm_options_update']) ) {
    … …  
    496483*/
    497484
    498                 // Get the pre-modified list of moods.
    499                 $mood_list = cm_process_moods();
    500 
    501485                // Parse the $_POST for the CM options we want.
    502486                foreach ($_POST as $name => $value) {
    … …  
    531515
    532516                // Finally, update the mood list.
     517                uasort($mood_list, 'cm_mood_sort');
    533518                update_option(CM_OPTION_MOODS, $mood_list);
    534519
    … …  
    549534        Directory containing the images associated with the moods.  Should be relative to the root of your domain.</li>
    550535        <li><input type="checkbox" id="cm_auto_print" name="cm_auto_print" <?php if ( get_option(CM_OPTION_AUTOPRINT) == "on" ) echo 'checked="true"' ?>/> <label for="cm_auto_print">Automatically print moods</label><br/>
    551         Causes Cricket Moods to automatically display moods directly after each post's time without the need to modify the active template.  Works best with the default WordPress theme.  Uncheck if you've manually added <code>cm_the_moods()</code> to your template(s).</li>
     536        Causes Cricket Moods to automatically display moods just before each post's content without the need to modify the active template.  Deselect if you've manually added <code>cm_the_moods()</code> to your template(s).</li>
    552537        <ul>
    553538</fieldset>
    554 <?php /* Removing presentation options.
    555 <fieldset class="options">
    556         <legend>Presentation</legend>
    557         <strong>Leave these options blank to use the default values.</strong>
    558         <ul>
    559         <li><label for="cm_before">Text to place before the first mood:</label><br/>
    560         <input type="text" id="cm_before" name="cm_before" value="<?php echo htmlspecialchars(get_option(CM_OPTION_BEFORE) ) ?>"/><br/>
    561         Default: <code>'Current Mood: '</code></li>
    562         <li><label for="cm_separator">Text to place in between multiple moods:</label><br/>
    563         <input type="text" id="cm_separator" name="cm_separator" value="<?php echo htmlspecialchars(get_option(CM_OPTION_SEPARATOR) ) ?>"/><br/>
    564         Will only display if the current post has two or more moods.<br/>
    565         Default: <code>' &amp;amp; '</code></li>
    566         <li><label for="cm_after">Text to place after the last mood:</label><br/>
    567         <input type="text" id="cm_after" name="cm_after" value="<?php echo htmlspecialchars(get_option(CM_OPTION_AFTER) ) ?>"/><br/>
    568         Default: <em>(blank)</em></li>
    569         </ul>
    570 </fieldset>
    571 */ ?>
     539
    572540<fieldset class="options">
    573541        <legend>Moods</legend>
    … …  
    581549<?php
    582550        // List the existing moods.
    583         foreach ( cm_process_moods() as $id => $mood ) {
     551        ksort($mood_list);
     552        foreach ( $mood_list as $id => $mood ) {
    584553?>
    585554                <tr<?php if ($alt == true) { echo ' class="alternate"'; $alt = false; } else { $alt = true; } ?> valign="middle">
    … …  
    683652        }
    684653
     654        if ( get_option(CM_OPTION_VERSION) != CM_VERSION ) {
     655                update_option(CM_OPTION_VERSION, CM_VERSION);
     656        }
    685657}
    686658
Note: See TracChangeset for help on using the changeset viewer.