<?php
/*
Plugin Name: Quick Code
Version: 0.94
Plugin URI: http://blog.netnerds.net/wp-plugins/
Author: Chrissy LeMaire
Author URI: http://netnerds.net
Description: Transforms any code between &lt;quickcode> and &lt;/quickcode> into a good looking div which can disappear or reappear with a click.

=>> Use this in conjunction with coffee2code's awesome Preserve Code Formatting plugin @ http://www.coffee2code.com/wp-plugins/  <<=
*/

function pretty_code_quick($text)
{
  
extract($GLOBALS);
        global 
$id;
        
$theID=$id;

  
$text=str_replace("</quickcode>","</code></div>",$text);
  
$text=str_replace("[/quickcode]","</code></div>",$text);

  while(!((
strpos($text,"<quickcode") ? strpos($text,"<quickcode")+0)==0))
  {

    
$i=$i+1;
    
$rStr="<a class=\"quickcode\" title=\"Code\" href=\"javascript:toggleLayer('quickcode".$theID.$i."');\">Change This Quickcode Title</a></p>"."\r\n";
    
$rStr=$rStr."<div id=\"quickcode".$theID.$i."\" class=\"quickcode\"><code>";

    
$firstHalf=substr($text,0,(strpos($text,"<quickcode") ? strpos($text,"<quickcode")+0)-1);
    
$secondHalf=substr($text,(strpos($text,"<quickcode") ? strpos($text,"<quickcode")+0)-1+strlen("<quickcode")+1-1,strlen($text));

    
$carrotNum=(strpos($secondHalf,">") ? strpos($secondHalf,">")+0)-1;

    if (
$carrotNum<1)
    {

      
$codeTitle="Quick Code";
      
$secondHalf=substr($secondHalf,strlen($secondHalf)-(strlen($secondHalf)-1));
      
$restofCode=substr($secondHalf,$carrotNum+2-1,strlen($secondHalf));
      
$text=$firstHalf.trim(str_replace("Change This Quickcode Title",$codeTitle,$rStr)).$restofCode;
 
    }
      else
    {
      
$codeTitle=substr($secondHalf,1,$carrotNum-1);
    } 

      if (
$codeTitle=="noclick") { $rStr="<div class=\"quickcodenoclick\"><code>"; }
      if (
is_single() && get_option('quickcodeExpandSingle') == TRUE)
{
      if (
$codeTitle!="noclick" && $codeTitle!="Quick Code")
      { 
$rStr "<br><img align=absmiddle src=".get_bloginfo('wpurl')."/wp-content/plugins/quickcode/quickcode.gif> ".$codeTitle."<br><div class=\"quickcodenoclick\"><code>"; }
      else
      { 
$rStr "<div class=\"quickcodenoclick\"><code>"; }
}
  

      
$restofCode=substr($secondHalf,$carrotNum+2-1,strlen($secondHalf));
      
$text=$firstHalf.trim(str_replace("Change This Quickcode Title",$codeTitle,$rStr)).$restofCode;
  } 

  while(!((
strpos($text,"[quickcode") ? strpos($text,"[quickcode")+0)==0)) //to take care of quickcode in a bracket
  
{

    
$i=$i+1;
    
$rStr="<a class=\"quickcode\" title=\"Code\" href=\"javascript:toggleLayer('quickcode".$theID.$i."');\">Change This Quickcode Title</a></p>"."\r\n";
    
$rStr=$rStr."<div id=\"quickcode".$theID.$i."\" class=\"quickcode\"><code>";

    
$firstHalf=substr($text,0,(strpos($text,"[quickcode") ? strpos($text,"[quickcode")+0)-1);
    
$secondHalf=substr($text,(strpos($text,"[quickcode") ? strpos($text,"[quickcode")+0)-1+strlen("[quickcode")+1-1,strlen($text));

    
$carrotNum=(strpos($secondHalf,"]") ? strpos($secondHalf,"]")+0)-1;

    if (
$carrotNum<1)
    {

      
$codeTitle="Quick Code";
      
$secondHalf=substr($secondHalf,strlen($secondHalf)-(strlen($secondHalf)-1));
      
$restofCode=substr($secondHalf,$carrotNum+2-1,strlen($secondHalf));
      
$text=$firstHalf.trim(str_replace("Change This Quickcode Title",$codeTitle,$rStr)).$restofCode;

    }
      else
    {
      
$codeTitle=substr($secondHalf,1,$carrotNum-1);
    }

      if (
$codeTitle=="noclick") { $rStr="<div class=\"quickcodenoclick\"><code>"; }
      if (
is_single() && get_option('quickcodeExpandSingle') == TRUE
{
      if (
$codeTitle!="noclick" && $codeTitle!="Quick Code")
      { 
$rStr "<br><img align=absmiddle src=".get_bloginfo('wpurl')."/wp-content/plugins/quickcode/quickcode.gif> ".$codeTitle."<br><div class=\"quickcodenoclick\"><code>"; }      
       else
      { 
$rStr "<div class=\"quickcodenoclick\"><code>"; }
}

      
$restofCode=substr($secondHalf,$carrotNum+2-1,strlen($secondHalf));
      
$text=$firstHalf.trim(str_replace("Change This Quickcode Title",$codeTitle,$rStr)).$restofCode;
  }

  return 
$text;

 
function 
pretty_code_converter_jscss() {
    
$wp_url get_bloginfo('wpurl') . "/";

    echo 
'
    <!-- Added By Quickcode Plugin -->
    <link rel="stylesheet" href="'
.$wp_url.'wp-content/plugins/quickcode/quickcode.css" type="text/css" />
    <script type="text/javascript" src="'
.$wp_url.'wp-content/plugins/quickcode/quickcode.js"></script>
                '
;

}

function 
quickcode_add_options_page()
        {
                
add_options_page('Quickcode Options''Quickcode'9'quickcode/quickcode-options.php');
        }


add_filter('the_content''pretty_code_quick',1);
add_filter('the_excerpt''pretty_code_quick',1);
add_action('wp_head''pretty_code_converter_jscss');
add_action('admin_head''quickcode_add_options_page');
?>