562c32d1 by Chris Boden

Overhaul of this library; organization, addition of ClientSettings, PagePermissions. refs #534

1 parent 7a88afc4
<?php
class ClientSettings {
const CAPABILITY = 'edit_client_settings';
const ADMIN_PAGE = 'client-settings';
public static function make() {
static $made = false;
if ($made) {
throw new OverflowException('ClientSettings has already been initialized');
}
$made = true;
$role = get_role('administrator');
$role->add_cap(self::CAPABILITY);
add_actions('ClientSettings_Actions');
}
public static function viewOptionsPage() {
}
}
// register_setting()
// settings_fields()
// add_settings_section()
// add_settings_field()
// settings_fields()
// do_settings_section()
class ClientSettings_Actions {
public static function admin_menu() {
$display = (current_user_can('manage_options') ? 'Client Settings' : 'Settings');
add_utility_page($display, $display, ClientSettings::CAPABILITY, ClientSettings::ADMIN_PAGE, Array('ClientSettings', 'viewOptionsPage'));
}
}
ClientSettings::make();
?>
\ No newline at end of file
var TzPagePermissions = function() {
var oSel;
var $select;
var $roles;
var init = function($) {
oSel = document.getElementById(TzPagePermissionsData.trigger);
if (!oSel) {
return;
}
$roles = $('#TzSpecific');
$(oSel).change(checkSetting);
checkSetting();
}
var checkSetting = function() {
var iVal = oSel.options[oSel.selectedIndex].value;
if (iVal == TzPagePermissionsData.focus) {
$roles.show();
} else {
$roles.hide();
}
}
jQuery(document).ready(init);
}();
\ No newline at end of file
<?php
/**
* Public API
*/
class PagePermissions {
const META = 'accessible_to_roles';
const OPT = '';
const ELE_SEL = 'general_access';
const ELE_CUST = 'roles';
const ELE_AUTH = 'message_auth';
const ELE_CUST_AUTH = 'message_cust_auth';
const ELE_DENIED = 'message_cust_denied';
const OPT_ALL = 0;
const OPT_AUTH = 1;
const OPT_CUST = 2;
private static $current_user = false;
public static function init() {
if (false !== self::$current_user) {
throw new OverflowException('PagePermissions already initialized');
}
self::$current_user = _get_current_user();
}
/**
* @param {Integer} $post_id
* @returns Boolean
* @throw InvalidArgumentException
*/
public static function current_user_can_view($post_id = false) {
static $settings = false;
if (false === $settings) {
$settings = new WP_Option(PagePermissionsAdmin::SETTING_NS);
}
if (false === $post_id) {
global $post;
$post_id = $post->ID;
}
// Meta value hasn't been set, assume public page (maybe should go off WP_Option default instead though...
if ('' === $data = get_custom_data(self::META, $post_id)) {
$data = Array(self::ELE_SEL => $settings[self::ELE_SEL], self::ELE_CUST => $settings[self::ELE_CUST]);
}
// Anyone has access, God has no limitations
if ($data[self::ELE_SEL] == self::OPT_ALL || is_site_admin()) {
return true;
}
// Login required and user is logged in
if ($data[self::ELE_SEL] == self::OPT_AUTH) {
if (is_user_logged_in()) {
return true;
}
return $settings[self::ELE_AUTH];
}
// Specific role required and user meets it
if ($data[self::ELE_SEL] == self::OPT_CUST) {
if (!is_user_logged_in()) {
return $settings[self::ELE_CUST_AUTH];
}
if (isset($data[self::ELE_CUST][self::get_user_role()])) {
return true;
}
return $settings[self::ELE_DENIED];
}
return 'An unknown permission error has occurred';
}
/**
* @param {Integer|String} $user Username or ID of user to lookup (or false for current user)
* @returns {String} $role The key of the users' role
*/
public static function get_user_role($user = false) {
if (false === $user) {
$user_data = self::$current_user;
} else {
$user_data = new WP_User($user);
}
// or should I throw an exception?
if ($user_data->ID == 0) {
return '';
}
$user_roles = $user_data->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
public static function getFieldNames() {
static $fields = false;
if (false !== $fields) {
return $fields;
}
$fields = Array();
$ref = new ReflectionClass(__CLASS__);
$consts = $ref->getConstants();
foreach ($consts as $const => $value) {
if (substr($const, 0, 4) == 'ELE_') {
$fields[$const] = $value;
}
}
return $fields;
}
}
class PagePermissionsAdmin {
const CAPABILITY = 'manage_page_permissions';
const ADMIN_PAGE = 'page-permission-settings';
const SUBMIT_HOOK = 'update_def_page_permissions';
const SETTING_NS = 'page_permission_defaults';
public static function make() {
static $made = false;
if ($made) {
throw new OverflowException('make has already beed called');
}
$made = true;
TzTools::import('ClientSettings');
$role = get_role('administrator');
$role->add_cap(self::CAPABILITY);
if (isset($_POST[self::SUBMIT_HOOK]) && current_user_can(self::CAPABILITY)) {
self::submit();
}
add_actions('PagePermissions_Actions');
}
public static function viewOptionsPage() {
$selected = self::getOptions();
include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'settings.php');
}
public static function viewMetaBox($post, $box_info) {
$selected = ($post->ID == 0 ? self::getOptions() : get_custom_data(PagePermissions::META, $post->ID));
if (empty($selected)) {
$selected = self::getOptions();
}
include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php');
}
public static function submit() {
unset($_POST[self::SUBMIT_HOOK]);
$options = self::getOptions();
$fields = PagePermissions::getFieldNames();
foreach ($fields as $field) {
if (isset($_POST[$field])) {
// not sure if stripslashes should go here or in WP_Options
$options[$field] = stripslashes($_POST[$field]);
} else {
$options[$field] = '';
}
}
$options->save();
}
private static function getOptions() {
static $options = false;
if (false !== $options) {
return $options;
}
$options = new WP_Option(self::SETTING_NS);
return $options;
}
}
class PagePermissions_Actions {
public static function admin_menu() {
if (current_user_can(ClientSettings::CAPABILITY)) {
add_submenu_page(ClientSettings::ADMIN_PAGE, 'Permission Defaults', 'Permission Defaults', PagePermissionsAdmin::CAPABILITY, PagePermissionsAdmin::ADMIN_PAGE, Array('PagePermissionsAdmin', 'viewOptionsPage'));
add_meta_box('page_permissions', 'Page Permissions', Array('PagePermissionsAdmin', 'viewMetaBox'), 'page', 'side', 'low');
}
}
public static function admin_print_scripts() {
_enqueue_script('page-permissions', plugins_url('PagePermissions.js', __FILE__));
_localize_script('page-permissions', 'TzPagePermissionsData', Array('trigger' => PagePermissions::ELE_SEL, 'focus' => PagePermissions::OPT_CUST));
}
public static function save_post($post_id) {
if (false === ($real_id = _is_post_revision($post_id))) {
$real_id = $post_id;
}
$current = get_custom_data(PagePermissions::META, $real_id);
$new = Array();
$new[PagePermissions::ELE_SEL] = $_POST[PagePermissions::ELE_SEL];
if (isset($_POST[PagePermissions::ELE_CUST])) {
$new[PagePermissions::ELE_CUST] = $_POST[PagePermissions::ELE_CUST];
} else {
$new[PagePermissions::ELE_CUST] = Array();
}
if (empty($current)) {
add_post_meta($real_id, PagePermissions::META, $new, true);
} else {
update_post_meta($real_id, PagePermissions::META, $new);
}
}
}
PagePermissions::init();
PagePermissionsAdmin::make();
?>
\ No newline at end of file
<?php
if (!isset($selected)) {
throw new UnderflowException('Current Page Permission settings not provided');
}
$opt_selected = $selected[PagePermissions::ELE_SEL];
?>
<select id="<?php echo PagePermissions::ELE_SEL; ?>" name="<?php echo PagePermissions::ELE_SEL; ?>">
<option value="<?php echo PagePermissions::OPT_ALL . '"' . (PagePermissions::OPT_ALL == $opt_selected ? ' selected' : ''); ?>>Anyone</option>
<option value="<?php echo PagePermissions::OPT_AUTH . '"' . (PagePermissions::OPT_AUTH == $opt_selected ? ' selected' : ''); ?>>Must be Logged In</option>
<option value="<?php echo PagePermissions::OPT_CUST . '"' . (PagePermissions::OPT_CUST == $opt_selected ? ' selected' : ''); ?>>Only Specific Users</option>
</select>
<div id="TzSpecific">
<?php
$rc = new WP_Roles();
$roles = $rc->role_names;
ksort($roles);
unset($rc, $roles['administrator']);
foreach ($roles as $key => $display) {
$checked = (isset($selected[PagePermissions::ELE_CUST][$key]) ? ' checked' : '');
echo '<br /><input type="checkbox" id="' . $key . '" name="' . PagePermissions::ELE_CUST . '[' . $key . ']" value="1"' . $checked . ' />';
echo '<label for="' . $key . '">' . $display . '</label>';
}
?>
</div>
\ No newline at end of file
<?php require(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'form.php'); ?>
\ No newline at end of file
<?php /*
Idea: Checkbox beside each textarea with option to include registration forum or not
*/ ?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>Permission Defaults</h2>
<form method="post">
<input type="hidden" name="<?php echo PagePermissionsAdmin::SUBMIT_HOOK; ?>" value="1" />
<h3>Defaults</h3>
<?php require(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'form.php'); ?>
<hr />
<h3>Messages</h3>
<p>
<label for="<?php echo PagePermissions::ELE_AUTH; ?>" />"Must be Logged In" message for un-authenticated visitors</label>
<br /><textarea id="<?php echo PagePermissions::ELE_AUTH; ?>" name="<?php echo PagePermissions::ELE_AUTH; ?>"><?php echo $selected[PagePermissions::ELE_AUTH]; ?></textarea>
</p>
<p>
<label for="<?php echo PagePermissions::ELE_CUST_AUTH; ?>" />"Only Specific Users" message for un-authenticated visitors</label>
<br /><textarea id="<?php echo PagePermissions::ELE_CUST_AUTH; ?>" name="<?php echo PagePermissions::ELE_CUST_AUTH; ?>"><?php echo $selected[PagePermissions::ELE_CUST_AUTH]; ?></textarea>
</p>
<p>
<label for="<?php echo PagePermissions::ELE_DENIED; ?>" />"Only Specific Users" message for authenticated users without sufficient privelages</label>
<br /><textarea id="<?php echo PagePermissions::ELE_DENIED; ?>" name="<?php echo PagePermissions::ELE_DENIED; ?>"><?php echo $selected[PagePermissions::ELE_DENIED]; ?></textarea>
</p>
<p class="submit"><input type="submit" class="button-primary" value="Save Changes" /></p>
</form>
</div>
\ No newline at end of file
......@@ -14,7 +14,6 @@ Author: Tenzing
class TzTools {
public static function load() {
// set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__));
spl_autoload_register(Array(__CLASS__, 'autoloader'));
require_once(dirname(__FILE__) . '/wp_functions.php');
......@@ -26,8 +25,16 @@ class TzTools {
add_action('widgets_init', Array('MenuWidget', 'init'));
}
public static function import($com) {
$dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'com' . DIRECTORY_SEPARATOR . $com . DIRECTORY_SEPARATOR;
$file = $dir . $com . '.php';
if (is_dir($dir) && is_file($file)) {
require_once($file);
}
}
public static function autoloader($class) {
$file = dirname(__FILE__) . DIRECTORY_SEPARATOR . $class . '.php';
$file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . $class . '.php';
if (is_file($file)) {
include($file);
}
......@@ -69,4 +76,74 @@ function add_filters($class) {
add_filter($method->name, Array($class, $method->name));
}
}
?>
function get_custom_data($name, $post_id = false) {
if (false === $type = get_post_type($post_id)) {
throw new InvalidArgumentException("Post {$post_id} does not exist");
}
$raw_data = call_user_func_array("_custom_{$type}", Array($post_id, $name));
if (null === $raw_data) {
return '';
}
return $raw_data;
/* @deprecated
if (is_array($raw_data)) {
return $raw_data;
}
return $raw_data
/* @deprecated
if (null === $maybe_data = json_decode($raw_data, true)) {
return $raw_data;
} else {
return $maybe_data;
}
*/
}
function _custom_attachment($post_id, $custom_name) {
if (false === ($tax_object = get_the_terms($post_id, $custom_name))) {
return '';
}
$tax_data = array_shift($tax_object);
return $tax_data->name;
}
function _custom_page($post_id, $custom_name) {
$custom = get_post_meta($post_id, $custom_name);
return array_shift($custom);
// @deprecated
static $custom = Array();
if (!isset($custom[$post_id])) {
$custom[$post_id] = get_post_custom($post_id);
if (!isset($custom[$post_id][$custom_name])) {
$custom[$post_id][$custom_name] = Array('');
}
}
return array_shift($custom[$post_id][$custom_name]);
}
function _custom_post() {
$args = func_get_args();
return call_user_func_array('_custom_page', $args);
}
function _custom_revision() {
$args = func_get_args();
return call_user_func_array('_custom_page', $args);
}
?>
\ No newline at end of file
......
......@@ -128,4 +128,9 @@ function _logout() {
$params = func_get_args();
return call_user_func_array('wp' . __FUNCTION__, $params);
}
function _is_post_revision() {
$params = func_get_args();
return call_user_func_array('wp' . __FUNCTION__, $params);
}
?>
......