diff --git a/classes/WhatDidTheySayAdmin.php b/classes/WhatDidTheySayAdmin.php index 5a4d99c..cc00b11 100644 --- a/classes/WhatDidTheySayAdmin.php +++ b/classes/WhatDidTheySayAdmin.php @@ -17,7 +17,8 @@ class WhatDidTheySayAdmin { 'approve_transcriptions' => 'administrator', 'change_languages' => 'administrator' ), - 'load_default_styles' => true + 'load_default_styles' => true, + 'excerpt_distance' => 30 ); var $capabilities = array(); @@ -56,8 +57,9 @@ class WhatDidTheySayAdmin { 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('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); add_filter('template_redirect', array(&$this, 'template_redirect')); @@ -80,11 +82,16 @@ class WhatDidTheySayAdmin { $search = get_query_var('s'); if (!empty($search)) { - $where .= $wpdb->prepare(" OR ($wpdb->postmeta.meta_key = %s ", 'approved_transcripts_words'); + $query = $wpdb->prepare(" OR ($wpdb->postmeta.meta_key = %s ", 'approved_transcripts_words'); $search = addslashes_gpc($search); - $where .= " AND $wpdb->postmeta.meta_value LIKE '%$search%') "; + $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); } - + return $where; } @@ -167,8 +174,8 @@ class WhatDidTheySayAdmin { * @param string $transcript The transcription text. * @return string The processed transcription text. */ - function the_media_transcript($transcript) { - return '
' . do_shortcode($transcript) . '
'; + function the_media_transcript($transcript, $output = "") { + return array($transcript, '
' . do_shortcode($transcript) . '
'); } /** @@ -176,8 +183,46 @@ class WhatDidTheySayAdmin { * @param string $language The name of the language. * @return string The processed language name. */ - function the_language_name($language) { - return '

' . $language . '

'; + function the_language_name($language, $output = "") { + return array($language, '

' . $language . '

'); + } + + function the_matching_transcript_excerpts($transcripts, $search_string = '', $output = "") { + var_dump($search_string); + ob_start(); + 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 '
'; + echo '

' . sprintf(__("%s transcript:", 'what-did-they-say'), $language_options->get_language_name($transcript['language'])) . '

'; + echo '

'; + $start_ellipsis = $end_ellipsis = true; + foreach (array( + 'start' => -1, + 'end' => 1 + ) as $variable => $direction) { + ${$variable} = $pos + ($options['excerpt_distance'] * $direction); + + 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, "" . $search_string . "", $transcript['transcript']); + if ($end_ellipsis) { $output .= " ..."; } + + echo $output; + echo '

'; + echo '
'; + } + } + } + return array($transcripts, $search_string, ob_get_clean()); } /** diff --git a/classes/admin.inc b/classes/admin.inc index e8d3ba0..51fb294 100644 --- a/classes/admin.inc +++ b/classes/admin.inc @@ -167,8 +167,10 @@ - + +

Miscellaneous Options

+

diff --git a/what-did-they-say.php b/what-did-they-say.php index 1f7bd53..2b07edc 100644 --- a/what-did-they-say.php +++ b/what-did-they-say.php @@ -48,7 +48,9 @@ function get_the_media_transcript($language = null) { if (is_null($language)) { $language = $what_did_they_say->get_default_language(); } $transcript = false; - $transcripts = $what_did_they_say->get_transcripts($post->ID); + $approved_transcripts = new WDTSApprovedTranscript($post->ID); + $transcripts = $approved_transcripts->get_transcripts(); + if (!empty($transcripts)) { if (isset($transcripts[$language])) { $transcript = $transcripts[$language]; } } @@ -60,8 +62,34 @@ function get_the_media_transcript($language = null) { * @param string $language The language code to use. If not specificed, use the default language. */ function the_media_transcript($language = null) { - $transcript = apply_filters('the_media_transcript', get_the_media_transcript($language)); - echo $transcript; + echo end(apply_filters('the_media_transcript', get_the_media_transcript($language))); +} + +/** + * Get the excerpt of all transcripts that match the provided search string. + */ +function get_the_matching_transcripts($search_string = '') { + global $post; + + if (empty($search_string)) { $search_string = get_query_var('s'); } + + $approved_transcripts = new WDTSApprovedTranscript($post->ID); + $transcripts = $approved_transcripts->get_transcripts(); + + $matching_transcripts = array(); + if (!empty($transcripts)) { + foreach ($transcripts as $transcript) { + if (strpos($transcript['transcript'], $search_string) !== false) { $matching_transcripts[] = $transcript; } + } + } + + return $matching_transcripts; +} + +function the_matching_transcript_excerpts($search_string = '') { + if (empty($search_string)) { $search_string = get_query_var('s'); } + + echo end(apply_filters('the_matching_transcript_excerpts', get_the_matching_transcripts($search_string), $search_string)); } /** @@ -81,8 +109,7 @@ function get_the_language_name($language = null) { * @param string $language The language code to use. If not specificed, use the default language. */ function the_language_name($language = null) { - $name = apply_filters('the_language_name', get_the_language_name($language)); - echo $name; + echo end(apply_filters('the_language_name', get_the_language_name($language))); } /**