Skip to content

Instantly share code, notes, and snippets.

@groggu
Last active December 19, 2019 16:12
Show Gist options
  • Save groggu/ad563a26f0b625c9c5952158b0ba8160 to your computer and use it in GitHub Desktop.
Save groggu/ad563a26f0b625c9c5952158b0ba8160 to your computer and use it in GitHub Desktop.
For Fishpig WordPress Series - Custom Wordpress Short Code in Magento http://www.demacmedia.com/magento-commerce/custom-wordpress-shortcode-in-magento-with-fishpig/
<?xml version="1.0"?>
<config>
<modules>
<Demac_FishpigExtension>
<version>0.1.0</version>
</Demac_FishpigExtension>
</modules>
<global>
<models>
<fishpigextension>
<class>Demac_FishpigExtension_Model</class>
</fishpigextension>
</models>
<helpers>
<fishpigextension>
<class>Demac_FishpigExtension_Helper</class>
</fishpigextension>
</helpers>
<events>
<wordpress_shortcode_apply>
<observers>
<fishpigextension>
<type>singleton</type>
<class>Demac_FishpigExtension_Model_Observer</class>
<method>applyShortCodes</method>
</fishpigextension>
</observers>
</wordpress_shortcode_apply>
</events>
</global>
</config>
<?php
/*
All extended shortcode class are extended from the helper class - Fishpig_Wordpress_Helper_Shortcode_Abstract
There are two abstract functions you must implement
public function getTag()
this functions defines [tagname].
protected function _apply(&$content, Fishpig_Wordpress_Model_Post_Abstract $object)
this function is the tag renderer.
*/
class Demac_FishpigExtension_Helper_Shortcodes_GCB extends Fishpig_Wordpress_Helper_Shortcode_Abstract
{
const GCB_TABLE = "_gcb";
/**
* 'contentblock' is the tagname for GCB in wordpress, so I follows for compatibility.
*/
public function getTag()
{
return 'contentblock';
}
protected function _apply(&$content, Fishpig_Wordpress_Model_Post $post)
{
if (($shortcodes = $this->_getShortcodes($content)) !== false) {
/* Add multisite support */
$dbhelper = Mage::helper('wordpress/database');
$multihelper = null;
$blogId = '';
if(Mage::getStoreConfigFlag('wordpress/mu/enabled')) {
$multihelper = Mage::helper('wordpressmu');
$blogId = $multihelper->getBlogId();
}
$tbname = Mage::helper('wordpress/database')->getTablePrefix().$blogId.self::GCB_TABLE;
/* End - Add multisit support */
$ra = $dbhelper->getReadAdapter();
foreach($shortcodes as $shortcode) {
$id = $shortcode->getParams()->getId();
$select = $ra->select();
$select->from($tbname);
if(is_numeric($id)) {
$select->where('id = ?', intval($id));
} else {
$select->where('custom_id = ?', $id);
}
$rc = $ra->fetchAll($select);
if(!empty($rc)) {
$content = str_replace($shortcode->getOpeningTag(), sprintf('<div class="gcd gcd-%s">%s</div>',$id,$rc[0]['value']), $content);
}
}
}
}
}
<?php
/*
* Hook the new shortcode to the wordpress_shortcode_apply event
*/
class Demac_FishpigExtension_Model_Observer
{
public function applyShortCodes($objs)
{
$content = $objs->getContent()->getContent();
$obj = $objs->getObject();
Mage::helper('fishpigextension/shortcodes_GCB')->apply($content,$obj);
$objs->getContent()->setContent($content);
}
}
@groggu
Copy link
Author

groggu commented Apr 14, 2016

Updated for modern FishPig 2016. Hope to release a OpenSource module that supports a subset of the shortcodes ultimate set

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