Skip to content

Commit eae36be

Browse files
committed
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.
1 parent 325c4d4 commit eae36be

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

functions.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
* Do not edit anything in this file unless you know what you're doing
55
*/
66

7+
/**
8+
* Require Composer autoloader if installed on it's own
9+
*/
10+
if (file_exists($composer = __DIR__ . '/vendor/autoload.php')) {
11+
require_once $composer;
12+
}
13+
714
/**
815
* Here's what's happening with these hooks:
916
* 1. WordPress detects theme in themes/sage
@@ -20,7 +27,9 @@
2027
});
2128
add_action('after_switch_theme', function () {
2229
$stylesheet = get_option('stylesheet');
23-
basename($stylesheet) == 'templates' || update_option('stylesheet', $stylesheet . '/templates');
30+
if (basename($stylesheet) !== 'templates') {
31+
update_option('stylesheet', $stylesheet . '/templates');
32+
}
2433
});
2534

2635
/**
@@ -30,27 +39,15 @@
3039
* Add or remove files to the array as needed. Supports child theme overrides.
3140
*
3241
* Please note that missing files will produce a fatal error.
33-
*
34-
* @link https://github.com/roots/sage/pull/1042
3542
*/
3643
$sage_includes = [
37-
'src/helpers.php', // Helper functions
38-
'src/setup.php', // Theme setup
39-
'src/filters.php', // Filters
40-
'src/admin.php' // Admin
44+
'src/helpers.php', // Helper functions
45+
'src/setup.php', // Theme setup
46+
'src/filters.php', // Filters
47+
'src/admin.php' // Admin
4148
];
42-
43-
foreach ($sage_includes as $file) {
44-
if (!$filepath = locate_template($file)) {
49+
array_walk($sage_includes, function ($file) {
50+
if (!locate_template($file, true, true)) {
4551
trigger_error(sprintf(__('Error locating %s for inclusion', 'sage'), $file), E_USER_ERROR);
4652
}
47-
require_once $filepath;
48-
}
49-
unset($file, $filepath);
50-
51-
/**
52-
* Require Composer autoloader if installed on it's own
53-
*/
54-
if (file_exists($composer = __DIR__ . '/vendor/autoload.php')) {
55-
require_once $composer;
56-
}
53+
});

0 commit comments

Comments
 (0)