Last active
December 19, 2019 16:12
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated for modern FishPig 2016. Hope to release a OpenSource module that supports a subset of the shortcodes ultimate set