My first step in developing a plugin for joomla. My plugin uses the helper.
i need to understand what i am doing wrong in calling my helper.
i think there is something wrong with my logic.
Here is the main code:
Code: Select all
//-- No direct access
defined('_JEXEC') or die;
jimport('joomla.plugin.plugin');
/**
* Content Plugin.
*
* @package JomSpintax
* @subpackage Plugin
*/
class plgContentJomSpintax extends JPlugin
{
private $callbackFunction;
/**
* Define Constructor function
*/
public function __construct(&$subject, $config = array())
{
/**
* Run the parent Constructor
* so we do not forget or ignor anything that is run by jplugin
*/
parent::__construct($subject, $config);
/**
* Define Helper class
*/
require_once __DIR__ . '/helper/helper.php';
/**
* Instantiate helper class
* $this->params contain all parameter define in the xml file
*/
$helper = new SpinTaxHelper($this->params);
$this->callbackFunction = array($helper, 'spintext');
}
/**
* Initiate the plugin
* @param string $context The context of the content passes to the plugin
* @param object $article The article object
* @param object $params The article params
*
* @return boolean True if the function is bypassed. Else True/False based on the replacement action
*/
public function onContentBeforeDisplay($context, $article, $params)
{
/**
* Fail safe check so it only run on content
*/
$parts = explode(".", $context);
if ($parts[0] != "com_content")
{
return;
}
if (stripos($article->text, '{spintext=off}') === true)
{
$article->text = str_ireplace('{spintext=off}', '', $article->text);
}
$app = JFactory::getApplication();
$NoSpin = $app->input->get('external', NULL);
if ($Nospin)
{
SpinTextHelper::SpinText($article, $external);
} else
{
$pattern = '/\{(((?>[^\{\}]+)|(?R))*)\}/x';
$article->text = preg_replace_callback($pattern, $this->callbackFunction, $article->text);
}
}
}
Code: Select all
//-- No direct access
defined('_JEXEC') || die('=;)')
class SpinTextHelper
{
/*
* init Helper
*/
public $params = null;
public function __construct($params = null)
{
$this->params = $params;
}
/*
* Function that does the spinning of the text
*/
public function SpinText($text)
{
return preg_replace_callback($pattern,array($this, 'replace'),$text);
}
public function replace($text)
{
$text = $this->process($text[1]);
$parts = explode('|', $text);
return $parts[array_rand($parts)];
}
}