Files
sage/functions.php
QWp6t eae36be2c9 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.
2016-07-16 00:35:45 -07:00

54 lines
1.8 KiB
PHP

<?php
/**
* 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:
* 1. WordPress detects theme in themes/sage
* 2. When we activate, we tell WordPress that the theme is actually in themes/sage/templates
* 3. When we call get_template_directory() or get_template_directory_uri(), we point it back to themes/sage
*
* We do this so that the Template Hierarchy will look in themes/sage/templates for core WordPress themes
* But functions.php, style.css, and index.php are all still located in themes/sage
*
* themes/sage/index.php also contains some self-correcting code, just in case the template option gets reset
*/
add_filter('stylesheet', function ($stylesheet) {
return dirname($stylesheet);
});
add_action('after_switch_theme', function () {
$stylesheet = get_option('stylesheet');
if (basename($stylesheet) !== 'templates') {
update_option('stylesheet', $stylesheet . '/templates');
}
});
/**
* Sage includes
*
* The $sage_includes array determines the code library included in your theme.
* Add or remove files to the array as needed. Supports child theme overrides.
*
* Please note that missing files will produce a fatal error.
*/
$sage_includes = [
'src/helpers.php', // Helper functions
'src/setup.php', // Theme setup
'src/filters.php', // Filters
'src/admin.php' // Admin
];
array_walk($sage_includes, function ($file) {
if (!locate_template($file, true, true)) {
trigger_error(sprintf(__('Error locating %s for inclusion', 'sage'), $file), E_USER_ERROR);
}
});