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 .= ' ';
}
$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 '
'. __('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') .'
'. sprintf( __('Stripped %s moods from all posts.', 'cricket-moods'), $results) .'
'. __('Options updated!', 'cricket-moods') .'
Manage » Moods panel', 'cricket-moods') ?>.
* chirp * chirp *
| '; } ?> | ||||
![]() |
'. __('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') .'
'. sprintf( __('Stripped %s moods from your posts.', 'cricket-moods'), $results ) .'
'. __('Moods updated!', 'cricket-moods') .'
* chirp * chirp *