From 49fca897a9d83de596d26c8678986dd420586908 Mon Sep 17 00:00:00 2001 From: QWp6t Date: Sat, 14 Mar 2015 19:33:49 -0700 Subject: [PATCH 1/3] Add ability to pass options to modules --- soil.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/soil.php b/soil.php index 7f6c79af..c6b82dd0 100644 --- a/soil.php +++ b/soil.php @@ -14,8 +14,11 @@ namespace Roots\Soil; function load_modules() { + global $_wp_theme_features; foreach (glob(__DIR__ . '/modules/*.php') as $file) { - if (current_theme_supports('soil-' . basename($file, '.php'))) { + $feature = 'soil-' . basename($file, '.php'); + if (isset($_wp_theme_features[$feature])) { + $options = (array) $_wp_theme_features[$feature]; require_once $file; } } From fd0f221691a51d6368e5c91b17f00c8cec0832c9 Mon Sep 17 00:00:00 2001 From: QWp6t Date: Sat, 14 Mar 2015 19:35:19 -0700 Subject: [PATCH 2/3] Add options for Google Analytics --- README.md | 19 +++++++++++---- modules/google-analytics.php | 45 ++++++++++++++++++++++-------------- soil.php | 39 ++++++++++++++++++++++++++++++- 3 files changed, 80 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index f5fdd910..e5235709 100644 --- a/README.md +++ b/README.md @@ -61,15 +61,24 @@ add_theme_support('soil-nice-search'); Enable HTML5 Boilerplate's Google Analytics snippet ```php -add_theme_support('soil-google-analytics'); -define('GOOGLE_ANALYTICS_ID', 'UA-XXXXXX'); +add_theme_support('soil-google-analytics', 'UA-XXXXX-Y'); ``` -By default, the GA snippet will only be shown for non-administrators. Administrators will get a dummy `ga()` function that writes its arguments to the console log for testing and development purposes. If you define `WP_ENV`, then non-production environments will always get dummy `ga()` function. You can override this via the `soil/displayGA` filter. +By default, the script will be loaded from the `wp_footer` hook, but you can specify any other action hook as the third parameter. ```php -add_filter('soil/displayGA', '__return_true'); // Appends H5BP's GA snippet -add_filter('soil/displayGA', '__return_false'); // Appends a dummy `ga()` function that writes arguments to console log +add_theme_support('soil-google-analytics', 'UA-XXXXX-Y', 'wp_head'); // script will load during wp_head +``` + +#### Dummy `ga()` Function + +This module will load a dummy `ga()` function for non-administrators as well as in non-`production` environments (when `WP_ENV` is defined). The function takes all arguments passed to it and logs them to the JavaScript console. + +You can override whether the dummy function is displayed via the `soil/dummyGA` filter. + +```php +add_filter('soil/dummyGA', '__return_true'); // Appends a dummy `ga()` function +add_filter('soil/dummyGA', '__return_false'); // Appends H5BP's GA snippet ``` ### JS to Footer diff --git a/modules/google-analytics.php b/modules/google-analytics.php index 9a76d740..cf8519cf 100644 --- a/modules/google-analytics.php +++ b/modules/google-analytics.php @@ -7,27 +7,38 @@ * * Cookie domain is 'auto' configured. See: http://goo.gl/VUCHKM * You can enable/disable this feature in functions.php (or lib/config.php if you're using Sage): - * add_theme_support('soil-google-analytics'); - * define('GOOGLE_ANALYTICS_ID', 'UA-XXXXXX'); + * add_theme_support('soil-google-analytics', 'UA-XXXXX-Y', 'wp_footer'); */ -function google_analytics() { - if (!defined('GOOGLE_ANALYTICS_ID') || !GOOGLE_ANALYTICS_ID) { - return; - } - $displayGA = (!defined('WP_ENV') || WP_ENV === 'production') && !current_user_can('manage_options'); +function load_script() { + $gaID = options('gaID'); + if (!$gaID) { return; } + $dummyGA = (defined('WP_ENV') && WP_ENV !== 'production') || current_user_can('manage_options'); ?> - 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; } } From 16720abc86304258779e343e56370f07e3822a11 Mon Sep 17 00:00:00 2001 From: QWp6t Date: Wed, 1 Apr 2015 15:03:56 -0700 Subject: [PATCH 3/3] Fix code style --- soil.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/soil.php b/soil.php index 545162e7..fe91942f 100644 --- a/soil.php +++ b/soil.php @@ -35,7 +35,7 @@ public static function get($module) { if (isset(self::$modules[$module])) { return self::$modules[$module]->options; } - if (substr($module, 0, 5)!=='soil-') { + if (substr($module, 0, 5) !== 'soil-') { return self::get('soil-' . $module); } return [];