Shortcode Plugin
Shortcode Plugin
Use wordpress style shortcodes in your page markup. This plugin allows you to make other plugins to implement more shortcodes. This plugin simply loads the shortcode engine, and then processes shortcodes defined in other plugins.
Why? Well, I don't understand regular expressions and not an advanced coder, so using shortcodes gave me an easy way to add functionality. In fact individual plugins not using shortcodes could be easily made by someone more advanced than me.
Currently the addon processes all shortcodes in the before_parse_content
event.
One simple shortcode is included in this plugin. It basically displays the current year and takes no parameters. You use it in your page content like this:
Page CONTENT:
YO! The current year is [show_current_year], is it right?
Results in:
YO! The current year is [show_current_year], is it right?
OTHER SHORTCODE PLUGINS:
- Include another file in the current page
- Search pages for a list of pages meeting your criteria and display info for all of them
- Amazon ASIN Image Link
- Show an image gallery
Notes
I found the shortcodes.php processor file here. It worked without any modification.
The shortcode plugin will include that file which then will control the registration of all other shortcodes you create. I have used individual plugins for each shortcode, but you can put several related shortcodes in a single plugin.
This is the source of the main shortcode processor. The source is displayed here using the include file shortcode
:
**included file: %SYSTEMFOLDER%plugins/jacshortcodes/Classes/Plugin.php not found**
<?php
/**
* Plugin class :: jacShortCodes
*/
namespace Phile\Plugin\Jacshortcodes\jacShortCodes;
// phileJacShortcode.php
// ---------------------------------------
//require_once PLUGINS_DIR.'/jacshortcodes/jacShortCodes/lib/shortcodes.php';
//require_once PLUGINS_DIR.'/jacshortcodes/jacShortCodes/lib/shortcodes_original_0.php';
// THis file has the global "add shortcode function". Should be changed to a plugon function in future.
// do it like info streams does it....in future
require_once PLUGINS_DIR.'/jacshortcodes/jacShortCodes/lib/shortcodes_mod_twigfilter.php';
// ---------------------------------------
// This plugin class must be loaded before all shortcodes.
// There are no shortcodes in this class. This is just an engine that will
// execute all registered shortcodes by other plugins.
// The shortcodes are executed in the hook before_parse_content
// ---------------------------------------
// The engine provides this function that the plugins use to register their shortcodes
// add_shortcode('shortcode text name', 'function to execute');
// i.e.
// add_shortcode('show_current_year', 'show_current_year');
// See sy_showyear.php for example of sinplest shortcode
// See other files for more shortcodes
// ---------------------------------------
class Plugin extends \Phile\Plugin\AbstractPlugin implements \Phile\Gateway\EventObserverInterface {
/*
This will execute ALL registered shortcodes.
NOTE: SHORTCODES ARE DONE BEFORE MARKDOWN......
*/
public function __construct() {
\Phile\Event::registerEvent('before_parse_content', $this);
\Phile\Event::registerEvent('after_parse_content', $this);
//to add the twig extension.
\Phile\Event::registerEvent('template_engine_registered', $this);
// Register Shortcode functions in this file. (see bottom)
// add_shortcode('show_current_year', array($this, 'show_current_year', 'before_parse'));
}
protected function printPhpAsMarkdown($input)
{
return "\n```php\n" . trim(print_r($input, true), "\n") . "\n```\n";
}
public function on($eventKey, $data = null) {
if ($eventKey == 'before_parse_content') {
//Event::triggerEvent('before_parse_content', array('content' => &$this->content, 'page' => &$this));
// --------------------------------------------------------------^ is HACK of core Page.php
//global $shortcode_tags; //from lib
//\Phile\Utility::printnice($shortcode_tags, $exit = false, $type = 'print');
//Check for any escaped shortcodes so we don;t process them
//look for [ but have to escape backslash so [\
$content = $data['content'];
$content = str_replace('[\', '[\jacescape', $content);
$out = do_shortcode($content, 'before_parse');
//Put the escaped shortcodes back in with true shortcode
$out = str_replace('[\jacescape', '[', $out);
$data['content'] = $out;
$data['page']->setContent($out);
/// $data['content'] = $out; //cannot set content this way without Hack.
}
if ($eventKey == 'after_parse_content') {
//Event::triggerEvent('before_parse_content', array('content' => $this->content, 'page' => &$this));
//Event::triggerEvent('after_parse_content', array('content' => &$content, 'page' => &$this));
//global $shortcode_tags; //from lib
//\Phile\Utility::printnice($shortcode_tags, $exit = false, $type = 'print');
//Check for any escaped shortcodes so we don;t process them
//look for [ but have to escape backslash so [\
$content = str_replace('[\', '[\jacescape', $data['content']);
$out = do_shortcode($content, 'after_parse');
//Put the escaped shortcodes back in with true shortcode
$out = str_replace('[\jacescape', '[', $out);
$data['content'] = $out;
$data['page']->setContent($out);
/// $data['content'] = $out; //cannot set content this way.
}
if ($eventKey == 'template_engine_registered') {
//add shortcode as a twig function
$twig_shortcode = $this->jacshortcode_twigfunction();
$data['engine']->addFunction($twig_shortcode);
}
}
// --------------------------------------------
// process an image upload Form
// Note the upload form was prepared in funcRenderImage
private function jacshortcode_twigfunction()
{
// check if this installation is using Twig
if (!\Phile\Utility::isPluginLoaded("phile\\templateTwig")) {
// find the template this install is using
$type = get_class(\Phile\ServiceLocator::getService($this->config['plugin_name']));
// log this issue
error_log("The current Template Service is {$type}. The {{ render }} function is only avaible in Twig.");
echo "The current Template Service is {$type}. The {{ render }} function is only avaible in Twig."; exit;
return false;
} else {
$twig_shortcode = new \Twig_SimpleFunction('jacshortcode', function ($shortcode) {
$out = do_shortcode_jac( $shortcode ); //does not check before or after
// or only do shortcodes that are after parse???
return $out;
});
/** @var $data['engine'] */
// $data['engine']->addFunction($jacimageurl);
return $twig_shortcode;
}
return;
}
// -----------------------------------------------
// SAMPLE SHORTCODES
// You could add more simple short codes here
// Or create a config or something
//
//the sample shortcode function
function show_current_year(){
return date('Y');
}
}