ID, CM_META_KEY); if( !empty($post_moods) ) { $count = count($post_moods) - 1; // Get a list of the available moods. $mood_list = cm_process_moods( get_the_author_ID() ); $output = ''; foreach( $post_moods as $i => $mood_id ) { if( $i == 0 ) { $output .= $before; } // Failsafe in case that mood ID doesn't actually exist. if( !empty($mood_list[$mood_id]) ) { $mood_name = wptexturize($mood_list[$mood_id]['mood_name']); // Only print the img tag if the mood has an associated image. if( !empty( $mood_list[$mood_id]['mood_image'] ) ) { $output .= ''. $mood_name .' '. __('emoticon', 'cricket-moods') .' '; } $output .= $mood_name; if( $i != $count ) { $output .= $separator; } } // Determine if this is the last mood. if( $i == $count ) { $output .= $after; } } if ($return) { return $output; } else { echo $output; } } } // cm_the_moods /** cm_has_moods $post_ID = integer (optional) The ID of the post you are inquiring about. Checks to see if the current post has mood information. Returns TRUE or FALSE accordingly. Could be useful if not all your posts have moods. Must be called from within The Loop if $post_ID is NULL. */ function cm_has_moods($post_ID = null) { if($post_ID === null) { global $post_ID; } if( cm_get_post_moods($post_ID) ) { return true; } else { return false; } } /** cm_process_moods Retrieves a list of available moods from the database. Returns them as a multi-dimensional array in the form: 'mood_id' => ('mood_name' => 'The Mood Name', 'mood_image' => 'themoodimage.gif') */ function cm_process_moods($user_ID = '') { if( $user_ID === '' ) { global $user_ID; } if( $user_ID != -1 ) { $moods = get_usermeta($user_ID, CM_OPTION_MOODS); } if($moods) { if( is_string($moods) ) { // Quick fix for serialization issues $moods = unserialize($moods); } return $moods; } else { return get_option(CM_OPTION_MOODS); } } /** cm_get_index **/ function cm_get_index($user_ID = '') { if( $user_ID === '' ) { global $user_ID; } if( $user_ID != -1 && get_usermeta($user_ID, CM_OPTION_INDEX) ) { return get_usermeta($user_ID, CM_OPTION_INDEX); } else { return get_option(CM_OPTION_INDEX); } } /** cm_get_submitted_moods Parses $_POST elements and returns an array of the values (mood ids) used by CM. Returns FALSE if no applicable values were submitted. */ function cm_get_submitted_moods() { $moods = array(); foreach($_POST as $key => $val) { // CM input element names are prefixed by 'cm_mood_'. if( substr($key, 0, 8) == 'cm_mood_' ) $moods[] = stripslashes( trim($val) ); } if( !empty($moods) ) return $moods; else return false; } /** cm_get_post_mood $post_id = integer ID number of the post to look up. Returns an array containing a post's mood IDs. */ function cm_get_post_moods($post_id) { return get_post_meta($post_id, CM_META_KEY); } /** cm_update_moods $post_ID = integer ID of the post to modify. $moods = array (optional) An array containing the list of moods to assign to the post. Modifies the moods associated with a post. If the $moods parameter is NULL, try to pull the moods from $_POST. */ function cm_update_post_moods($post_ID, $post) { if($post->post_type == 'revision') { return; } // Pull moods from $_POST. if( !current_user_can('edit_post', $post_ID) || !wp_verify_nonce($_POST['cricket-moods_verify-key'], 'update-postmoods_cricket-moods') ) return $post_ID; $moods = cm_get_submitted_moods(); // If the current post already has moods associated with it. if( cm_has_moods($post_ID) ) { if($moods) { // Remove any doubled moods. $moods = array_unique($moods); // Find out what moods the post currently has. $current_moods = cm_get_post_moods($post_ID); // Diff the arrays and add any moods that weren't there before. foreach( array_diff($moods, $current_moods) as $mood_id ) { add_post_meta($post_ID, CM_META_KEY, $mood_id); } // Diff the other way and remove any deselected moods. foreach( array_diff($current_moods, $moods) as $mood_id ) { // Use a while statement to delete possibly duplicated moods. while( delete_post_meta($post_ID, CM_META_KEY, $mood_id) ) { // delete_post_meta returns false if there is nothing to delete. continue; } } } // If no moods were posted and no moods were passed, remove all moods // from the post. else { delete_post_meta($post_ID, CM_META_KEY); } // If the post doesn't currently have any moods, don't bother diffing. } elseif($moods) { foreach($moods as $mood_id) { add_post_meta($post_ID, CM_META_KEY, $mood_id); } } // Add the current default moodlist to the usermeta to prevent issues. if( !empty($moods) && !get_usermeta($GLOBALS['user_ID'], CM_OPTION_MOODS) ) { update_usermeta( $GLOBALS['user_ID'], CM_OPTION_MOODS, get_option(CM_OPTION_MOODS) ); update_usermeta( $GLOBALS['user_ID'], CM_OPTION_INDEX, get_option(CM_OPTION_INDEX) ); } return $post_ID; } // cm_update_moods add_action('save_post', 'cm_update_post_moods', 10, 2); /** cm_list_select_moods Prints a fieldset full of checkboxes. Each checkbox corresponds with a mood. You get the idea. */ function cm_list_select_moods() { global $post, $user_ID; // Get a list of the available moods. if( isset($post->post_author) && $post->post_author != $user_ID ) $moods = cm_process_moods($post->post_author); else $moods = cm_process_moods($user_ID); // If we are editing an existing post, get that post's moods. if( !empty($post->ID) ) { $post_moods = cm_get_post_moods($post->ID); } echo '
'. __('Moods', 'cricket-moods') .'
'; } // cm_list_select_moods add_action('submitpost_box', 'cm_list_select_moods'); /** cm_mood_sort **/ function cm_mood_sort( $row1,$row2 ) { if ( $first = strnatcasecmp($row1['mood_name'], $row2['mood_name']) ) { return $first; } else { return strnatcasecmp($row1['mood_image'], $row2['mood_image']); } } /** cm_auto_moods Used if the AutoPrint option is enabled. */ function cm_auto_moods($content) { $pos = get_option(CM_OPTION_AUTOPRINT); $output = cm_the_moods(' & ', '

'. __('Current Mood:', 'cricket-moods'), '

', true); if ($pos == "above") { return $output . $content; } elseif ($pos == "below") { return $content . $output; } } if( !is_admin() && get_option(CM_OPTION_AUTOPRINT) != "off" ) { add_filter('the_content', 'cm_auto_moods'); } /** cm_admin_style Prints the stylesheet that makes my crap look decent. */ function cm_admin_style() { header('Content-Type: text/css'); ?> #cm_moodlist img { vertical-align: middle; padding: 0 2px; } #cm_mood_table { text-align: center; width: 80%; margin: 0 auto; } #cm_mood_table input.cm_text { width: 95%; } #cm_mood_table .delete:hover { background-color: #c00; } #mood_image_box { float: left; width: 18%; max-height: 40em; overflow: scroll; } #mood_image_box h4 { margin: 0; } #mood_image_list { list-style: none; margin: 0; padding: 0; } #mood_image_list li { margin: 2px 0; padding: 2px; background-color: silver; } .cm_danger { border: 2px groove red; padding: 0 1em; } .cm_danger:hover { background-color: #FFDDDD; color: black; } .cm_danger legend { font-size: 1.2em; font-weight: bold; } #cm_options_panel hr { clear: both; border-color: transparent; } #cm_reset_moods { float: left; width: 45%; } #cm_strip_posts { float: right; width: 45%; } p#cm_chirp { clear: both; font-style: italic; font-size: .85em; text-align: center; padding-top: 1em; } enqueue('jquery'); } if( strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false ) { add_action('wp_print_scripts', 'cm_add_jquery'); } function cm_admin_head() { $u = parse_url(get_option('siteurl')); $p = str_replace($_SERVER['DOCUMENT_ROOT'], '', __FILE__); ?>

'. __('Moods reset!', 'cricket-moods') .'

'; } elseif ( isset($_POST['cm_strip_moods']) ){ check_admin_referer('stripreset-moods_cricket-moods'); $results = $wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_key='". CM_META_KEY ."'"); if ( $results === false ) { echo '

'. __('Stripping failed.', 'cricket-moods') .'

'; } else { echo '

'. sprintf( __('Stripped %s moods from all posts.', 'cricket-moods'), $results) .'

'; } } // If the user pushed the update button. elseif ( isset($_POST['cm_options_update']) ) { check_admin_referer('update-options_cricket-moods'); $err = array(); // Check if we're running in WPMU. if ( !_cm_is_mu() || _cm_is_mu(TRUE) ) { // We don't like a blank image directory. if ( !empty($_POST['cm_image_dir']) ) { // Add a trailing slash if it doesn't have one. if ( substr( $_POST['cm_image_dir'], -1, 1 ) != '/' ) { $_POST['cm_image_dir'] .= '/'; } if ( _cm_is_mu() ) { update_site_option( CM_OPTION_DIR, $_POST['cm_image_dir'] ); } else { update_option( CM_OPTION_DIR, $_POST['cm_image_dir'] ); } if( !is_readable($_SERVER['DOCUMENT_ROOT'].$_POST['cm_image_dir']) ) { $err['cm_image_dir'] = __('The image directory you supplied either does not exist or is not accessible.', 'cricket-moods'); } } else { $err['cm_image_dir'] = __('You must supply an image directory!', 'cricket-moods'); } } // Pretty obvious. Set or unset the autoprint option. if ( !empty($_POST['cm_auto_print'] ) ) { update_option(CM_OPTION_AUTOPRINT, $_POST['cm_auto_print']); } foreach ($_POST as $name => $value) { // Existing moods start with 'cm_id_'. if ( substr($name, 0, 6) == 'cm_id_' ) { // If the user chose to delete this mood, delete the mood. if ( !empty($_POST["cm_delete_$value"]) ) { unset($mood_list[$value]); // Otherwise, update the mood name and image if both the name and the image are not blank. } elseif ( !empty($_POST["cm_name_$value"]) || !empty($_POST["cm_image_$value"]) ) { $mood_list[$value]['mood_name'] = $_POST["cm_name_$value"]; $mood_list[$value]['mood_image'] = $_POST["cm_image_$value"]; } else { $err['cm_id_'.$value] = sprintf( __('You must supply either a mood name or an image name for the mood with ID # %s!', 'cricket-moods'), $value ); } } // New moods start with 'cm_new_id_' and should have either a name or an image. elseif ( substr($name, 0, 10) == 'cm_new_id_' && ( !empty($_POST["cm_new_name_$value"]) || !empty($_POST["cm_new_image_$value"]) ) ) { // Add the new mood to the mood list. $mood_list[$index++] = array( 'mood_name' => $_POST["cm_new_name_$value"], 'mood_image' => $_POST["cm_new_image_$value"] ); } } // Update the option containing the index. update_option(CM_OPTION_INDEX, $index); // Finally, update the mood list. uasort($mood_list, 'cm_mood_sort'); update_option(CM_OPTION_MOODS, stripslashes_deep($mood_list) ); if ( empty($err) ) { echo '

'. __('Options updated!', 'cricket-moods') .'

'; } else { echo '
'; } } // End if update button pushed. } // End if user can manage options. ?>

Manage » Moods panel', 'cricket-moods') ?>.

> >

/>
/>
/>
cm_the_moods() to your template.', 'cricket-moods') ?>

default list of moods for new users. You may leave either the name or the image blank, but not both. Use the blank entries at the bottom to add new moods.', 'cricket-moods'); ?>


all posts by all users of any moods associated with them.', 'cricket-moods') ?>

* chirp * chirp *

read() ) ) { if ( eregi('\.gif|\.png|\.jp(g|eg?)', $entry) ) { $files[$entry] = _cm_get_option(CM_OPTION_DIR) . $entry; } } $d->close(); } natcasesort($files); reset($files); ?>

