diff --git a/classes/WDTSLanguageOptions.php b/classes/WDTSLanguageOptions.php index c60410c..ff1a3ce 100644 --- a/classes/WDTSLanguageOptions.php +++ b/classes/WDTSLanguageOptions.php @@ -12,7 +12,9 @@ class WDTSLanguageOptions { $options = get_option($this->key); foreach ($options['languages'] as $code => $info) { if (is_null($language)) { $language = $code; } - if ($info['default']) { $language = $code; break; } + if (isset($info['default'])) { + if ($info['default']) { $language = $code; break; } + } } return $language; } diff --git a/classes/WDTSTranscript.php b/classes/WDTSTranscript.php index 006e7fd..73cd2e8 100644 --- a/classes/WDTSTranscript.php +++ b/classes/WDTSTranscript.php @@ -3,6 +3,7 @@ class WDTSTranscriptManager { var $key = null; var $post_id = null; + var $allow_multiple = false; function __construct($post_id = null) { if (is_numeric($post_id)) { $this->post_id = $post_id; } @@ -36,36 +37,63 @@ class WDTSTranscriptManager { if (!empty($user)) { $transcript_info = (array)$transcript_info; $transcript_info['user_id'] = $user->ID; + unset($transcript_info['key']); - if (($transcripts = $this->_get_transcripts_metadata()) !== false) { - $new_transcripts = array(); - $was_added = false; + if (($transcripts = $this->_get_transcripts_metadata()) !== false) { + $max_key = 0; foreach ($transcripts as $transcript) { - if ($transcript['language'] == $transcript_info['language']) { - $was_added = true; - $new_transcripts[] = $transcript_info; - } else { - $new_transcripts[] = $transcript; - } + $max_key = max($max_key, $transcript['key']) + 1; } - if (!$was_added) { $new_transcripts[] = $transcript_info; } + $transcript_info['key'] = $max_key; - return update_post_meta($this->post_id, $this->key, $new_transcripts); + if ($this->allow_multiple) { + $transcripts[] = $transcript_info; + } else { + $new_transcripts = array(); + $was_added = false; + foreach ($transcripts as $transcript) { + if ($transcript['language'] == $transcript_info['language']) { + $was_added = true; + $transcript_info['key']--; + $new_transcripts[] = $transcript_info; + } else { + $new_transcripts[] = $transcript; + } + } + if (!$was_added) { $new_transcripts[] = $transcript_info; } + $transcripts = $new_transcripts; + } + + return update_post_meta($this->post_id, $this->key, $transcripts); } } return false; } function delete_transcript($language = null) { + return $this->_delete_transcript_by_field('language', $language); + } + + function delete_transcript_by_key($key = null) { + return $this->_delete_transcript_by_field('key', $key); + } + + function _delete_transcript_by_field($field, $value) { if (($transcripts = $this->_get_transcripts_metadata()) !== false) { $new_transcripts = array(); + $deleted_transcript = false; foreach ($transcripts as $transcript) { - if ($transcript['language'] != $language) { $new_transcripts[] = $transcript; } + if ($transcript[$field] != $value) { + $new_transcripts[] = $transcript; + } else { + $deleted_transcript = $transcript; + } } - return update_post_meta($this->post_id, $this->key, $new_transcripts); + update_post_meta($this->post_id, $this->key, $new_transcripts); + return $deleted_transcript; } - return false; + return false; } function get_transcripts() { diff --git a/classes/WDTSTranscriptClasses.php b/classes/WDTSTranscriptClasses.php index 1b16444..177c64a 100644 --- a/classes/WDTSTranscriptClasses.php +++ b/classes/WDTSTranscriptClasses.php @@ -2,7 +2,13 @@ require_once('WDTSTranscript.php'); -class WDTSQueuedTranscript extends WDTSTranscriptManager { var $key = "queued_transcripts"; } -class WDTSApprovedTranscript extends WDTSTranscriptManager { var $key = "approved_transcripts"; } +class WDTSQueuedTranscript extends WDTSTranscriptManager { + var $key = "queued_transcripts"; + var $allow_multiple = true; +} + +class WDTSApprovedTranscript extends WDTSTranscriptManager { + var $key = "approved_transcripts"; +} ?> \ No newline at end of file diff --git a/classes/WDTSTranscriptOptions.php b/classes/WDTSTranscriptOptions.php index eb9ca55..8aa7b11 100644 --- a/classes/WDTSTranscriptOptions.php +++ b/classes/WDTSTranscriptOptions.php @@ -26,15 +26,15 @@ class WDTSTranscriptOptions { } function _get_transcript_options() { - $current_transcripts = get_post_meta($this->post_id, "transcript_options", true); - if (!is_array($current_transcripts)) { $current_transcripts = array(); } - return $current_transcripts; + $current_options = get_post_meta($this->post_id, "transcript_options", true); + if (!is_array($current_options)) { $current_options = array(); } + return $current_options; } function _update_option($option, $value) { $current_options = $this->_get_transcript_options(); - $current_transcripts[$option] = $value; - update_post_meta($this->post_id, "transcript_options", $current_transcripts); + $current_options[$option] = $value; + update_post_meta($this->post_id, "transcript_options", $current_options); } } diff --git a/classes/WhatDidTheySayAdmin.php b/classes/WhatDidTheySayAdmin.php index 285a3d7..9226b1b 100644 --- a/classes/WhatDidTheySayAdmin.php +++ b/classes/WhatDidTheySayAdmin.php @@ -25,6 +25,8 @@ class WhatDidTheySayAdmin { 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. @@ -45,7 +47,6 @@ class WhatDidTheySayAdmin { add_action('admin_menu', array(&$this, 'admin_menu')); add_action('admin_notices', array(&$this, 'admin_notices')); - add_action('admin_init', array(&$this, 'admin_init')); wp_enqueue_script('prototype'); @@ -53,24 +54,27 @@ class WhatDidTheySayAdmin { add_filter('the_media_transcript', array(&$this, 'the_media_transcript')); add_filter('the_language_name', array(&$this, 'the_language_name')); - add_filter('wp_footer', array(&$this, 'wp_footer')); + add_filter('template_redirect', array(&$this, 'template_redirect')); 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); } } } } $this->read_language_file(); + + if (current_user_can('submit_transcriptions')) { + wp_enqueue_script('scriptaculous-effects'); + } } - /** - * Handle admin_init action. - */ - function admin_init() { - wp_enqueue_script('scriptaculous-effects'); + function template_redirect() { + wp_enqueue_script('toggle-transcript', plugin_dir_url(dirname(__FILE__)) . 'toggle-transcript.js', array('prototype'), false, true); } /** @@ -90,27 +94,6 @@ class WhatDidTheySayAdmin { function the_language_name($language) { return '

' . $language . '

'; } - - /** - * Handle the wp_footer action. - */ - function wp_footer() { ?> - - id] = true; } - if (isset($post_transcript_info['queue'])) { - foreach ($post_transcript_info['queue'] as $id => $keep) { unset($transcripts_to_delete[$id]); } + 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'); - break; } 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'); + } + /** * Handle updates to languages. * @param array $info The part of the $_POST array for What Did They Say?!? @@ -329,7 +343,7 @@ class WhatDidTheySayAdmin { if (file_exists($this->language_file)) { foreach (file($this->language_file) as $language) { $language = trim($language); - list($code, $date_added, $name, $additional) = explode("\t", $language); + list($code, $date_added, $name) = explode("\t", $language); $this->all_languages[$code] = $name; } } @@ -425,6 +439,8 @@ class WhatDidTheySayAdmin { ${"${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'); } diff --git a/classes/meta-box.inc b/classes/meta-box.inc index 46f55a6..32c0e81 100644 --- a/classes/meta-box.inc +++ b/classes/meta-box.inc @@ -3,28 +3,28 @@

user_id); + $user = get_userdata($transcript['user_id']); if (!empty($user)) { ?>
- - + +

%s in %s:', 'what-did-they-say'), $user->display_name, - $this->what_did_they_say->get_language_name($transcript->language) + $language_options->get_language_name($transcript['language']) ) ?>

-
transcript ?>
- +
+
Approve Delete @@ -44,54 +44,23 @@ - +

+ \ No newline at end of file diff --git a/edit-transcripts.js b/edit-transcripts.js new file mode 100644 index 0000000..625317a --- /dev/null +++ b/edit-transcripts.js @@ -0,0 +1,91 @@ +function switch_transcript() { + $$('.edit-transcript').each(function(t) { + (t.id == "wdts-transcripts-" + $F('wdts-language')) ? t.show() : t.hide(); + }); +} +switch_transcript(); +Event.observe(language_selector, 'change', switch_transcript); + +$$('.approve-transcript').each(function(b) { + Event.observe(b, 'click', function(e) { + Event.stop(e); + var lang = b.parentNode.parentNode.select("input[name*=[language]]").shift(); + var post_id = b.parentNode.parentNode.parentNode.select("input[name*=[post_id]]").shift(); + var key = b.parentNode.parentNode.select("input[name*=[key]]").shift(); + if (lang && post_id && key) { + lang = lang.value; + post_id = post_id.value; + key = key.value; + var editor = $('wdts-transcripts-' + lang); + + var raw_transcript = b.parentNode.parentNode.select(".queued-transcription-raw").shift(); + if (raw_transcript && editor) { + var ok = true; + if (editor.value.match(/[^ ]/)) { + ok = confirm(messages.overwrite); + } + if (ok) { + editor.value = raw_transcript.innerHTML; + var p = b.parentNode.parentNode; + + new Ajax.Request( + ajax_url, { + 'method': 'post', + 'parameters': { + 'wdts[_nonce]': nonce, + 'wdts[module]': 'approve-transcript', + 'wdts[key]': key, + 'wdts[post_id]': post_id + }, + 'onSuccess': function() { + p.update(messages.approved); + new Effect.Highlight(p); + var i,il; + + for (i = 0, il = language_selector.options.length; i < il; ++i) { + if (language_selector.options[i].value == lang) { + language_selector.selectedIndex = i; + switch_transcript(); + break; + } + } + } + } + ); + } + } + } + }); +}); + +$$('.delete-transcript').each(function(b) { + Event.observe(b, 'click', function(e) { + Event.stop(e); + + if (confirm(messages.delete)) { + var post_id = b.parentNode.parentNode.parentNode.select("input[name*=[post_id]]").shift(); + var key = b.parentNode.parentNode.select("input[name*=[key]]").shift(); + if (post_id && key) { + post_id = post_id.value; + key = key.value; + var p = b.parentNode.parentNode; + + new Ajax.Request( + ajax_url, { + 'method': 'post', + 'parameters': { + 'wdts[_nonce]': nonce, + 'wdts[module]': 'delete-transcript', + 'wdts[key]': key, + 'wdts[post_id]': post_id + }, + 'onSuccess': function() { + p.update(messages.deleted); + new Effect.Highlight(p); + } + } + ); + } + } + }); +}); \ No newline at end of file diff --git a/test/WDTSTranscriptTest.php b/test/WDTSTranscriptTest.php index 22c9dfb..fc4618e 100644 --- a/test/WDTSTranscriptTest.php +++ b/test/WDTSTranscriptTest.php @@ -17,6 +17,8 @@ class WDTSTranscriptTest extends PHPUnit_Framework_TestCase { } function testSaveTranscript() { + $this->w->allow_multiple = false; + $this->w->save_transcript(array( 'language' => 'en', 'transcript' => 'this is a transcript' @@ -27,7 +29,8 @@ class WDTSTranscriptTest extends PHPUnit_Framework_TestCase { array( 'language' => 'en', 'transcript' => 'this is a transcript', - 'user_id' => 1 + 'user_id' => 1, + 'key' => 0 ) ), get_post_meta(1, $this->w->key, true) @@ -43,7 +46,8 @@ class WDTSTranscriptTest extends PHPUnit_Framework_TestCase { array( 'language' => 'en', 'transcript' => 'this is another transcript', - 'user_id' => 1 + 'user_id' => 1, + 'key' => 0 ) ), get_post_meta(1, $this->w->key, true) @@ -59,39 +63,101 @@ class WDTSTranscriptTest extends PHPUnit_Framework_TestCase { array( 'language' => 'en', 'transcript' => 'this is another transcript', - 'user_id' => 1 + 'user_id' => 1, + 'key' => 0 ), array( 'language' => 'fr', 'transcript' => "il s'agit d'une nouvelle transcription", - 'user_id' => 1 + 'user_id' => 1, + 'key' => 1 ), ), get_post_meta(1, $this->w->key, true) ); - } + + $this->w->allow_multiple = true; + + $this->w->save_transcript(array( + 'language' => 'en', + 'transcript' => 'this is yet another transcript' + )); + + $this->assertEquals( + array( + array( + 'language' => 'en', + 'transcript' => 'this is another transcript', + 'user_id' => 1, + 'key' => 0 + ), + array( + 'language' => 'fr', + 'transcript' => "il s'agit d'une nouvelle transcription", + 'user_id' => 1, + 'key' => 1 + ), + array( + 'language' => 'en', + 'transcript' => 'this is yet another transcript', + 'user_id' => 1, + 'key' => 2 + ), + ), + get_post_meta(1, $this->w->key, true) + ); + } function testDeleteTranscript() { - update_post_meta(1, $this->w->key, array( - array( - 'language' => 'en', - 'transcript' => 'this is another transcript', - 'user_id' => 1 - ), - array( - 'language' => 'fr', - 'transcript' => "il s'agit d'une nouvelle transcription", - 'user_id' => 1 - ), + $this->w->save_transcript(array( + 'language' => 'en', + 'transcript' => 'this is another transcript' )); + $this->w->save_transcript(array( + 'language' => 'fr', + 'transcript' => "il s'agit d'une nouvelle transcription", + )); + $this->w->delete_transcript('en'); $this->assertEquals(array( array( 'language' => 'fr', 'transcript' => "il s'agit d'une nouvelle transcription", - 'user_id' => 1 + 'user_id' => 1, + 'key' => 1 + ), + ), get_post_meta(1, $this->w->key, true)); + } + + function testDeleteTranscriptByKey() { + $this->w->save_transcript(array( + 'language' => 'en', + 'transcript' => 'this is another transcript' + )); + + $this->w->save_transcript(array( + 'language' => 'fr', + 'transcript' => "il s'agit d'une nouvelle transcription", + )); + + $this->assertEquals( + $this->w->delete_transcript_by_key(0), + array( + 'language' => 'en', + 'transcript' => "this is another transcript", + 'user_id' => 1, + 'key' => 0 + ) + ); + + $this->assertEquals(array( + array( + 'language' => 'fr', + 'transcript' => "il s'agit d'une nouvelle transcription", + 'user_id' => 1, + 'key' => 1 ), ), get_post_meta(1, $this->w->key, true)); } diff --git a/toggle-transcript.js b/toggle-transcript.js new file mode 100644 index 0000000..916a038 --- /dev/null +++ b/toggle-transcript.js @@ -0,0 +1,13 @@ +$$('.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) + } +}); diff --git a/what-did-they-say.php b/what-did-they-say.php index 7179c77..7af5100 100644 --- a/what-did-they-say.php +++ b/what-did-they-say.php @@ -26,11 +26,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA foreach (glob(dirname(__FILE__) . '/classes/*.php') as $file) { require_once($file); } -$what_did_they_say_admin =& new WhatDidTheySayAdmin(&$what_did_they_say); +$what_did_they_say_admin = new WhatDidTheySayAdmin(&$what_did_they_say); add_action('init', array(&$what_did_they_say_admin, 'init')); -register_activation_hook(__FILE__, array(&$what_did_they_say, 'install')); -register_activation_hook(__FILE__, array(&$what_did_they_say_admin, 'install')); // template tags // please, if you use any of these, wrap them in function_exists() so your site doesn't @@ -72,10 +70,10 @@ function the_media_transcript($language = null) { * @return string The name of the requested language. */ function get_the_language_name($language = null) { - global $what_did_they_say; - - if (is_null($language)) { $language = $what_did_they_say->get_default_language(); } - return $what_did_they_say->get_language_name($language); + $language_options = new WDTSLanguageOptions(); + + if (is_null($language)) { $language = $language_options->get_default_language(); } + return $language_options->get_language_name($language); } /** @@ -106,19 +104,21 @@ function transcripts_display($dropdown_message = null, $single_language_message $post_transcripts = $approved_transcripts->get_transcripts(); if (!empty($post_transcripts)) { - foreach ($post_transcripts as $code => $transcript) { - if (substr($code, 0, 1) != "_") { - $transcript = trim($transcript); - if (!empty($transcript)) { - $transcripts[$code] = $transcript; - } + foreach ($post_transcripts as $transcript) { + extract($transcript, EXTR_PREFIX_ALL, "transcript"); + $transcript_transcript = trim($transcript_transcript); + if (!empty($transcript_transcript)) { + $transcripts[$transcript_language] = $transcript_transcript; } } + $language_options = new WDTSLanguageOptions(); + if (count($transcripts) > 0) { - $default_language = $what_did_they_say->get_default_language(); + $default_language = $language_options->get_default_language(); $output[] = '
'; + if (count($transcripts) == 1) { list($code, $transcript) = each($transcripts); $output[] = apply_filters('the_language_name', get_the_language_name($code)); @@ -152,7 +152,7 @@ function transcripts_display($dropdown_message = null, $single_language_message * If you're allowing users to submit transcripts to the post transcript queue, use this tag in your Loop. */ function the_media_transcript_queue_editor() { - global $post, $what_did_they_say; + global $post; if (current_user_can('submit_transcriptions')) { $queued_transcripts_for_user = false; @@ -164,40 +164,50 @@ function the_media_transcript_queue_editor() { $queued_transcripts_for_user = $queued_transcripts->get_transcripts_for_user($user->ID); } - $transcript_options = new WDTSTranscriptOptions($post->ID); + $language_options = new WDTSLanguageOptions(); - if (is_array($queued_transcripts_for_user)) { - if (count($queued_transcripts_for_user) > 0) { ?> -
-

- -

get_language_name($transcript->language) ?>

-
- transcript ?> -
- -
- - - are_new_transcripts_allowed()) { ?> + $transcript_options = new WDTSTranscriptOptions($post->ID); + + $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(); + } + + $nonce = wp_create_nonce('what-did-they-say'); + + ?> + +

- - - -

-
- - + + +
+ + + are_new_transcripts_allowed()) { ?> +

+
+ + + +
+ + +