Wed, April 14, 2021
 
https://devcodepro.comdevcodepro
Home · Latest · Trends  /  PHP
4

esoTalk-BBCode plugin with Color Tag

esoTalk forum script / esoTalk-BBCode plugin with color tag [plugin.php] [resources/bbcode.js]
=======plugin.php=======

<?php
// Copyright 2011 Toby Zerner, Simon Zerner
// This file is part of esoTalk. Please see the included license file for usage information.
if(!defined("IN_ESOTALK"))
 exit;
ET::$pluginInfo["BBCode"] = array("name" => "BBCode","description" => "Formats BBCode within posts, allowing users to style their text.","version" => ESOTALK_VERSION,"author" => "esoTalk Team","authorEmail" => "[email protected]","authorURL" => "http://esotalk.org","license" => "GPLv2");
/**
 * ETPlugin_BBCode
 * @package   
 * @author devcodepro.com
 * @version 2017
 * @access public
 */
class ETPlugin_BBCode extends ETPlugin {
 public function handler_conversationController_renderBefore($sender) {
  $sender->addJSFile($this->resource("bbcode.js"));
  $sender->addCSSFile($this->resource("bbcode.css"));
 }
 public function handler_conversationController_getEditControls($sender,&$controls,$id) {
  addToArrayString($controls,"fixed","<a href='javascript:BBCode.fixed(\"$id\");void(0)' title='".T("Code")."' class='control-fixed'><i class='icon-code'></i></a>",0);
  addToArrayString($controls,"image","<a href='javascript:BBCode.image(\"$id\");void(0)' title='".T("Image")."' class='control-img'><i class='icon-picture'></i></a>",0);
  addToArrayString($controls,"link","<a href='javascript:BBCode.link(\"$id\");void(0)' title='".T("Link")."' class='control-link'><i class='icon-link'></i></a>",0);
  addToArrayString($controls,"strike","<a href='javascript:BBCode.strikethrough(\"$id\");void(0)' title='".T("Strike")."' class='control-s'><i class='icon-strikethrough'></i></a>",0);
  addToArrayString($controls,"header","<a href='javascript:BBCode.header(\"$id\");void(0)' title='".T("Header")."' class='control-h'><i class='icon-h-sign'></i></a>",0);
  addToArrayString($controls,"italic","<a href='javascript:BBCode.italic(\"$id\");void(0)' title='".T("Italic")."' class='control-i'><i class='icon-italic'></i></a>",0);
  addToArrayString($controls,"bold","<a href='javascript:BBCode.bold(\"$id\");void(0)' title='".T("Bold")."' class='control-b'><i class='icon-bold'></i></a>",0);
  addToArrayString($controls,"color","<a href='javascript:BBCode.color(\"$id\");void(0)' title='".T("Color")."' class='control-b'>COLOR</a>",0);
 }
 public function handler_format_beforeFormat($sender) {
  $hideBlock = create_function('&$blockFixedContents, $contents','
		$blockFixedContents[] = $contents;
		return "</p><pre></pre><p>";');
  $hideInline = create_function('&$inlineFixedContents, $contents','
		$inlineFixedContents[] = $contents;
		return "<code></code>";');
  $this->blockFixedContents = array();
  $this->inlineFixedContents = array();
  $regexp = "/(.*)^\s*\[code\]\n?(.*?)\n?\[\/code]$/imse";
  while(preg_match($regexp,$sender->content)) {
   if($sender->inline)
    $sender->content = preg_replace($regexp,"'$1' . \$hideInline(\$this->inlineFixedContents, '$2')",$sender->content);
   else
    $sender->content = preg_replace($regexp,"'$1' . \$hideBlock(\$this->blockFixedContents, '$2')",$sender->content);
  }
  // Inline-level [fixed] tags will become <code>.
  $sender->content = preg_replace("/\[code\]\n?(.*?)\n?\[\/code]/ise","\$hideInline(\$this->inlineFixedContents, '$1')",$sender->content);
 }
 public function handler_format_format($sender) {
  $replacement = $sender->inline?"[image]":"<img src='$1' alt='-image-'/>";
  $sender->content = preg_replace("/\[img\](.*?)\[\/img\]/i",$replacement,$sender->content);
  $sender->content = preg_replace_callback("/\[url=(\w{2,6}:\/\/)?([^\]]*?)\](.*?)\[\/url\]/i",array($this,"linksCallback"),$sender->content);
  $sender->content = preg_replace("|\[b\](.*?)\[/b\]|si","<b>$1</b>",$sender->content);
  $sender->content = preg_replace("/\[color=(\#[0-9A-F]{6}|[a-z]+)\](.*)\[\/color\]/Usi","<span style=\"color:\\1\">\\2</span>",$sender->content);
  $sender->content = preg_replace("/\[i\](.*?)\[\/i\]/si","<i>$1</i>",$sender->content);
  $sender->content = preg_replace("/\[s\](.*?)\[\/s\]/si","<del>$1</del>",$sender->content);
  $replacement = $sender->inline?"<b>$1</b>":"</p><h4>$1</h4><p>";
  $sender->content = preg_replace("/\[h\](.*?)\[\/h\]/",$replacement,$sender->content);
 }
 public function linksCallback($matches) {
  return ET::formatter()->formatLink($matches[1].$matches[2],$matches[3]);
 }
 public function handler_format_afterFormat($sender) {
  $sender->content = preg_replace("/<code><\/code>/ie","'<code>' . array_shift(\$this->inlineFixedContents) . '</code>'",$sender->content);
  if(!$sender->inline)
   $sender->content = preg_replace("/<pre><\/pre>/ie","'<pre>' . array_pop(\$this->blockFixedContents) . '</pre>'",$sender->content);
 }
}

=======resources/bbcode.js=======

var BBCode = {

    bold: function(id) {
        ETConversation.wrapText($("#" + id + " textarea"), "[b]", "[/b]");
    },
    italic: function(id) {
        ETConversation.wrapText($("#" + id + " textarea"), "[i]", "[/i]");
    },
    strikethrough: function(id) {
        ETConversation.wrapText($("#" + id + " textarea"), "[s]", "[/s]");
    },
    header: function(id) {
        ETConversation.wrapText($("#" + id + " textarea"), "[h]", "[/h]");
    },
    link: function(id) {
        ETConversation.wrapText($("#" + id + " textarea"), "[url=http://example.com]", "[/url]", "http://example.com", "link text");
    },
    image: function(id) {
        ETConversation.wrapText($("#" + id + " textarea"), "[img]", "[/img]", "", "http://example.com/image.jpg");
    },
    fixed: function(id) {
        ETConversation.wrapText($("#" + id + " textarea"), "[code]", "[/code]");
    },
    color: function(id) {
        ETConversation.wrapText($("#" + id + " textarea"), "[color=red]", "[/color]");
    },

};


rated 4 times (4) (0)
comments: 0 / hits: 3517  / 4 years ago, sat, jan 21, 17, 05:41:42
More From » PHP
 

Comments

There are no comments for this Snippet yet
Only authorized users can post. Please sign in first, or register a free account
 
Share
Posted
jimmy
Member since Dec 15, 2016
Total Code Snippets: 16
Total Comments: 2
Location: n/a
jimmy snippets
4 years ago, mon, dec 5, 16, 1:47:20
comments: 1 / hits: 4105
PHP
4 years ago, mon, jan 9, 17, 2:50:09
comments: 1 / hits: 19088
PHP
4 years ago, mon, jan 16, 17, 2:18:41
comments: 1 / hits: 2525
PHP
4 years ago, fri, feb 10, 17, 12:02:07
comments: 1 / hits: 2633
PHP
4 years ago, fri, feb 10, 17, 12:15:34
comments: 0 / hits: 1934
PHP
4 years ago, tue, feb 28, 17, 9:40:06
comments: 0 / hits: 1847
PHP
4 years ago, sun, mar 5, 17, 1:51:18
comments: 1 / hits: 4487
PHP
4 years ago, thu, mar 16, 17, 5:51:45
comments: 1 / hits: 1958
PHP
4 years ago, sun, apr 16, 17, 3:44:59
comments: 1 / hits: 6421
CSS