<?php
/*
Plugin Name: Open Mic Friday
Version: 0.2
Plugin URI: http://www.technosailor.com/wordpress-plugin-open-mic-friday/
Description: Temporarily promotes all users to a minimum of Contributor role.
Author: Aaron Brazell
Author URI: http://www.technosailor.com

*/


function manage_openmic() {
    global 
$wpdb;
    if ( 
function_exists('add_submenu_page') )
        
add_submenu_page('options-general.php''Open Mic Friday''Open Mic Friday'1basename(__FILE__), 'mic_config');
}

function 
mic_config()
{
    echo
'<div class="wrap">';
    echo
'<h2>'.__('Open Mic Friday').'</h2>';
    if(isset(
$_POST['save']))
    {
        if(
get_option('open-mic-day'))
            
update_option('open-mic-day',$_POST['open-mic-day']);
        else 
            
add_option('open-mic-day',$_POST['open-mic-day']);
        echo 
"<div id='moderated' class='updated fade'>\n<p>";
        
_e('Settings Updated.');
        echo
'</p></div>';
    }
    
    
    
$open_mic_day = (get_option('open-mic-day')) ? get_option('open-mic-day') : 'Fri';
    
$days = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
        echo
'<p>'.__('Open Mic Friday can be on any day... not just Friday.  Pick which day you want open Mic Friday to be on.').'</p>';
        echo
'<form action="options-general.php?page=open-mic-friday.php" method="post">';
            echo
'<label for="open-mic-day">Day of Week: </label>';
            echo
'<select name="open-mic-day">';
            foreach(
$days as $day)
            {
                echo (
$day == $open_mic_day) ? '<option value="'.$day.'" selected="selected">'.$day.'</option>' '<option value="'.$day.'">'.$day.'</option>';
            }
            echo
'</select>';
            echo
'<input type="submit" name="save" value="Save" />';
        echo
'</form>';
    echo
'</div>';
}

function 
temp_promote()
{
    global 
$current_user;

    
$open_mic_day = (get_option('open-mic-day')) ? get_option('open-mic-day') : 'Fri';
    
// If there is no user id set or it is less than 1, break out
    
if(>= $current_user->id || empty($current_user->id))
        return;
        
    
// If today is our appointed day...
    
if($open_mic_day == date('D',current_time('timestamp')))
    {
        
// ...and the current user cannot edit posts
        
if(!current_user_can('edit_posts'))
        {
            foreach(
$current_user->roles as $oldrole)
                
update_usermeta($current_user->id,'temp_promotion',$oldrole);
            
$current_user->set_role('contributor');
        }
        else 
            return;
            
    }
    else 
    {
        if(
current_user_can('edit_posts') && (get_usermeta($current_user->id,'temp_promotion')))
        {
            
$current_user->set_role(get_usermeta($current_user->id,'temp_promotion'));
            
delete_usermeta($current_user->id,'temp_promotion');
            return;
        }
    }
    return;
}

add_action('wp_head','temp_promote');
add_action('admin_menu''manage_openmic');
?>