2009-08-12 23:51:23 +00:00
< ? php
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'
),
'only_allowed_users' => false ,
2009-08-15 19:38:12 +00:00
'users' => array (),
'capabilities' => array (
'submit_transcription' => 'administrator' ,
'approve_transcription' => 'administrator' ,
'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-13 17:12:10 +00:00
2009-08-12 23:51:23 +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
}
function init ( $what_did_they_say ) {
$this -> what_did_they_say = $what_did_they_say ;
2009-08-15 19:38:12 +00:00
$this -> capabilities = array (
'submit_transcription' => __ ( 'Submit transcriptions to a post' , 'what-did-they-say' ),
'approve_transcription' => __ ( 'Approve transcriptions to a post' , 'what-did-they-say' ),
'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-15 19:38:12 +00:00
if ( current_user_can ( 'edit_users' )) {
add_action ( 'edit_user_profile' , array ( & $this , 'edit_user_profile' ));
add_action ( 'show_user_profile' , array ( & $this , 'edit_user_profile' ));
}
2009-08-14 02:13:46 +00:00
wp_enqueue_script ( 'prototype' );
2009-08-12 23:51:23 +00:00
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' )) {
2009-08-14 23:29:06 +00:00
2009-08-14 17:45:50 +00:00
$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-13 23:32:42 +00:00
function _update_options ( $which , $value ) {
$options = get_option ( 'what-did-they-say-options' );
$options [ $which ] = $value ;
update_option ( 'what-did-they-say-options' , $options );
}
2009-08-14 17:45:50 +00:00
function admin_notices () {
if ( ! empty ( $this -> notices )) {
echo '<div class="updated fade">' ;
echo implode ( " <br /> " , $this -> notices );
echo '</div>' ;
}
}
function handle_update ( $info ) {
$result = $this -> handle_update_languages ( $info );
if ( ! empty ( $result )) {
$this -> notices [] = $result ;
}
}
2009-08-13 17:12:10 +00:00
function handle_update_languages ( $language_info ) {
2009-08-14 10:54:25 +00:00
$options = get_option ( 'what-did-they-say-options' );
2009-08-14 17:45:50 +00:00
$updated = false ;
2009-08-14 10:54:25 +00:00
switch ( $language_info [ 'action' ]) {
case " delete " :
2009-08-14 17:45:50 +00:00
$updated = sprintf ( __ ( '%s deleted.' , 'what-did-they-say' ), $options [ 'languages' ][ $language_info [ 'code' ]][ 'name' ]);
2009-08-14 10:54:25 +00:00
unset ( $options [ 'languages' ][ $language_info [ 'code' ]]);
break ;
case " add " :
2009-08-14 23:49:03 +00:00
$this -> read_language_file ();
2009-08-14 10:54:25 +00:00
if ( isset ( $this -> all_languages [ $language_info [ 'code' ]])) {
$options [ 'languages' ][ $language_info [ 'code' ]] = array ( 'name' => $this -> all_languages [ $language_info [ 'code' ]]);
2009-08-14 17:45:50 +00:00
$updated = sprintf ( __ ( '%s added.' , 'what-did-they-say' ), $this -> all_languages [ $language_info [ 'code' ]]);
2009-08-14 10:54:25 +00:00
}
break ;
case " default " :
if ( isset ( $options [ 'languages' ][ $language_info [ 'code' ]])) {
foreach ( $options [ 'languages' ] as $code => $info ) {
if ( $code == $language_info [ 'code' ]) {
$options [ 'languages' ][ $code ][ 'default' ] = true ;
2009-08-14 17:45:50 +00:00
$updated = sprintf ( __ ( '%s set as default.' , 'what-did-they-say' ), $info [ 'name' ]);
2009-08-14 10:54:25 +00:00
} else {
unset ( $options [ 'languages' ][ $code ][ 'default' ]);
}
}
}
break ;
2009-08-14 11:09:37 +00:00
case " rename " :
if ( isset ( $options [ 'languages' ][ $language_info [ 'code' ]])) {
if ( ! empty ( $language_info [ 'name' ])) {
2009-08-14 17:45:50 +00:00
$updated = sprintf ( __ ( '%1$s renamed to %2$s.' , 'what-did-they-say' ), $options [ 'languages' ][ $language_info [ 'code' ]][ 'name' ], $language_info [ 'name' ]);
2009-08-14 11:09:37 +00:00
$options [ 'languages' ][ $language_info [ 'code' ]][ 'name' ] = $language_info [ 'name' ];
}
}
break ;
2009-08-13 17:12:10 +00:00
}
2009-08-14 23:49:03 +00:00
ksort ( $options [ 'languages' ]);
2009-08-14 10:54:25 +00:00
update_option ( 'what-did-they-say-options' , $options );
2009-08-14 17:45:50 +00:00
return $updated ;
2009-08-13 23:32:42 +00:00
}
function handle_update_allowed_users ( $users ) {
$allowed_users = array ();
foreach ( $users as $user ) {
if ( is_numeric ( $user )) {
$user_info = get_userdata ( $user );
if ( ! empty ( $user_info )) {
$allowed_users [] = $user ;
}
}
}
$this -> _update_options ( 'allowed_users' , $allowed_users );
2009-08-13 17:12:10 +00:00
}
2009-08-15 19:38:12 +00:00
function handle_update_capabilities ( $capabilities ) {
$options = get_option ( 'what-did-they-say-options' );
$updated = false ;
switch ( $language_info [ 'action' ]) {
case " capabilities " :
}
}
2009-08-13 17:12:10 +00:00
2009-08-13 23:36:47 +00:00
function handle_update_options ( $requested_options ) {
$updated_options = array (
'only_allowed_users' => false
);
foreach ( $requested_options as $option => $value ) {
switch ( $option ) {
case 'only_allowed_users' :
$updated_options [ 'only_allowed_users' ] = true ;
break ;
}
}
$options = array_merge ( get_option ( 'what-did-they-say-options' ), $updated_options );
update_option ( 'what-did-they-say-options' , $options );
}
2009-08-13 17:12:10 +00:00
function read_language_file () {
if ( file_exists ( $this -> language_file )) {
foreach ( file ( $this -> language_file , FILE_IGNORE_NEW_LINES ) as $language ) {
list ( $code , $date_added , $name , $additional ) = explode ( " \t " , $language );
$this -> all_languages [ $code ] = $name ;
}
}
return $this -> all_languages ;
}
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-14 02:13:46 +00:00
update_option ( 'what-did-they-say-options' , $this -> default_options );
2009-08-13 16:37:33 +00:00
}
}
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-12 23:51:23 +00:00
function admin_menu () {
add_submenu_page (
'edit-comments.php' ,
__ ( 'Manage Transcriptions' , 'what-did-they-say' ),
__ ( 'Transcripts' , 'what-did-they-say' ),
'edit_posts' ,
'manage-transcriptions-wdts' ,
array ( & $this , 'manage_transcriptions_admin' )
);
if ( current_user_can ( 'edit_posts' )) {
add_meta_box (
'manage-transcriptions' ,
__ ( 'Manage Transcriptions' , 'what-did-they-say' ),
array ( & $this , 'manage_transcriptions_meta_box' ),
'post' ,
'normal' ,
'low'
);
}
}
function manage_transcriptions_admin () {
2009-08-13 23:32:42 +00:00
$options = get_option ( 'what-did-they-say-options' );
2009-08-14 02:13:46 +00:00
$nonce = wp_create_nonce ( 'what-did-they-say' );
2009-08-14 17:45:50 +00:00
2009-08-13 16:37:33 +00:00
include ( dirname ( __FILE__ ) . '/admin.inc' );
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-12 23:51:23 +00:00
2009-08-13 16:37:33 +00:00
var_dump ( $post -> ID );
2009-08-12 23:51:23 +00:00
}
2009-08-15 19:38:12 +00:00
function edit_user_profile ( $user ) {
$options = get_option ( 'what-did-they-say-options' );
if ( $options [ 'only_allowed_users' ]) {
$nonce = wp_create_nonce ( 'what-did-they-say' );
$active = in_array ( $user -> ID , $options [ 'allowed_users' ]); ?>
< h3 >< ? php _e ( 'Transcription Roles' , 'whay-did-they-say' ) ?> </h3>
< input type = " hidden " name = " wdts[_nonce] " value = " <?php echo $nonce ?> " />
< input type = " hidden " name = " wdts[action] " value = " change-active " />
< div >
< input type = " checkbox " name = " wdts[active] " value = " yes " < ? php echo $active ? 'checked="checked"' : " " ?> /> <?php _e('Allow this user to submit and approve transcripts', 'what-did-they-say') ?>
</ div >
< ? php
}
}
2009-08-12 23:51:23 +00:00
}
?>