Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Add options for Google Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
QWp6t committed Mar 21, 2015
1 parent 8443193 commit 8beab05
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
28 changes: 20 additions & 8 deletions modules/google-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
* You can enable/disable this feature in functions.php (or lib/config.php if you're using Sage):
* add_theme_support('soil-google-analytics', 'UA-XXXXX-Y', 'wp_footer');
*/
list($ga_id, $hook) = $options + ['', 'wp_footer'];
add_action($hook, function () use ($ga_id) {
if (!$ga_id) {
return;
}
function load_script() {
$gaID = options('gaID');
if (!$gaID) { return; }
$dummyGA = (defined('WP_ENV') && WP_ENV !== 'production') || current_user_can('manage_options');
?>
<script>
<script>
<?php if (apply_filters('soil/dummyGA', $dummyGA)) : ?>
function ga() {if (window.console) {console.log('Google Analytics: ' + [].slice.call(arguments));}}
<?php else : ?>
Expand All @@ -26,7 +24,21 @@ function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
e.src='//www.google-analytics.com/analytics.js';
r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
<?php endif; ?>
ga('create','<?= $ga_id; ?>','auto');ga('send','pageview');
ga('create','<?= $gaID; ?>','auto');ga('send','pageview');
</script>
<?php
}, 20);
}

function options($option = null) {
static $options;
if (!isset($options)) {
$options = \Roots\Soil\Options::getByFile(__FILE__) + ['', 'wp_footer'];
$options['gaID'] = &$options[0];
$options['hook'] = &$options[1];
}
return is_null($option) ? $options : $options[$option];
}

$hook = options('hook');

add_action($hook, __NAMESPACE__ . '\\load_script', 20);
39 changes: 38 additions & 1 deletion soil.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,49 @@

namespace Roots\Soil;

class Options {
protected static $modules = [];
protected $options = [];

public static function init($module, $options = []) {
if (!isset(self::$modules[$module])) {
self::$modules[$module] = new static((array) $options);
}
return self::$modules[$module];
}

public static function getByFile($file) {
if (file_exists($file) || file_exists(__DIR__ . '/modules/' . $file)) {
return self::get('soil-' . basename($file, '.php'));
}
return [];
}

public static function get($module) {
if (isset(self::$modules[$module])) {
return self::$modules[$module]->options;
}
if (substr($module, 0, 5)!=='soil-') {
return self::get('soil-' . $module);
}
return [];
}

protected function __construct($options) {
$this->set($options);
}

public function set($options) {
$this->options = $options;
}
}

function load_modules() {
global $_wp_theme_features;
foreach (glob(__DIR__ . '/modules/*.php') as $file) {
$feature = 'soil-' . basename($file, '.php');
if (isset($_wp_theme_features[$feature])) {
$options = (array) $_wp_theme_features[$feature];
Options::init($feature, $_wp_theme_features[$feature]);
require_once $file;
}
}
Expand Down

0 comments on commit 8beab05

Please sign in to comment.