<?
/*
Plugin Name: Simple Spoiler
Plugin URI: http://waikay.net/2005/03/plugin-spoiler/
Description: Add show/hide spoiler text. Usage: The syntax is <code>[spoiler '<em>show text</em>' '<em>hide text</em>'] <em>spoiler text</em> [/spoiler]</code>. Put this in either blog entries or comments.
Version: 1.6
Author: Leong Wai Kay
Author URI: http://www.waikay.net

Features:
  Displays a simple link instead of a button.
  Less code per tag use.
  Supports multiple spoiler tags on the same page.

Installation: Place this file in your plugins directory and activate it in your admin panel.
Usuage: Enclose spoiler text between <spoiler> </spoiler> in either blog entries or comments.

Change log:
1.6 - Tag changed to [spoiler] to remain compatable with WP 2.0 WYSIWYG editor.

1.5 - Made the show and hide text into a variable for intiutive editing
    - Added new syntax
      <spoiler 'show text' 'hide text'> :: show text and hide text will be used as the link.
      <spoiler 'tag'> :: tag will be used as the hide tag
      <spoiler> :: the defaults as defined in the variables will be used.

1.4 - Added some newlines to output code so it will be XHTML valid when used with the Markdown plugin

1.3 - Multiple spoiler works within a single post. However wp formatting might make it XHTML invalid

1.2 - XHTML 1.0 Transitional Compliant

1.1 - Spelling error add_filrer fixed.
    - Does not reposition page when link is clicked.

1.0 - Initial release.

*/

// add filter hook
add_filter('the_content''yk_spoiler'2);
add_filter('comment_text''yk_spoiler'2);

function 
yk_spoiler($content)
{
  
srand((double) microtime()*100000);
  return 
preg_replace_callback(
    
"%[\[<]spoiler.*(?:'([^']*)')?\s*(?:'([^']*)')?\s*[\]>](.*)[\[<]/spoiler[\]>]%isU",
    
"yk_callback",
    
$content);
}

function 
yk_callback($m)
{
  
// show and hide text
  
$spoiler_show_text = ($m[1] ? $m[1] : "&#9660; Show");
  
$spoiler_hide_text = ($m[2] ? $m[2] : "&#9650; Hide");
  
$rand "SID".rand();
  return 
"<a href='javascript:void(null);' onclick=\"s_toggleDisplay(document.getElementById('".$rand."'), this, '$spoiler_show_text', '$spoiler_hide_text');\">$spoiler_show_text</a>\n<div class='spoiler' id='$rand' style='display:none;'>\n".$m[3]."\n</div>";
}

function 
add_spoiler_header($text)
{
  echo 
"
    <script type='text/javascript' language='Javascript'>
      function s_toggleDisplay(his, me, show, hide) {
        if (his.style.display != 'none') {
          his.style.display = 'none';
          me.innerHTML = show;
        } else {
          his.style.display = 'block';
          me.innerHTML = hide;
        }
      }
      </script>"
;
  return 
$text;
}

add_action('wp_head''add_spoiler_header');

 
add_filter('mce_valid_elements''shoutbox_extend_editor_mce_valid_elements');

    function 
shoutbox_extend_editor_mce_valid_elements($valid_elements)
    {
        
$valid_elements .= 'spoiler';
        return 
$valid_elements;
    }

?>