Skip to content

Commit

Permalink
Merge pull request #61 from vinkla/config
Browse files Browse the repository at this point in the history
Add new config method with dot notation.
  • Loading branch information
vinkla committed Mar 30, 2015
2 parents b81bbb7 + 4c488c9 commit 9b8b7ad
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 68 deletions.
2 changes: 1 addition & 1 deletion wp-content/themes/boilerplate/includes/includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Register menus.
*/
require get_template_directory().'/includes/menus/primary-enu.php';
require get_template_directory().'/includes/menus/primary-menu.php';

/**
* Register sidebars.
Expand Down
4 changes: 2 additions & 2 deletions wp-content/themes/boilerplate/includes/menus/primary-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
/**
* Add a primary WordPress menu.
*/
add_action('after_setup_theme', function() {
register_nav_menu('primary-menu', __('Primary Menu', 'boilerplate');
add_action('after_setup_theme', function () {
register_nav_menu('primary-menu', __('Primary Menu', 'boilerplate'));
});
54 changes: 43 additions & 11 deletions wp-content/themes/boilerplate/library/config.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
<?php

return [
'app' => require get_template_directory().'/config/app.php',
'dashboard' => require get_template_directory().'/config/dashboard.php',
'editor' => require get_template_directory().'/config/editor.php',
'footer' => require get_template_directory().'/config/footer.php',
'login' => require get_template_directory().'/config/login.php',
'menus' => require get_template_directory().'/config/menus.php',
'theme' => require get_template_directory().'/config/theme.php',
'plugins' => require get_template_directory().'/config/plugins.php',
'widgets' => require get_template_directory().'/config/widgets.php',
];
if (!function_exists('config')) {

/**
* Get the specified configuration value.
*
* @param array|string $key
* @param mixed $default
*
* @return mixed
*/
function config($key = null, $default = null)
{
$config = [
'app' => require get_template_directory().'/config/app.php',
'dashboard' => require get_template_directory().'/config/dashboard.php',
'editor' => require get_template_directory().'/config/editor.php',
'footer' => require get_template_directory().'/config/footer.php',
'login' => require get_template_directory().'/config/login.php',
'menus' => require get_template_directory().'/config/menus.php',
'theme' => require get_template_directory().'/config/theme.php',
'plugins' => require get_template_directory().'/config/plugins.php',
'widgets' => require get_template_directory().'/config/widgets.php',
];

if (is_null($key)) {
return $config;
}

if (isset($config[$key])) {
return $config[$key];
}

foreach (explode('.', $key) as $segment) {
if (!is_array($config) || !array_key_exists($segment, $config)) {
return $default;
}

$config = $config[$segment];
}

return $config;
}
}
47 changes: 32 additions & 15 deletions wp-content/themes/boilerplate/library/helpers.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
<?php

/**
* Format bytes to kilobytes, megabytes, gigabytes.
*
* @param int $bytes
* @param int $precision
* @return string
*/
function format_bytes($bytes, $precision = 2)
{
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
if (!function_exists('dd')) {

$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
/**
* Dump the passed variables and end the script.
*
* @param mixed
*/
function dd()
{
array_map(function ($x) { var_dump($x); }, func_get_args());
die;
}
}

if (!function_exists('format_bytes')) {

/**
* Format bytes to kilobytes, megabytes, gigabytes.
*
* @param int $bytes
* @param int $precision
* @return string
*/
function format_bytes($bytes, $precision = 2)
{
$units = ['B', 'KB', 'MB', 'GB', 'TB'];

$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);

$bytes /= pow(1024, $pow);
$bytes /= pow(1024, $pow);

return sprintf('%s %s', round($bytes, $precision), $units[$pow]);
return sprintf('%s %s', round($bytes, $precision), $units[$pow]);
}
}
8 changes: 4 additions & 4 deletions wp-content/themes/boilerplate/library/library.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

/**
* Setup global configuration variable.
* Load helper methods.
*/
$config = require __DIR__.'/config.php';
require __DIR__.'/helpers.php';

/**
* Load helper methods.
* Setup global configuration variable.
*/
require __DIR__.'/helpers.php';
require __DIR__.'/config.php';

/**
* Load Framework Components.
Expand Down
4 changes: 2 additions & 2 deletions wp-content/themes/boilerplate/library/modules/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
/**
* Enable Gzip if available.
*/
if (extension_loaded('zlib') && (ini_get('output_handler') !== 'ob_gzhandler') && $config['app']['gzip']) {
if (extension_loaded('zlib') && (ini_get('output_handler') !== 'ob_gzhandler') && config('app.gzip')) {
add_action('wp', create_function('', '@ob_end_clean();@ini_set("zlib.output_compression", 1);'));
}

/**
* Prevent file edit from WordPress administrator dashboard.
*/
if (!defined('DISALLOW_FILE_EDIT')) {
define('DISALLOW_FILE_EDIT', $config['app']['disallow_file_edit']);
define('DISALLOW_FILE_EDIT', config('app.disallow_file_edit'));
}
4 changes: 2 additions & 2 deletions wp-content/themes/boilerplate/library/modules/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*
* @return void
*/
add_action('wp_dashboard_setup', function () use ($config) {
add_action('wp_dashboard_setup', function () {
global $wp_meta_boxes;

$positions = $config['dashboard']['remove_dashboard_widgets'];
$positions = config('dashboard.remove_dashboard_widgets');

foreach ($positions as $position => $boxes) {
foreach ($boxes as $box) {
Expand Down
16 changes: 8 additions & 8 deletions wp-content/themes/boilerplate/library/modules/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*
* @return int
*/
add_filter('jpeg_quality', function () use ($config) {
return $config['editor']['jpeg_quality'] ?: 100;
add_filter('jpeg_quality', function () {
return config('editor.jpeg_quality', 100);
});

/**
Expand Down Expand Up @@ -56,16 +56,16 @@
*
* @return array
*/
add_filter('tiny_mce_before_init', function ($init) use ($config) {
add_filter('tiny_mce_before_init', function ($init) {
// Add block format elements you want to show in dropdown.
$init['block_formats'] = implode(';', $config['editor']['tinymce_blockformats']);
$init['block_formats'] = implode(';', config('editor.tinymce_blockformats'));

// Disable buttons for the two toolbars.
$toolbar1 = explode(',', $init['toolbar1']);
$buttons1 = array_diff($toolbar1, $config['editor']['tinymce_disabled']);
$buttons1 = array_diff($toolbar1, config('editor.tinymce_disabled'));

$toolbar2 = explode(',', $init['toolbar2']);
$buttons2 = array_diff($toolbar2, $config['editor']['tinymce_disabled']);
$buttons2 = array_diff($toolbar2, config('editor.tinymce_disabled'));

$init['toolbar1'] = implode(',', $buttons1);
$init['toolbar2'] = implode(',', $buttons2);
Expand Down Expand Up @@ -95,8 +95,8 @@
*
* @return void
*/
add_action('admin_menu', function () use ($config) {
$types = $config['editor']['meta_boxes'];
add_action('admin_menu', function () {
$types = config('editor.meta_boxes');

foreach ($types as $type => $boxes) {
foreach ($boxes as $box) {
Expand Down
4 changes: 2 additions & 2 deletions wp-content/themes/boilerplate/library/modules/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
*
* @return string
*/
add_filter('admin_footer_text', function () use ($config) {
return $config['footer']['footer_text'];
add_filter('admin_footer_text', function () {
return config('footer.footer_text');
});
10 changes: 5 additions & 5 deletions wp-content/themes/boilerplate/library/modules/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
*
* @return void
*/
add_action('login_head', function () use ($config) {
$path = $config['login']['image_path'];
$width = sprintf('%spx', $config['login']['image_width']);
add_action('login_head', function () {
$path = config('login.image_path');
$width = sprintf('%spx', config('login.image_width'));

echo "<style> h1 a { background-image:url($path) !important; background-size: 100% auto !important; width: $width !important; } </style>";
});

/**
* Add custom login error message.
*/
add_filter('login_errors', function () use ($config) {
return $config['login']['error_message'];
add_filter('login_errors', function () {
return config('login.error_message');
});

/**
Expand Down
20 changes: 9 additions & 11 deletions wp-content/themes/boilerplate/library/modules/menus.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
*
* @return void
*/
add_action('admin_head', function () use ($config) {
add_action('admin_head', function () {
$elements = '#menu-';
$separator = ', #menu-';

if (current_user_can('manage_options')) {
$elements .= implode($separator, $config['menus']['remove_menu_items']['administrator']);
$elements .= implode($separator, config('menus.remove_menu_items.administrator'));
} else {
$elements .= implode($separator, $config['menus']['remove_menu_items']['default']);
$elements .= implode($separator, config('menus.remove_menu_items.default'));
}

echo sprintf('<style> %s { display: none !important; } </style>', $elements);
Expand All @@ -25,8 +25,8 @@
*
* @return void
*/
add_action('admin_bar_menu', function ($wp_admin_bar) use ($config) {
$nodes = $config['menus']['remove_menu_bar_links'];
add_action('admin_bar_menu', function ($wp_admin_bar) {
$nodes = config('menus.remove_menu_bar_links');

foreach ($nodes as $node) {
$wp_admin_bar->remove_node($node);
Expand All @@ -38,8 +38,8 @@
*
* @return void
*/
add_action('admin_head', function () use ($config) {
if (!$config['menus']['panel_tabs']['help']) {
add_action('admin_head', function () {
if (!config('menus.panel_tabs.help')) {
$screen = get_current_screen();
$screen->remove_help_tabs();
}
Expand All @@ -50,8 +50,6 @@
*
* @return void
*/
add_filter('screen_options_show_screen', function () use ($config) {
if (isset($config['menus']['panel_tabs']['screen_options'])) {
return $config['menus']['panel_tabs']['screen_options'];
}
add_filter('screen_options_show_screen', function () {
return config('menus.panel_tabs.screen_options', true);
});
4 changes: 2 additions & 2 deletions wp-content/themes/boilerplate/library/modules/plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/**
* Add Roots Soil plugin features.
*/
if (count($config['plugins']['soil']['features']) > 0) {
foreach ($config['plugins']['soil']['features'] as $feature) {
if (count(config('plugins.soil.features')) > 0) {
foreach (config('plugins.soil.features') as $feature) {
add_theme_support($feature);
}
}
6 changes: 3 additions & 3 deletions wp-content/themes/boilerplate/library/modules/widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
*
* @return void
*/
add_action('widgets_init', function () use ($config) {
foreach ($config['widgets']['widgets'] as $widget) {
add_action('widgets_init', function () {
foreach (config('widgets.widgets') as $widget) {
unregister_widget($widget);
}
}, 1);

/**
* Filters that allow shortcodes in text widgets.
*/
if ($config['widgets']['shortcodes']) {
if (config('widgets.shortcodes')) {
add_filter('widget_text', 'shortcode_unautop');
add_filter('widget_text', 'do_shortcode');
}

0 comments on commit 9b8b7ad

Please sign in to comment.