dynamic filter addition through ninja skills

This commit is contained in:
John Bintz 2009-10-06 22:26:31 -04:00
parent d651800bd8
commit f60656b738
4 changed files with 266 additions and 164 deletions

View File

@ -0,0 +1,139 @@
<?php
class WDTSDisplayFilters {
/**
* Build the opener/closer for transcripts.
*/
function the_transcript_opener($content = '') {
ob_start(); ?>
<div class="wdts-transcript-opener" style="display:none"> [
<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();
}
/**
* Handle the_media_transcript filter.
* @param string $transcript The transcription text.
* @return string The processed transcription text.
*/
function the_media_transcript($transcript, $content = "") {
$content = do_shortcode($transcript);
$options = get_option('what-did-they-say-options');
if ($options['use_nl2br']) { $content = nl2br($content); }
return array($transcript, '<div class="wdts-transcript">' . $content . '</div>');
}
/**
* Handle the_language_name filter.
* @param string $language The name of the language.
* @return string The processed language name.
*/
function the_language_name($language, $content = "") {
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.
*/
function the_transcript_language_name($language_format, $code, $content) {
if (is_null($language_format)) { $language_format = apply_filters('the_transcript_format_string', ''); }
$language_options = new WDTSLanguageOptions();
return array($language_format, $code, '<h3 class="wdts-transcript-language">' . sprintf($language_format, $language_options->get_language_name($code)) . '</h3>');
}
/**
* 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');
}
/**
* The script.aculo.us effects to use when fancy effects are enabled.
*/
function the_transcript_transition_effect($is_opening = true, $content) {
if ($is_opening) {
return array(true, "function(t) { new Effect.BlindDown(t, { duration: 0.25 }); }");
} else {
return array(false, "function(t) { new Effect.BlindUp(t, { duration: 0.25 }); }");
}
}
/**
* Handle the_matching_transcript_excerpts.
*/
function the_matching_transcript_excerpts($transcripts, $search_string = '', $content = "") {
$options = get_option('what-did-they-say-options');
ob_start();
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>';
}
}
}
}
return array($transcripts, $search_string, ob_get_clean());
}
/**
* Filter for dialog short code.
*/
function filter_shortcode_dialog($name, $direction, $speech, $content) {
$content = '<div class="wdts-dialog"><span class="wdts-name">' . $name . '</span>';
if (!empty($direction)) {
$content .= ' <span class="wdts-direction">' . $direction . '</span>';
}
$content .= ' <span class="wdts-speech">' . $speech . '</span></div>';
return array($name, $direction, $speech, $content);
}
/**
* Filter for scene heading short code.
*/
function filter_shortcode_scene_heading($description, $content) {
return array($description, '<div class="wdts-scene-heading">' . $description . '</div>');
}
/**
* Filter for scene action short code.
*/
function filter_shortcode_scene_action($description, $content) {
return array($description, '<div class="wdts-scene-action">' . $description . '</div>', );
}
}
?>

View File

