* @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, $zensor_table; $query = "DROP TABLE $zensor_table"; $wpdb->query( $query ); $zensor_options = Zensor_Common::get_options(); foreach( $zensor_options as $option ) { 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); 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_table, $zensor_db_version, $wpdb; // Must build $zensor_table here because init is not called for new plugins $zensor_table = $wpdb->prefix . $zensor_table; // TODO: Needs to be rewritten to update database if db version is old if($wpdb->get_var("show tables like '$zensor_table'") != $zensor_table) { $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, UNIQUE KEY `post_id` (`post_id`) );"; require_once(ABSPATH . 'wp-admin/upgrade-functions.php'); dbDelta($sql); 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'); } // Now insert new records for posts that aren't in the moderation system. $query = "SELECT ID FROM {$wpdb->posts} LEFT JOIN $zensor_table ON {$wpdb->posts}.ID=$zensor_table.post_id". " WHERE ({$wpdb->posts}.post_type='post' OR {$wpdb->posts}.post_type='page') AND $zensor_table.moderation_status IS NULL"; $post_ids = $wpdb->get_col($query); foreach ( $post_ids as $post_id ) { $query = "INSERT INTO $zensor_table (post_id, message, moderation_status) VALUES ($post_id, 'Zensor " . __('Activation', 'zensor') . "', '".ZENSOR_AWAITING."')"; $wpdb->query( $query ); } Zensor_Admin::schedule_daily_emailing(); } /** * 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_daily_emailing() { wp_clear_scheduled_hook('zensor_daily_email'); if (!wp_next_scheduled( 'zensor_daily_email')) { // TODO: Make this configurable (daily, immediately, hourly, etc.) wp_schedule_event( mktime(0,0,0), 'daily', 'zensor_daily_email' ); } } /** * Sends an email to the moderaters if there are posts in the queue */ function do_daily_email() { $counts = Zensor_Common::get_counts(); if( $counts[ZENSOR_AWAITING] > 0 ) { $emails = Zensor_Common::get_moderator_emails(); $email_subject = Zensor_Common::replace_tags(get_option( "zensor_moderator_email_subject" )); $tags = array("%COUNT%" => $counts[ZENSOR_AWAITING]); $email_body = Zensor_Common::replace_tags(get_option( "zensor_moderator_email_body" ), NULL, $tags); // TODO: use BCC? foreach( $emails as $mod_email ) { wp_mail( $mod_email, $email_subject, $email_body ); } } } /** * Plugs Zensor into the admin menu structure * * Called by the 'admin_menu' action hook. */ function admin_menu() { global $zensor_table, $wpdb; // Get the number of posts in the moderation system so it can be part of the menu $counts = Zensor_Common::get_counts(); $count = $counts[ZENSOR_AWAITING] + $counts[ZENSOR_REJECTED]; // 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( 'prototype'); wp_enqueue_script( 'zensor', get_option('siteurl') . '/wp-content/plugins/zensor/scripts.php'); } /** * 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_daily_emailing(); } } } /* * Hook into the init action to initialize things at the right time. */ if( function_exists('add_action' ) ) { add_action('admin_menu', array('Zensor_Admin', 'admin_menu')); add_action('activate_zensor/zensor.php', array('Zensor_Admin', 'activate')); add_action('init', array('Zensor_Admin', 'handle_posts')); add_action('zensor_daily_email', array('Zensor_Admin', 'do_daily_email')); } ?>