Skip to content

Commit

Permalink
Added API class and support for settings within YAML config
Browse files Browse the repository at this point in the history
  • Loading branch information
colintucker committed Nov 16, 2017
1 parent 06ba8e7 commit f1105af
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 5 deletions.
101 changes: 101 additions & 0 deletions src/API/GoogleAPI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/**
* This file is part of SilverWare.
*
* PHP version >=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 <[email protected]>
* @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 <[email protected]>
* @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;
}
}
11 changes: 7 additions & 4 deletions src/Extensions/GoogleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/Extensions/PageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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):

Expand Down

0 comments on commit f1105af

Please sign in to comment.