@ -7,10 +7,7 @@ class WhatDidTheySayAdmin {
var $default_options = array( var $default_options = array(
'languages' => array( 'languages' => array(
array('code' => 'en', 'default' => true), array('code' => 'en', 'default' => true),
'fr', 'fr', 'es', 'it', 'de'
'es',
'it',
'de'
), ),
'capabilities' => array( 'capabilities' => array(
'submit_transcriptions' => 'administrator', 'submit_transcriptions' => 'administrator',
@ -28,7 +25,8 @@ class WhatDidTheySayAdmin {
'single' => false 'single' => false
), ),
'transcript_effects' => false, 'transcript_effects' => false,
'allow_html' => false 'allow_html' => false,
'filters_to_use' => 'simple'
); );
var $capabilities = array(); var $capabilities = array();
@ -36,6 +34,7 @@ class WhatDidTheySayAdmin {
var $language_file; var $language_file;
var $all_languages = array(); var $all_languages = array();
var $notices = array(); var $notices = array();
var $override_filter_info = false;
var $is_ajax = false; var $is_ajax = false;
@ -47,6 +46,10 @@ class WhatDidTheySayAdmin {
$this->language_file = dirname(__FILE__) . '/../data/lsr-language.txt'; $this->language_file = dirname(__FILE__) . '/../data/lsr-language.txt';
} }
// just in case things need to be unit tested...
function get_filters_dir() { return WP_CONTENT_DIR . '/transcript-filters'; }
function _get_abspath() { return ABSPATH; }
/** /**
* Initialize the object. * Initialize the object.
*/ */
@ -81,13 +84,52 @@ class WhatDidTheySayAdmin {
add_filter('user_has_cap', array(&$this, 'user_has_cap'), 5, 3); add_filter('user_has_cap', array(&$this, 'user_has_cap'), 5, 3);
// display effects // display effects
add_filter('the_media_transcript', array(&$this, 'the_media_transcript'), 10, 2); $filters = new WDTSDisplayFilters();
add_filter('the_language_name', array(&$this, 'the_language_name'), 10, 2); $reset_filter_value = true;
add_filter('the_matching_transcript_excerpts', array(&$this, 'the_matching_transcript_excerpts'), 10, 3);
add_filter('the_transcript_language_name', array(&$this, 'the_transcript_language_name'), 10, 3); if (is_string($options['filters_to_use'])) {
add_filter('the_transcript_format_string', array(&$this, 'the_transcript_format_string')); $target = $this->get_filters_dir() . '/' . preg_replace('#[^a-z0-9_-]#', '', strtolower($options['filters_to_use']));
add_filter('the_transcript_opener', array(&$this, 'the_transcript_opener')); if (is_dir($target)) {
add_filter('the_transcript_transition_effect', array(&$this, 'the_transcript_transition_effect'), 10, 2); $this->override_filter_info = array();
foreach (glob($target . '/*') as $file) {
if (preg_match('#\.(php|inc)$#', $file) > 0) { $this->override_filter_info['php'] = $file; }
if (preg_match('#\.(css)$#', $file) > 0) { $this->override_filter_info['css'] = $file; }
}
if (isset($this->override_filter_info['php'])) {
$class_name = preg_replace('#\..*$#', '', basename($this->override_filter_info['php']));
require_once($this->override_filter_info['php']);
if (class_exists($class_name)) {
$filters = new $class_name();
$reset_filter_value = false;
}
}
}
}
if ($reset_filter_value) {
// $options['filters_to_use'] = false;
}
foreach (array(
array('the_media_transcript', 2),
array('the_language_name', 2),
array('the_matching_transcript_excerpts', 3),
array('the_transcript_language_name', 3),
array('the_transcript_format_string', 1),
array('the_transcript_opener', 1),
array('the_transcript_transition_effect', 2),
array('filter_shortcode_dialog', 4),
array('filter_shortcode_scene_action', 2),
array('filter_shortcode_scene_heading', 2)
) as $filter_info) {
list($method, $parameters) = $filter_info;
if (method_exists($filters, $method)) {
add_filter($method, array(&$filters, $method), 10, $parameters);
}
}
// short codes // short codes
foreach (get_class_methods($this) as $method) { foreach (get_class_methods($this) as $method) {
@ -97,10 +139,6 @@ class WhatDidTheySayAdmin {
} }
} }
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);
// search transcripts, too? // search transcripts, too?
if ($options['search_integration']) { if ($options['search_integration']) {
add_filter('posts_where', array(&$this, 'posts_where')); add_filter('posts_where', array(&$this, 'posts_where'));
@ -165,6 +203,10 @@ class WhatDidTheySayAdmin {
wp_enqueue_style('wdts-defaults', plugin_dir_url(dirname(__FILE__)) . 'css/wdts-defaults.css'); wp_enqueue_style('wdts-defaults', plugin_dir_url(dirname(__FILE__)) . 'css/wdts-defaults.css');
} }
if (isset($this->override_filter_info['css'])) {
wp_enqueue_style('wdts-override', str_replace(realpath($this->_get_abspath()), '', realpath($this->override_filter_info['css'])));
}
if ($options['transcript_effects']) { if ($options['transcript_effects']) {
wp_enqueue_script('scriptaculous-effects'); wp_enqueue_script('scriptaculous-effects');
} }
@ -237,6 +279,10 @@ class WhatDidTheySayAdmin {
wp_enqueue_style('wdts-admin', plugin_dir_url(dirname(__FILE__)) . 'css/wdts-admin.css'); wp_enqueue_style('wdts-admin', plugin_dir_url(dirname(__FILE__)) . 'css/wdts-admin.css');
wp_enqueue_style('wdts-defaults', plugin_dir_url(dirname(__FILE__)) . 'css/wdts-defaults.css'); wp_enqueue_style('wdts-defaults', plugin_dir_url(dirname(__FILE__)) . 'css/wdts-defaults.css');
if (isset($this->override_filter_info['css'])) {
wp_enqueue_style('wdts-override', str_replace(realpath($this->_get_abspath()), '', realpath($this->override_filter_info['css'])));
}
wp_enqueue_script('scriptaculous-effects'); wp_enqueue_script('scriptaculous-effects');
wp_enqueue_script('edit-transcripts', plugin_dir_url(dirname(__FILE__)) . 'js/edit-transcripts.js', array('scriptaculous-effects', 'wdts-script')); wp_enqueue_script('edit-transcripts', plugin_dir_url(dirname(__FILE__)) . 'js/edit-transcripts.js', array('scriptaculous-effects', 'wdts-script'));
} }
@ -255,113 +301,6 @@ class WhatDidTheySayAdmin {
return $content . ob_get_clean(); return $content . ob_get_clean();
} }
/**
* Build the opener/closer for transcripts.
*/
function the_transcript_opener($content = '') {
ob_start(); ?>
<div class="wdts-transcript-opener" style="display:none"> [
<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();
}
/**
* Handle the_media_transcript filter.
* @param string $transcript The transcription text.
* @return string The processed transcription text.
*/
function the_media_transcript($transcript, $content = "") {
$content = do_shortcode($transcript);
$options = get_option('what-did-they-say-options');
if ($options['use_nl2br']) { $content = nl2br($content); }
return array($transcript, '<div class="wdts-transcript">' . $content . '</div>');
}
/**
* Handle the_language_name filter.
* @param string $language The name of the language.
* @return string The processed language name.
*/
function the_language_name($language, $content = "") {
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.
*/
function the_transcript_language_name($language_format, $code, $content) {
if (is_null($language_format)) { $language_format = apply_filters('the_transcript_format_string', ''); }
$language_options = new WDTSLanguageOptions();
return array($language_format, $code, '<h3 class="wdts-transcript-language">' . sprintf($language_format, $language_options->get_language_name($code)) . '</h3>');
}
/**
* 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');
}
/**
* The script.aculo.us effects to use when fancy effects are enabled.
*/
function the_transcript_transition_effect($is_opening = true, $content) {
if ($is_opening) {
return array(true, "function(t) { new Effect.BlindDown(t, { duration: 0.25 }); }");
} else {
return array(false, "function(t) { new Effect.BlindUp(t, { duration: 0.25 }); }");
}
}
/**
* Handle the_matching_transcript_excerpts.
*/
function the_matching_transcript_excerpts($transcripts, $search_string = '', $content = "") {
$options = get_option('what-did-they-say-options');
ob_start();
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>';
}
}
}
}
return array($transcripts, $search_string, ob_get_clean());
}
/** Transcript Search Filters **/ /** Transcript Search Filters **/
@ -400,7 +339,8 @@ class WhatDidTheySayAdmin {
return $join; return $join;
} }
/** Short codes and their filters **/
/** Short codes **/
/** /**
* Dialog short code. * Dialog short code.
@ -414,19 +354,6 @@ class WhatDidTheySayAdmin {
return end(apply_filters('filter_shortcode_dialog', $name, $direction, $speech, "")); return end(apply_filters('filter_shortcode_dialog', $name, $direction, $speech, ""));
} }
/**
* Filter for dialog short code.
*/
function filter_shortcode_dialog($name, $direction, $speech, $content) {
$content = '<div class="wdts-dialog"><span class="wdts-name">' . $name . '</span>';
if (!empty($direction)) {
$content .= ' <span class="wdts-direction">' . $direction . '</span>';
}
$content .= ' <span class="wdts-speech">' . $speech . '</span></div>';
return array($name, $direction, $speech, $content);
}
/** /**
* Scene action short code. * Scene action short code.
*/ */
@ -436,13 +363,6 @@ class WhatDidTheySayAdmin {
return end(apply_filters('filter_shortcode_scene_action', $description, "")); return end(apply_filters('filter_shortcode_scene_action', $description, ""));
} }
/**
* Filter for scene action short code.
*/
function filter_shortcode_scene_action($description, $content) {
return array($description, '<div class="wdts-scene-action">' . $description . '</div>', );
}
/** /**
* Scene heading short code. * Scene heading short code.
*/ */
@ -452,13 +372,6 @@ class WhatDidTheySayAdmin {
return end(apply_filters('filter_shortcode_scene_heading', $description, "")); return end(apply_filters('filter_shortcode_scene_heading', $description, ""));
} }
/**
* Filter for scene heading short code.
*/
function filter_shortcode_scene_heading($description, $content) {
return array($description, '<div class="wdts-scene-heading">' . $description . '</div>');
}
/** Capabilities **/ /** Capabilities **/
@ -696,8 +609,14 @@ class WhatDidTheySayAdmin {
if (current_user_can('edit_themes')) { if (current_user_can('edit_themes')) {
$options = get_option('what-did-they-say-options'); $options = get_option('what-did-they-say-options');
foreach (array('load_default_styles', 'use_nl2br', 'transcript_effects') as $field) { foreach (array('load_default_styles', 'use_nl2br', 'transcript_effects', 'allow_html') as $field) { $options[$field] = isset($info[$field]); }
$options[$field] = isset($info[$field]); $options['excerpt_distance'] = !empty($info['excerpt_distance']) ? $info['excerpt_distance'] : 30;
foreach (array_keys($options['hide_transcript']) as $type) {
$options['hide_transcript'][$type] = isset($info['hide_transcript'][$type]);
}
if (isset($info['filters_to_use'])) {
$options['filters_to_use'] = preg_replace('#[^a-z0-9_-]#', '', strtolower($info['filters_to_use']));
} }
update_option('what-did-they-say-options', $options); update_option('what-did-they-say-options', $options);
@ -875,6 +794,24 @@ class WhatDidTheySayAdmin {
/** What Did They Say?!? administration screens **/ /** What Did They Say?!? administration screens **/
function _get_available_override_filters() {
$available_filters = array();
if (is_dir($this->get_filters_dir())) {
foreach (glob($this->get_filters_dir() . '/*') as $dir) {
if (is_dir($dir)) {
if (basename($dir) == preg_replace('#[^a-z0-9_-]#', '', strtolower(basename($dir)))) {
foreach (glob($dir . '/*') as $file) {
if (preg_match('#^(.*)\.(inc|php)$#', basename($file), $matches) > 0) {
$available_filters[] = basename($dir);
}
}
}
}
}
}
return $available_filters;
}
/** /**
* Show the admin page. * Show the admin page.
*/ */
@ -884,10 +821,12 @@ class WhatDidTheySayAdmin {
$options = get_option('what-did-they-say-options'); $options = get_option('what-did-they-say-options');
$nonce = wp_create_nonce('what-did-they-say'); $nonce = wp_create_nonce('what-did-they-say');
$transcript_count = (int)$wpdb->get_var($wpdb->prepare("SELECT count($wpdb->postmeta.meta_key) FROM $wpdb->postmeta WHERE $wpdb->postmeta.meta_key = 'approved_transcripts'"));
$transcript_count = (int)$wpdb->get_var($wpdb->prepare("SELECT count($wpdb->postmeta.meta_key) FROM $wpdb->postmeta WHERE $wpdb->postmeta.meta_key = 'approved_transcripts'"));
$suggested_amount = 20 + ($transcript_count * 0.1); $suggested_amount = 20 + ($transcript_count * 0.1);
$available_filters = $this->_get_available_override_filters();
include('partials/admin.inc'); include('partials/admin.inc');
} }

View File

@ -63,6 +63,19 @@
<?php _e('On individual post pages', 'what-did-they-say') ?> <?php _e('On individual post pages', 'what-did-they-say') ?>
</label> </label>
</div> </div>
<?php if (!empty($available_filters)) { ?>
<label>
<?php _e('Use the selected transcript filter set:', 'what-did-they-say') ?>
<select name="wdts[filters_to_use]">
<option value="__default__">(default)</option>
<?php foreach ($available_filters as $filter_name) { ?>
<option value="<?php echo $filter_name ?>"<?php echo ($options['filters_to_use'] == $filter_name) ? ' selected="selected"' : '' ?>><?php echo $filter_name ?></option>
<?php } ?>
</select>
</label>
<?php } ?>
<input class="button" type="submit" value="<?php _e('Change default styles', 'what-did-they-say') ?>" /> <input class="button" type="submit" value="<?php _e('Change default styles', 'what-did-they-say') ?>" />
</form> </form>

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: what-did-they-say 0.1\n" "Project-Id-Version: what-did-they-say 0.1\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2009-10-06 07:01-0400\n" "POT-Creation-Date: 2009-10-06 07:11-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -196,13 +196,20 @@ msgid ""
msgstr "" msgstr ""
#: classes/partials/_default-styles.inc:36 #: classes/partials/_default-styles.inc:36
msgid "Turn transcript line breaks into HTML new lines (nl2br())" msgid ""
"Turn transcript line breaks into HTML new lines (uses <a href=\"http://us2."
"php.net/nl2br\" target=\"external\">nl2br()</a>)"
msgstr "" msgstr ""
#: classes/partials/_default-styles.inc:44 #: classes/partials/_default-styles.inc:44
msgid "" msgid ""
"Allow HTML in transcripts. If disabled, only short codes are allowed. Script " "Allow HTML in transcripts. If disabled, only short codes are allowed, which "
"and style tags are always filtered out." "is <strong>recommended</strong>. Script, style, and link tags are "
"<strong>always</strong> filtered out."
msgstr ""
#: classes/partials/_default-styles.inc:47
msgid "By default, transcripts should start hidden on these types of pages:"
msgstr "" msgstr ""
#: classes/partials/_default-styles.inc:55 #: classes/partials/_default-styles.inc:55
@ -472,8 +479,8 @@ msgstr ""
msgid "Capabilities" msgid "Capabilities"
msgstr "" msgstr ""
#: classes/partials/admin.inc:7 classes/partials/_shortcodes-info.inc:2 #: classes/partials/admin.inc:7
msgid "Shortcodes Info" msgid "Short codes Info"
msgstr "" msgstr ""
#: classes/partials/admin.inc:8 #: classes/partials/admin.inc:8
@ -487,7 +494,7 @@ msgstr ""
#: classes/partials/admin.inc:48 #: classes/partials/admin.inc:48
#, php-format #, php-format
msgid "Might I suggest a $%0.2f donation?" msgid "Might I suggest a $%1$0.2f donation for your %2$d transcripts?"
msgstr "" msgstr ""
#: classes/partials/_manage-queued-transcripts.inc:2 #: classes/partials/_manage-queued-transcripts.inc:2
@ -520,6 +527,10 @@ msgstr ""
msgid "Change capabilities" msgid "Change capabilities"
msgstr "" msgstr ""
#: classes/partials/_shortcodes-info.inc:2
msgid "Shortcodes Info"
msgstr ""
#: classes/partials/_shortcodes-info.inc:5 #: classes/partials/_shortcodes-info.inc:5
msgid "" msgid ""
"you can easily use these shortcodes with the appropriate buttons above all " "you can easily use these shortcodes with the appropriate buttons above all "