Ajax Spoiler plugin renamed to 'Advanced Spoiler'.
Show or hide contents(text, image etc.) wrapped by spoiler markup tag([spoiler][/spoiler]). Requires WP 2.7 or greater.
Version: 2.02
Author: Choen, Young-Min
Author URI: http://082net.com/
*/
/* Copyright 2006 Cheon, Young-Min (email : 082net@gmail.com, site : http://082net.com)
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
This Plugin is based on Leong Wai Kay(http://www.waikay.net)'s Simple-Spoiler
Installation :
1. Put "advanced-spoiler" folder to plugin folder (e.g. wp-content/plugins)
2. Open advanced-spoiler.php file with text-editor and edit some options like
$feed_visibility, $use_html_tagset, $js_library, $default_effect
2. Activate the plugin in your WP admin
3. That's all :)
Usage :
1. Enclose your post text, images.. or something within [spoiler] [/spoiler] that's all.
2. You can change showtext, hidetext, effect.
[spoiler effect="blind" show="showtext" hide="hidetext"]your content...[/spoiler] or just [spoiler] [/spoiler]
3. Available effect : appear, blind, apblind, phase, slide, simple
*/
class AdvSpoiler {
var $use_html_tagset = false; // Use set. true or false (recommended is false)
var $counts;
var $mode, $js_library;
var $url, $hook, $folder, $admin_page;
var $sep = "||"; // old plugin support
var $version = '2.02';
var $effects = array('appear', 'blind', 'apblind', 'phase', 'slide', 'simple');
// var $regex = '|\[spoiler\s+([^]]*)\]((?!\[/?spoiler.*?\]).*?)\[/spoiler\]|isme';
var $regex = '#\[spoiler\s+([^]]*)\]((?!\[spoiler\s+([^]]*)\)).)?\[/spoiler]#ise';
var $domain = 'adv-spoiler';
function AdvSpoiler() {
$this->__construct();
}
function __construct() {
$this->hook = plugin_basename(__FILE__);
$this->folder = dirname($this->hook);
$this->url = plugins_url($this->folder);
$this->init_options();
$this->register_hooks();
$this->js_library = 'jquery';// locked now...
}
function default_options() {
$defaults = array(
'default_effect' => 'appear',
'show_text' => __('Show', $this->domain),
'hide_text' => __('Hide', $this->domain),
'effect_speed' => '3',
'show_marker' => '»',
'hide_marker' => '«',
'feed_visibility' => 'hide',
'plugin_mode' => 'jquery',
'allow_nested' => 0,
'parse_old_param' => 0,
);
return $defaults;
}
function init_options() {
$defaults = $this->default_options();
$this->op = get_option('advanced_spoiler');
if (!is_array($this->op)) {
$this->op = $defaults;
return;
}
$this->op = array_merge($defaults, $this->op);
}
function register_hooks() {
// Register Hooks
add_filter('the_content', array(&$this, '_spoiler'), 0);
add_filter('the_content_rss', array(&$this, '_spoiler'), 0);
add_filter('the_excerpt', array(&$this, '_spoiler'), 0);
add_filter('the_excerpt_rss', array(&$this, '_spoiler'), 0);
add_filter('comment_text', array(&$this, '_spoiler'), 0);
add_filter('comment_text_rss', array(&$this, '_spoiler'), 0);
if (is_admin()) {
add_action('admin_menu', array(&$this, 'add_options_page'));
} else {
load_plugin_textdomain($this->domain, false, $this->folder . '/langs');
if ($this->op['plugin_mode'] == 'simple')
add_action('wp_head', array(&$this, 'wp_head'), 15);
else
add_action('wp_head', array(&$this, 'enqueue_script'), 5);
add_action('wp_head', array(&$this, 'enqueue_style'), 5);
}
// add_shortcode('spoiler', array(&$this, 'shortcode'));
}
function add_options_page() {
global $pagenow;
if (function_exists('add_options_page')) {
$page = add_options_page(__("Advanced Spoiler", $this->domain), __("Advanced Spoiler", $this->domain), 9, 'adv-spoiler-options', array(&$this, 'option_page'));
$this->admin_page = $page;
}
if ( in_array( $pagenow, array('post.php', 'post-new.php', 'page.php', 'page-new.php') ) ) {
load_plugin_textdomain($this->domain, false, $this->folder . '/langs');
// init process for tinymce button control
$this->mce_addbuttons();
add_action("admin_print_styles", array(&$this, 'admin_style'));
add_action("admin_print_scripts", array(&$this, 'admin_script'));
}
}
function _fix_attr($attr, $key=0) {
$sep = strpos($attr[$key], $this->sep) !== false ? $this->sep : "''";
$v = explode($sep, $attr[$key], 4);
$v = array_map(create_function('$a', 'return trim($a, " '.$sep[0].'");'), $v);
if ($v[0] && strpos($v[0], '_hidden') !== false) // removed. just use effect name
$v[0] = str_replace('_hidden', '', $v[0]);
if ($v[0] && !in_array($v[0], $this->effects))
array_unshift($v, '');
if (!empty($v[0]) && !isset($attr['effect']))
$attr['effect'] = $v[0];
if (!empty($v[1]) && !isset($attr['show']))
$attr['show'] = $v[1];
if (!empty($v[2]) && !isset($attr['hide']))
$attr['hide'] = $v[2];
unset($attr[$key]);
return $attr;
}
function _reset() {
$this->open = $this->depth = $this->counts = 0;
$this->prv = '';
$this->sp = array();
}
function _spoiler($content) {
$this->_reset();
if ($this->use_html_tagset) {
$content = preg_replace('/<(\/?spoiler)(\s*)([^>]*)>/s', '[\1\2\3]', $content);
}
if ($this->op['allow_nested'])
return $this->_spoiler_deep($content);
$content = preg_replace_callback('/(<(p|div)>)?\[spoiler\b(.*?)\](.*?)\[\/spoiler\](<\/\2>)?/s', array(&$this, '_replace'), $content);
return $content;
}
function _spoiler_deep($content) {
$temp_content = preg_replace_callback('/\[(\/?spoiler)\b(.*?)\]/s', array(&$this, '_temp'), $content);
if (!$this->open)
return $content;
for ($i=1; $i<=$this->open; $i++) {
$regex[] = '/(<(p|div)>)?\[spoiler'.$i.'\b(.*?)\](.*?)\[\/spoiler'.$i.'\](<\/\2>)?/s';
}
$temp_content = preg_replace_callback($regex, array(&$this, '_replace'), $temp_content);
return $temp_content;
/* while(preg_match($this->regex, $content)) {
$content = preg_replace($this->regex, '$this->_replace("\1", "\2");', $content);
}
return $content;*/
}
function _temp($m) {
if (strpos($m[1], '/') === 0) {
if ($this->prv == 'close') $this->depth--;
$num = array_pop($this->sp[$this->depth]);
$this->prv = 'close';
} else {
$this->open++;
if ($this->prv == 'open') $this->depth++;
$this->sp[$this->depth][] = $this->open;
$num = $this->open;
$this->prv = 'open';
}
$text = '['.$m[1].$num.$m[2].']';
return $text;
}
function _replace($m) {
global $post, $doing_rss, $comment;
$this->counts++;
$op = stripslashes($m[3]);
$text = stripslashes($m[4]);
$id = "SID".$post->ID."_".$this->counts;
if (isset($comment->comment_ID)) {
$id .= "c";
}
$href = get_permalink()."#{$id}_tgl";
if ($this->op['feed_visibility'] == 'hide' && (is_feed() || $doing_rss)) {
return "
domain)."'>[[".__('Visit blog to check out this spoiler', $this->domain)."]]
";
}
$def = array('effect' => $this->op['default_effect'],
'show' => $this->op['show_text'],
'hide' => $this->op['hide_text'],
'speed' => $this->op['effect_speed'],
);
$attr = shortcode_parse_atts($op);
if ( $this->op['parse_old_param'] && isset($attr[0]) && (strpos($attr[0], $this->sep) !== false || strpos($attr[0], "''") !== false) ) {
$attr = $this->_fix_attr($attr);
}
extract(shortcode_atts($def, $attr));
$show = $show ." ". $this->op['show_marker'];
$hide = $hide ." ". $this->op['hide_marker'];
if ($this->op['plugin_mode'] == 'simple') {
return "$show
\n\n\n\n".trim($text)."
";
}
$effect = in_array($effect, $this->effects) ? $effect : 'simple';
$speed = (int)$speed;
if ($speed < 2) $speed = 2;
elseif ($speed > 10) $speed = 10;
$speed = $speed*100;
$rev = $effect.'||'.attribute_escape($show).'||'.attribute_escape($hide).'||'.$speed;
return "$show
\n\n\n\n".trim($text)."
";
}
function wp_head($s) {
echo "
";
return $s;
}
function admin_script() {
wp_enqueue_script('admin-adv-spoiler', $this->url . '/js/spoiler-admin.js', array('jquery-ui-dialog', 'jquery-form'), $this->version);
wp_localize_script( 'admin-adv-spoiler', '_spoilerL10n', array(
'title' => __('Make a spoiler', $this->domain),
'effect_title' => __('Select a effect type:', $this->domain),
'showtext_title' => __('Show Text:', $this->domain),
'hidetext_title' => __('Hide Text:', $this->domain),
'default_effect' => $this->op['default_effect'],
'showtext' => $this->op['show_text'],
'hidetext' => $this->op['hide_text'],
'button_title' => __('Insert Spoiler', $this->domain)
) );
}
function admin_style() {
wp_enqueue_style('jquery-ui-lightness-dialog', $this->url . '/css/ui-lightness/ui.dialog.css', false, '1.5.2');
}
function enqueue_script() {
global $wp_scripts;
switch($this->op['plugin_mode']):
case 'simple':
return;
break;
case 'prototype':
wp_enqueue_script('adv-spoiler', $this->url.'/js/prototype-spoiler.js', array('scriptaculous-effects'), $this->version);
break;
case 'mootools':
if (!in_array('mootools-Fx', $wp_scripts->registered))
return;
wp_enqueue_script('adv-spoiler', $this->url.'/js/moo-spoiler.js', array('mootools-Fx'), $this->version);
break;
case 'jquery': default:
wp_enqueue_script('adv-spoiler', $this->url.'/js/jquery-spoiler.js', array('jquery'), $this->version);
break;
endswitch;
}
function enqueue_style() {
wp_enqueue_style('adv-spoiler', $this->url . '/css/advanced-spoiler.css', false, $this->version);
}
function mce_addbuttons() {
// Don't bother doing this stuff if the current user lacks permissions
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') )
return;
// Add only in Rich Editor mode
if ( get_user_option('rich_editing') == 'true') {
add_filter("mce_external_plugins", array(&$this, "add_mce_plugin"));
add_filter('mce_buttons_2', array(&$this, 'register_mce_button'));
add_filter( 'tiny_mce_version', array(&$this, 'mce_version') );
}
}
function register_mce_button($buttons) {
array_push($buttons, "separator", "spoiler");
return $buttons;
}
function add_mce_plugin($plugin_array) {
$plugin_array['spoiler'] = $this->url . '/tinymce/editor_plugin.js';
return $plugin_array;
}
function mce_version( $version ) {
return $version+6;
}
function save_changes() {
if ( $_POST['option_page'] != $this->admin_page )
return;
check_admin_referer( $this->admin_page . '-options' );
$htmls = array('show_text', 'show_marker', 'hide_text', 'hide_marker');
foreach ($this->op as $key => $val) {
$this->op[$key] = stripslashes(trim($_POST[$key]));
if ( in_array($key, $htmls) )
$this->op[$key] = wp_specialchars($this->op[$key]);
}
update_option('advanced_spoiler', $this->op);
echo ''.__('Settings saved.', $this->domain).'
';
}
function option_page() {
load_plugin_textdomain($this->domain, false, $this->folder . '/langs');
$this->save_changes();
extract($this->op, EXTR_SKIP);
?>