* @copyright Copyright 2007 Edward Dale * @license http://www.gnu.org/licenses/gpl.txt GPL 2.0 * @version $Id: common.php 21 2007-06-21 12:55:01Z scompt $ * @link http://www.scompt.com/projects/zensor * @since 0.5 */ define('ZENSOR_AWAITING', 'awaiting'); define('ZENSOR_REJECTED', 'rejected'); define('ZENSOR_APPROVED', 'approved'); /** * A collection of static functions used throughout the plugin */ class Zensor_Common { /** * @return array All of Wordpress options used by Zensor */ function get_options() { return array( 'zensor_awaiting_moderation_message', 'zensor_rejected_moderation_footer', 'zensor_awaiting_moderation_footer', 'zensor_rejected_page_edit_message', 'zensor_approved_page_edit_message', 'zensor_awaiting_page_edit_message', 'zensor_new_page_edit_message', 'zensor_author_email_body', 'zensor_moderator_email_body', 'zensor_author_notification_frequency', 'zensor_moderator_notification_frequency' ); } /** * @return array All of the Wordpress options used by Zensor, along with * their localized default values. */ function get_default_options() { return array( 'zensor_awaiting_moderation_message' => __('

Awaiting Moderation

', 'zensor'), 'zensor_rejected_moderation_footer' => __('

This %PAGE/POST% has been rejected by %MODERATOR_NAME% for the reason below. It is now awaiting re-publication by the original author.

\n

Reason: %MESSAGE%

', 'zensor'), 'zensor_awaiting_moderation_footer' => __('

This %PAGE/POST% is awaiting moderation, potentially by YOU!

', 'zensor'), 'zensor_rejected_page_edit_message' => __('

This %PAGE/POST% has been rejected by the moderator %MODERATOR_NAME% for the reason below. Any changes you make here will be resubmitted for approval by a moderator before becoming visible to the public.

\n

Reason: %MESSAGE%

', 'zensor'), 'zensor_approved_page_edit_message' => __('

This %PAGE/POST% has already been approved by a moderator. Any changes that you make will not be visible to the public until a moderator has approved them.

', 'zensor'), 'zensor_awaiting_page_edit_message' => __('

This %PAGE/POST% is already awaiting moderation. The content you see here is not visible to the public and any changes that are made will not be visible until a moderator has approved them.

', 'zensor'), 'zensor_new_page_edit_message' => __('

When published, this %PAGE/POST% will not be displayed to the public until a moderator has approved it.

