what-did-they-say/classes/WhatDidTheySayAdmin.php

440 lines
14 KiB
PHP
Raw Normal View History

2009-08-12 23:51:23 +00:00
<?php
2009-08-19 23:58:24 +00:00
/**
* Administrative functions for What Did They Say?!?
*/
2009-08-12 23:51:23 +00:00
class WhatDidTheySayAdmin {
2009-08-14 02:13:46 +00:00
var $default_options = array(
'languages' => array(
array('code' => 'en', 'default' => true),
'fr',
'es',
'it',
'de'
),
2009-08-15 19:38:12 +00:00
'capabilities' => array(
2009-08-15 20:21:52 +00:00
'submit_transcriptions' => 'administrator',
'approve_transcriptions' => 'administrator',
2009-08-15 19:38:12 +00:00
'change_languages' => 'administrator'
)
2009-08-13 16:37:33 +00:00
);
2009-08-15 19:38:12 +00:00
var $capabilities = array();
2009-08-13 17:12:10 +00:00
var $language_file;
var $all_languages = array();
2009-08-14 17:45:50 +00:00
var $notices = array();
2009-08-19 23:58:24 +00:00
/**
* Initialize the admin interface.
* @param WhatDidTheySay $what_did_they_say The WhatDidTheySay object to use for all transcript transactions.
*/
2009-09-06 22:15:37 +00:00
function WhatDidTheySayAdmin() {
2009-08-13 17:12:10 +00:00
$this->language_file = dirname(__FILE__) . '/../data/lsr-language.txt';
2009-08-12 23:51:23 +00:00
}
2009-08-16 18:17:20 +00:00
/**
* Initialize the object.
*/
2009-08-15 20:45:08 +00:00
function init() {
2009-08-15 19:38:12 +00:00
$this->capabilities = array(
2009-08-15 20:21:52 +00:00
'submit_transcriptions' => __('Submit transcriptions to a post', 'what-did-they-say'),
'approve_transcriptions' => __('Approve transcriptions to a post', 'what-did-they-say'),
2009-08-15 19:38:12 +00:00
'change_languages' => __('Change the available languages', 'what-did-they-say')
);
2009-08-12 23:51:23 +00:00
add_action('admin_menu', array(&$this, 'admin_menu'));
2009-08-14 17:45:50 +00:00
add_action('admin_notices', array(&$this, 'admin_notices'));
2009-08-19 22:54:12 +00:00
add_action('admin_init', array(&$this, 'admin_init'));
2009-08-14 02:13:46 +00:00
wp_enqueue_script('prototype');
2009-08-15 20:21:52 +00:00
add_filter('user_has_cap', array(&$this, 'user_has_cap'), 5, 3);
2009-08-16 18:20:37 +00:00
add_filter('the_media_transcript', array(&$this, 'the_media_transcript'));
add_filter('the_language_name', array(&$this, 'the_language_name'));
2009-08-12 23:51:23 +00:00
2009-08-16 20:15:01 +00:00
add_filter('wp_footer', array(&$this, 'wp_footer'));
2009-08-14 17:45:50 +00:00
if (isset($_REQUEST['wdts'])) {
if (isset($_REQUEST['wdts']['_nonce'])) {
if (wp_verify_nonce($_REQUEST['wdts']['_nonce'], 'what-did-they-say')) {
$this->handle_update($_REQUEST['wdts']);
2009-08-12 23:51:23 +00:00
}
}
}
2009-08-14 02:13:46 +00:00
$this->read_language_file();
2009-08-12 23:51:23 +00:00
}
2009-08-13 16:37:33 +00:00
2009-08-19 23:58:24 +00:00
/**
* Handle admin_init action.
*/
2009-08-19 22:54:12 +00:00
function admin_init() {
wp_enqueue_script('scriptaculous-effects');
}
2009-08-19 23:58:24 +00:00
/**
* Handle the_media_transcript filter.
* @param string $transcript The transcription text.
* @return string The processed transcription text.
*/
2009-08-16 18:20:37 +00:00
function the_media_transcript($transcript) {
return '<div class="transcript">' . $transcript . '</div>';
}
2009-08-19 23:58:24 +00:00
/**
* Handle the_language_name filter.
* @param string $language The name of the language.
* @return string The processed language name.
*/
2009-08-16 18:20:37 +00:00
function the_language_name($language) {
2009-08-19 23:58:24 +00:00
return '<h3 class="transcript-language">' . $language . '</h3>';
2009-08-16 18:20:37 +00:00
}
2009-08-19 23:58:24 +00:00
/**
* Handle the wp_footer action.
*/
2009-08-16 20:15:01 +00:00
function wp_footer() { ?>
<script type="text/javascript">
$$('.transcript-bundle').each(function(d) {
var select = d.select("select");
if (select.length == 1) {
select = select[0];
var toggle_transcripts = function() {
d.select(".transcript-holder").each(function(div) {
div.hasClassName($F(select)) ? div.show() : div.hide();
});
};
Event.observe(select, 'change', toggle_transcripts);
Event.observe(window, 'load', toggle_transcripts)
}
});
</script>
<?php }
2009-08-16 18:17:20 +00:00
/**
2009-08-19 23:58:24 +00:00
* Handle the user_has_cap filter.
* @param array $capabilities The capabilities the user already has.
* @param array $requested_capabilities The capabilities requested by current_user_can.
* @param object $capability_name
* @return array The list of capabilities this user now has.
2009-08-16 18:17:20 +00:00
*/
2009-08-15 20:21:52 +00:00
function user_has_cap($capabilities, $requested_capabilities, $capability_name) {
$options = get_option('what-did-they-say-options');
2009-08-20 01:40:28 +00:00
if (is_array($options)) {
$role_cascade = array('administrator', 'editor', 'author', 'contributor', 'subscriber');
$allowed_roles = array();
$capture_roles = false;
for ($i = 0; $i < count($role_cascade); ++$i) {
if (in_array($role_cascade, $capabilities)) { $capture_roles = true; }
if ($capture_roles) { $allowed_roles[] = $role_cascade[$i]; }
}
2009-08-15 20:21:52 +00:00
2009-08-20 01:40:28 +00:00
foreach ($requested_capabilities as $requested_capability) {
if (in_array($options['capabilities'][$requested_capability], $allowed_roles)) {
$capabilities[$requested_capability] = true;
}
2009-08-15 20:21:52 +00:00
}
}
return $capabilities;
}
2009-08-16 18:17:20 +00:00
/**
2009-08-19 23:58:24 +00:00
* Handle show_admin action.
2009-08-16 18:17:20 +00:00
*/
2009-08-14 17:45:50 +00:00
function admin_notices() {
if (!empty($this->notices)) {
echo '<div class="updated fade">';
2009-08-16 16:40:54 +00:00
foreach ($this->notices as $notice) { echo "<p>" . $notice . "</p>"; }
2009-08-14 17:45:50 +00:00
echo '</div>';
}
}
2009-08-16 18:17:20 +00:00
/**
* Handle an update to options.
2009-08-19 23:58:24 +00:00
* @param array $info The part of the $_POST array for What Did They Say?!?
2009-08-16 18:17:20 +00:00
*/
2009-08-14 17:45:50 +00:00
function handle_update($info) {
foreach (get_class_methods($this) as $method) {
if (strpos($method, "handle_update_") === 0) {
$result = $this->{$method}($info);
if (!empty($result)) { $this->notices[] = $result; }
}
2009-08-14 17:45:50 +00:00
}
}
2009-08-19 23:58:24 +00:00
/**
* Handle updates to queued transcripts.
* @param array $info The part of the $_POST array for What Did They Say?!?
* @return string|false A string if a message is to be displayed, or false if no message.
*/
function handle_update_queue_transcript($info) {
2009-08-16 20:54:11 +00:00
$updated = false;
if (current_user_can('submit_transcriptions')) {
2009-08-19 23:58:24 +00:00
if ($this->what_did_they_say->get_allow_transcripts_for_post($info['post_id'])) {
switch ($info['action']) {
2009-08-19 02:33:59 +00:00
case 'submit_queued_transcript':
2009-08-19 23:58:24 +00:00
$result = $this->what_did_they_say->add_queued_transcription_to_post($info['post_id'], $info);
2009-08-19 02:33:59 +00:00
if ($result) {
$updated = __('Transcript added to queue.', 'what-did-they-say');
}
}
2009-08-16 20:54:11 +00:00
}
2009-08-19 02:33:59 +00:00
}
2009-08-16 20:54:11 +00:00
return $updated;
}
2009-08-14 17:45:50 +00:00
2009-08-19 23:58:24 +00:00
/**
* Handle updates to post transcripts.
* @param array $info The part of the $_POST array for What Did They Say?!?
* @return string|false A string if a message is to be displayed, or false if no message.
*/
function handle_update_post_transcripts($info) {
2009-08-16 16:40:54 +00:00
$updated = false;
if (current_user_can('approve_transcriptions')) {
$options = get_option('what-did-they-say-options');
2009-08-19 02:33:59 +00:00
2009-08-19 23:58:24 +00:00
switch ($info['action']) {
2009-08-16 16:40:54 +00:00
case "manage_post_transcripts":
2009-08-19 23:58:24 +00:00
foreach ($info['transcripts'] as $language => $transcript) {
2009-08-19 02:33:59 +00:00
switch ($language) {
case "_allow":
$allow = true;
break;
default:
2009-08-19 23:58:24 +00:00
$this->what_did_they_say->save_transcript($info['post_id'], $language, $transcript);
2009-08-19 02:33:59 +00:00
break;
}
2009-08-16 16:40:54 +00:00
}
2009-08-19 22:54:12 +00:00
2009-08-19 23:58:24 +00:00
$this->what_did_they_say->set_allow_transcripts_for_post($info['post_id'], isset($info['allow_on_post']));
2009-08-19 22:54:12 +00:00
2009-08-19 23:58:24 +00:00
$queued_transcriptions = $this->what_did_they_say->get_queued_transcriptions_for_post($info['post_id']);
2009-08-19 22:54:12 +00:00
if (is_array($queued_transcriptions)) {
$transcriptions_to_delete = array();
foreach ($queued_transcriptions as $transcription) { $transcriptions_to_delete[$transcription->id] = true; }
if (isset($post_transcript_info['queue'])) {
foreach ($post_transcript_info['queue'] as $id => $keep) { unset($transcriptions_to_delete[$id]); }
}
foreach (array_keys($transcriptions_to_delete) as $id) {
$this->what_did_they_say->delete_queued_transcription($id);
}
}
2009-08-20 01:40:28 +00:00
$updated = __('Transcripts updated.', 'what-did-they-say');
2009-08-16 16:40:54 +00:00
break;
}
}
return $updated;
}
2009-08-19 23:58:24 +00:00
/**
* Handle updates to languages.
* @param array $info The part of the $_POST array for What Did They Say?!?
* @return string|false A string if a message is to be displayed, or false if no message.
*/
function handle_update_languages($info) {
2009-08-14 17:45:50 +00:00
$updated = false;
2009-08-16 16:40:54 +00:00
if (current_user_can('change_languages')) {
2009-09-09 12:20:07 +00:00
$language_options = new WDTSLanguageOptions();
2009-08-19 23:58:24 +00:00
switch ($info['action']) {
2009-08-16 16:40:54 +00:00
case "delete":
2009-09-09 12:20:07 +00:00
if ($result = $language_options->delete_language($info['code'])) {
$updated = sprintf(__('%s deleted.', 'what-did-they-say'), $result['name']);
}
2009-08-16 16:40:54 +00:00
break;
case "add":
$this->read_language_file();
2009-08-19 23:58:24 +00:00
if (isset($this->all_languages[$info['code']])) {
2009-09-09 12:20:07 +00:00
2009-08-19 23:58:24 +00:00
$options['languages'][$info['code']] = array('name' => $this->all_languages[$info['code']]);
$updated = sprintf(__('%s added.', 'what-did-they-say'), $this->all_languages[$info['code']]);
2009-08-16 16:40:54 +00:00
}
break;
case "default":
2009-08-19 23:58:24 +00:00
if (isset($options['languages'][$info['code']])) {
foreach ($options['languages'] as $code => $lang_info) {
if ($code == $info['code']) {
2009-08-16 16:40:54 +00:00
$options['languages'][$code]['default'] = true;
2009-08-19 23:58:24 +00:00
$updated = sprintf(__('%s set as default.', 'what-did-they-say'), $lang_info['name']);
2009-08-16 16:40:54 +00:00
} else {
unset($options['languages'][$code]['default']);
}
2009-08-14 10:54:25 +00:00
}
}
2009-08-16 16:40:54 +00:00
break;
case "rename":
2009-08-19 23:58:24 +00:00
if (isset($options['languages'][$info['code']])) {
if (!empty($info['name'])) {
$updated = sprintf(__('%1$s renamed to %2$s.', 'what-did-they-say'), $options['languages'][$info['code']]['name'], $info['name']);
$options['languages'][$info['code']]['name'] = $info['name'];
2009-08-16 16:40:54 +00:00
}
2009-08-14 11:09:37 +00:00
}
2009-08-16 16:40:54 +00:00
break;
}
2009-08-19 23:58:24 +00:00
}
2009-08-14 17:45:50 +00:00
return $updated;
2009-08-13 23:32:42 +00:00
}
2009-08-19 23:58:24 +00:00
/**
* Handle updates to user capabilities.
* @param array $info The part of the $_POST array for What Did They Say?!?
* @return string|false A string if a message is to be displayed, or false if no message.
*/
function handle_update_capabilities($info) {
2009-08-15 19:38:12 +00:00
$updated = false;
2009-08-16 16:40:54 +00:00
if (current_user_can('edit_users')) {
$options = get_option('what-did-they-say-options');
2009-08-19 23:58:24 +00:00
switch ($info['action']) {
2009-08-16 16:40:54 +00:00
case "capabilities":
2009-08-19 23:58:24 +00:00
if (isset($info['capabilities'])) {
2009-08-16 16:40:54 +00:00
foreach (array_keys($this->default_options['capabilities']) as $capability) {
2009-08-19 23:58:24 +00:00
if (isset($info['capabilities'][$capability])) {
$options['capabilities'][$capability] = $info['capabilities'][$capability];
2009-08-16 16:40:54 +00:00
}
2009-08-15 19:53:55 +00:00
}
2009-08-20 01:40:28 +00:00
$updated = __('User capabilities updated.', 'what-did-they-say');
2009-08-15 19:53:55 +00:00
}
2009-08-16 16:40:54 +00:00
break;
}
if ($updated !== false) {
update_option('what-did-they-say-options', $options);
}
2009-08-15 19:53:55 +00:00
}
return $updated;
2009-08-15 19:38:12 +00:00
}
2009-08-13 17:12:10 +00:00
2009-08-20 01:40:28 +00:00
/**
* Handle resettings what-did-they-say-options.
* @param array $info The part of the $_POST array for What Did They Say?!?
* @return string|false A string if a message is to be displayed, or false if no message.
*/
function handle_update_reset_options($info) {
$updated = false;
if (current_user_can('manage_options')) {
switch ($info['action']) {
case "reset_options":
delete_option('what-did-they-say-options');
$this->install();
$updated = __('<strong>What Did They Say?!?</strong> options reset.', 'what-did-they-say');
break;
}
}
return $updated;
}
2009-08-19 23:58:24 +00:00
/**
* Read a data file containing all the known languages on Earth.
* The data originally came from http://www.langtag.net/, specifically http://www.langtag.net/registries/lsr-language.txt.
* The data file format is tab-delimited, with the following fields:
* language_code date_added name_of_language additional_information
* @return array The list of all known languages on Earth as code => language.
*/
2009-08-13 17:12:10 +00:00
function read_language_file() {
if (file_exists($this->language_file)) {
foreach (file($this->language_file) as $language) {
$language = trim($language);
2009-08-13 17:12:10 +00:00
list($code, $date_added, $name, $additional) = explode("\t", $language);
$this->all_languages[$code] = $name;
}
}
return $this->all_languages;
}
2009-08-19 23:58:24 +00:00
/**
* Handle plugin installation.
*/
2009-08-13 16:37:33 +00:00
function install() {
2009-08-14 11:03:58 +00:00
$this->read_language_file();
2009-08-14 02:13:46 +00:00
$options = get_option('what-did-they-say-options');
if (empty($options)) {
2009-08-14 11:03:58 +00:00
$this->default_options['languages'] = $this->build_default_languages();
2009-08-20 01:40:28 +00:00
ksort($this->default_options['languages']);
2009-08-14 02:13:46 +00:00
update_option('what-did-they-say-options', $this->default_options);
2009-08-20 01:40:28 +00:00
}
2009-08-13 16:37:33 +00:00
}
2009-08-14 11:03:58 +00:00
2009-08-19 23:58:24 +00:00
/**
* From $this->default_options, fill in the language details from the language file.
* @return array The language info will all details filled in.
*/
2009-08-14 11:03:58 +00:00
function build_default_languages() {
$full_default_language_info = array();
foreach ($this->default_options['languages'] as $info) {
$code = null;
if (is_string($info)) {
$code = $info;
$default = false;
}
if (is_array($info)) {
extract($info);
}
if (isset($this->all_languages[$code])) {
$full_default_language_info[$code] = array('name' => $this->all_languages[$code]);
if (!empty($default)) {
$full_default_language_info[$code]['default'] = true;
}
}
}
return $full_default_language_info;
}
2009-08-19 23:58:24 +00:00
/**
* Handle admin_menu action.
*/
2009-08-12 23:51:23 +00:00
function admin_menu() {
2009-08-15 20:21:52 +00:00
if (current_user_can('edit_users')) {
add_options_page(
__('What Did They Say?!? Settings', 'what-did-they-say'),
__('What Did They Say?!?', 'what-did-they-say'),
'manage_options',
'manage-wdts',
array(&$this, 'manage_admin')
);
}
2009-08-12 23:51:23 +00:00
2009-08-15 20:21:52 +00:00
if (current_user_can('approve_transcriptions')) {
2009-08-12 23:51:23 +00:00
add_meta_box(
'manage-transcriptions',
__('Manage Transcriptions', 'what-did-they-say'),
array(&$this, 'manage_transcriptions_meta_box'),
'post',
'normal',
'low'
);
}
}
2009-08-19 23:58:24 +00:00
/**
* Show the admin page.
*/
2009-08-15 19:58:01 +00:00
function manage_admin() {
2009-08-13 23:32:42 +00:00
$options = get_option('what-did-they-say-options');
2009-08-20 01:40:28 +00:00
2009-08-14 02:13:46 +00:00
$nonce = wp_create_nonce('what-did-they-say');
2009-08-13 16:37:33 +00:00
include(dirname(__FILE__) . '/admin.inc');
2009-08-12 23:51:23 +00:00
}
2009-08-19 23:58:24 +00:00
/**
* Show the Manage Transcriptions meta box.
*/
2009-08-12 23:51:23 +00:00
function manage_transcriptions_meta_box() {
2009-08-13 16:37:33 +00:00
global $post;
2009-08-15 20:21:52 +00:00
2009-08-15 19:38:12 +00:00
$options = get_option('what-did-they-say-options');
2009-08-20 01:40:28 +00:00
2009-08-15 20:45:08 +00:00
$transcripts = $this->what_did_they_say->get_transcripts($post->ID);
2009-08-19 22:54:12 +00:00
$queued_transcriptions = $this->what_did_they_say->get_queued_transcriptions_for_post($post->ID);
2009-08-15 20:45:08 +00:00
$nonce = wp_create_nonce('what-did-they-say');
include(dirname(__FILE__) . '/meta-box.inc');
2009-08-15 19:38:12 +00:00
}
2009-08-12 23:51:23 +00:00
}
?>