/** * Fix link in the canonical metatag * It works only with * - item * - category */ public function canonicalFix() { if (!$this->_jbrequest->is('option', 'com_zoo')) { return null; } $this->_jbdebug->mark('jbzoo-sef::canonicalFix::start'); $flags = array($this->_jbrequest->getWord('task'), $this->_jbrequest->getWord('view'), $this->_jbrequest->getWord('layout')); $newCanUrl = null; if (in_array('item', $flags)) { $itemId = $this->_jbrequest->getSystem('item'); $newCanUrl = $this->_getUrl($this->_itemTable->get($itemId), 'item'); } else { if (in_array('category', $flags)) { $categoryId = $this->_jbrequest->getSystem('category'); $newCanUrl = $this->_getUrl($this->_catTable->get($categoryId), 'category'); } } if ($newCanUrl) { // remove all canocical link $headData = $this->_joomlaDoc->getHeadData(); $canKey = array_search(array('relation' => 'canonical', 'relType' => 'rel', 'attribs' => array()), $headData['links']); unset($headData['links'][$canKey]); $this->_joomlaDoc->setHeadData($headData); // set new url $baseUrl = $this->_jbrouter->getHostUrl(); $this->_joomlaDoc->addHeadLink($baseUrl . $newCanUrl, 'canonical'); } $this->_jbdebug->mark('jbzoo-sef::canonicalFix::finish'); }
/** * Test getHeadData * * @return void */ public function testSetAndGetHeadData() { // Get default values $default = $this->object->getHeadData(); // Test invalid data $return = $this->object->setHeadData('invalid'); $this->assertThat($this->object->getHeadData(), $this->equalTo($default), 'JDocumentHtml::setHeadData invalid data allowed to be set'); // Test return value $this->assertThat($return, $this->isNull(), 'JDocumentHtml::setHeadData did not return null'); // Test setting/ getting values $test_data = array('title' => 'My Custom Title', 'description' => 'My Description', 'link' => 'http://joomla.org', 'metaTags' => array('myMetaTag' => 'myMetaContent'), 'links' => array('index.php' => array('relation' => 'Start', 'relType' => 'rel', 'attribs' => array())), 'styleSheets' => array('test.css' => array('mime' => 'text/css', 'media' => null, 'attribs' => array())), 'style' => array('text/css' => 'body { background: white; }'), 'scripts' => array('test.js' => array('mime' => 'text/javascript', 'defer' => false, 'async' => false)), 'script' => array('text/javascript' => "window.addEvent('load', function() { new JCaption('img.caption'); });"), 'custom' => array("<script>var html5 = true;</script>")); foreach ($test_data as $dataKey => $dataValue) { // Set $return = $this->object->setHeadData(array($dataKey => $dataValue)); // Get $compareTo = $this->object->getHeadData(); // Assert $this->assertThat($compareTo[$dataKey], $this->equalTo($dataValue), 'JDocumentHtml::setHeadData did not return ' . $dataKey . ' properly or setHeadData with ' . $dataKey . ' did not work'); // Test return value $this->assertThat($return, $this->equalTo($this->object), 'JDocumentHtml::setHeadData did not return JDocumentHtml instance'); } // Could use native methods (JDocument::addStyleSheet, etc) like $this->mergeHeadData }
/** * Merging all css or js files (that already have been included via Joomla API) * USE IT ON YOUR OWN RISK!!! * @param string $type * @param bool $isCompress * @return $this */ public function merge($type = 'css', $isCompress = false) { $mergeFiles = array(); $dataKey = $type == 'css' ? 'styleSheets' : 'scripts'; if (method_exists($this->doc, 'getHeadData')) { $docData = $this->doc->getHeadData(); } if (isset($docData) && !empty($docData[$dataKey])) { foreach ($docData[$dataKey] as $pathOrig => $attrs) { // don't get external files $path = str_replace($this->baseurl, '', $pathOrig); $path = preg_replace('#(\\?.*)$#', '', $path); if ($this->_isExternal($path)) { continue; } if ($attrs['mime'] == 'text/css' && (!isset($attrs['media']) || strtolower($attrs['media']) == 'all') || $attrs['mime'] == 'text/javascript') { $fullPath = JPath::clean(JPATH_ROOT . '/' . $path); $fullPathFolder = JPath::clean($_SERVER['DOCUMENT_ROOT'] . '/' . $path); $resPath = false; if (JFile::exists($fullPath)) { $resPath = $fullPath; } else { if (JFile::exists($fullPathFolder)) { $resPath = $fullPathFolder; } } if ($resPath) { $mergeFiles[] = $resPath; unset($docData[$dataKey][$pathOrig]); } } } } if (count($mergeFiles)) { $processor = JBlankMinify::getProcessor($type, $this); if ($path = $processor->minify($mergeFiles, $isCompress)) { $this->setHeadData($dataKey, $docData); if ('css' == $type) { $this->doc->addStylesheet($path, 'text/css'); } else { if ('js' == $type) { $this->doc->addScript($path, "text/javascript", false, false); } } } } return $this; }
/** * Cleanup system links from Joomla, Zoo, JBZoo */ protected function _excludeAssets(array $allPatterns) { $data = $this->doc->getHeadData(); foreach ($allPatterns as $type => $patterns) { foreach ($data[$type] as $path => $meta) { $found = false; foreach ($patterns as $pattern) { if (preg_match('#' . $pattern . '#iu', $path)) { $found = true; break; } } if ($found) { unset($data[$type][$path]); } } } $this->doc->setHeadData($data); }
/** * @testdox Validate the 'title' key exists in the array returned by getHeadData */ public function testValidateKeyExistsInDataReturnedByGetHeadData() { $this->assertArrayHasKey('title', $this->object->getHeadData()); }