languages and admin cleanup

This commit is contained in:
John Bintz 2009-09-10 21:00:10 -04:00
parent 60f5ace20b
commit ca75c5cf27
9 changed files with 135 additions and 89 deletions

View File

@ -17,6 +17,30 @@ class WDTSLanguageOptions {
return $language; return $language;
} }
function set_default_language($language) {
$options = get_option($this->key);
if (isset($options['languages'][$language])) {
$ok = true;
if (isset($options['languages'][$language]['default'])) {
$ok = !$options['languages'][$language]['default'];
}
if ($ok) {
$updated_languages = array();
foreach ($options['languages'] as $code => $info) {
unset($info['default']);
if ($code == $language) { $info['default'] = true; }
$updated_languages[$code] = $info;
}
$options['languages'] = $updated_languages;
update_option($this->key, $options);
return true;
}
}
return false;
}
/** /**
* Get the name of a language from the language code. * Get the name of a language from the language code.
* @param string $language The language code to search for. * @param string $language The language code to search for.
@ -49,6 +73,7 @@ class WDTSLanguageOptions {
if (isset($options['languages'][$code_to_delete])) { if (isset($options['languages'][$code_to_delete])) {
$did_delete = $options['languages'][$code_to_delete]; $did_delete = $options['languages'][$code_to_delete];
unset($options['languages'][$code_to_delete]); unset($options['languages'][$code_to_delete]);
ksort($options['languages']);
} }
update_option($this->key, $options); update_option($this->key, $options);
@ -63,11 +88,12 @@ class WDTSLanguageOptions {
if (!isset($options['languages'][$code])) { if (!isset($options['languages'][$code])) {
if (!empty($info['name'])) { if (!empty($info['name'])) {
$options['languages'][$code] = $info; $options['languages'][$code] = $info;
ksort($options['languages']);
$result = true; $result = true;
} }
} }
} }
update_option($this->key, $options); update_option($this->key, $options);
return $result; return $result;
} }
@ -86,6 +112,7 @@ class WDTSLanguageOptions {
$new_languages[$code] = $info; $new_languages[$code] = $info;
} }
$options['languages'] = $new_languages; $options['languages'] = $new_languages;
ksort($options['languages']);
update_option($this->key, $options); update_option($this->key, $options);
} }

View File

@ -2,7 +2,7 @@
require_once('WDTSTranscript.php'); require_once('WDTSTranscript.php');
class WDTSQueuedTranscript extends WDTSTranscript { var $key = "queued_transcripts"; } class WDTSQueuedTranscript extends WDTSTranscriptManager { var $key = "queued_transcripts"; }
class WDTSApprovedTranscript extends WDTSTranscript { var $key = "approved_transcripts"; } class WDTSApprovedTranscript extends WDTSTranscriptManager { var $key = "approved_transcripts"; }
?> ?>

View File

@ -14,20 +14,27 @@ class WDTSTranscriptOptions {
* @param int $post_id The post ID to affect. * @param int $post_id The post ID to affect.
* @param boolean $allow True if the post can accept new queued transcriptions. * @param boolean $allow True if the post can accept new queued transcriptions.
*/ */
function set_allow_transcripts_for_post($post_id, $allow = true) { function set_allow_transcripts($allow = true) { $this->_update_option('allow_transcripts', $allow); }
$current_transcripts = get_post_meta($post_id, "transcript_options", true);
if (!is_array
$current_transcripts['_allow'] = $allow;
update_post_meta($post_id, "provided_transcripts", $current_transcripts);
}
/** /**
* See if the indicated post is accepting new transcripts. * See if the indicated post is accepting new transcripts.
* @return boolean True if the post is acceptin new transcripts. * @return boolean True if the post is acceptin new transcripts.
*/ */
function get_allow_transcripts_for_post($post_id) { function get_allow_transcripts() {
$current_transcripts = get_post_meta($post_id, "provided_transcripts", true); $options = $this->_get_transcript_options();
return $current_transcripts['_allow']; return issset($options['allow_transcripts']) ? $options['allow_transcripts'] : false;
}
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;
}
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);
} }
} }

View File

