Refactor functions.php

* Isolate scope of included files
* Load composer dependencies before loading Sage procedural code

Note that scope isolation was originally proposed by @al-the-x in 2014,
and we rejected it at the time. Our theory at the time was that shared
scope might be expected by WordPress developers. But since then we've done
quite a number of things that are atypical of WordPress development,
including our use of namespaces, which in itself is a form of scope
isolation.
This commit is contained in:
QWp6t
2016-07-16 00:35:45 -07:00
parent 325c4d4cc8
commit eae36be2c9

View File

@@ -4,6 +4,13 @@
* Do not edit anything in this file unless you know what you're doing * Do not edit anything in this file unless you know what you're doing
*/ */
/**
* Require Composer autoloader if installed on it's own
*/
if (file_exists($composer = __DIR__ . '/vendor/autoload.php')) {
require_once $composer;
}
/** /**
* Here's what's happening with these hooks: * Here's what's happening with these hooks:
* 1. WordPress detects theme in themes/sage * 1. WordPress detects theme in themes/sage
@@ -20,7 +27,9 @@ add_filter('stylesheet', function ($stylesheet) {
}); });
add_action('after_switch_theme', function () { add_action('after_switch_theme', function () {
$stylesheet = get_option('stylesheet'); $stylesheet = get_option('stylesheet');
basename($stylesheet) == 'templates' || update_option('stylesheet', $stylesheet . '/templates'); if (basename($stylesheet) !== 'templates') {
update_option('stylesheet', $stylesheet . '/templates');
}
}); });
/** /**
@@ -30,8 +39,6 @@ add_action('after_switch_theme', function () {
* Add or remove files to the array as needed. Supports child theme overrides. * Add or remove files to the array as needed. Supports child theme overrides.
* *
* Please note that missing files will produce a fatal error. * Please note that missing files will produce a fatal error.
*
* @link https://github.com/roots/sage/pull/1042
*/ */
$sage_includes = [ $sage_includes = [
'src/helpers.php', // Helper functions 'src/helpers.php', // Helper functions
@@ -39,18 +46,8 @@ $sage_includes = [
'src/filters.php', // Filters 'src/filters.php', // Filters
'src/admin.php' // Admin 'src/admin.php' // Admin
]; ];
array_walk($sage_includes, function ($file) {
foreach ($sage_includes as $file) { if (!locate_template($file, true, true)) {
if (!$filepath = locate_template($file)) {
trigger_error(sprintf(__('Error locating %s for inclusion', 'sage'), $file), E_USER_ERROR); trigger_error(sprintf(__('Error locating %s for inclusion', 'sage'), $file), E_USER_ERROR);
} }
require_once $filepath; });
}
unset($file, $filepath);
/**
* Require Composer autoloader if installed on it's own
*/
if (file_exists($composer = __DIR__ . '/vendor/autoload.php')) {
require_once $composer;
}