553 lines
19 KiB
PHP
553 lines
19 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Administrative functions for What Did They Say?!?
|
|
*/
|
|
class WhatDidTheySayAdmin {
|
|
var $default_options = array(
|
|
'languages' => array(
|
|
array('code' => 'en', 'default' => true),
|
|
'fr',
|
|
'es',
|
|
'it',
|
|
'de'
|
|
),
|
|
'capabilities' => array(
|
|
'submit_transcriptions' => 'administrator',
|
|
'approve_transcriptions' => 'administrator',
|
|
'change_languages' => 'administrator'
|
|
),
|
|
'load_default_styles' => true
|
|
);
|
|
|
|
var $capabilities = array();
|
|
|
|
var $language_file;
|
|
var $all_languages = array();
|
|
var $notices = array();
|
|
|
|
var $is_ajax = false;
|
|
|
|
/**
|
|
* Initialize the admin interface.
|
|
* @param WhatDidTheySay $what_did_they_say The WhatDidTheySay object to use for all transcript transactions.
|
|
*/
|
|
function WhatDidTheySayAdmin() {
|
|
$this->language_file = dirname(__FILE__) . '/../data/lsr-language.txt';
|
|
}
|
|
|
|
/**
|
|
* Initialize the object.
|
|
*/
|
|
function init() {
|
|
$this->capabilities = array(
|
|
'submit_transcriptions' => __('Submit transcriptions to a post', 'what-did-they-say'),
|
|
'approve_transcriptions' => __('Approve transcriptions to a post', 'what-did-they-say'),
|
|
'change_languages' => __('Change the available languages', 'what-did-they-say')
|
|
);
|
|
|
|
$options = get_option('what-did-they-say-options');
|
|
if (!is_array($options)) {
|
|
$options = $this->default_options;
|
|
update_option('what-did-they-say-options', $options);
|
|
}
|
|
|
|
add_action('admin_menu', array(&$this, 'admin_menu'));
|
|
add_action('admin_notices', array(&$this, 'admin_notices'));
|
|
|
|
add_filter('user_has_cap', array(&$this, 'user_has_cap'), 5, 3);
|
|
add_filter('the_media_transcript', array(&$this, 'the_media_transcript'));
|
|
add_filter('the_language_name', array(&$this, 'the_language_name'));
|
|
|
|
add_filter('template_redirect', array(&$this, 'template_redirect'));
|
|
|
|
add_filter('posts_where', array(&$this, 'posts_where'));
|
|
add_filter('posts_join', array(&$this, 'posts_join'));
|
|
|
|
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']);
|
|
|
|
if ($this->is_ajax) { exit(0); }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
function posts_where($where) {
|
|
global $wpdb;
|
|
|
|
$search = get_query_var('s');
|
|
if (!empty($search)) {
|
|
$where .= $wpdb->prepare(" OR ($wpdb->postmeta.meta_key = %s ", 'approved_transcripts');
|
|
$search = addslashes_gpc($search);
|
|
$where .= " AND $wpdb->postmeta.meta_value LIKE '%$search%') ";
|
|
}
|
|
|
|
return $where;
|
|
}
|
|
|
|
function posts_join($join) {
|
|
global $wpdb;
|
|
|
|
$search = get_query_var('s');
|
|
if (!empty($search)) {
|
|
$join .= " JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
|
|
}
|
|
|
|
return $join;
|
|
}
|
|
|
|
function template_redirect() {
|
|
wp_enqueue_script('toggle-transcript', plugin_dir_url(dirname(__FILE__)) . 'js/toggle-transcript.js', array('prototype'), false, true);
|
|
if (current_user_can('submit_transcriptions')) { wp_enqueue_script('scriptaculous-effects'); }
|
|
|
|
foreach (get_class_methods($this) as $method) {
|
|
if (strpos($method, "shortcode_") === 0) {
|
|
$shortcode_name = str_replace("_", "-", str_replace("shortcode_", "", $method));
|
|
add_shortcode($shortcode_name, array(&$this, $method));
|
|
}
|
|
}
|
|
|
|
add_filter('filter_shortcode_dialog', array(&$this, 'filter_shortcode_dialog'), 10, 4);
|
|
add_filter('filter_shortcode_scene_action', array(&$this, 'filter_shortcode_scene_action'), 10, 2);
|
|
add_filter('filter_shortcode_scene_heading', array(&$this, 'filter_shortcode_scene_heading'), 10, 2);
|
|
|
|
$options = get_option('what-did-they-say-options');
|
|
if (!empty($options['load_default_styles'])) {
|
|
wp_enqueue_style('wdts-defaults', plugin_dir_url(dirname(__FILE__)) . 'css/wdts-defaults.css');
|
|
}
|
|
}
|
|
|
|
function shortcode_dialog($atts, $speech) {
|
|
extract(shortcode_atts(array(
|
|
'name' => 'Nobody',
|
|
'direction' => ''
|
|
), $atts));
|
|
|
|
list($content) = apply_filters('filter_shortcode_dialog', "", $name, $direction, $speech);
|
|
return $content;
|
|
}
|
|
|
|
function filter_shortcode_dialog($content, $name, $direction, $speech) {
|
|
$content = '<div class="dialog"><span class="name">' . $name . '</span>';
|
|
if (!empty($direction)) {
|
|
$content .= ' <span class="direction">' . $direction . '</span>';
|
|
}
|
|
$content .= ' <span class="speech">' . $speech . '</span></div>';
|
|
|
|
return array($content, $name, $direction, $speech);
|
|
}
|
|
|
|
function shortcode_scene_action($atts, $description) {
|
|
extract(shortcode_atts(array(), $atts));
|
|
|
|
list($content) = apply_filters('filter_shortcode_scene_action', "", $description);
|
|
return $content;
|
|
}
|
|
|
|
function filter_shortcode_scene_action($content, $description) {
|
|
return array('<div class="scene-action">' . $description . '</div>', $description);
|
|
}
|
|
|
|
function shortcode_scene_heading($atts, $description) {
|
|
extract(shortcode_atts(array(), $atts));
|
|
|
|
list($content) = apply_filters('filter_shortcode_scene_heading', "", $description);
|
|
return $content;
|
|
}
|
|
|
|
function filter_shortcode_scene_heading($content, $description) {
|
|
return array('<div class="scene-heading">' . $description . '</div>', $description);
|
|
}
|
|
|
|
/**
|
|
* Handle the_media_transcript filter.
|
|
* @param string $transcript The transcription text.
|
|
* @return string The processed transcription text.
|
|
*/
|
|
function the_media_transcript($transcript) {
|
|
return '<div class="transcript">' . do_shortcode($transcript) . '</div>';
|
|
}
|
|
|
|
/**
|
|
* Handle the_language_name filter.
|
|
* @param string $language The name of the language.
|
|
* @return string The processed language name.
|
|
*/
|
|
function the_language_name($language) {
|
|
return '<h3 class="transcript-language">' . $language . '</h3>';
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
function user_has_cap($capabilities, $requested_capabilities, $capability_name) {
|
|
$options = get_option('what-did-they-say-options');
|
|
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]; }
|
|
}
|
|
|
|
foreach ($requested_capabilities as $requested_capability) {
|
|
if (in_array($options['capabilities'][$requested_capability], $allowed_roles)) {
|
|
$capabilities[$requested_capability] = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $capabilities;
|
|
}
|
|
|
|
/**
|
|
* Handle show_admin action.
|
|
*/
|
|
function admin_notices() {
|
|
if (!empty($this->notices)) {
|
|
echo '<div class="updated fade">';
|
|
foreach ($this->notices as $notice) { echo "<p>" . $notice . "</p>"; }
|
|
echo '</div>';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle an update to options.
|
|
* @param array $info The part of the $_POST array for What Did They Say?!?
|
|
*/
|
|
function handle_update($info) {
|
|
if (isset($info['module'])) {
|
|
$method_name = "handle_update_" . str_replace("-", "_", $info['module']);
|
|
if (method_exists($this, $method_name)) {
|
|
$result = $this->{$method_name}($info);
|
|
if (!empty($result)) { $this->notices[] = $result; }
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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) {
|
|
$updated = false;
|
|
if (current_user_can('submit_transcriptions')) {
|
|
$transcript_options = new WDTSTranscriptOptions($info['post_id']);
|
|
|
|
if ($transcript_options->are_new_transcripts_allowed()) {
|
|
$queued_transcript_manager = new WDTSQueuedTranscript($info['post_id']);
|
|
|
|
if ($queued_transcript_manager->save_transcript($info)) {
|
|
$updated = __('Transcript added to queue.', 'what-did-they-say');
|
|
} else {
|
|
$updated = __('Transcript not added to queue.', 'what-did-they-say');
|
|
}
|
|
}
|
|
}
|
|
return $updated;
|
|
}
|
|
|
|
/**
|
|
* 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_manage_post_transcripts($info) {
|
|
$updated = false;
|
|
if (current_user_can('approve_transcriptions')) {
|
|
$approved_transcript_manager = new WDTSApprovedTranscript($info['post_id']);
|
|
|
|
foreach ($info['transcripts'] as $language => $transcript) {
|
|
$approved_transcript_manager->save_transcript(array(
|
|
'language' => $language,
|
|
'transcript' => $transcript
|
|
));
|
|
}
|
|
|
|
$transcript_options = new WDTSTranscriptOptions($info['post_id']);
|
|
$transcript_options->set_allow_transcripts(isset($info['allow_on_post']));
|
|
|
|
$queued_transcript_manager = new WDTSQueuedTranscript($info['post_id']);
|
|
$queued_transcripts = $queued_transcript_manager->get_transcripts();
|
|
|
|
if (is_array($queued_transcriptions)) {
|
|
$transcripts_to_delete = array();
|
|
|
|
foreach ($queued_transcriptions as $transcription) { $transcripts_to_delete[$transcription->id] = true; }
|
|
if (isset($info['queue'])) {
|
|
foreach ($info['queue'] as $id => $keep) { unset($transcripts_to_delete[$id]); }
|
|
}
|
|
|
|
foreach (array_keys($transcripts_to_delete) as $id) { $queued_transcripts->delete_transcript($id); }
|
|
}
|
|
|
|
$updated = __('Transcripts updated.', 'what-did-they-say');
|
|
}
|
|
return $updated;
|
|
}
|
|
|
|
function handle_update_approve_transcript($info) {
|
|
$this->is_ajax = true;
|
|
|
|
if (current_user_can('approve_transcriptions')) {
|
|
$approved_transcript_manager = new WDTSApprovedTranscript($info['post_id']);
|
|
$queued_transcript_manager = new WDTSQueuedTranscript($info['post_id']);
|
|
|
|
if (($transcript = $queued_transcript_manager->delete_transcript_by_key($info['key'])) !== false) {
|
|
$approved_transcript_manager->save_transcript($transcript);
|
|
return;
|
|
}
|
|
header('HTTP/1.1 500 Internal Server Error');
|
|
return;
|
|
}
|
|
header('HTTP/1.1 401 Unauthorized');
|
|
}
|
|
|
|
function handle_update_delete_transcript($info) {
|
|
$this->is_ajax = true;
|
|
|
|
if (current_user_can('approve_transcriptions')) {
|
|
$queued_transcript_manager = new WDTSQueuedTranscript($info['post_id']);
|
|
|
|
if (($transcript = $queued_transcript_manager->delete_transcript_by_key($info['key'])) !== false) {
|
|
return;
|
|
}
|
|
header('HTTP/1.1 500 Internal Server Error');
|
|
return;
|
|
}
|
|
header('HTTP/1.1 401 Unauthorized');
|
|
}
|
|
|
|
function handle_update_default_styles($info) {
|
|
$updated = false;
|
|
if (current_user_can('edit_themes')) {
|
|
$options = get_option('what-did-they-say-options');
|
|
|
|
$options['load_default_styles'] = isset($info['default_styles']);
|
|
|
|
update_option('what-did-they-say-options', $options);
|
|
$updated = __('Default styles option updated.', 'what-did-they-say');
|
|
}
|
|
return $updated;
|
|
}
|
|
|
|
/**
|
|
* 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) {
|
|
$updated = false;
|
|
if (current_user_can('change_languages')) {
|
|
$language_options = new WDTSLanguageOptions();
|
|
|
|
switch ($info['action']) {
|
|
case "delete":
|
|
if ($result = $language_options->delete_language($info['code'])) {
|
|
$updated = sprintf(__('%s deleted.', 'what-did-they-say'), $result['name']);
|
|
} else {
|
|
$updated = sprintf(__('Language not deleted!', 'what-did-they-say'));
|
|
}
|
|
break;
|
|
case "add":
|
|
$this->read_language_file();
|
|
if (isset($this->all_languages[$info['code']])) {
|
|
if ($language_options->add_language($info['code'], array('name' => $this->all_languages[$info['code']]))) {
|
|
$updated = sprintf(__('%s added.', 'what-did-they-say'), $this->all_languages[$info['code']]);
|
|
} else {
|
|
$updated = sprintf(__('Language not added!', 'what-did-they-say'));
|
|
}
|
|
}
|
|
break;
|
|
case "default":
|
|
if ($language_options->set_default_language($info['code'])) {
|
|
$updated = sprintf(__('%s set as default.', 'what-did-they-say'), $language_options->get_language_name($info['code']));
|
|
} else {
|
|
$updated = sprintf(__('Language not set as default!', 'what-did-they-say'));
|
|
}
|
|
break;
|
|
case "rename":
|
|
$original_language_name = $language_options->get_language_name($info['code']);
|
|
if ($language_options->rename_language($info['code'], $info['name'])) {
|
|
$updated = sprintf(__('%1$s renamed to %2$s.', 'what-did-they-say'), $original_language_name, $info['name']);
|
|
} else {
|
|
$updated = sprintf(__('Language not renamed!', 'what-did-they-say'));
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
return $updated;
|
|
}
|
|
|
|
/**
|
|
* 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) {
|
|
$updated = false;
|
|
if (current_user_can('edit_users')) {
|
|
$options = get_option('what-did-they-say-options');
|
|
if (isset($info['capabilities'])) {
|
|
foreach (array_keys($this->default_options['capabilities']) as $capability) {
|
|
if (isset($info['capabilities'][$capability])) {
|
|
$options['capabilities'][$capability] = $info['capabilities'][$capability];
|
|
}
|
|
}
|
|
$updated = __('User capabilities updated.', 'what-did-they-say');
|
|
}
|
|
if ($updated !== false) {
|
|
update_option('what-did-they-say-options', $options);
|
|
}
|
|
}
|
|
return $updated;
|
|
}
|
|
|
|
/**
|
|
* 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')) {
|
|
delete_option('what-did-they-say-options');
|
|
$this->install();
|
|
$updated = __('<strong>What Did They Say?!?</strong> options reset.', 'what-did-they-say');
|
|
}
|
|
return $updated;
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
function read_language_file() {
|
|
if (file_exists($this->language_file)) {
|
|
foreach (file($this->language_file) as $language) {
|
|
$language = trim($language);
|
|
list($code, $date_added, $name) = explode("\t", $language);
|
|
$this->all_languages[$code] = $name;
|
|
}
|
|
}
|
|
return $this->all_languages;
|
|
}
|
|
|
|
/**
|
|
* Handle plugin installation.
|
|
*/
|
|
function install() {
|
|
$this->read_language_file();
|
|
$options = get_option('what-did-they-say-options');
|
|
if (empty($options)) {
|
|
$this->default_options['languages'] = $this->build_default_languages();
|
|
ksort($this->default_options['languages']);
|
|
update_option('what-did-they-say-options', $this->default_options);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* From $this->default_options, fill in the language details from the language file.
|
|
* @return array The language info will all details filled in.
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Handle admin_menu action.
|
|
*/
|
|
function admin_menu() {
|
|
global $plugin_page;
|
|
|
|
if (current_user_can('submit_transcriptions')) {
|
|
add_options_page(
|
|
__('What Did They Say?!?', 'what-did-they-say'),
|
|
__('What Did They Say?!?', 'what-did-they-say'),
|
|
'manage_options',
|
|
'manage-wdts',
|
|
array(&$this, 'manage_admin')
|
|
);
|
|
|
|
if ($plugin_page == "manage-wdts") { $this->read_language_file(); }
|
|
|
|
wp_enqueue_script('scriptaculous-effects');
|
|
}
|
|
|
|
if (current_user_can('approve_transcriptions')) {
|
|
add_meta_box(
|
|
'manage-transcriptions',
|
|
__('Manage Transcriptions', 'what-did-they-say'),
|
|
array(&$this, 'manage_transcriptions_meta_box'),
|
|
'post',
|
|
'normal',
|
|
'low'
|
|
);
|
|
|
|
wp_enqueue_script('scriptaculous-effects');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the admin page.
|
|
*/
|
|
function manage_admin() {
|
|
$options = get_option('what-did-they-say-options');
|
|
|
|
$nonce = wp_create_nonce('what-did-they-say');
|
|
include(dirname(__FILE__) . '/admin.inc');
|
|
}
|
|
|
|
/**
|
|
* Show the Manage Transcriptions meta box.
|
|
*/
|
|
function manage_transcriptions_meta_box() {
|
|
global $post;
|
|
|
|
$options = get_option('what-did-they-say-options');
|
|
|
|
foreach (array('Approved', 'Queued') as $name) {
|
|
$var_name = strtolower($name);
|
|
$class_name = "WDTS${name}Transcript";
|
|
${"${var_name}_transcript_manager"} = new $class_name($post->ID);
|
|
${"${var_name}_transcripts"} = ${"${var_name}_transcript_manager"}->get_transcripts();
|
|
}
|
|
|
|
$transcript_options = new WDTSTranscriptOptions($post->ID);
|
|
|
|
$nonce = wp_create_nonce('what-did-they-say');
|
|
include(dirname(__FILE__) . '/meta-box.inc');
|
|
}
|
|
}
|
|
|
|
?>
|