Images

$mood ) { ?> valign="middle"> valign="middle">
'; } ?>

'. __('Moods reset!', 'cricket-moods') .'

'; } elseif ( isset($_POST['cm_strip_moods']) ){ check_admin_referer('stripreset-moods_cricket-moods'); $results = $wpdb->query("DELETE $wpdb->postmeta FROM $wpdb->postmeta JOIN $wpdb->posts ON (". $wpdb->postmeta. ".post_id=". $wpdb->posts .".ID) WHERE meta_key='". CM_META_KEY ."' AND post_author=$user_ID"); if ( $results === false ) { echo '

'. __('Stripping failed.', 'cricket-moods') .'

'; } else { echo '

'. sprintf( __('Stripped %s moods from your posts.', 'cricket-moods'), $results ) .'

'; } } // Begin updating moods from manage panel. elseif ( isset($_POST['cm_mood_update']) ) { check_admin_referer('update-options_cricket-moods'); $err = array(); // Parse the $_POST for the CM options we want. foreach ($_POST as $name => $value) { // Existing moods start with 'cm_id_'. if ( substr($name, 0, 6) == 'cm_id_' ) { // If the user chose to delete this mood, delete the mood and any references to it. if ( !empty($_POST["cm_delete_$value"]) ) { if ( $wpdb->query("DELETE $wpdb->postmeta FROM $wpdb->postmeta JOIN $wpdb->posts ON (". $wpdb->postmeta.".post_id=". $wpdb->posts.".ID) WHERE meta_key='". CM_META_KEY ."' AND meta_value='$value' AND post_author=$user_ID") !== false ) { unset($mood_list[$value]); } // Otherwise, update the mood name and image if both the name and the image are not blank. } elseif ( !empty($_POST["cm_name_$value"]) || !empty($_POST["cm_image_$value"]) ) { $mood_list[$value]['mood_name'] = $_POST["cm_name_$value"]; $mood_list[$value]['mood_image'] = $_POST["cm_image_$value"]; } else { $err['cm_id_'.$value] = sprintf( __('You must supply either a mood name or an image name for the mood with ID # %s!', 'cricket-moods'), $value ); } } // New moods start with 'cm_new_id_' and should have either a name or an image. elseif ( substr($name, 0, 10) == 'cm_new_id_' && ( !empty($_POST["cm_new_name_$value"]) || !empty($_POST["cm_new_image_$value"]) ) ) { // Add the new mood to the mood list. $mood_list[$index++] = array( 'mood_name' => $_POST["cm_new_name_$value"], 'mood_image' => $_POST["cm_new_image_$value"] ); } } // Update the option containing the index. update_usermeta($user_ID, CM_OPTION_INDEX, $index); // Finally, update the mood list. uasort($mood_list, 'cm_mood_sort'); update_usermeta($user_ID, CM_OPTION_MOODS, stripslashes_deep($mood_list) ); if ( empty($err) ) { echo '

'. __('Moods updated!', 'cricket-moods') .'

'; } else { echo '
'; } } } ?>


either the name or the image blank, but not both. Use the blank entries at the bottom to add new moods.', 'cricket-moods'); ?>

all of your posts of any moods associated with them.', 'cricket-moods') ?>

* chirp * chirp *

__('Alarmed', 'cricket-moods'), 'mood_image' => 'icon_eek.gif'), array('mood_name' => __('Angry', 'cricket-moods'), 'mood_image' => 'icon_evil.gif'), array('mood_name' => __('Bored', 'cricket-moods'), 'mood_image' => 'icon_neutral.gif'), array('mood_name' => __('Confused', 'cricket-moods'), 'mood_image' => 'icon_confused.gif'), array('mood_name' => __('Cool', 'cricket-moods'), 'mood_image' => 'icon_cool.gif'), array('mood_name' => __('Esctatic', 'cricket-moods'), 'mood_image' => 'icon_biggrin.gif'), array('mood_name' => __('Flirtatious', 'cricket-moods'), 'mood_image' => 'icon_wink.gif'), array('mood_name' => __('Happy', 'cricket-moods'), 'mood_image' => 'icon_smile.gif'), array('mood_name' => __('Mischievous', 'cricket-moods'), 'mood_image' => 'icon_twisted.gif'), array('mood_name' => __('Playful', 'cricket-moods'), 'mood_image' => 'icon_razz.gif'), array('mood_name' => __('Sad', 'cricket-moods'), 'mood_image' => 'icon_cry.gif'), array('mood_name' => __('Sickly', 'cricket-moods'), 'mood_image' => 'icon_sad.gif'), array('mood_name' => __('Surprised', 'cricket-moods'), 'mood_image' => 'icon_surprised.gif') ); update_option(CM_OPTION_MOODS, $inital_moods); update_option(CM_OPTION_INDEX, count($inital_moods) ); } if ( !_cm_get_option(CM_OPTION_DIR) ) { if ( _cm_is_mu() ) { $basepath = parse_url(get_blog_option(1, 'siteurl')); update_site_option(CM_OPTION_DIR, $basepath['path'] .'/wp-includes/images/smilies/'); } else { $basepath = parse_url(get_option('siteurl')); update_option(CM_OPTION_DIR, $basepath['path'] .'/wp-includes/images/smilies/'); } } if ( !get_option(CM_OPTION_AUTOPRINT) || get_option(CM_OPTION_AUTOPRINT) == "on" ) { update_option(CM_OPTION_AUTOPRINT, 'above'); } } } // cm_install add_action('admin_init', 'cm_install');