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-09-13 18:46:02 +00:00
),
2009-09-22 02:10:17 +00:00
'load_default_styles' => true ,
2009-09-24 02:38:11 +00:00
'automatic_embedding' => true ,
2009-09-24 11:29:33 +00:00
'search_integration' => true ,
2009-10-01 01:50:16 +00:00
'excerpt_distance' => 30 ,
2009-10-02 02:43:31 +00:00
'use_nl2br' => false ,
2009-10-05 00:09:59 +00:00
'high_insertion_level' => false ,
'hide_transcript' => array (
'home' => true ,
'single' => false
2009-10-05 02:37:22 +00:00
),
'transcript_effects' => false
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
2009-09-13 16:35:20 +00:00
var $is_ajax = false ;
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-09-13 18:46:02 +00:00
$options = get_option ( 'what-did-they-say-options' );
if ( ! is_array ( $options )) {
2009-09-25 03:37:05 +00:00
$this -> install ();
2009-10-02 01:24:56 +00:00
} else {
$any_changes = false ;
foreach ( $this -> default_options as $key => $value ) {
if ( ! isset ( $options [ $key ])) {
$any_changes = true ; $options [ $key ] = $value ;
}
}
if ( $any_changes ) { update_option ( 'what-did-they-say-options' , $options ); }
2009-09-13 18:46:02 +00:00
}
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
2009-08-15 20:21:52 +00:00
add_filter ( 'user_has_cap' , array ( & $this , 'user_has_cap' ), 5 , 3 );
2009-09-22 02:10:17 +00:00
add_filter ( 'the_media_transcript' , array ( & $this , 'the_media_transcript' ), 10 , 2 );
add_filter ( 'the_language_name' , array ( & $this , 'the_language_name' ), 10 , 2 );
add_filter ( 'the_matching_transcript_excerpts' , array ( & $this , 'the_matching_transcript_excerpts' ), 10 , 3 );
2009-10-04 22:54:22 +00:00
add_filter ( 'the_transcript_language_name' , array ( & $this , 'the_transcript_language_name' ), 10 , 3 );
2009-10-05 01:53:46 +00:00
add_filter ( 'the_transcript_format_string' , array ( & $this , 'the_transcript_format_string' ));
add_filter ( 'the_transcript_opener' , array ( & $this , 'the_transcript_opener' ));
2009-08-12 23:51:23 +00:00
2009-09-13 16:35:20 +00:00
add_filter ( 'template_redirect' , array ( & $this , 'template_redirect' ));
2009-09-24 11:29:33 +00:00
if ( $options [ 'search_integration' ]) {
add_filter ( 'posts_where' , array ( & $this , 'posts_where' ));
add_filter ( 'posts_join' , array ( & $this , 'posts_join' ));
}
2009-09-24 02:38:11 +00:00
if ( $options [ 'automatic_embedding' ]) {
2009-10-02 02:43:31 +00:00
add_filter ( 'the_content' , array ( & $this , 'the_content_automatic_embedding' ), $options [ 'high_insertion_level' ] ? 100 : 15 );
2009-09-24 02:38:11 +00:00
}
2009-09-29 01:54:02 +00:00
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 ));
}
}
2009-10-05 00:09:59 +00:00
if ( current_user_can ( 'submit_transcriptions' )) {
add_action ( 'wp_footer' , array ( & $this , 'wp_footer' ));
}
2009-09-29 01:54:02 +00:00
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 );
2009-08-14 17:45:50 +00:00
if ( isset ( $_REQUEST [ 'wdts' ])) {
2009-10-05 00:09:59 +00:00
if ( isset ( $_REQUEST [ 'wdts' ][ 'script' ])) {
$language_options = new WDTSLanguageOptions ();
include ( 'partials/_editor-script.inc' );
exit ( 0 );
}
2009-08-14 17:45:50 +00:00
if ( isset ( $_REQUEST [ 'wdts' ][ '_nonce' ])) {
if ( wp_verify_nonce ( $_REQUEST [ 'wdts' ][ '_nonce' ], 'what-did-they-say' )) {
$this -> handle_update ( $_REQUEST [ 'wdts' ]);
2009-09-13 16:35:20 +00:00
if ( $this -> is_ajax ) { exit ( 0 ); }
2009-08-12 23:51:23 +00:00
}
2009-09-13 18:46:02 +00:00
}
2009-08-12 23:51:23 +00:00
}
2009-09-13 18:46:02 +00:00
}
2009-08-14 02:13:46 +00:00
2009-10-05 00:09:59 +00:00
function wp_footer () {
2009-09-30 02:39:37 +00:00
$nonce = wp_create_nonce ( 'what-did-they-say' );
2009-10-05 00:09:59 +00:00
echo '<script type="text/javascript">' ;
echo 'WhatDidTheySay.ajax_url = "' . addslashes ( $_SERVER [ 'REQUEST_URI' ]) . '";' ;
echo 'WhatDidTheySay.nonce = "' . $nonce . '";' ;
echo '</script>' ;
2009-09-30 02:39:37 +00:00
}
2009-09-24 02:38:11 +00:00
/**
* Attempt to automatically embed transcripts in posts .
*/
function the_content_automatic_embedding ( $content ) {
ob_start ();
transcripts_display ();
the_media_transcript_queue_editor ();
return $content . ob_get_clean ();
}
2009-10-05 01:53:46 +00:00
function the_transcript_opener ( $content = '' ) {
ob_start (); ?>
< div class = " wdts-transcript-opener " > [
< a href = " # " class = " wdts-opener " >< ? php _e ( 'Show transcripts' ) ?> </a>
< a href = " # " class = " wdts-closer " >< ? php _e ( 'Hide transcripts' ) ?> </a>
] </ div >
< ? php return ob_get_clean ();
}
2009-09-24 01:58:02 +00:00
/**
* Filter for WP_Query #get_posts to add searching for transcripts.
*/
2009-09-20 16:54:57 +00:00
function posts_where ( $where ) {
global $wpdb ;
$search = get_query_var ( 's' );
if ( ! empty ( $search )) {
2009-09-22 02:10:17 +00:00
$query = $wpdb -> prepare ( " OR ( $wpdb->postmeta .meta_key = %s " , 'approved_transcripts_words' );
2009-09-20 16:54:57 +00:00
$search = addslashes_gpc ( $search );
2009-09-22 02:10:17 +00:00
$query .= " AND $wpdb->postmeta .meta_value LIKE '% $search %') " ;
$exact = get_query_var ( 'exact' );
$n = ! empty ( $exact ) ? '' : '%' ;
$where = preg_replace ( " #( \ ( $wpdb->posts .post_title LIKE ' { $n } { $search } { $n } ' \ ))# " , '\1' . $query , $where );
2009-09-20 16:54:57 +00:00
}
2009-09-22 02:10:17 +00:00
2009-09-20 16:54:57 +00:00
return $where ;
}
2009-09-24 01:58:02 +00:00
/**
* Filter for WP_Query #get_posts to add searching for transcripts.
*/
2009-09-20 16:54:57 +00:00
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 ;
}
2009-09-24 01:58:02 +00:00
/**
* Action for when a non - admin page is displayed .
*/
2009-09-13 18:46:02 +00:00
function template_redirect () {
2009-10-05 00:09:59 +00:00
wp_enqueue_script ( 'wdts-script' , get_bloginfo ( 'url' ) . '?wdts[script]=true' , array ( 'prototype' ));
wp_enqueue_script ( 'toggle-transcript' , plugin_dir_url ( dirname ( __FILE__ )) . 'js/toggle-transcript.js' , array ( 'prototype' , 'wdts-script' ), false , true );
2009-10-05 02:37:22 +00:00
2009-09-25 12:22:24 +00:00
if ( current_user_can ( 'submit_transcriptions' )) {
wp_enqueue_script ( 'scriptaculous-effects' );
2009-10-05 00:09:59 +00:00
wp_enqueue_script ( 'edit-transcripts' , plugin_dir_url ( dirname ( __FILE__ )) . 'js/edit-transcripts.js' , array ( 'scriptaculous-effects' , 'wdts-script' ));
2009-09-29 01:54:02 +00:00
wp_enqueue_style ( 'wdts-admin' , plugin_dir_url ( dirname ( __FILE__ )) . 'css/wdts-admin.css' );
2009-09-25 12:22:24 +00:00
}
2009-09-13 18:46:02 +00:00
$options = get_option ( 'what-did-they-say-options' );
if ( ! empty ( $options [ 'load_default_styles' ])) {
2009-10-05 02:37:22 +00:00
wp_enqueue_style ( 'wdts-defaults' , plugin_dir_url ( dirname ( __FILE__ )) . 'css/wdts-defaults.css' );
}
if ( $options [ 'transcript_effects' ]) {
wp_enqueue_script ( 'scriptaculous-effects' );
2009-09-13 18:46:02 +00:00
}
}
2009-09-13 16:35:20 +00:00
2009-09-24 01:58:02 +00:00
/**
* Dialog short code .
*/
2009-09-13 18:46:02 +00:00
function shortcode_dialog ( $atts , $speech ) {
extract ( shortcode_atts ( array (
'name' => 'Nobody' ,
'direction' => ''
), $atts ));
2009-09-24 01:58:02 +00:00
return end ( apply_filters ( 'filter_shortcode_dialog' , $name , $direction , $speech , " " ));
2009-09-13 18:46:02 +00:00
}
2009-09-24 01:58:02 +00:00
/**
* Filter for dialog short code .
*/
function filter_shortcode_dialog ( $name , $direction , $speech , $content ) {
2009-10-02 01:51:09 +00:00
$content = '<div class="wdts-dialog"><span class="wdts-name">' . $name . '</span>' ;
2009-09-13 18:46:02 +00:00
if ( ! empty ( $direction )) {
2009-10-02 01:51:09 +00:00
$content .= ' <span class="wdts-direction">' . $direction . '</span>' ;
2009-09-13 16:35:20 +00:00
}
2009-10-02 01:51:09 +00:00
$content .= ' <span class="wdts-speech">' . $speech . '</span></div>' ;
2009-09-13 18:46:02 +00:00
2009-09-24 01:58:02 +00:00
return array ( $name , $direction , $speech , $content );
2009-09-13 18:46:02 +00:00
}
2009-09-24 01:58:02 +00:00
/**
* Scene action short code .
*/
2009-09-13 18:46:02 +00:00
function shortcode_scene_action ( $atts , $description ) {
extract ( shortcode_atts ( array (), $atts ));
2009-09-24 01:58:02 +00:00
return end ( apply_filters ( 'filter_shortcode_scene_action' , $description , " " ));
2009-09-13 18:46:02 +00:00
}
2009-09-24 01:58:02 +00:00
/**
* Filter for scene action short code .
*/
function filter_shortcode_scene_action ( $description , $content ) {
2009-10-02 01:51:09 +00:00
return array ( $description , '<div class="wdts-scene-action">' . $description . '</div>' , );
2009-08-12 23:51:23 +00:00
}
2009-08-13 16:37:33 +00:00
2009-09-24 01:58:02 +00:00
/**
* Scene heading short code .
*/
2009-09-13 18:46:02 +00:00
function shortcode_scene_heading ( $atts , $description ) {
extract ( shortcode_atts ( array (), $atts ));
2009-09-24 01:58:02 +00:00
return end ( apply_filters ( 'filter_shortcode_scene_heading' , $description , " " ));
2009-09-13 18:46:02 +00:00
}
2009-09-24 01:58:02 +00:00
/**
* Filter for scene heading short code .
*/
function filter_shortcode_scene_heading ( $description , $content ) {
2009-10-02 01:51:09 +00:00
return array ( $description , '<div class="wdts-scene-heading">' . $description . '</div>' );
2009-08-19 22:54:12 +00:00
}
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-09-24 02:38:11 +00:00
function the_media_transcript ( $transcript , $content = " " ) {
2009-10-01 01:50:16 +00:00
$content = do_shortcode ( $transcript );
$options = get_option ( 'what-did-they-say-options' );
if ( $options [ 'use_nl2br' ]) { $content = nl2br ( $content ); }
2009-10-02 01:51:09 +00:00
return array ( $transcript , '<div class="wdts-transcript">' . $content . '</div>' );
2009-08-16 18:20:37 +00:00
}
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-09-24 02:38:11 +00:00
function the_language_name ( $language , $content = " " ) {
2009-10-02 11:41:12 +00:00
return array ( $language , '<h3 class="wdts-transcript-language">' . sprintf ( apply_filters ( 'the_transcript_format_string' ), $language ) . '</h3>' );
}
/**
* Handle showing the header above a bundle of live transcripts .
*/
2009-10-04 22:54:22 +00:00
function the_transcript_language_name ( $language_format , $code , $content ) {
2009-10-02 11:41:12 +00:00
if ( is_null ( $language_format )) { $language_format = apply_filters ( 'the_transcript_format_string' , '' ); }
2009-10-04 22:54:22 +00:00
$language_options = new WDTSLanguageOptions ();
return array ( $language_format , $code , '<h3 class="wdts-transcript-language">' . sprintf ( $language_format , $language_options -> get_language_name ( $code )) . '</h3>' );
2009-10-02 11:41:12 +00:00
}
/**
* The format string used to display ither a single or multiple language transcript header .
*/
function the_transcript_format_string ( $content ) {
return __ ( 'Transcript: %s' , 'what-did-they-say' );
2009-09-22 02:10:17 +00:00
}
2009-09-24 01:58:02 +00:00
/**
* Handle the_matching_transcript_excerpts .
*/
2009-09-24 02:38:11 +00:00
function the_matching_transcript_excerpts ( $transcripts , $search_string = '' , $content = " " ) {
2009-09-24 11:29:33 +00:00
$options = get_option ( 'what-did-they-say-options' );
2009-09-22 02:10:17 +00:00
ob_start ();
2009-09-24 11:29:33 +00:00
if ( $options [ 'search_integration' ]) {
if ( ! empty ( $search_string )) {
$language_options = new WDTSLanguageOptions ();
$options = get_option ( 'what-did-they-say-options' );
foreach ( $transcripts as $transcript ) {
if (( $pos = strpos ( $transcript [ 'transcript' ], $search_string )) !== false ) {
$l = strlen ( $transcript [ 'transcript' ]) - 1 ;
echo '<div class="transcript-match">' ;
echo '<h4>' . sprintf ( __ ( " %s transcript excerpt: " , 'what-did-they-say' ), $language_options -> get_language_name ( $transcript [ 'language' ])) . '</h4>' ;
echo '<p>' ;
$start_ellipsis = $end_ellipsis = true ;
foreach ( array (
'start' => - 1 ,
'end' => 1
) as $variable => $direction ) {
${$variable} = $pos + ( $options [ 'excerpt_distance' ] * $direction );
if ( $variable == " end " ) { ${$variable} += strlen ( $search_string ); }
if ( ${$variable} < 0 ) { ${$variable} = 0 ; $start_ellipsis = false ; }
if ( ${$variable} > $l ) { ${$variable} = $l ; $end_ellipsis = false ; }
}
$output = " " ;
if ( $start_ellipsis ) { $output .= " ... " ; }
$output .= str_replace ( $search_string , " <strong> " . $search_string . " </strong> " , trim ( substr ( $transcript [ 'transcript' ], $start , $end - $start )));
if ( $end_ellipsis ) { $output .= " ... " ; }
echo $output ;
echo '</p>' ;
echo '</div>' ;
}
2009-09-22 02:10:17 +00:00
}
}
}
return array ( $transcripts , $search_string , ob_get_clean ());
2009-08-16 18:20:37 +00:00
}
2009-08-16 20:15:01 +00:00
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 )) {
2009-09-25 12:22:24 +00:00
$role_cascade = array ( 'subscriber' , 'contributor' , 'author' , 'editor' , 'administrator' );
2009-08-20 01:40:28 +00:00
$allowed_roles = array ();
for ( $i = 0 ; $i < count ( $role_cascade ); ++ $i ) {
2009-09-25 12:22:24 +00:00
if ( isset ( $capabilities [ $role_cascade [ $i ]])) {
$allowed_roles = array_slice ( $role_cascade , 0 , $i + 1 ); break ;
}
2009-08-20 01:40:28 +00:00
}
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 ) {
2009-09-11 01:00:10 +00:00
if ( isset ( $info [ 'module' ])) {
$method_name = " handle_update_ " . str_replace ( " - " , " _ " , $info [ 'module' ]);
if ( method_exists ( $this , $method_name )) {
$result = $this -> { $method_name }( $info );
2009-08-16 16:29:25 +00:00
if ( ! empty ( $result )) { $this -> notices [] = $result ; }
2009-09-11 01:00:10 +00:00
}
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-09-11 16:45:18 +00:00
$transcript_options = new WDTSTranscriptOptions ( $info [ 'post_id' ]);
2009-09-10 02:42:46 +00:00
2009-09-11 16:45:18 +00:00
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' );
2009-08-19 02:33:59 +00:00
}
2009-08-16 20:54:11 +00:00
}
2009-08-19 02:33:59 +00:00
}
2009-10-02 01:24:56 +00:00
if ( ! is_admin ()) {
header ( 'Location: ' . $_SERVER [ 'HTTP_REFERER' ]);
exit ( 0 );
} else {
return $updated ;
}
2009-08-16 20:54:11 +00:00
}
2009-08-14 17:45:50 +00:00
2009-09-29 01:54:02 +00:00
function handle_update_update_queued_transcript ( $info ) {
$this -> is_ajax = true ;
if ( $this -> current_user_can_edit_this_transcript ( $info [ 'post_id' ], $info [ 'key' ])) {
$queued_transcript_manager = new WDTSQueuedTranscript ( $info [ 'post_id' ]);
$queued_transcript_manager -> update_transcript ( $info );
$post = get_post ( $info [ 'post_id' ]);
$language_options = new WDTSLanguageOptions ();
$transcript_options = new WDTSTranscriptOptions ( $post -> ID );
$transcript = $queued_transcript_manager -> get_transcript_by_key ( $info [ 'key' ]);
$user = wp_get_current_user ();
include ( dirname ( __FILE__ ) . '/partials/_queued-transcript-contents.inc' );
return ;
}
header ( 'HTTP/1.1 401 Unauthorized' );
}
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 .
*/
2009-09-11 01:20:01 +00:00
function handle_update_manage_post_transcripts ( $info ) {
2009-10-01 00:45:05 +00:00
if ( ! is_admin ()) { $this -> is_ajax = true ; }
2009-08-16 16:40:54 +00:00
$updated = false ;
if ( current_user_can ( 'approve_transcriptions' )) {
2009-09-11 01:20:01 +00:00
$approved_transcript_manager = new WDTSApprovedTranscript ( $info [ 'post_id' ]);
2009-08-19 22:54:12 +00:00
2009-09-11 01:20:01 +00:00
foreach ( $info [ 'transcripts' ] as $language => $transcript ) {
$approved_transcript_manager -> save_transcript ( array (
'language' => $language ,
'transcript' => $transcript
));
}
2009-08-19 22:54:12 +00:00
2009-09-11 01:20:01 +00:00
$transcript_options = new WDTSTranscriptOptions ( $info [ 'post_id' ]);
$transcript_options -> set_allow_transcripts ( isset ( $info [ 'allow_on_post' ]));
2009-08-19 22:54:12 +00:00
2009-09-11 16:45:18 +00:00
$queued_transcript_manager = new WDTSQueuedTranscript ( $info [ 'post_id' ]);
$queued_transcripts = $queued_transcript_manager -> get_transcripts ();
2009-09-11 01:20:01 +00:00
if ( is_array ( $queued_transcriptions )) {
2009-09-11 16:45:18 +00:00
$transcripts_to_delete = array ();
2009-08-19 22:54:12 +00:00
2009-09-11 16:45:18 +00:00
foreach ( $queued_transcriptions as $transcription ) { $transcripts_to_delete [ $transcription -> id ] = true ; }
2009-09-13 16:35:20 +00:00
if ( isset ( $info [ 'queue' ])) {
foreach ( $info [ 'queue' ] as $id => $keep ) { unset ( $transcripts_to_delete [ $id ]); }
2009-09-11 01:20:01 +00:00
}
2009-08-19 22:54:12 +00:00
2009-09-11 16:45:18 +00:00
foreach ( array_keys ( $transcripts_to_delete ) as $id ) { $queued_transcripts -> delete_transcript ( $id ); }
2009-08-16 16:40:54 +00:00
}
2009-09-11 01:20:01 +00:00
2009-10-01 00:45:05 +00:00
if ( is_admin ()) {
$updated = __ ( 'Transcripts updated.' , 'what-did-they-say' );
} else {
return ;
}
}
if ( is_admin ()) {
return $updated ;
} else {
header ( 'HTTP/1.1 401 Unauthorized' );
2009-08-16 16:40:54 +00:00
}
}
2009-09-24 01:58:02 +00:00
/**
* Handle transcript approval .
*/
2009-09-13 16:35:20 +00:00
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' );
}
2009-09-24 01:58:02 +00:00
2009-09-29 01:54:02 +00:00
function current_user_can_edit_this_transcript ( $post_id , $transcript_id ) {
if ( current_user_can ( 'approve_transcriptions' )) {
$ok = true ;
} else {
$ok = false ;
if ( current_user_can ( 'submit_transcriptions' )) {
$queued_transcript_manager = new WDTSQueuedTranscript ( $post_id );
$user = wp_get_current_user ();
$transcripts = $queued_transcript_manager -> get_transcripts_for_user ( $user -> ID );
foreach ( $transcripts as $transcript ) {
if ( $transcript [ 'key' ] == $transcript_id ) {
$ok = true ; break ;
}
}
}
}
return $ok ;
}
2009-09-24 01:58:02 +00:00
/**
* Handle transcript deletion .
*/
2009-09-13 16:35:20 +00:00
function handle_update_delete_transcript ( $info ) {
$this -> is_ajax = true ;
2009-08-16 16:40:54 +00:00
2009-09-29 01:54:02 +00:00
if ( $this -> current_user_can_edit_this_transcript ( $info [ 'post_id' ], $info [ 'key' ])) {
2009-09-13 16:35:20 +00:00
$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' );
}
2009-09-24 01:58:02 +00:00
/**
* Handle updating default styles .
*/
2009-09-23 02:30:16 +00:00
function handle_update_styles ( $info ) {
2009-09-13 18:46:02 +00:00
$updated = false ;
if ( current_user_can ( 'edit_themes' )) {
$options = get_option ( 'what-did-they-say-options' );
2009-10-05 02:37:22 +00:00
foreach ( array ( 'load_default_styles' , 'use_nl2br' , 'transcript_effects' ) as $field ) {
$options [ $field ] = isset ( $info [ $field ]);
}
2009-09-13 18:46:02 +00:00
update_option ( 'what-did-they-say-options' , $options );
$updated = __ ( 'Default styles option updated.' , 'what-did-they-say' );
}
return $updated ;
}
2009-10-02 02:43:31 +00:00
/**
* Handle updating the insertion level .
*/
function handle_update_high_insertion_level ( $info ) {
$updated = false ;
if ( current_user_can ( 'manage_options' )) {
$options = get_option ( 'what-did-they-say-options' );
$options [ 'high_insertion_level' ] = isset ( $info [ 'high_insertion_level' ]);
update_option ( 'what-did-they-say-options' , $options );
$updated = __ ( 'Insertion level updated.' , 'what-did-they-say' );
}
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-09-11 01:00:10 +00:00
} else {
$updated = sprintf ( __ ( 'Language not deleted!' , 'what-did-they-say' ));
2009-09-09 12:20:07 +00:00
}
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-11 01:00:10 +00:00
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' ));
}
2009-08-16 16:40:54 +00:00
}
break ;
case " default " :
2009-09-11 01:00:10 +00:00
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' ));
2009-08-14 10:54:25 +00:00
}
2009-08-16 16:40:54 +00:00
break ;
case " rename " :
2009-09-11 01:00:10 +00:00
$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' ));
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-09-11 01:03:27 +00:00
if ( isset ( $info [ 'capabilities' ])) {
foreach ( array_keys ( $this -> default_options [ 'capabilities' ]) as $capability ) {
if ( isset ( $info [ 'capabilities' ][ $capability ])) {
$options [ 'capabilities' ][ $capability ] = $info [ 'capabilities' ][ $capability ];
2009-08-15 19:53:55 +00:00
}
2009-09-11 01:03:27 +00:00
}
$updated = __ ( 'User capabilities updated.' , 'what-did-they-say' );
2009-08-16 16:40:54 +00:00
}
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' )) {
2009-09-11 01:03:27 +00:00
delete_option ( 'what-did-they-say-options' );
$this -> install ();
$updated = __ ( '<strong>What Did They Say?!?</strong> options reset.' , 'what-did-they-say' );
2009-08-20 01:40:28 +00:00
}
return $updated ;
}
2009-09-24 11:29:33 +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_core_features ( $info ) {
$updated = false ;
if ( current_user_can ( 'manage_options' )) {
$options = get_option ( 'what-did-they-say-options' );
foreach ( array ( 'automatic_embedding' , 'search_integration' ) as $field ) {
$options [ $field ] = isset ( $info [ $field ]);
}
update_option ( 'what-did-they-say-options' , $options );
$updated = __ ( '<strong>What Did They Say?!?</strong> core options changed.' , 'what-did-they-say' );
}
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 )) {
2009-09-04 00:04:08 +00:00
foreach ( file ( $this -> language_file ) as $language ) {
$language = trim ( $language );
2009-09-13 16:35:20 +00:00
list ( $code , $date_added , $name ) = explode ( " \t " , $language );
2009-08-13 17:12:10 +00:00
$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-09-13 18:46:02 +00:00
function admin_menu () {
2009-09-24 01:34:06 +00:00
global $pagenow , $plugin_page ;
2009-09-13 18:46:02 +00:00
2009-10-05 00:09:59 +00:00
wp_enqueue_script ( 'wdts-script' , get_bloginfo ( 'url' ) . '?wdts[script]=true' , array ( 'prototype' ));
2009-09-23 23:22:59 +00:00
if ( current_user_can ( 'submit_transcriptions' )) {
2009-08-15 20:21:52 +00:00
add_options_page (
2009-09-13 18:46:02 +00:00
__ ( 'What Did They Say?!?' , 'what-did-they-say' ),
2009-08-15 20:21:52 +00:00
__ ( 'What Did They Say?!?' , 'what-did-they-say' ),
2009-09-25 12:22:24 +00:00
'submit_transcriptions' ,
2009-08-15 20:21:52 +00:00
'manage-wdts' ,
array ( & $this , 'manage_admin' )
);
2009-09-13 18:46:02 +00:00
2009-09-23 02:30:16 +00:00
if ( $plugin_page == " manage-wdts " ) {
$this -> read_language_file ();
wp_enqueue_style ( 'wdts-admin' , plugin_dir_url ( dirname ( __FILE__ )) . 'css/wdts-admin.css' );
2009-09-24 01:34:06 +00:00
wp_enqueue_style ( 'wdts-defaults' , plugin_dir_url ( dirname ( __FILE__ )) . 'css/wdts-defaults.css' );
2009-09-23 23:22:59 +00:00
$this -> plugin_data = get_plugin_data ( $this -> _parent_file );
2009-09-23 02:30:16 +00:00
}
2009-09-13 18:46:02 +00:00
wp_enqueue_script ( 'scriptaculous-effects' );
2009-09-30 02:39:37 +00:00
add_action ( 'wp_head' , array ( & $this , 'include_editor_javascript' ));
2009-08-15 20:21:52 +00:00
}
2009-09-24 01:34:06 +00:00
2009-08-15 20:21:52 +00:00
if ( current_user_can ( 'approve_transcriptions' )) {
2009-09-24 01:34:06 +00:00
if ( strpos ( $pagenow , " post " ) === 0 ) {
add_meta_box (
'manage-transcriptions' ,
2009-09-24 11:29:33 +00:00
__ ( 'Manage Transcripts' , 'what-did-they-say' ),
2009-09-24 01:34:06 +00:00
array ( & $this , 'manage_transcriptions_meta_box' ),
'post' ,
'normal' ,
'low'
);
2009-09-13 18:46:02 +00:00
2009-09-24 01:34:06 +00:00
wp_enqueue_style ( 'wdts-admin' , plugin_dir_url ( dirname ( __FILE__ )) . 'css/wdts-admin.css' );
2009-09-29 01:58:33 +00:00
wp_enqueue_style ( 'wdts-defaults' , plugin_dir_url ( dirname ( __FILE__ )) . 'css/wdts-defaults.css' );
2009-09-24 01:34:06 +00:00
wp_enqueue_script ( 'scriptaculous-effects' );
2009-10-05 00:09:59 +00:00
wp_enqueue_script ( 'edit-transcripts' , plugin_dir_url ( dirname ( __FILE__ )) . 'js/edit-transcripts.js' , array ( 'scriptaculous-effects' , 'wdts-script' ));
2009-09-24 01:34:06 +00:00
}
2009-08-12 23:51:23 +00:00
}
}
2009-08-19 23:58:24 +00:00
/**
* Show the admin page .
*/
2009-08-15 19:58:01 +00:00
function manage_admin () {
2009-09-24 02:44:30 +00:00
global $wpdb ;
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-09-24 02:44:30 +00:00
$suggested_amount = 20 + (( int ) $wpdb -> get_var ( $wpdb -> prepare ( " SELECT count( $wpdb->postmeta .meta_key) FROM $wpdb->postmeta WHERE $wpdb->postmeta .meta_key = 'approved_transcripts' " )) * 0.1 );
2009-09-24 01:34:06 +00:00
include ( 'partials/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-09-11 01:20:01 +00:00
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();
}
2009-09-13 16:35:20 +00:00
$transcript_options = new WDTSTranscriptOptions ( $post -> ID );
2009-09-29 01:58:33 +00:00
$language_options = new WDTSLanguageOptions ();
2009-08-15 20:45:08 +00:00
$nonce = wp_create_nonce ( 'what-did-they-say' );
2009-10-01 01:43:07 +00:00
include ( 'partials/meta-box.inc' );
2009-08-15 19:38:12 +00:00
}
2009-08-12 23:51:23 +00:00
}
?>