Skip to content

Instantly share code, notes, and snippets.

@mneil
Created February 4, 2015 18:08
Show Gist options
  • Save mneil/69265d0d0cbbb2515dcc to your computer and use it in GitHub Desktop.
Save mneil/69265d0d0cbbb2515dcc to your computer and use it in GitHub Desktop.
moot joomla loader muut patch
<?php
/*
* MootLoader
* @version $Id: mootloader.php 1.0.1
* @date 04/15/2013
* @sikumbang @erwinschro @mneil [email protected]
* @site http://www.templateplazza.com
* @package Joomla 2.5.x
* @license GNU General Public License version 2 or later; see LICENSE.txt
*
* 1.0.1
* - Change api.moot.it to https://muut.com
* - update CDN path with production releases for client/css
* - COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
*/
// no direct access
defined('_JEXEC') or die;
jimport( 'joomla.event.plugin' );
class plgContentMootLoader extends JPlugin
{
protected $muut_url = 'https://muut.com/i/';
//protected $_pluginbase = ''.JURI::base(true).'/plugins/content/paywithsocial/';
protected static $modules = array();
function plgContentMootLoader(&$subject, $params){
parent::__construct( $subject, $params );
}
function onContentPrepare( $context, &$article, &$params, $limitstart=0 )
{
$view = JRequest::getString('view', '');
// Don't run this plugin when the content is being indexed
if ($context == 'com_finder.indexer') { return true; }
//if ($context == 'com_media.file' ) { return true; }
// check if auto load enabled, if yes then no need to find tags in article page but it's ok to find tag in category blog layout
if($this->params->get('auto_load') && ($view=="article") ) { return; }
// reguler expression to find plugin tag in content component
$regex = '#{moot}(.*?){/moot}#s';
if ( preg_match_all( $regex, $article->text, $matches, PREG_SET_ORDER ) > 0)
{
// Inject meta, js, css etc
$this->_loadjs($article, $params);
$this->_doReplace($article, $params);
} else {
return;
}
} // end onContentPrepare function
function onContentAfterDisplay($context, &$article, &$params, $page = 0)
{
$view = JRequest::getString('view', '');
// get params
$theme = $this->params->get('color_theme','default');
$mainframe = JFactory::getApplication();
if ($mainframe->isAdmin()) {
return '';
}
// is auto load option enabled? if no then don't execute this
if(!$this->params->get('auto_load')) { return; }
// don;t load on category blog or featured page
if ( ($view=='category') || ($view=='featured') ) { return; }
$categories = array();
$cats = $this->params->get('catids');
if (is_array($cats)) {
$categories = $cats;
} else {
$categories[] = $cats;
}
if ( in_array($article->catid, $categories) ) {
$this->_loadcss($theme);
$this->_loadjs($article, $params);
return $this->_doAutoLoad($context, $article);
} else {
return;
}
}
protected function _doAutoLoad($context, &$article) {
$doc = JFactory::getDocument();
$view = JRequest::getString('view', '');
$useJqueryMethod = false; // useful for debugging
if ($view == 'article') {
//$article_path = JRoute::_(ContentHelperRoute::getArticleRoute($article->id, $article->catid));
$article_path = $article->alias;
} else {
$article_path = '';
}
$forum_name = $this->params->get('uname'); // forum name
$cat_name = $this->params->get('cat_name');
$data_label = $this->params->get('comment_label'); // label prompt for a new post
$moot_url = "{$this->muut_url}{$forum_name}/{$cat_name}/{$article_path}";
if ($useJqueryMethod) {
$script = '
jQuery(document).ready(function() {
jQuery("#tp-moot").moot({
url: "'.$moot_url.'", label: "'.$data_label.'"
});
});
';
$doc->addScriptDeclaration($script);
$replacement = '<div id="tp-moot"></div>';
} else {
$replacement = '<a class="moot" href="'.$moot_url.'" data-label="'.$data_label.'"></a>';
}
return $replacement;
}
private function _doReplace($article, $params)
{
$doc = JFactory::getDocument();
// Get Plugin info
$pluginbase = ''.JURI::base(true).'/plugins/content/mootloader/';
$view = JRequest::getString('view', '');
$useJqueryMethod = false; // useful for debugging
$args = array();
// reguler expression to find plugin tag in content
$regex = '#{moot}(.*?){/moot}#s';
if ( preg_match_all($regex, $article->text, $matches, PREG_PATTERN_ORDER) > 0)
{
foreach ( $matches[0] as $match )
{
$args = preg_replace("/{.+?}/", "", $match);
$argsArr = explode( "|", $args);
$numofArgs = count($argsArr);
$article_path = $article->alias;
/*
if ( ($view == 'category') && ($numofArgs == 1) ) {
$forum_name = ( $argsArr[0] && (trim($argsArr[0]) != "") ) ? trim($argsArr[0]) : ''; // forum name
$color_theme = 'default';
$data_label = '';
$moot_url = "http://api.moot.it/".$forum_name;
} elseif ( ($view == 'category') && ($numofArgs == 2) ) {
$forum_name = ( $argsArr[0] && (trim($argsArr[0]) != "") ) ? trim($argsArr[0]) : ''; // forum name
$color_theme = ( $argsArr[1] && (trim($argsArr[1]) != "") ) ? trim($argsArr[1]) : ''; // theme
$data_label = '';
$moot_url = "http://api.moot.it/".$forum_name;
}
*/
if ( ($view == 'article') && ($numofArgs == 1) ) {
$forum_name = ( $argsArr[0] && (trim($argsArr[0]) != "") ) ? trim($argsArr[0]) : ''; // forum name
$color_theme = 'default';
$data_label = '';
$moot_url = "{$this->muut_url}{$forum_name}";
} elseif ( ($view=='article') && ($numofArgs == 2) ) {
$forum_name = ( $argsArr[0] && (trim($argsArr[0]) != "") ) ? trim($argsArr[0]) : ''; // forum name
$color_theme = ( $argsArr[1] && (trim($argsArr[1]) != "") ) ? trim($argsArr[1]) : 'default'; // theme
$cat_name = '';
$data_label = '';
$moot_url = "{$this->muut_url}{$forum_name}/{$article_path}";
} elseif ( ($view=='article') && ($numofArgs == 3) ) {
$forum_name = ( $argsArr[0] && (trim($argsArr[0]) != "") ) ? trim($argsArr[0]) : ''; // forum name
$color_theme = ( $argsArr[1] && (trim($argsArr[1]) != "") ) ? trim($argsArr[1]) : 'default'; // color theme
$cat_name = ( $argsArr[2] && (trim($argsArr[2]) != "") ) ? trim($argsArr[2]) : ''; // moot category name
$data_label = '';
if($cat_name =='') {
$moot_url = "{$this->muut_url}{$forum_name}}";
} else {
$moot_url = "{$this->muut_url}{$forum_name}/{$cat_name}/{$article_path}";
}
} elseif ( ($view=='article') && ($numofArgs == 4) ) {
$forum_name = ( $argsArr[0] && (trim($argsArr[0]) != "") ) ? trim($argsArr[0]) : ''; // forum name
$color_theme = ( $argsArr[1] && (trim($argsArr[1]) != "") ) ? trim($argsArr[1]) : 'default'; // theme
$cat_name = ( $argsArr[2] && (trim($argsArr[2]) != "") ) ? trim($argsArr[2]) : ''; // article alias
$data_label = ( $argsArr[3] && (trim($argsArr[3]) != "") ) ? trim($argsArr[3]) : ''; // label prompt for a new post
if($cat_name =='') {
$moot_url = "{$this->muut_url}{$forum_name}/{$article_path}";;
} else {
$moot_url = "{$this->muut_url}{$forum_name}/{$cat_name}/{$article_path}";
}
}
if($view=='article') {
$this->_loadcss( $color_theme);
}
if ($useJqueryMethod) {
$script = '
jQuery(document).ready(function() {
jQuery("#tp-moot").moot({
url: "'.$moot_url.'", label: "'.$data_label.'"
});
});
';
$doc->addScriptDeclaration($script);
$replacement = '<div id="tp-moot"></div>';
} else {
$replacement = '<a class="moot" href="'.$moot_url.'" data-label="'.$data_label.'"></a>';
}
if ( ($view=='category') || ($view=='featured') ) {
$replacement = '';
}
$article->text = self::replaceTag("{moot}".$args."{/moot}", $replacement, $article->text);
} // end foreach
} else {
while ( preg_match_all( $regex, $article->text, $matches, PREG_SET_ORDER ) )
{
$article->text = preg_replace($regex, '', $article->text, 1);
}
}
} // end getMoot HTML function
private function _loadcss( $theme)
{
$pluginbase = ''.JURI::base(true).'/plugins/content/mootloader/';
$doc = JFactory::getDocument();
if($theme=='default') {
$doc->addStyleSheet('//cdn.muut.com/1/moot.css');
} else {
$doc->addStyleSheet( $pluginbase.'themes/'.$theme.'/style.css'); // local
}
}
private function _loadjs(&$article, &$params)
{
$format = JRequest::getCMD('format');
if (($format=='pdf')||($format=='feed')) return;
$pluginbase = ''.JURI::base(true).'/plugins/content/mootloader/';
$doc = JFactory::getDocument();
// Get plugin params
$loadjquery = $this->params->get('load_jquery');
// load javascripts
JLoader::import( 'joomla.version' );
$version = new JVersion();
if (version_compare( $version->RELEASE, '2.5', '<='))
{
if ( !JFactory::getApplication()->get('jquery') && $loadjquery ) {
if ($loadjquery==1) {
$doc->addScript("http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js");
} elseif($loadjquery==2) {
$doc->addScript($pluginbase.'assets/jquery.min.js');
}
JFactory::getApplication()->set('jquery', true);
}
$doc->addScript($pluginbase.'assets/jquery.noconflict.js');
} else {
JHtml::_('jquery.framework');
}
// load moot js
$doc->addScript('//cdn.muut.com/1/moot.min.js'); // cdn
}
// replace plugin-calls and try to remove enclosing paragraphs
// http://www.bretteleben.de
/* parameters
$myneedle the string to replace
$myreplacement what to insert
$myhaystack where to search
*/
private static function replaceTag( $myneedle, $myreplacement, $myhaystack) {
$myneedle = preg_quote($myneedle, '#');
if(preg_match("#<p>(\s|<br />)*".$myneedle."(\s|<br />)*</p>#s", $myhaystack)>=1){
$myhaystack = preg_replace( "#<p>(\s|<br />)*".$myneedle."(\s|<br />)*</p>#s", $myreplacement , $myhaystack ,1);
}
else{
$myhaystack = preg_replace( "#".$myneedle."#s", $myreplacement , $myhaystack ,1);
}
return $myhaystack;
}
} // end class
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment