Ok guys, here is the fix that I did, so far it works on mine.
* Fixed loading an uncached snippet client script when using the given API
* Fixed double loading client script on uncached document and using cached snippet tag
* Fixed MODx tag parsing on uncached snippet
Replace outputContent function on document.parser.class.inc.php with the one provided below.
function outputContent($noEvent=false) {
$this->documentOutput = $this->documentContent;
// check for non-cached snippet output
if(strpos($this->documentOutput, '[!')>-1) {
$this->documentOutput = str_replace('[!', '[[', $this->documentOutput);
$this->documentOutput = str_replace('!]', ']]', $this->documentOutput);
// Clean up client script array
$this->jscripts = array();
$this->sjscripts = array();
// Parse document source
$this->documentOutput = $this->parseDocumentSource($this->documentOutput);
// Insert Startup jscripts & CSS scripts into template - template must have a <head> tag
if ($js = $this->getRegisteredClientStartupScripts()){
// change to just before closing </head>
$this->documentOutput = preg_replace("/(<\/head>)/i", $js."\n\\1", $this->documentOutput);
}
// Insert jscripts & html block into template - template must have a </body> tag
if ($js = $this->getRegisteredClientScripts()){
$this->documentOutput = preg_replace("/(<\/body>)/i", $js."\n\\1", $this->documentOutput);
}
// Parse document source
$this->documentOutput = $this->parseDocumentSource($this->documentOutput);
}
// Parse document source
$this->documentOutput = $this->parseDocumentSource($this->documentOutput);
// remove all unused placeholders
if(strpos($this->documentOutput, '[+')>-1) {
$matches= array();
preg_match_all('~\[\+(.*?)\+\]~', $this->documentOutput, $matches);
if($matches[0]) $this->documentOutput = str_replace($matches[0],'',$this->documentOutput);
}
$this->documentOutput = $this->rewriteUrls($this->documentOutput);
$totalTime = ($this->getMicroTime() - $this->tstart);
$queryTime = $this->queryTime;
$phpTime = $totalTime-$queryTime;
$queryTime = sprintf("%2.4f s", $queryTime);
$totalTime = sprintf("%2.4f s", $totalTime);
$phpTime = sprintf("%2.4f s", $phpTime);
$source = $this->documentGenerated==1 ? "database" : "cache";
$queries = isset($this->executedQueries) ? $this->executedQueries : 0 ;
// send out content-type and content-disposition headers
if(IN_PARSER_MODE=="true") {
$type = !empty($this->contentTypes[$this->documentIdentifier]) ? $this->contentTypes[$this->documentIdentifier] : "text/html";
$header = 'Content-Type: '.$type.'; charset='.$this->config['etomite_charset'];
header($header);
if(!$this->checkPreview() && $this->documentObject['content_dispo']==1) {
if($this->documentObject['alias']) $name = $this->documentObject['alias'];
else {
// strip title of special characters
$name = $this->documentObject['pagetitle'];
$name = strip_tags($name);
$name = strtolower($name);
$name = preg_replace('/&.+?;/', '', $name); // kill entities
$name = preg_replace('/[^\.%a-z0-9 _-]/', '', $name);
$name = preg_replace('/\s+/', '-', $name);
$name = preg_replace('|-+|', '-', $name);
$name = trim($name, '-');
}
$header = 'Content-Disposition: attachment; filename='.$name;
header($header);
}
}
$out = $this->addNotice($this->documentOutput, $type);
if($this->dumpSQL) {
$out .= $this->queryCode;
}
$out = str_replace("[^q^]", $queries, $out);
$out = str_replace("[^qt^]", $queryTime, $out);
$out = str_replace("[^p^]", $phpTime, $out);
$out = str_replace("[^t^]", $totalTime, $out);
$out = str_replace("[^s^]", $source, $out);
$this->documentOutput = $out;
// invoke OnWebPagePrerender event
if(!$noEvent) {
$this->invokeEvent("OnWebPagePrerender");
}
echo $this->documentOutput;
ob_end_flush();
}
Replace all these 3 function called regClientCSS, regClientStartupScript, and regClientScript function located on document.parser.class.inc.php file as well with the one provided below. It contains improvement from Kunal to have a valid javascript and stylsheet output.
# Registers Client-side CSS scripts - these scripts are loaded at inside the <head> tag
function regClientCSS($src){
if ($this->loadedjscripts[$src]) return '';
$this->loadedjscripts[$src] = true;
if (strpos(strtolower($src),"<style")!==false||strpos(strtolower($src),"<link")!==false)
$this->sjscripts[count($this->sjscripts)] = $src;
else $this->sjscripts[count($this->sjscripts)] = "<style type='text/css'>@import url($src)</style>";
}
# Registers Startup Client-side JavaScript - these scripts are loaded at inside the <head> tag
function regClientStartupScript($src, $plaintext=false){
if (!empty($src) && !array_key_exists($src, $this->loadedjscripts)) {
if ($this->loadedjscripts[$src]) return '';
$this->loadedjscripts[$src] = true;
if ($plaintext==true) $this->sjscripts[count($this->sjscripts)] = $src;
elseif (strpos(strtolower($src),"<script")!==false) $this->sjscripts[count($this->sjscripts)] = $src;
else $this->sjscripts[count($this->sjscripts)] = "<script type='text/javascript' src='$src'></script>";
}
}
# Registers Client-side JavaScript - these scripts are loaded at the end of the page
function regClientScript($src, $plaintext=false){
if ($this->loadedjscripts[$src]) return '';
$this->loadedjscripts[$src] = true;
if ($plaintext==true) $this->jscripts[count($this->jscripts)] = $src;
elseif (strpos(strtolower($src),"<script")!==false) $this->jscripts[count($this->jscripts)] = $src;
else $this->jscripts[count($this->jscripts)] = "<script type='text/javascript' src='$src'></script>";
}
That’s all the fix that you need to get the API works correctly. I tried this on brand new installation from MODx0.9.1 without applying any other fixes given on this thread, and so far everything works fine.
Try it and report back to us. If it works fine, I believe this will be included in the next release 0.9.2
Regards,