Skip to content

Commit bd64a2a

Browse files
committed
Adding support for making settings pages using MvcSettings
1 parent 66cb945 commit bd64a2a

File tree

6 files changed

+236
-1
lines changed

6 files changed

+236
-1
lines changed

core/functions/functions.php

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ function mvc_model($model_name) {
2323
return null;
2424
}
2525

26+
function mvc_setting($settings_name, $setting_key) {
27+
$settings_name = 'mvc_'.MvcInflector::underscore($settings_name);
28+
$option = get_option($settings_name);
29+
if (isset($option[$setting_key])) {
30+
return $option[$setting_key];
31+
}
32+
return null;
33+
}
34+
2635
function mvc_render_to_string($view, $vars=array()) {
2736
$view_pieces = explode('/', $view);
2837
$model_tableized = $view_pieces[0];

core/loaders/mvc_admin_loader.php

+45-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44

55
class MvcAdminLoader extends MvcLoader {
66

7+
public $settings = null;
8+
79
public function admin_init() {
10+
$this->register_settings();
11+
$this->dispatch();
12+
}
13+
14+
public function dispatch() {
815

916
global $plugin_page;
1017

@@ -41,7 +48,7 @@ public function admin_init() {
4148
$title = apply_filters('mvc_admin_title', $title);
4249

4350
}
44-
51+
4552
}
4653

4754
public function add_menu_pages() {
@@ -176,6 +183,43 @@ protected function process_admin_pages($controller_name, $pages) {
176183
return $processed_pages;
177184
}
178185

186+
public function init_settings() {
187+
$this->settings = array();
188+
if (!empty($this->settings_names) && empty($this->settings)) {
189+
foreach ($this->settings_names as $settings_name) {
190+
$instance = MvcSettingsRegistry::get_settings($settings_name);
191+
$this->settings[$settings_name] = array(
192+
'settings' => $instance->settings
193+
);
194+
}
195+
}
196+
}
197+
198+
public function register_settings() {
199+
$this->init_settings();
200+
foreach ($this->settings as $settings_name => $settings) {
201+
$instance = MvcSettingsRegistry::get_settings($settings_name);
202+
$title = $instance->title;
203+
$settings_key = $instance->key;
204+
$section_key = $settings_key.'_main';
205+
add_settings_section($section_key, '', array($instance, 'description'), $settings_key);
206+
register_setting($settings_key, $settings_key, array($instance, 'validate_fields'));
207+
foreach ($instance->settings as $setting_key => $setting) {
208+
add_settings_field($setting_key, $setting['label'], array($instance, 'display_field_'.$setting_key), $settings_key, $section_key);
209+
}
210+
}
211+
}
212+
213+
public function add_settings_pages() {
214+
$this->init_settings();
215+
foreach ($this->settings as $settings_name => $settings) {
216+
$title = MvcInflector::titleize($settings_name);
217+
$title = str_replace(' Settings', '', $title);
218+
$instance = MvcSettingsRegistry::get_settings($settings_name);
219+
add_options_page($title, $title, 'manage_options', $instance->key, array($instance, 'page'));
220+
}
221+
}
222+
179223
public function add_admin_ajax_routes() {
180224
$routes = MvcRouter::get_admin_ajax_routes();
181225
if (!empty($routes)) {

core/loaders/mvc_loader.php

+21
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ protected function load_core() {
4040
'mvc_file_includer',
4141
'mvc_model_registry',
4242
'mvc_object_registry',
43+
'mvc_settings_registry',
4344
'mvc_plugin_loader',
4445
'mvc_templater',
4546
'mvc_inflector',
4647
'mvc_router',
48+
'mvc_settings',
4749
'controllers/mvc_controller',
4850
'controllers/mvc_admin_controller',
4951
'controllers/mvc_public_controller',
@@ -124,6 +126,7 @@ public function init() {
124126
$this->load_controllers();
125127
$this->load_libs();
126128
$this->load_models();
129+
$this->load_settings();
127130
$this->load_functions();
128131

129132
}
@@ -219,6 +222,24 @@ protected function load_models() {
219222

220223
}
221224

225+
protected function load_settings() {
226+
227+
$settings_names = array();
228+
229+
foreach ($this->plugin_app_paths as $plugin_app_path) {
230+
231+
$settings_filenames = $this->file_includer->require_php_files_in_directory($plugin_app_path.'settings/');
232+
233+
foreach ($settings_filenames as $filename) {
234+
$settings_names[] = MvcInflector::class_name_from_filename($filename);
235+
}
236+
237+
}
238+
239+
$this->settings_names = $settings_names;
240+
241+
}
242+
222243
protected function load_functions() {
223244

224245
$this->file_includer->require_php_files_in_directory($this->core_path.'functions/');

core/mvc_settings.php

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
class MvcSettings {
4+
5+
public $name = null;
6+
public $title = null;
7+
public $key = null;
8+
public $settings = null;
9+
10+
function __construct() {
11+
$this->name = get_class($this);
12+
$this->title = MvcInflector::titleize($this->name);
13+
$this->key = 'mvc_'.MvcInflector::underscore($this->name);
14+
$this->init_settings();
15+
}
16+
17+
public function description() {
18+
}
19+
20+
public function page() {
21+
echo '<div>';
22+
echo '<h2>'.$this->title.'</h2>';
23+
echo '<form action="options.php" method="post">';
24+
settings_fields($this->key);
25+
do_settings_sections($this->key);
26+
echo '<input name="Submit" type="submit" value="'.esc_attr('Save Changes').'" />';
27+
echo '</form>';
28+
echo '</div>';
29+
}
30+
31+
public function display_field($setting_key) {
32+
$setting = $this->settings[$setting_key];
33+
$options = get_option($this->key);
34+
$value = isset($options[$setting_key]) ? $options[$setting_key] : null;
35+
if (is_null($value)) {
36+
if ($setting['default']) {
37+
$value = $setting['default'];
38+
} else if ($setting['default_method']) {
39+
$value = $this->{$setting['default_method']}();
40+
}
41+
}
42+
$input_value = $value;
43+
if ($setting['type'] == 'checkbox') {
44+
$input_value = '1';
45+
}
46+
$input_options = array(
47+
'id' => $setting['key'],
48+
'name' => $this->key.'['.$setting['key'].']',
49+
'type' => $setting['type'],
50+
'value' => $input_value
51+
);
52+
if ($setting['type'] == 'checkbox' && $value) {
53+
$input_options['checked'] = 'checked';
54+
}
55+
if ($setting['type'] == 'select') {
56+
if ($setting['options']) {
57+
$input_options['options'] = $setting['options'];
58+
} else if ($setting['options_method']) {
59+
$input_options['options'] = $this->{$setting['options_method']}();
60+
}
61+
}
62+
$html = MvcFormTagsHelper::input($setting_key, $input_options);
63+
echo $html;
64+
}
65+
66+
public function validate_fields($inputs) {
67+
foreach ($inputs as $setting_key => $value) {
68+
if (method_exists($this, 'validate_field_'.$setting_key)) {
69+
$inputs[$setting_key] = $this->{'validate_field_'.$setting_key}($setting_key, $value);
70+
} else {
71+
$inputs[$setting_key] = $this->validate_field($setting_key, $value);
72+
}
73+
}
74+
return $inputs;
75+
}
76+
77+
public function validate_field($setting_key, $value) {
78+
return $value;
79+
}
80+
81+
protected function init_settings() {
82+
if (empty($this->settings)) {
83+
$this->settings = array();
84+
}
85+
$settings = array();
86+
foreach ($this->settings as $key => $setting) {
87+
$defaults = array(
88+
'key' => $key,
89+
'type' => 'text',
90+
'label' => MvcInflector::titleize($key),
91+
'value' => null,
92+
'value_method' => null,
93+
'default' => null,
94+
'default_method' => null,
95+
'options' => null,
96+
'options_method' => null
97+
);
98+
$setting = array_merge($defaults, $setting);
99+
$settings[$key] = $setting;
100+
}
101+
$this->settings = $settings;
102+
}
103+
104+
public function __call($method_name, $arguments) {
105+
if (substr($method_name, 0, 14) == 'display_field_') {
106+
$setting_key = substr($method_name, 14);
107+
$this->display_field($setting_key);
108+
return true;
109+
}
110+
if (substr($method_name, 0, 15) == 'validate_field_') {
111+
$setting_key = substr($method_name, 15);
112+
$this->validate_field($setting_key, $arguments[0]);
113+
return true;
114+
}
115+
MvcError::fatal('Undefined method: '.get_class($this).'::'.$method_name.'.');
116+
}
117+
118+
}
119+
120+
?>

core/mvc_settings_registry.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
class MvcSettingsRegistry {
4+
5+
var $__settings = array();
6+
7+
private function &get_instance() {
8+
static $instance = array();
9+
if (!$instance) {
10+
$instance[0] =& new MvcSettingsRegistry();
11+
}
12+
return $instance[0];
13+
}
14+
15+
public function &get_settings($key) {
16+
$_this =& self::get_instance();
17+
$key = MvcInflector::camelize($key);
18+
$return = false;
19+
if (isset($_this->__settings[$key])) {
20+
$return =& $_this->__settings[$key];
21+
} else if (class_exists($key)) {
22+
$_this->__settings[$key] = new $key();
23+
$return =& $_this->__settings[$key];
24+
}
25+
return $return;
26+
}
27+
28+
public function add_settings($key, &$settings) {
29+
$_this =& self::get_instance();
30+
$key = MvcInflector::camelize($key);
31+
if (!isset($_this->__settings[$key])) {
32+
$_this->__settings[$key] = $settings;
33+
return true;
34+
}
35+
return false;
36+
}
37+
38+
}
39+
40+
?>

wp_mvc.php

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
add_action('admin_init', array($loader, 'admin_init'));
2323
add_action('admin_menu', array($loader, 'add_menu_pages'));
24+
add_action('admin_menu', array($loader, 'add_settings_pages'));
2425
add_action('plugins_loaded', array($loader, 'add_admin_ajax_routes'));
2526

2627
} else {

0 commit comments

Comments
 (0)