manage_contests capability that can be given to other users with the Capability Manager plugin. Requires WP 2.7, PHP 5 and JavaScript. Tested on WP 2.9.2 and PHP 5.3. * License: GPL3 * Author: Spencer Sokol * Author URI: http://spencersokol.com/ * Version: 1.0 * * * * Copyright (C) 2010 Spencer Sokol * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ /** * Core plugin file for And The Winner Is... containing basic plugin * functionality and defined constants. * * @package AndTheWinnerIs */ if (!class_exists('AndTheWinnerIs')) { define( 'ATWI_DOMAIN', 'and-the-winner-is' ); define( 'ATWI', __('And The Winner Is', ATWI_DOMAIN) ); define( 'ATWI_VERSION', '1.0' ); define( 'ATWI_VERSION_TXT', 'atwi_version' ); define( 'ATWI_PATH', WP_PLUGIN_DIR . '/' . ATWI_DOMAIN ); define( 'ATWI_URL', WP_PLUGIN_URL . '/' . ATWI_DOMAIN ); define( 'ATWI_WINNERS_TABLE', 'atwi_winners' ); define( 'ATWI_CAPABILITY', 'manage_contests' ); define( 'ATWI_POST_META_IS_CONTEST', '_and_the_winner_is_contest' ); define( 'ATWI_POST_META_WINNERS_UNCONFIRMED', '_and_the_winner_is' ); define( 'ATWI_POST_META_WINNERS_CONFIRMED', '_and_the_winner_is_confirmed' ); define( 'ATWI_POST_META_WINNERS_REJECTED', '_and_the_winner_is_rejected' ); define( 'ATWI_POST_META_NUMBER_OF_WINNERS', '_and_the_winner_is_number_of_winners' ); define( 'ATWI_FORM_POST_IS_CONTEST', 'atwi_is_contest' ); define( 'ATWI_FORM_POST_NUMBER_OF_WINNERS', 'atwi_number_of_winners' ); define( 'ATWI_ERROR_PERMISSION_DENIED', __('You do not have permission to do that.', ATWI_DOMAIN) ); define( 'ATWI_ERROR_INVALID_POST_ID', __('Invalid post ID.', ATWI_DOMAIN) ); define( 'ATWI_ERROR_INVALID_COMMENT_ID', __('Invalid comment ID.', ATWI_DOMAIN) ); define( 'ATWI_ERROR_COMMENTS_ARE_OPEN', __('Comments are still open for this post.', ATWI_DOMAIN) ); define( 'ATWI_ERROR_POST_NOT_A_CONTEST', __('Post is not a contest.', ATWI_DOMAIN) ); define( 'ATWI_ERROR_WINNER_ALREADY_CONFIRMED', __('This winner has already been confirmed.', ATWI_DOMAIN) ); define( 'ATWI_ERROR_WINNER_ALREADY_REJECTED', __('This winner has already been rejected.', ATWI_DOMAIN) ); define( 'ATWI_MESSAGE_PLEASE_WAIT', __('Please wait', ATWI_DOMAIN) ); require_once(ATWI_PATH.'/ajax.php'); require_once(ATWI_PATH.'/contest.php'); require_once(ATWI_PATH.'/winner.php'); register_activation_hook( __FILE__, 'AndTheWinnerIs::Activate' ); register_deactivation_hook( __FILE__, 'AndTheWinnerIs::Deactivate' ); class AndTheWinnerIs { private static $installed_version; private static $AJAX; public function __construct() { $this->AJAX = new AndTheWinnerIs_AJAX(); add_action('admin_menu', array(&$this, 'AdminMenus')); add_action('admin_menu', array(&$this, 'AddMetaBox')); add_action('admin_init', array(&$this, 'AdminInit')); add_action('wp_insert_post', array(&$this, 'InsertPostMeta')); add_action('wp_ajax_atwi_set_comments', array(&$this->AJAX, 'SetComments')); add_action('wp_ajax_atwi_get_winner', array(&$this->AJAX, 'GetRandomWinner')); add_action('wp_ajax_atwi_confirm_winner', array(&$this->AJAX, 'ConfirmWinner')); add_action('wp_ajax_atwi_reject_winner', array(&$this->AJAX, 'RejectWinner')); add_action('wp_ajax_atwi_uninstall', array(&$this->AJAX, 'Uninstall')); if (version_compare(PHP_VERSION, '5.0.0', '<')) { add_action('admin_notices', array(&$this, 'DisplayPHPRequirementError')); } load_plugin_textdomain( ATWI_DOMAIN, false, dirname( plugin_basename( __FILE__ ) ).'/translations' ); } /** * Actives the plugin. */ public static function Activate() { if (!current_user_can('activate_plugins')) return; if (AndTheWinnerIs::GetInstalledVersion() < 1.0) { $admin = get_role('administrator'); $admin->add_cap(ATWI_CAPABILITY); update_option(ATWI_VERSION_TXT, ATWI_VERSION); } // version < 1.0 } /** * Deactivates the plugin */ public static function Deactivate() { if (!current_user_can('activate_plugins')) return; $admin = get_role('administrator'); $admin->remove_cap('administrator', ATWI_CAPABILITY); delete_option(ATWI_VERSION_TXT); } /** * Uninstalls all post meta data for the plugin. Requires an Admistrator. * * @global wpdb $wpdb * @return true if the uninstall succeeded */ public static function Uninstall() { if (!current_user_can('administrator')) return false; global $wpdb; $sql = "DELETE FROM `".$wpdb->postmeta."` WHERE `meta_key` IN ('". ATWI_POST_META_WINNERS_UNCONFIRMED."','".ATWI_POST_META_WINNERS_CONFIRMED."','".ATWI_POST_META_WINNERS_REJECTED."','". ATWI_POST_META_IS_CONTEST."','".ATWI_POST_META_NUMBER_OF_WINNERS."')"; $result = $wpdb->query($wpdb->prepare($sql)); return (false !== $result); } /** * Gets the currently installed version of the plugin * * @return */ public static function GetInstalledVersion() { if (empty(self::$installed_version)) { $v = get_option(ATWI_VERSION_TXT); self::$installed_version = (empty($v)) ? 0 : ((float)$v); } return (self::$installed_version); } /** * Outputs an PHP version error message */ public function DisplayPHPRequirementError() { echo '

