* @copyright Copyright 2007 Edward Dale
* @license http://www.gnu.org/licenses/gpl.txt GPL 2.0
* @version $Id: admin.php 25 2007-06-21 13:07:25Z scompt $
* @link http://www.scompt.com/projects/zensor
* @since 0.5
*/
/**
* Common functions used all over the Zensor package
*/
require_once(dirname(__FILE__).'/common.php');
/**
* A collection of static functions for the admin screen
*/
class Zensor_Admin
{
/**
* Uninstalls all traces of the Zensor plugin
*
* Gets rid of the Zensor database table, Zensor options, and Zensor
* user role.
*/
function uninstall()
{
global $wpdb;
$query = "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '_zensor_%'";
$wpdb->query( $query );
$zensor_options = Zensor_Common::get_default_options();
foreach( $zensor_options as $option=>$default_value ) {
delete_option( $option );
}
delete_option( "zensor_db_version");
// Remove Zensor from the active_plugins list
$current = get_option('active_plugins');
array_splice($current, array_search( "zensor/zensor.php", $current), 1 );
update_option('active_plugins', $current);
// Removed scheduled notifications
wp_clear_scheduled_hook('zensor_author_notification');
wp_clear_scheduled_hook('zensor_moderator_notification');
remove_role( 'zensor_moderator' );
}
/**
* Creates everything Zensor needs to exist
*
* Creates the Zensor table if it doesn't exist. Also adds all the Zensor
* options and the user role. All posts/pages are added to the table
* with a default status of awaiting. Called using the
* 'activate_zensor/zensor.php' action hook.
*/
function activate()
{
global $zensor_db_version, $wpdb;
// activate is called before init, so we need to load translations here too
load_plugin_textdomain('zensor', PLUGINDIR.'/zensor/gettext');
if( !get_option( 'zensor_db_version') ) {
add_option("zensor_db_version", $zensor_db_version);
$zensor_options = Zensor_Common::get_default_options();
foreach( $zensor_options as $option=>$value ) {
add_option( $option, $value);
}
add_role( 'zensor_moderator', "Zensor " . __('Moderator', 'zensor') , array('read'=>1, 'zensor_moderate'=>1) );
$admin_role = get_role('administrator');
$admin_role->add_cap('zensor_moderate');
}
Zensor_Admin::schedule_notifications();
}
/**
* Schedules the 'zensor_daily_mail' hook for midnight of the current day
*
* If the hook isn't already scheduled, schedule it to occur daily
* at midnight.
*/
function schedule_notifications()
{
foreach( array('author', 'moderator') as $notification_type) {
wp_clear_scheduled_hook("zensor_{$notification_type}_notification");
$freq = get_option( "zensor_{$notification_type}_notification_frequency" );
if( $freq != 'immediately' ) {
wp_schedule_event( time(), $freq, "zensor_{$notification_type}_notification" );
remove_action('shutdown', array('Zensor_Admin', "do_{$notification_type}_notifications"));
}
}
}
/**
* Looks at the zensor_author_notifications option and notifies the authors
* of all of the posts stored there.
*/
function do_author_notifications()
{
global $wpdb;
$notifications = get_option('zensor_author_notifications');
if( $notifications ) {
update_option('zensor_author_notifications', array());
$default_body = get_option('zensor_author_email_body');
$subject = '['.get_option('blogname').'] '. __('Zensor Notification', 'zensor');
$headers = "MIME-Version: 1.0\n" .
"From: " . apply_filters('wp_mail_from', "wordpress@" . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']))) . "\n" .
"Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
foreach( array_unique($notifications) as $post_id ) {
// Gather data
$author_email = $wpdb->get_var("SELECT user_email from {$wpdb->users} JOIN {$wpdb->posts} ON {$wpdb->users}.ID={$wpdb->posts}.post_author WHERE {$wpdb->posts}.ID=$post_id");
$mod_id = get_post_meta($post_id, '_zensor_moderator', true);
if( $mod_id ) {
$mod = get_userdata($mod_id);
$mod_email = $mod->user_email;
$headers = $headers . "Reply-To: $mod_email\n";
}
// Build up the email body
$body = Zensor_Common::replace_tags($default_body, $post_id);
$subject = '['.get_option('blogname').'] '. __('Zensor Notification', 'zensor');
@wp_mail( $author_email, $subject, $body, $headers );
}
}
}
/**
* Sends an email to the moderaters if there are posts in the queue
*/
function do_moderator_notifications()
{
global $wpdb;
$notifications = get_option('zensor_moderator_notifications');
if( $notifications == 1 ) {
$posts = $wpdb->get_var( "SELECT GROUP_CONCAT(ID) FROM {$wpdb->posts} WHERE post_status='pending'" );
$ids = explode(',', $posts);
$default_body = get_option('zensor_moderator_email_body');
foreach( $ids as $post_id ) {
// Build up the email body
$body = Zensor_Common::replace_tags($default_body, $post_id);
$moderator_emails []= $body;
}
if( !empty($moderator_emails) ) {
$subject = '['.get_option('blogname').'] '. __('Zensor Notification', 'zensor');
$body = implode("\n------------------------------\n", $moderator_emails);
foreach( Zensor_Common::get_moderator_emails() as $email ) {
@wp_mail( $email, $subject, $body );
}
}
update_option('zensor_moderator_notifications', 0);
}
}
/**
* Plugs Zensor into the admin menu structure
*
* Called by the 'admin_menu' action hook.
*/
function admin_menu()
{
global $wpdb;
// Get the number of posts in the moderation system so it can be part of the menu
$count = Zensor_Common::get_pending_count();
// Add a Zensor menu underneath the options and management page
add_options_page('Zensor', 'Zensor', 'manage_options', 'zensor_admin_options_page', array('Zensor_Options', 'admin_options_page') );
$page = add_management_page('Zensor', "Zensor ($count)", 'zensor_moderate', 'zensor_admin_manage_page', array('Zensor_Manage', 'admin_manage_page') );
// Add some scripts and stylesheets to the admin section
add_action("admin_print_scripts-$page", array('Zensor_Admin', 'scripts') );
add_action("admin_head", array('Zensor_Admin', 'styles') );
}
/**
* Prints a stylesheet line
*
* Called by the 'admin_head' action hook.
*/
function styles()
{
echo '';
}
/**
* Enqueues a script for the admin screen
*
* Called by the 'admin_print_scripts' action hook.
*/
function scripts()
{
wp_enqueue_script( 'zensor', trailingslashit(get_option('siteurl')).PLUGINDIR.'/zensor/zensor.js', array('prototype'));
wp_localize_script( 'zensor', 'zensorL10n', array(
'previewOn' => __('Preview On', 'zensor'),
'previewOff' => __('Preview Off', 'zensor')
) );
}
/**
* Handles POSTs from the options screen that do admin-type things
*
* Such admin-type things include:
* - Uninstalling the script
* - Making sure the daily email is scheduled
*/
function handle_posts()
{
if( isset( $_POST['zensor_uninstall'] ) ) {
check_admin_referer('zensor_uninstall');
if( current_user_can( 'edit_plugins' ) ) {
Zensor_Admin::uninstall();
wp_redirect('plugins.php?deactivate=true');
} else {
wp_die(__('You are not allowed to edit plugins.', 'zensor'));
}
} else if( isset($_POST['zensor_reset_options'])) {
check_admin_referer('zensor_reset-options');
Zensor_Admin::schedule_notifications();
} else if ( isset($_POST['zensor_update_options']) ) {
check_admin_referer('zensor_update-options');
Zensor_Admin::schedule_notifications();
}
}
/**
* Checks if Zensor has just been upgraded. If so, upgrades the tables.
*/
function check_upgrade()
{
global $zensor_db_version, $wpdb;
if( get_option('zensor_db_version') != $zensor_db_version ) {
$zensor_table = $wpdb->prefix . 'zensor';
// $sql = "CREATE TABLE $zensor_table (
// post_id bigint(20) default NULL,
// moderation_status enum('".ZENSOR_AWAITING."','".ZENSOR_APPROVED."','".ZENSOR_REJECTED."') NOT NULL default '".ZENSOR_AWAITING."',
// moderator_id bigint(20) default NULL,
// last_updated timestamp NOT NULL default NOW() on update CURRENT_TIMESTAMP,
// message mediumtext,
// notified enum('Y','N') NOT NULL default 'N',
// UNIQUE KEY post_id (post_id)
// );";
//
// require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
// dbDelta($sql);
//
switch( get_option('zensor_db_version') ) {
case "0.5":
wp_clear_scheduled_hook('zensor_daily_email');
$defaults = Zensor_Common::get_default_options();
add_option('zensor_author_notification_frequency', $defaults['zensor_author_notification_frequency']);
add_option('zensor_moderator_notification_frequency', $defaults['zensor_moderator_notification_frequency']);
add_option('zensor_author_notifications', $defaults['zensor_author_notifications'] );
add_option('zensor_author_email_body', $defaults['zensor_author_email_body'] );
update_option('zensor_moderator_email_body', $defaults['zensor_moderator_email_body'] );
foreach( array('zensor_approval_email_subject','zensor_approval_email_body',
'zensor_moderator_email_body','zensor_rejection_email_subject',
'zensor_rejection_email_body') as $option ) {
delete_option($options);
}
// Get rid of any orphaned entries. These are taken care of in this version.
$query = "DELETE FROM $zensor_table WHERE post_id NOT IN (SELECT ID FROM {$wpdb->posts})";
$wpdb->query($query);
// assume any posts that are not awaiting moderation have already been notified
$query = "UPDATE $zensor_table SET notified='Y' WHERE moderation_status!='".ZENSOR_AWAITING."'";
$wpdb->query($query);
break;
case "2":
$query = "SELECT * FROM $zensor_table";
$rows = $wpdb->get_results($query);
foreach( $rows as $row ) {
add_post_meta($row->post_id, '_zensor_message', $row->message);
switch( $row->moderation_status ) {
case 'awaiting':
$postarr = array('ID'=>$row->post_id, 'post_status'=>'pending');
wp_update_post($postarr);
break;
case 'rejected':
$postarr = array('ID'=>$row->post_id, 'post_status'=>'draft');
wp_update_post($postarr);
add_post_meta($row->post_id, '_zensor_rejected', true, true);
break;
case 'approved':
$postarr = array('ID'=>$row->post_id, 'post_status'=>'publish');
wp_update_post($postarr);
break;
}
if( $row->moderator_id != NULL) {
add_post_meta($row->post_id, '_zensor_moderator', $row->moderator_id);
}
}
break;
}
update_option("zensor_db_version", $zensor_db_version);
}
}
}
/*
* Hook into the init action to initialize things at the right time.
*/
if( function_exists('add_action' ) )
{
add_action('init', array('Zensor_Admin', 'check_upgrade'), 2);
add_action('init', array('Zensor_Admin', 'handle_posts'), 3); // After Zensor_Options::handle_update_options
add_action('admin_menu', array('Zensor_Admin', 'admin_menu'));
add_action('activate_zensor/zensor.php', array('Zensor_Admin', 'activate'));
add_action('zensor_author_notification', array('Zensor_Admin', 'do_author_notifications'));
add_action('zensor_moderator_notification', array('Zensor_Admin', 'do_moderator_notifications'));
if( get_option('zensor_author_notification_frequency') == 'immediately' )
add_action('shutdown', array('Zensor_Admin', 'do_author_notifications'));
if( get_option('zensor_moderator_notification_frequency') == 'immediately' )
add_action('shutdown', array('Zensor_Admin', 'do_moderator_notifications'));
}
?>