@ -157,11 +157,12 @@ class WhatDidTheySayAdmin {
* @param array $info The part of the $_POST array for What Did They Say?!? * @param array $info The part of the $_POST array for What Did They Say?!?
*/ */
function handle_update($info) { function handle_update($info) {
foreach (get_class_methods($this) as $method) { if (isset($info['module'])) {
if (strpos($method, "handle_update_") === 0) { $method_name = "handle_update_" . str_replace("-", "_", $info['module']);
$result = $this->{$method}($info); if (method_exists($this, $method_name)) {
$result = $this->{$method_name}($info);
if (!empty($result)) { $this->notices[] = $result; } if (!empty($result)) { $this->notices[] = $result; }
} }
} }
} }
@ -173,9 +174,9 @@ class WhatDidTheySayAdmin {
function handle_update_queue_transcript($info) { function handle_update_queue_transcript($info) {
$updated = false; $updated = false;
if (current_user_can('submit_transcriptions')) { if (current_user_can('submit_transcriptions')) {
$queued_transcript = new WDTSTranscriptOptions($info['post_id'])); $queued_transcript = new WDTSTranscriptOptions($info['post_id']);
if ($this->what_did_they_say->get_allow_transcripts_for_post() { if ($this->what_did_they_say->get_allow_transcripts_for_post()) {
switch ($info['action']) { switch ($info['action']) {
case 'submit_queued_transcript': case 'submit_queued_transcript':
$result = $this->what_did_they_say->add_queued_transcription_to_post($info['post_id'], $info); $result = $this->what_did_they_say->add_queued_transcription_to_post($info['post_id'], $info);
@ -248,35 +249,33 @@ class WhatDidTheySayAdmin {
case "delete": case "delete":
if ($result = $language_options->delete_language($info['code'])) { if ($result = $language_options->delete_language($info['code'])) {
$updated = sprintf(__('%s deleted.', 'what-did-they-say'), $result['name']); $updated = sprintf(__('%s deleted.', 'what-did-they-say'), $result['name']);
} else {
$updated = sprintf(__('Language not deleted!', 'what-did-they-say'));
} }
break; break;
case "add": case "add":
$this->read_language_file(); $this->read_language_file();
if (isset($this->all_languages[$info['code']])) { 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']]);
$options['languages'][$info['code']] = array('name' => $this->all_languages[$info['code']]); } else {
$updated = sprintf(__('%s added.', 'what-did-they-say'), $this->all_languages[$info['code']]); $updated = sprintf(__('Language not added!', 'what-did-they-say'));
}
} }
break; break;
case "default": case "default":
if (isset($options['languages'][$info['code']])) { if ($language_options->set_default_language($info['code'])) {
foreach ($options['languages'] as $code => $lang_info) { $updated = sprintf(__('%s set as default.', 'what-did-they-say'), $language_options->get_language_name($info['code']));
if ($code == $info['code']) { } else {
$options['languages'][$code]['default'] = true; $updated = sprintf(__('Language not set as default!', 'what-did-they-say'));
$updated = sprintf(__('%s set as default.', 'what-did-they-say'), $lang_info['name']);
} else {
unset($options['languages'][$code]['default']);
}
}
} }
break; break;
case "rename": case "rename":
if (isset($options['languages'][$info['code']])) { $original_language_name = $language_options->get_language_name($info['code']);
if (!empty($info['name'])) { if ($language_options->rename_language($info['code'], $info['name'])) {
$updated = sprintf(__('%1$s renamed to %2$s.', 'what-did-they-say'), $options['languages'][$info['code']]['name'], $info['name']); $updated = sprintf(__('%1$s renamed to %2$s.', 'what-did-they-say'), $original_language_name, $info['name']);
$options['languages'][$info['code']]['name'] = $info['name']; } else {
} $updated = sprintf(__('Language not renamed!', 'what-did-they-say'));
} }
break; break;
} }

View File

@ -37,8 +37,7 @@
<table class="widefat fixed"> <table class="widefat fixed">
<tr class="thead"> <tr class="thead">
<th scope="col" class="manage-col"><?php _e('Language', 'what-did-they-say') ?></th> <th scope="col" class="manage-col" width="35%"><?php _e('Language', 'what-did-they-say') ?></th>
<th scope="col" class="manage-col"><?php _e('Default?', 'what-did-they-say') ?></th>
<th scope="col" class="manage-col"><?php _e('Rename?', 'what-did-they-say') ?></th> <th scope="col" class="manage-col"><?php _e('Rename?', 'what-did-they-say') ?></th>
<th scope="col" class="manage-col"><?php _e('Make default?', 'what-did-they-say') ?></th> <th scope="col" class="manage-col"><?php _e('Make default?', 'what-did-they-say') ?></th>
<th scope="col" class="manage-col"><?php _e('Delete?', 'what-did-they-say') ?></th> <th scope="col" class="manage-col"><?php _e('Delete?', 'what-did-they-say') ?></th>
@ -50,17 +49,16 @@
<tr> <tr>
<th scope="row"> <th scope="row">
<span><?php echo $name ?></span> <span><?php echo $name ?></span>
<?php if ($default) { _e('(default)', 'what-did-they-say'); } ?>
<form method="post" style="display: none"> <form method="post" style="display: none">
<input type="hidden" name="wdts[_nonce]" value="<?php echo $nonce ?>" /> <input type="hidden" name="wdts[_nonce]" value="<?php echo $nonce ?>" />
<input type="hidden" name="wdts[code]" value="<?php echo $code ?>" /> <input type="hidden" name="wdts[code]" value="<?php echo $code ?>" />
<input type="hidden" name="wdts[action]" value="rename" /> <input type="hidden" name="wdts[action]" value="rename" />
<input type="hidden" name="wdts[module]" value="languages" />
<input type="text" name="wdts[name]" value="<?php echo $name ?>" style="width: 50%" /> <input type="text" name="wdts[name]" value="<?php echo $name ?>" style="width: 50%" />
<input type="submit" class="button" value="Rename" /> <input type="submit" class="button" value="Rename" />
</form> </form>
</th> </th>
<td>
<?php if ($default) { _e('(yes)', 'what-did-they-say'); } ?>
</td>
<td style="vertical-align: inherit"> <td style="vertical-align: inherit">
<a class="rename button" href="#"><?php _e('Rename', 'what-did-they-say') ?></a> <a class="rename button" href="#"><?php _e('Rename', 'what-did-they-say') ?></a>
</td> </td>
@ -69,7 +67,12 @@
<input type="hidden" name="wdts[_nonce]" value="<?php echo $nonce ?>" /> <input type="hidden" name="wdts[_nonce]" value="<?php echo $nonce ?>" />
<input type="hidden" name="wdts[code]" value="<?php echo $code ?>" /> <input type="hidden" name="wdts[code]" value="<?php echo $code ?>" />
<input type="hidden" name="wdts[action]" value="default" /> <input type="hidden" name="wdts[action]" value="default" />
<input type="submit" class="button" value="<?php _e('Default', 'what-did-they-say') ?>" /> <input type="hidden" name="wdts[module]" value="languages" />
<input
<?php echo ($default ? 'disabled="disabled"' : '') ?>
type="submit"
class="button"
value="<?php _e('Default', 'what-did-they-say') ?>" />
</form> </form>
</td> </td>
<td> <td>
@ -77,6 +80,7 @@
<input type="hidden" name="wdts[_nonce]" value="<?php echo $nonce ?>" /> <input type="hidden" name="wdts[_nonce]" value="<?php echo $nonce ?>" />
<input type="hidden" name="wdts[code]" value="<?php echo $code ?>" /> <input type="hidden" name="wdts[code]" value="<?php echo $code ?>" />
<input type="hidden" name="wdts[action]" value="delete" /> <input type="hidden" name="wdts[action]" value="delete" />
<input type="hidden" name="wdts[module]" value="languages" />
<input type="submit" class="button" value="<?php _e('Delete', 'what-did-they-say') ?>" /> <input type="submit" class="button" value="<?php _e('Delete', 'what-did-they-say') ?>" />
</form> </form>
</td> </td>
@ -84,10 +88,11 @@
<?php } ?> <?php } ?>
<tr> <tr>
<th scope="row"><?php _e('Add new:', 'what-did-they-say') ?></th> <th scope="row"><?php _e('Add new:', 'what-did-they-say') ?></th>
<td colspan="4"> <td colspan="3">
<form method="post"> <form method="post">
<input type="hidden" name="wdts[_nonce]" value="<?php echo $nonce ?>" /> <input type="hidden" name="wdts[_nonce]" value="<?php echo $nonce ?>" />
<input type="hidden" name="wdts[action]" value="add" /> <input type="hidden" name="wdts[action]" value="add" />
<input type="hidden" name="wdts[module]" value="languages" />
<select name="wdts[code]"> <select name="wdts[code]">
<option value="">-- select --</option> <option value="">-- select --</option>
<?php foreach ($this->all_languages as $code => $language) { ?> <?php foreach ($this->all_languages as $code => $language) { ?>

View File

@ -6,19 +6,44 @@ require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php');
class WDTSLanguageOptionsTest extends PHPUnit_Framework_TestCase { class WDTSLanguageOptionsTest extends PHPUnit_Framework_TestCase {
function setUp() { function setUp() {
_reset_wp();
$this->l = new WDTSLanguageOptions(); $this->l = new WDTSLanguageOptions();
} }
function testGetDefaultLanguage() { function testGetDefaultLanguage() {
update_option($this->l->key, array( update_option($this->l->key, array(
'languages' => array( 'languages' => array(
'en' => array('default' => false), 'en' => array(),
'de' => array('default' => true) 'de' => array('default' => true)
) )
)); ));
$this->assertEquals('de', $this->l->get_default_language()); $this->assertEquals('de', $this->l->get_default_language());
} }
function providerTestSetDefaultLanguage() {
return array(
array('en', true, array('en' => array('default' => true), 'de' => array())),
array('de', false, array('en' => array(), 'de' => array('default' => true))),
array('fr', false, array('en' => array(), 'de' => array('default' => true))),
);
}
/**
* @dataProvider providerTestSetDefaultLanguage
*/
function testSetDefaultLanguage($set_default, $expected_result, $expected_language_info) {
update_option($this->l->key, array(
'languages' => array(
'en' => array(),
'de' => array('default' => true)
)
));
$this->assertEquals($expected_result, $this->l->set_default_language($set_default));
$this->assertEquals($expected_language_info, $this->l->get_languages());
}
function testGetLanguageName() { function testGetLanguageName() {
update_option($this->l->key, array( update_option($this->l->key, array(

View File

@ -0,0 +1,28 @@
<?php
require_once('PHPUnit/Framework.php');
require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php');
require_once(dirname(__FILE__) . '/../classes/WDTSTranscriptOptions.php');
class WDTSTranscriptOptionsTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
wp_insert_post((object)array('ID' => 1));
$this->to = new WDTSTranscriptOptions(1);
}
function testUpdateOption() {
update_post_meta(1, "transcript_options", array('test' => false));
$this->to->_update_option('test', true);
$this->assertEquals(
array('test' => true),
get_post_meta(1, 'transcript_options', true)
);
}
}
?>

View File

@ -16,50 +16,6 @@ class WhatDidTheySayAdminTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(count($admin->read_language_file()) > 0); $this->assertTrue(count($admin->read_language_file()) > 0);
} }
function providerTestHandleUpdateLanguages() {
return array(
array(
array('en' => array('name' => 'English'), 'de' => array('name' => 'German')),
array('code' => 'en', 'action' => 'delete'),
array('de' => array('name' => 'German'))
),
array(
array('de' => array('name' => 'German')),
array('code' => 'en', 'action' => 'add'),
array('en' => array('name' => 'English'), 'de' => array('name' => 'German')),
),
array(
array('en' => array('name' => 'English', 'default' => true), 'de' => array('name' => 'German')),
array('code' => 'de', 'action' => 'default'),
array('en' => array('name' => 'English'), 'de' => array('name' => 'German', 'default' => true)),
),
array(
array('en' => array('name' => 'English'), 'de' => array('name' => 'German')),
array('code' => 'de', 'action' => 'rename', 'name' => 'Deutsch'),
array('en' => array('name' => 'English'), 'de' => array('name' => 'Deutsch')),
),
);
}
/**
* @dataProvider providerTestHandleUpdateLanguages
*/
function testHandleUpdateLanguages($original_options, $form_submission, $expected_results) {
$admin = new WhatDidTheySayAdmin();
$admin->all_languages = array(
'en' => 'English',
'de' => 'German',
'fr' => 'French'
);
update_option('what-did-they-say-options', array('languages' => $original_options));
$admin->handle_update_languages($form_submission);
$options = get_option('what-did-they-say-options');
$this->assertEquals($expected_results, $options['languages']);
}
function testBuildFullDefaultLanguageInfo() { function testBuildFullDefaultLanguageInfo() {
$admin = new WhatDidTheySayAdmin(); $admin = new WhatDidTheySayAdmin();
$admin->all_languages = array( $admin->all_languages = array(

View File

@ -26,7 +26,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
foreach (glob(dirname(__FILE__) . '/classes/*.php') as $file) { require_once($file); } foreach (glob(dirname(__FILE__) . '/classes/*.php') as $file) { require_once($file); }
$what_did_they_say =& new WhatDidTheySay();
$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')); add_action('init', array(&$what_did_they_say_admin, 'init'));