From ab3f264ff13a4887d577190ac8a5287b168d863e Mon Sep 17 00:00:00 2001 From: sgiehl <stefan@piwik.org> Date: Thu, 16 Jan 2014 15:07:19 +0100 Subject: [PATCH] code cleanup; moved require of Socials.php to seperate method --- core/Common.php | 15 +++++++++++++++ plugins/Referrers/API.php | 14 ++++++-------- plugins/Referrers/functions.php | 9 +++++---- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/core/Common.php b/core/Common.php index 26dec8e25e..3312a83569 100644 --- a/core/Common.php +++ b/core/Common.php @@ -749,6 +749,21 @@ class Common return $searchEngines; } + /** + * Returns list of social networks by URL + * + * @see core/DataFiles/Socials.php + * + * @return array Array of ( URL => Social Network Name ) + */ + public static function getSocialUrls() + { + require_once PIWIK_INCLUDE_PATH . '/core/DataFiles/Socials.php'; + + $searchEngines = $GLOBALS['Piwik_socialUrl']; + return $searchEngines; + } + /** * Returns list of provider names * diff --git a/plugins/Referrers/API.php b/plugins/Referrers/API.php index 11975517ea..fe8248fc89 100644 --- a/plugins/Referrers/API.php +++ b/plugins/Referrers/API.php @@ -316,8 +316,6 @@ class API extends \Piwik\Plugin\API */ public function getSocials($idSite, $period, $date, $segment = false, $expanded = false) { - require PIWIK_INCLUDE_PATH . '/core/DataFiles/Socials.php'; - $dataTable = $this->getDataTable(Archiver::WEBSITES_RECORD_NAME, $idSite, $period, $date, $segment, $expanded); $dataTable->filter('ColumnCallbackDeleteRow', array('label', function ($url) { return !isSocialUrl($url); })); @@ -349,21 +347,21 @@ class API extends \Piwik\Plugin\API */ public function getUrlsForSocial($idSite, $period, $date, $segment = false, $idSubtable = false) { - require PIWIK_INCLUDE_PATH . '/core/DataFiles/Socials.php'; - $dataTable = $this->getDataTable(Archiver::WEBSITES_RECORD_NAME, $idSite, $period, $date, $segment, $expanded = true); // get the social network domain referred to by $idSubtable + $socialNetworks = Common::getSocialUrls(); + $social = false; if ($idSubtable !== false) { --$idSubtable; - reset($GLOBALS['Piwik_socialUrl']); + reset($socialNetworks); for ($i = 0; $i != (int)$idSubtable; ++$i) { - next($GLOBALS['Piwik_socialUrl']); + next($socialNetworks); } - $social = current($GLOBALS['Piwik_socialUrl']); + $social = current($socialNetworks); } // filter out everything but social network indicated by $idSubtable @@ -457,7 +455,7 @@ class API extends \Piwik\Plugin\API $socialName = $row->getColumn('label'); $i = 1; // start at one because idSubtable=0 is equivalent to idSubtable=false - foreach ($GLOBALS['Piwik_socialUrl'] as $domain => $name) { + foreach (Common::getSocialUrls() as $domain => $name) { if ($name == $socialName) { $row->c[Row::DATATABLE_ASSOCIATED] = $i; break; diff --git a/plugins/Referrers/functions.php b/plugins/Referrers/functions.php index 96c231b8b3..12a175fbbd 100644 --- a/plugins/Referrers/functions.php +++ b/plugins/Referrers/functions.php @@ -37,7 +37,7 @@ function getPathFromUrl($url) */ function getSocialNetworkFromDomain($url) { - foreach ($GLOBALS['Piwik_socialUrl'] AS $domain => $name) { + foreach (Common::getSocialUrls() AS $domain => $name) { if(preg_match('/(^|[\.\/])'.$domain.'([\.\/]|$)/', $url)) { @@ -58,7 +58,7 @@ function getSocialNetworkFromDomain($url) */ function isSocialUrl($url, $socialName = false) { - foreach ($GLOBALS['Piwik_socialUrl'] AS $domain => $name) { + foreach (Common::getSocialUrls() AS $domain => $name) { if (preg_match('/(^|[\.\/])'.$domain.'([\.\/]|$)/', $url) && ($socialName === false || $name == $socialName)) { @@ -79,11 +79,12 @@ function isSocialUrl($url, $socialName = false) function getSocialsLogoFromUrl($domain) { $social = getSocialNetworkFromDomain($domain); + $socialNetworks = Common::getSocialUrls(); $filePattern = 'plugins/Referrers/images/socials/%s.png'; - foreach ($GLOBALS['Piwik_socialUrl'] as $domainKey => $name) { - if ($social == $GLOBALS['Piwik_socialUrl'][$domainKey] && file_exists(PIWIK_DOCUMENT_ROOT . '/' . sprintf($filePattern, $domainKey))) { + foreach ($socialNetworks as $domainKey => $name) { + if ($social == $socialNetworks[$domainKey] && file_exists(PIWIK_DOCUMENT_ROOT . '/' . sprintf($filePattern, $domainKey))) { return sprintf($filePattern, $domainKey); } } -- GitLab