1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-04 13:47:31 +02:00

Shortcode parser: major changes (could break stuff), e_shortcode renamed to e_parse_shortcode, abstract e_shortcode class added, functions moved to e_parse_shortcode, more to do, getScObject added to e107 class, work in progress

This commit is contained in:
secretr
2010-04-23 15:54:11 +00:00
parent ee3e6ed9d9
commit 26e4de97e6
3 changed files with 246 additions and 174 deletions

View File

@@ -156,6 +156,7 @@ class e107
'e_news_tree' => '{e_HANDLER}news_class.php', 'e_news_tree' => '{e_HANDLER}news_class.php',
'e_online' => '{e_HANDLER}online_class.php', 'e_online' => '{e_HANDLER}online_class.php',
'e_parse' => '{e_HANDLER}e_parse_class.php', 'e_parse' => '{e_HANDLER}e_parse_class.php',
'e_parse_shortcode' => '{e_HANDLER}shortcode_handler.php',
'e_ranks' => '{e_HANDLER}e_ranks_class.php', 'e_ranks' => '{e_HANDLER}e_ranks_class.php',
'e_upgrade' => '{e_HANDLER}e_upgrade_class.php', 'e_upgrade' => '{e_HANDLER}e_upgrade_class.php',
'e_user_model' => '{e_HANDLER}user_model.php', 'e_user_model' => '{e_HANDLER}user_model.php',
@@ -843,26 +844,27 @@ class e107
*/ */
public static function getParser() public static function getParser()
{ {
return self::getSingleton('e_parse', e_HANDLER.'e_parse_class.php'); //WARNING - don't change this - inifinite loop!!!
return self::getSingleton('e_parse', e_HANDLER.'e_parse_class.php');
} }
/** /**
* Retrieve sc parser singleton object * Retrieve sc parser singleton object
* *
* @return e_shortcode * @return e_parse_shortcode
*/ */
public static function getScParser() public static function getScParser()
{ {
$sc = self::getSingleton('e_shortcode', e_HANDLER.'shortcode_handler.php'); return self::getSingleton('e_parse_shortcode', true);
if(!self::$_sc_core_loaded)
{
$sc->loadCoreShortcodes();
self::$_sc_core_loaded = true;
} }
return $sc;
/**
* Retrieve registered sc object (batch) by class name
*
* @return e_shortcode
*/
public static function getScObject($className)
{
return self::getScParser($className);
} }
/** /**

View File

@@ -447,24 +447,6 @@ class e_parse
return utf8_substr($str, $start, $length); return utf8_substr($str, $start, $length);
} }
// Initialise the shortcode handler - has to be done when $prefs valid, so can't be done in constructor ATM
/*
function sch_load($noCore = FALSE)
{
if (!is_object($this->e_sc))
{
require_once(e_HANDLER."shortcode_handler.php");
$this->e_sc = new e_shortcode;
if(!$noCore)
{
$this->e_sc->loadCoreShortcodes();
}
}
}
*/
/** /**
* Converts the supplied text (presumed to be from user input) to a format suitable for storing in a database table. * Converts the supplied text (presumed to be from user input) to a format suitable for storing in a database table.
* *

View File

@@ -2,38 +2,89 @@
/* /*
* e107 website system * e107 website system
* *
* Copyright (C) 2008-2009 e107 Inc (e107.org) * Copyright (C) 2008-2010 e107 Inc (e107.org)
* Released under the terms and conditions of the * Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
* *
* e107 Shortcode handler * e107 Shortcode handler
* *
* $Source: /cvs_backup/e107_0.8/e107_handlers/shortcode_handler.php,v $ * $URL$
* $Revision$ * $Id$
* $Date$
* $Author$
*/ */
if (!defined('e107_INIT')) { exit; } if (!defined('e107_INIT')) { exit; }
/** /**
* FIXME: to be removed
*/
function register_shortcode($classFunc, $codes, $path='', $force = false)
{
return e107::getScParser()->registerShortcode($classFunc, $codes, $path, $force);
}
/**
* FIXME: to be removed
*/
function setScVar($className, $scVarName, $value)
{
return e107::getScParser()->setScVar($className, $scVarName, $value);
}
/**
* FIXME: to be removed
*/
function callScFunc($className, $scFuncName, $param = '')
{
return e107::getScParser()->callScFunc($className, $scFuncName, $param);
}
/**
* FIXME: to be removed
*/
function initShortcodeClass($class, $force = false, $eVars = null)
{
return e107::getScParser()->initShortcodeClass($class, $eVars, $force);
}
class e_parse_shortcode
{
protected $scList = array(); // The actual code - added by parsing files or when plugin codes encountered. Array key is the shortcode name.
protected $parseSCFiles; // True if individual shortcode files are to be used
protected $addedCodes; // Apparently not used
protected $registered_codes = array(); // Shortcodes added by plugins TODO make it private
protected $scClasses = array(); // Batch shortcode classes - TODO make it private
protected $scOverride = array(); // Array of codes found in override/ dir
protected $eVars = '';
function __construct()
{
$this->parseSCFiles = true; // Default probably never used, but make sure its defined.
$this->loadOverrideShortcodes();
$this->loadThemeShortcodes();
$this->loadPluginShortcodes();
$this->loadPluginSCFiles();
$this->loadCoreShortcodes();
}
/**
* Register shortcode * Register shortcode
* $classFunc could be function name, class name or object * $classFunc could be function name, class name or object
* $code could be 'true' when class name/object is passed to automate the * $code could be 'true' when class name/object is passed to automate the
* registration of shortcode methods * registration of shortcode methods
* XXX - maybe we could move this to the class?
* *
* @param mixed $classFunc * @param mixed $classFunc
* @param mixed $codes * @param mixed $codes
* @param string $path * @param string $path
* @param boolean $force * @param boolean $force override
* @return e_parse_shortcode
*/ */
function register_shortcode($classFunc, $codes, $path='', $force = false) function registerShortcode($classFunc, $codes, $path = '', $force = false)
{ {
$sc = e107::getScParser();
//If codes is set to true, let's go get a list of shortcode methods //If codes is set to true, let's go get a list of shortcode methods
if(/*is_bool($codes) && */$codes === true) //double check fix if($codes === true)
{ {
$codes = array(); $codes = array();
$tmp = get_class_methods($classFunc); $tmp = get_class_methods($classFunc);
@@ -63,46 +114,47 @@ function register_shortcode($classFunc, $codes, $path='', $force = false)
foreach($codes as $code) foreach($codes as $code)
{ {
$code = strtoupper($code); $code = strtoupper($code);
if((!$sc->isRegistered($code) || $force == true) && !$sc->isOverride($code)) if((!$this->isRegistered($code) || $force == true) && !$this->isOverride($code))
{ {
$sc->registered_codes[$code] = array('type' => 'class', 'path' => $path, 'class' => $classFunc); $this->registered_codes[$code] = array('type' => 'class', 'path' => $path, 'class' => $classFunc);
} }
} }
//register object if required //register object if required
if(null !== $classObj && !isset($sc->scClasses[$classFunc])) if(null !== $classObj && (!isset($this->scClasses[$classFunc]) || $force == true))
{ {
$sc->scClasses[$classFunc] = $classObj; $this->scClasses[$classFunc] = $classObj;
} }
} }
else else
{ {
$codes = strtoupper($codes); $codes = strtoupper($codes);
if((!$sc->isRegistered($code) || $force == true) && !$sc->isOverride($code)) if((!$this->isRegistered($code) || $force == true) && !$this->isOverride($code))
{ {
$sc->registered_codes[$codes] = array('type' => 'func', 'path' => $path, 'function' => $classFunc); $this->registered_codes[$codes] = array('type' => 'func', 'path' => $path, 'function' => $classFunc);
} }
} }
} return $this;
}
/** /**
* Add value to already registered SC object * Add value to already registered SC object
* *
* @param string $className * @param string $className
* @param string $scVarName * @param string $scVarName
* @param mixed $value * @param mixed $value
* @return e_parse_shortcode
*/ */
function setScVar($className, $scVarName, $value) public function setScVar($className, $scVarName, $value)
{
$sc = e107::getScParser();
if(isset($sc->scClasses[$className]))
{ {
$sc->scClasses[$className]->$scVarName = $value; if(isset($this->scClasses[$className]))
{
$this->scClasses[$className]->$scVarName = $value;
}
return $this;
} }
}
/**
/**
* Call function on an already registered SC object * Call function on an already registered SC object
* *
* @param string $className * @param string $className
@@ -111,57 +163,49 @@ function setScVar($className, $scVarName, $value)
* *
* @return mixed|boolean - FALSE if class doesn't exist; otherwise whatever the function returns. * @return mixed|boolean - FALSE if class doesn't exist; otherwise whatever the function returns.
*/ */
function callScFunc($className, $scFuncName, $param= '') public function callScFunc($className, $scFuncName, $param = '')
{
$sc = e107::getScParser();
if(isset($sc->scClasses[$className]))
{ {
return call_user_func(array($sc->scClasses[$className],$scFuncName), $param); if(isset($this->scClasses[$className]))
{
return call_user_func(array($this->scClasses[$className], $scFuncName), $param);
} }
else else
{ {
return FALSE; return FALSE;
} }
} }
/** /**
* Create shortcode object - DEPRECATED. use e_shortcode.php or * Create shortcode object - don't forget you still can use e_shortcode.php
* *
* @param string $class * @param string $class
* @param boolean $force * @param boolean $force
* @return e_shortcode
*/ */
function initShortcodeClass($class, $force = false) public function initShortcodeClass($class, $eVars = null, $force = false)
{
$sc = e107::getScParser();
if(class_exists($class) && ($force || !isset($sc->scClasses[$class])))
{ {
$sc->scClasses[$class] = new $class(); if(class_exists($class, false) && ($force || !isset($this->scClasses[$class])))
{
$this->scClasses[$class] = new $class($eVars);
return $this->scClasses[$class];
}
return null;
} }
}
/**
* Get registered SC object
class e_shortcode *
{ * @param string $className
var $scList = array(); // The actual code - added by parsing files or when plugin codes encountered. Array key is the shortcode name. * @return e_shortcode
var $parseSCFiles; // True if individual shortcode files are to be used */
var $addedCodes; // Apparently not used public function getScObject($className)
var $registered_codes = array(); // Shortcodes added by plugins
var $scClasses = array(); // Batch shortcode classes
var $scOverride = array(); // Array of codes found in override/ dir
private $eVars = '';
function e_shortcode($noload=false)
{ {
global $pref; if(isset($this->scClasses[$className]))
{
$this->parseSCFiles = true; // Default probably never used, but make sure its defined. return call_user_func(array($this->scClasses[$className], $scFuncName), $param);
}
$this->loadOverrideShortcodes(); // TODO - throw exception?
$this->loadThemeShortcodes(); return new e_shortcode();
$this->loadPluginShortcodes();
$this->loadPluginSCFiles();
} }
/** /**
@@ -169,14 +213,11 @@ class e_shortcode
* *
* @return void * @return void
*/ */
protected function loadOverrideShortcodes() protected function loadOverrideShortcodes()
{ {
$pref = e107::getConfig('core')->getPref(); if(e107::getPref('sc_override'))
if(varset($pref['sc_override']))
{ {
$tmp = explode(',', $pref['sc_override']); $tmp = explode(',', e107::getPref('sc_override'));
foreach($tmp as $code) foreach($tmp as $code)
{ {
$code = strtoupper(trim($code)); $code = strtoupper(trim($code));
@@ -276,9 +317,8 @@ class e_shortcode
* Common Auto-Register function for class methods. * Common Auto-Register function for class methods.
* *
*/ */
private function registerClassMethods($classFunc,$path) private function registerClassMethods($classFunc, $path)
{ {
$this->scClasses[$classFunc] = new $classFunc; $this->scClasses[$classFunc] = new $classFunc;
$tmp = get_class_methods($classFunc); $tmp = get_class_methods($classFunc);
@@ -296,7 +336,6 @@ class e_shortcode
} }
} }
/** /**
* Register Core Shortcode Batches. * Register Core Shortcode Batches.
* FIXME - currently loaded all the time (even on front-end) * FIXME - currently loaded all the time (even on front-end)
@@ -358,7 +397,7 @@ class e_shortcode
//register once //register once
if(!$this->isScClass($classname)) if(!$this->isScClass($classname))
{ {
register_shortcode($extraCodes, true); $this->registerShortcode($extraCodes, true);
} }
//always overwrite object //always overwrite object
@@ -373,6 +412,7 @@ class e_shortcode
} }
$ret = preg_replace_callback('#\{(\S[^\x02]*?\S)\}#', array(&$this, 'doCode'), $text); $ret = preg_replace_callback('#\{(\S[^\x02]*?\S)\}#', array(&$this, 'doCode'), $text);
$this->parseSCFiles = $saveParseSCFiles; // Restore previous value $this->parseSCFiles = $saveParseSCFiles; // Restore previous value
$this->eVars = null; // reset eVars
return $ret; return $ret;
} }
@@ -460,11 +500,13 @@ class e_shortcode
{ {
include_once($this->registered_codes[$code]['path']); include_once($this->registered_codes[$code]['path']);
} }
$this->scClasses[$_class] = new $_class; $this->initShortcodeClass($_class, $this->eVars, false);
//$this->scClasses[$_class] = new $_class;
// make eVars available to the SC object members
//$this->scClasses[$_class]->eVars = $this->eVars;
} }
if(method_exists($this->scClasses[$_class], $_method)) if(method_exists($this->scClasses[$_class], $_method))
{ {
$ret = $this->scClasses[$_class]->$_method($parm, $sc_mode); $ret = $this->scClasses[$_class]->$_method($parm, $sc_mode);
} }
else else
@@ -649,4 +691,50 @@ class e_shortcode
return $cur_shortcodes; return $cur_shortcodes;
} }
} }
?>
class e_shortcode
{
/**
* Stores passed to shortcode handler simple parser object
* @var e_vars
*/
protected $eVars = null;
/**
* Storage for external values
* @var e_vars
*/
protected $scVars = null;
public function __construct($eVars = null)
{
$this->eVars = $eVars;
$this->scVars = new e_vars();
}
/**
* Add shortcode value
* <code>e107::getScObject('class_name')->setScVar('some_property', $some_value);</code>
*
* @param string $name
* @param mixed $value
* @return e_parse_shortcode
*/
public function setScVar($name, $value)
{
$this->scVars->$name = $value;
return $this;
}
/**
* Retrieve shortcode value
* code>$some_value = e107::getScObject('class_name')->getScVar('some_property');</code>
*
* @param string $name
* @return mixed
*/
public function getScVar($name)
{
return $this->scVars->$name;
}
}