From f1105af7c3a057a1e48b5067ae05df04c325b531 Mon Sep 17 00:00:00 2001 From: Colin Tucker Date: Thu, 16 Nov 2017 16:06:31 +1100 Subject: [PATCH] Added API class and support for settings within YAML config --- src/API/GoogleAPI.php | 101 +++++++++++++++++++++++++++++++ src/Extensions/GoogleConfig.php | 11 ++-- src/Extensions/PageExtension.php | 3 +- 3 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 src/API/GoogleAPI.php diff --git a/src/API/GoogleAPI.php b/src/API/GoogleAPI.php new file mode 100644 index 0000000..673fb06 --- /dev/null +++ b/src/API/GoogleAPI.php @@ -0,0 +1,101 @@ +=5.6.0 + * + * For full copyright and license information, please view the + * LICENSE.md file that was distributed with this source code. + * + * @package SilverWare\Google\API + * @author Colin Tucker + * @copyright 2017 Praxis Interactive + * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause + * @link https://github.com/praxisnetau/silverware-google + */ + +namespace SilverWare\Google\API; + +use SilverStripe\Core\Config\Configurable; +use SilverStripe\Core\Injector\Injectable; +use SilverStripe\SiteConfig\SiteConfig; + +/** + * An object to encapsulate Google API data and methods. + * + * @package SilverWare\Google\API + * @author Colin Tucker + * @copyright 2017 Praxis Interactive + * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause + * @link https://github.com/praxisnetau/silverware-google + */ +class GoogleAPI +{ + use Injectable; + use Configurable; + + /** + * Answers the API language from site or YAML configuration. + * + * @return string + */ + public function getAPILanguage() + { + $lang = SiteConfig::current_site_config()->GoogleAPILanguage; + + if (!$lang) { + $lang = self::config()->api_language; + } + + return $lang; + } + + /** + * Answers the analytics tracking ID from site or YAML configuration. + * + * @return string + */ + public function getAnalyticsTrackingID() + { + $id = SiteConfig::current_site_config()->GoogleAnalyticsTrackingID; + + if (!$id) { + $id = self::config()->analytics_tracking_id; + } + + return $id; + } + + /** + * Answers the site verification code from site or YAML configuration. + * + * @return string + */ + public function getVerificationCode() + { + $code = SiteConfig::current_site_config()->GoogleVerificationCode; + + if (!$code) { + $code = self::config()->verification_code; + } + + return $code; + } + + /** + * Answers true if analytics is enabled for the site. + * + * @return boolean + */ + public function isAnalyticsEnabled() + { + $enabled = SiteConfig::current_site_config()->GoogleAnalyticsEnabled; + + if (!$enabled) { + $enabled = self::config()->analytics_enabled; + } + + return (boolean) $enabled; + } +} diff --git a/src/Extensions/GoogleConfig.php b/src/Extensions/GoogleConfig.php index a48575a..41fa8f4 100644 --- a/src/Extensions/GoogleConfig.php +++ b/src/Extensions/GoogleConfig.php @@ -23,6 +23,7 @@ use SilverStripe\Forms\TextField; use SilverWare\Extensions\Config\ServicesConfig; use SilverWare\Forms\FieldSection; +use SilverWare\Google\API\GoogleAPI; /** * An extension of the services config class which adds Google settings to site configuration. @@ -161,12 +162,14 @@ public function getBodyAttributes() { $attributes = []; - if ($this->getSiteConfig()->GoogleAPILanguage) { - $attributes['data-google-api-lang'] = $this->getSiteConfig()->GoogleAPILanguage; + $api = GoogleAPI::singleton(); + + if ($lang = $api->getAPILanguage()) { + $attributes['data-google-api-lang'] = $lang; } - if ($this->getSiteConfig()->GoogleAnalyticsEnabled) { - $attributes['data-google-tracking-id'] = $this->getSiteConfig()->GoogleAnalyticsTrackingID; + if ($api->isAnalyticsEnabled()) { + $attributes['data-google-tracking-id'] = $api->getAnalyticsTrackingID(); } return $attributes; diff --git a/src/Extensions/PageExtension.php b/src/Extensions/PageExtension.php index 545e261..25783de 100644 --- a/src/Extensions/PageExtension.php +++ b/src/Extensions/PageExtension.php @@ -20,6 +20,7 @@ use SilverStripe\Core\Convert; use SilverStripe\Core\Extension; use SilverStripe\SiteConfig\SiteConfig; +use SilverWare\Google\API\GoogleAPI; /** * An extension which adds Google features to pages. @@ -46,7 +47,7 @@ class PageExtension extends Extension */ public function MetaTags(&$tags) { - if ($code = SiteConfig::current_site_config()->GoogleVerificationCode) { + if ($code = GoogleAPI::singleton()->getVerificationCode()) { // Add New Line (if does not exist):