Skip to content

Instantly share code, notes, and snippets.

@tungd
Created July 2, 2014 13:38
Show Gist options
  • Save tungd/cef0ca1ac1063c1ee90b to your computer and use it in GitHub Desktop.
Save tungd/cef0ca1ac1063c1ee90b to your computer and use it in GitHub Desktop.
Shortcode for Presestashop's CMS page.
<?php
if (!defined('_PS_VERSION_')) exit;
class TD_Shortcode extends Module {
protected static $initialized = false;
public function __construct() {
$this->name = 'td_shortcode';
$this->tab = 'front_office_feature';
$this->version = 1.0;
$this->author = 'Tung Dao';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Shortcode plugin for Prestashop CMS Content.');
$this->description = $this->l('Supported: [b], [slide] and [module].');
// The module could be initialized multiple times, this guard make sure
// the plugins only be registered with Smarty once.
if (!self::$initialized) {
global $smarty;
self::$initialized = true;
$smarty->registerPlugin('modifier',
'b', array($this, 'smarty_modifier_b'));
$smarty->registerPlugin('modifier',
'module', array($this, 'smarty_modifier_module'));
}
}
public function install() {
if (parent::install() == false ||
$this->registerHook('displayHeader') == false) {
return false;
}
return true;
}
public function hookDisplayHeader() {}
/**
* [b] shortcode
*
* Usage: [b]Some text[/b]
*/
public function smarty_modifier_b($content) {
return preg_replace_callback('/\[b\](.*?)\[\/b\]/ism',
array($this, 'render_b'), $content);
}
protected function render_b($text) {
return sprintf('<b>%s</b>', $text[1]);
}
/**
* [module] shortcode.
* Inspired by http://www.ecartservice.net/prestashop-articles/tired-of-hooks-try-a-plugin-prestashop-1-4/
*
* Usage: [module name="" hook="" ...]
*/
public function smarty_modifier_module($content) {
return preg_replace_callback('/\[module(.*?)\](.*?)\[\/module\]/ism',
array($this, 'render_module'), $content);
}
protected function render_module($text) {
global $cart, $cookie;
$params = $this->parse_shortcode_atts($text[2]);
if (!isset($params['cookie']) OR !$params['cookie'])
$params['cookie'] = $cookie;
if (!isset($params['cart']) OR !$params['cart'])
$params['cart'] = $cart;
$params['altern'] = 1;
$output = '';
$instance = Module::getInstanceByName($params['name']);
if (is_callable(array($instance, 'hook'.$params['hook'])))
$output = call_user_func(array($instance,'hook'.$params['hook']), $params);
return $output;
}
protected function parse_shortcode_atts($text) {
// Copied from Wordpress
// https://core.trac.wordpress.org/browser/tags/3.9.1/src/wp-includes/shortcodes.php#L306
$atts = array();
$pattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/';
$text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
if ( preg_match_all($pattern, $text, $match, PREG_SET_ORDER) ) {
foreach ($match as $m) {
if (!empty($m[1]))
$atts[strtolower($m[1])] = stripcslashes($m[2]);
elseif (!empty($m[3]))
$atts[strtolower($m[3])] = stripcslashes($m[4]);
elseif (!empty($m[5]))
$atts[strtolower($m[5])] = stripcslashes($m[6]);
elseif (isset($m[7]) and strlen($m[7]))
$atts[] = stripcslashes($m[7]);
elseif (isset($m[8]))
$atts[] = stripcslashes($m[8]);
}
} else {
$atts = ltrim($text);
}
return $atts;
}
}
@madc
Copy link

madc commented Aug 3, 2015

See http://stackoverflow.com/a/24532873/709769 for more information to this gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment