diff --git a/Classes/Helper.php b/Classes/Helper.php index cb69d6e..b98b435 100644 --- a/Classes/Helper.php +++ b/Classes/Helper.php @@ -10,6 +10,7 @@ namespace WapplerSystems\ABTest2; */ use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; use TYPO3\CMS\Frontend\Page\PageRepository; /** @@ -18,98 +19,100 @@ use TYPO3\CMS\Frontend\Page\PageRepository; * @package WapplerSystems\ABTest2 * @author Sven Wapler */ -class Helper { +class Helper +{ - /** @var int|null */ - protected $currentPageId = null; + /** @var int|null */ + protected $currentPageId = null; - /** @var int|null */ - protected $rootpage_id = null; + /** @var int|null */ + protected $rootpage_id = null; - /** @var int|null */ - protected $realurlConfig = null; + /** @var int|null */ + protected $realurlConfig = null; - /** @var int|null */ - protected $selectBSite = null; + /** @var int|null */ + protected $selectBSite = null; - /** @var int|null */ - protected $cookieLifeTime = null; + /** @var int|null */ + protected $cookieLifeTime = null; - /** @var int|null */ - protected $randomAbPageId = null; + /** @var int|null */ + protected $randomAbPageId = null; - /** @var string */ - protected $additionalHeaderData; + /** @var string */ + protected $additionalHeaderData; /** * * @param array $params - * @param $pObj + * @param $pObj TypoScriptFrontendController * @return void * @throws \InvalidArgumentException */ - public function determineContentId(array $params, &$pObj) { + public function determineContentId(array $params, &$pObj) + { - // only try to change the page if it's not the googlebot. - if(false === stripos($_SERVER['HTTP_USER_AGENT'], 'googlebot')) { + // only try to change the page if it's not the googlebot. + if (false === stripos($_SERVER['HTTP_USER_AGENT'], 'googlebot')) { - $this->currentPageId = $params['pObj']->id; + $this->currentPageId = $pObj->id; - // Get the rootpage_id from realurl config. - $this->realurlConfig = $params['pObj']->TYPO3_CONF_VARS['EXTCONF']['realurl']; - if(array_key_exists($_SERVER['SERVER_NAME'], $this->realurlConfig)) { - $this->rootpage_id = $this->realurlConfig[$_SERVER['SERVER_NAME']]['pagePath']['rootpage_id']; - } else { - $this->rootpage_id = $this->realurlConfig['_DEFAULT']['pagePath']['rootpage_id']; - } - - // If the ID is NULL, then we set this value to the rootpage_id. NULL is the "Home"page, ID is a specific sub-page, e.g. www.domain.de (NULL) - www.domain.de/page.html (ID) - if(!$this->currentPageId) { - if($this->rootpage_id) { - $this->currentPageId = $this->rootpage_id; - } else { - // Leave the function because we can not determine the ID. - return; - } - } + // Get the rootpage_id from realurl config. + $this->realurlConfig = $pObj->TYPO3_CONF_VARS['EXTCONF']['realurl']; + if (array_key_exists($_SERVER['SERVER_NAME'], $this->realurlConfig)) { + $this->rootpage_id = $this->realurlConfig[$_SERVER['SERVER_NAME']]['pagePath']['rootpage_id']; + } else { + $this->rootpage_id = $this->realurlConfig['_DEFAULT']['pagePath']['rootpage_id']; + } - $pageRepository = GeneralUtility::makeInstance(PageRepository::class); - $currentPagePropertiesArray = $pageRepository->getPage($this->currentPageId); + // If the ID is NULL, then we set this value to the rootpage_id. NULL is the "Home"page, ID is a specific sub-page, e.g. www.domain.de (NULL) - www.domain.de/page.html (ID) + if (!$this->currentPageId) { + if ($this->rootpage_id) { + $this->currentPageId = $this->rootpage_id; + } else { + // Leave the function because we can not determine the ID. + return; + } + } - $this->selectBSite = $currentPagePropertiesArray['tx_abtest2_b_id']; - $this->cookieLifeTime = $currentPagePropertiesArray['tx_abtest2_cookie_time']; + $pageRepository = GeneralUtility::makeInstance(PageRepository::class); + $currentPagePropertiesArray = $pageRepository->getPage($this->currentPageId); - if($this->selectBSite) { - if((int)$_COOKIE['abtest2-'.$this->currentPageId] > 0) { - $this->randomAbPageId = (int)$_COOKIE['abtest2-'.$this->currentPageId]; - } else { - $randomPage = rand(0,1); // 0 = original ID; 1 = "B" site. - if($randomPage) { - $this->randomAbPageId = $this->selectBSite; - } else { - $this->randomAbPageId = $this->currentPageId; - } - setcookie('abtest2-'.$this->currentPageId,$this->randomAbPageId,time()+$this->cookieLifeTime); - } + $this->selectBSite = $currentPagePropertiesArray['tx_abtest2_b_id']; + $this->cookieLifeTime = $currentPagePropertiesArray['tx_abtest2_cookie_time']; - // If current page ID is different from the random page ID we set the correct page ID. - if($this->currentPageId != $this->randomAbPageId) { + if ($this->selectBSite) { + if ((int)$_COOKIE['abtest2-' . $this->currentPageId] > 0) { + $this->randomAbPageId = (int)$_COOKIE['abtest2-' . $this->currentPageId]; + } else { + $randomPage = rand(0, 1); // 0 = original ID; 1 = "B" site. + if ($randomPage) { + $this->randomAbPageId = $this->selectBSite; + } else { + $this->randomAbPageId = $this->currentPageId; + } + setcookie('abtest2-' . $this->currentPageId, $this->randomAbPageId, time() + $this->cookieLifeTime); + } + + // If current page ID is different from the random page ID we set the correct page ID. + if ($this->currentPageId !== $this->randomAbPageId) { $pObj->contentPid = $this->randomAbPageId; $GLOBALS['TSFE']->page['content_from_pid'] = $this->randomAbPageId; $GLOBALS['TSFE']->page['no_cache'] = true; - } - } + } + } - // If additional headerdata is present then we specify additionalHeaderData. - $randomPagePropertiesArray = $pageRepository->getPage($this->randomAbPageId); - $this->additionalHeaderData = $randomPagePropertiesArray['tx_abtest2_header']; - if($this->additionalHeaderData) { - $GLOBALS['TSFE']->additionalHeaderData['abtest2'] = $this->additionalHeaderData; - } + // If additional headerdata is present then we specify additionalHeaderData. + $randomPagePropertiesArray = $pageRepository->getPage($this->randomAbPageId); + $this->additionalHeaderData = $randomPagePropertiesArray['tx_abtest2_header']; + if ($this->additionalHeaderData) { + $GLOBALS['TSFE']->additionalHeaderData['abtest2'] = $this->additionalHeaderData; + } - } + } - } + } } diff --git a/ext_emconf.php b/ext_emconf.php index fe103cd..bcf8975 100644 --- a/ext_emconf.php +++ b/ext_emconf.php @@ -10,34 +10,29 @@ * writing. "version" and "dependencies" must not be touched! ***************************************************************/ -$EM_CONF[$_EXTKEY] = array ( - 'title' => 'AB Test Pages', - 'description' => 'This extension supports TYPO3 administrators in performing A/B tests. This is useful when a site owner want to measure whether a new version improves or reduces user interaction compared to the current version.', - 'category' => 'misc', - 'author' => 'IllusionFACTORY', - 'author_email' => 'info@illusion-factory.de', - 'state' => 'alpha', - 'uploadfolder' => false, - 'createDirs' => '', - 'clearCacheOnLoad' => 1, - 'version' => '1.0.2.sw', - 'constraints' => - array ( - 'depends' => - array ( - 'typo3' => '7.6.0-8.7.99', - 'realurl' => '2.0.0-0.0.0', - ), - 'conflicts' => - array ( - ), - 'suggests' => - array ( - ), - ), - 'comment' => '', - 'user' => 'timof', - 'clearcacheonload' => true, - 'author_company' => NULL, +$EM_CONF[$_EXTKEY] = array( + 'title' => 'AB Test Pages', + 'description' => 'With this extension, administrators can deliver different content for the same URL (AB test), depending on cookies or parameters.', + 'category' => 'misc', + 'author' => 'Sven Wappler', + 'author_email' => 'typo3YYYY@wappler.systems', + 'author_company' => 'WapplerSystems', + 'state' => 'stable', + 'uploadfolder' => false, + 'createDirs' => '', + 'clearCacheOnLoad' => true, + 'version' => '0.1.0', + 'constraints' => + array( + 'depends' => + array( + 'typo3' => '7.6.0-8.7.99', + 'realurl' => '2.0.0-0.0.0', + ), + 'conflicts' => + array(), + 'suggests' => + array(), + ), ); diff --git a/ext_localconf.php b/ext_localconf.php index 8df6bce..404fc9a 100644 --- a/ext_localconf.php +++ b/ext_localconf.php @@ -3,4 +3,4 @@ defined('TYPO3_MODE') or die(); -$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['determineId-PostProc']['abtest2'] = 'WapplerSystems\\ABTest2\\ShowPage->SelectId'; \ No newline at end of file +$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['determineId-PostProc']['abtest2'] = 'WapplerSystems\\ABTest2\\Helper->SelectId'; \ No newline at end of file