', 'zensor'), 'zensor_author_email_body' => __("Your %PAGE/POST% (%PERMALINK%) entitled %TITLE% has been %STATUS% by %MODERATOR_NAME%. The following message was given:\n\n%MESSAGE%", 'zensor'), 'zensor_moderator_email_body' => __("A %PAGE/POST% (%THE_LINK%) entitled %TITLE% has been posted by %AUTHOR_NAME%. The following message was given:\n\n%MESSAGE%", 'zensor'), 'zensor_author_notification_frequency' => 'immediately', 'zensor_moderator_notification_frequency' => 'immediately', 'zensor_author_notifications' => array() ); } /** * Returns the number of posts/pages pending review. * * @return int The number of posts/pages with a status of 'pending' */ function get_pending_count() { global $wpdb; $query = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_status='pending'"; $count = $wpdb->get_var( $query ); return (int)$count; } /** * Looks at the Wordpress users table to find the email of users who can moderate * * Uses the wp_cache to cache the emails found as this could be an * expensive operation. Finds users by checking if they have the * 'zensor_moderate' capability. * * @return array The moderator emails */ function get_moderator_emails() { if ($emails = wp_cache_get('moderator_emails', 'zensor')) { return $emails; } global $wpdb; $emails = array(); $ids = $wpdb->get_col('SELECT ID from ' . $wpdb->users); foreach( $ids as $userid ) { $tmp_user = new WP_User($userid); if( $tmp_user->has_cap( 'zensor_moderate') ) { $emails[] = $tmp_user->user_email; } } $emails = array_unique($emails); // trim out any dupes wp_cache_set('moderator_emails', $emails, 'zensor'); return $emails; } /** * Replaces some tags that can be used to add more context to messages * * The following tags are replaced: * %THE_LINK% -> A link to the moderation page of the current post * %PAGE/POST% -> 'page' or 'post' depending on what the current thing is * %MESSAGE% -> The message given by the post author or moderator * %TITLE% -> Title of the current post * %PERMALINK% -> Permalink to the post * %AUTHOR_NAME% -> Author of the post * %STATUS% -> Zensor status of the post * * @param string $message The message containing tags to be replaced. * @param array $replacements Replacements that should take precedence over others * @return string The message with all tags replaced with valid values */ function replace_tags( $message, $post_id=NULL, $replacements=array() ) { global $post, $user_identity; if( $post_id != NULL ) { $the_post = get_post($post_id); } else if( $post ) { $the_post = $post; $post_id = $the_post->ID; } $message = stripslashes( $message ); $context_replacements = array(); if( $the_post ) { $author = get_userdata($the_post->post_author); $statii = array( 'publish' => __('Approved', 'zensor'), 'draft' => __('Rejected', 'zensor'), 'pending' => __('Awaiting', 'zensor') ); $context_replacements['%THE_LINK%'] = Zensor_Common::moderation_page_link( $post_id ); $context_replacements['%PAGE/POST%'] = $the_post->post_type == 'page' ? __('page', 'zensor') : __('post', 'zensor'); $context_replacements['%TITLE%'] = $the_post->post_title; $context_replacements['%AUTHOR_NAME%'] = $author->display_name; $context_replacements['%PERMALINK%'] = get_permalink($post_id); $context_replacements['%MESSAGE%'] = get_post_meta($post_id, '_zensor_message', true); $context_replacements['%REASON%'] = $context_replacements['%MESSAGE%']; $context_replacements['%STATUS%'] = $statii[$the_post->post_status]; if( $user_identity ) $context_replacements['%MODERATOR_NAME%'] = $user_identity; } $default_replacements = array("%TITLE%" => __('Untitled', 'zensor'), "%PAGE/POST%" => __('page', 'zensor'), "%MODERATOR_NAME%" => __('A moderator', 'zensor'), "%MESSAGE%" => __('No message.', 'zensor'), "%THE_LINK%" => Zensor_Common::uri()); $replacements = array_merge($default_replacements, $context_replacements, $replacements); $message = str_replace(array_keys($replacements), array_values($replacements), $message); return $message; } /** * Returns a direct link to the moderation page for a particular post * * @param int $post_id The ID of the post to link to * @return string A link to the moderation page for a particular post */ function moderation_page_link( $post_id ) { return Zensor_Common::uri() . "&id=$post_id#zensor-$post_id"; } /** * @return string The uri of the main Zensor management page */ function uri() { return get_settings('siteurl') . '/wp-admin/admin.php?page=zensor_admin_manage_page'; } /** * */ function approve_post($id, $message) { Zensor_Common::moderate_post($id, $message, 'publish'); } /** * */ function reject_post($id, $message) { Zensor_Common::moderate_post($id, $message, 'draft'); add_post_meta($id, '_zensor_rejected', true, true); } /** * */ function moderate_post($post_id, $message, $status) { global $user_ID, $wpdb; $postarr = array('ID'=>$post_id, 'post_status'=>$status); wp_update_post($postarr); if ( ! update_post_meta($post_id, '_zensor_message', $message)) add_post_meta($post_id, '_zensor_message', $message, true); if ( ! update_post_meta($post_id, '_zensor_moderator', $user_ID)) add_post_meta($post_id, '_zensor_moderator', $user_ID, true); } } ?>