Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Add ability to pass options to modules #57

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion soil.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,49 @@

namespace Roots\Soil;

class Options {
protected static $modules = [];
protected $options = [];

public static function init($module, $options = []) {
if (!isset(self::$modules[$module])) {
self::$modules[$module] = new static((array) $options);
}
return self::$modules[$module];
}

public static function getByFile($file) {
if (file_exists($file) || file_exists(__DIR__ . '/modules/' . $file)) {
return self::get('soil-' . basename($file, '.php'));
}
return [];
}

public static function get($module) {
if (isset(self::$modules[$module])) {
return self::$modules[$module]->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) {
if (current_theme_supports('soil-' . basename($file, '.php'))) {
$feature = 'soil-' . basename($file, '.php');
if (isset($_wp_theme_features[$feature])) {
Options::init($feature, $_wp_theme_features[$feature]);
require_once $file;
}
}
Expand Down