'.ATWI.' '.__('plugin requires PHP 5 or higher to work properly. Your current version is', ATWI_DOMAIN).' '.PHP_VERSION.'

'; } /** * Adds a meta box to the side of the edit post screen. */ public function AddMetaBox() { add_meta_box(ATWI_DOMAIN, ATWI, array(&$this, 'ContestMetaBox'), 'post', 'side'); } /** * Outputs a small form for a post meta box * * @global $post */ public function ContestMetaBox() { global $post; $is_contest = get_post_meta($post->ID, ATWI_POST_META_IS_CONTEST, true) ? true : false; $number_of_winners = get_post_meta($post->ID, ATWI_POST_META_NUMBER_OF_WINNERS, true); $number_of_winners = (empty($number_of_winners)) ? 1 : intval($number_of_winners); echo ' '; echo '

'; echo ' '; echo ''; } /** * Inserts posts meta for this plugin * * @param $post_id * @uses $_POST[ATWI_FORM_POST_IS_CONTEST] * @uses $_POST[ATWI_FORM_POST_NUMBER_OF_WINNERS] */ public function InsertPostMeta($post_id) { if (isset($_POST[ATWI_FORM_POST_IS_CONTEST])) { update_post_meta($post_id, ATWI_POST_META_IS_CONTEST, true); $number = intval($_POST[ATWI_FORM_POST_NUMBER_OF_WINNERS]); $number = ($number < 1) ? 1 : $number; update_post_meta($post_id, ATWI_POST_META_NUMBER_OF_WINNERS, $number); } else { delete_post_meta($post_id, ATWI_POST_META_IS_CONTEST); delete_post_meta($post_id, ATWI_POST_META_NUMBER_OF_WINNERS); } } /** * Builds a displayable block of information for a comment * * @param $comment A valid comment object * @param $confirmed Whether or not this comment is the confirmed winner. * @return Comment block */ public static function FormatWinner($comment, $confirmed) { $result = ''; if (!empty($comment->comment_ID)) { $result .= '

'; $result .= ''; $result .= '
'; $result .= '

Your Winner

'; if ($confirmed) { $result .= '('.__('confirmed', ATWI_DOMAIN).')'; } $result .= '
'; $result .= ''.__('Confirm Winner', ATWI_DOMAIN).'
'; $result .= '
'; $result .= ' '.__('Reject Winner', ATWI_DOMAIN).' '; $result .= '
'; $result .= '
'; $result .= ''.__('Author:', ATWI_DOMAIN).' '.$comment->comment_author.' '; $result .= '('.$comment->comment_author_email.')'; if (!empty($comment->comment_author_url)) { $result .= '
'.__('Website:', ATWI_DOMAIN).' '.$comment->comment_author_url.''; } $result .= '
'; $result .= ''.__('Comment:', ATWI_DOMAIN).'

'.$comment->comment_content.'

'; $result .= '
'; } return ($result); } /** * Adds the admin area menu options. */ public function AdminMenus() { add_menu_page(ATWI, ATWI, ATWI_CAPABILITY, ATWI_DOMAIN, array(&$this, 'ContestsPage')); add_submenu_page(ATWI_DOMAIN, ATWI, 'Contests', ATWI_CAPABILITY, ATWI_DOMAIN, array(&$this, 'ContestsPage')); add_submenu_page(ATWI_DOMAIN, ATWI, 'Uninstall', 'administrator', ATWI_DOMAIN . '-uninstall', array(&$this, 'UninstallPage')); } /** * Queues up various scripts and styles for the admin area. */ public function AdminInit() { wp_enqueue_script('jquery'); wp_enqueue_script(ATWI_DOMAIN, ATWI_URL . '/js/atwi.js', array('jquery')); wp_enqueue_style(ATWI_DOMAIN, ATWI_URL . '/css/atwi.css'); } /** * Displays the 'Your Contests' page. */ public function ContestsPage() { include('admin-pages/contests.php'); } /** * Displays the 'Uninstall' page. */ public function UninstallPage() { include('admin-pages/uninstall.php'); } } // class new AndTheWinnerIs(); } /* class_exists */ ?>