Changeset 374609
- Timestamp:
- 04/19/2011 03:41:08 AM (15 years ago)
- Location:
- vbpress/trunk
- Files:
-
- 3 added
- 1 edited
-
core (added)
-
core/views (added)
-
core/views/settings.php (added)
-
vbpress.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
vbpress/trunk/vbpress.php
r374590 r374609 13 13 14 14 class Vbpress { 15 15 16 16 /** 17 17 * Singleton 18 * @static19 18 */ 20 19 function &init() { … … 30 29 31 30 /** 32 * Constructor. Initializes WordPress hooks31 * Constructor. Initializes WordPress hooks 33 32 */ 34 33 function Vbpress() { … … 40 39 /** 41 40 * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook() 42 * @static43 41 */ 44 42 function plugin_activation() { … … 48 46 /** 49 47 * Removes all options. 50 * @static51 48 */ 52 49 function plugin_deactivation() { … … 58 55 */ 59 56 function admin_menu() { 60 $hook = add_menu_page( 'vBPress', 'vBPress', 'manage_options', 'vbpress ', array( $this, 'admin_page' ), '' );57 $hook = add_menu_page( 'vBPress', 'vBPress', 'manage_options', 'vbpress_settings', array( $this, 'settings' ), '' ); 61 58 } 62 63 59 64 60 /** 65 61 * vBPress settings page 66 62 */ 67 function admin_page() {63 function settings() { 68 64 65 $options = new Vbpress_Options(); 66 67 if ( !empty($_POST) && !wp_verify_nonce($_POST['vbpress_update_settings'], 'vbpress_update_settings') ) { 68 echo 'Sorry, your nonce did not verify.'; 69 exit; 70 } else if ( !empty($_POST) ) { 71 72 foreach ( $_POST as $name => $value ) { 73 $options->set($name, $value); 74 } 75 76 $options->save(); 77 } 78 79 echo '<pre>'.print_r($options->get(), 1).'</pre>'; 80 81 // TODO: Could we create 'View' class that is used for loading action views? 82 require('core/views/settings.php'); 83 } 84 } 85 86 class Vbpress_Options { 87 88 var $optionName = 'vbpress'; 89 90 var $validOptionNames = array( 91 'enabled', 92 'vbulletin_path' 93 ); 94 95 var $options = array(); 96 97 /** 98 * Constructor 99 */ 100 function Vbpress_Options() { 101 $this->load(); 102 } 103 104 /** 105 * Populates $options from WordPress database 106 */ 107 function load() { 108 $options = get_option($this->optionName, array()); 109 if (!empty($options)) { 110 $options = $this->decode($options); 111 } 112 } 113 114 /** 115 * Get a specified vBPress option 116 */ 117 function get($name = '') { 118 if ($name == '') { 119 return $this->options; 120 } if (isset($this->options[$name])) { 121 return $this->options[$name]; 122 } 123 124 return null; 125 } 126 127 /** 128 * Set a specified vBPress option 129 */ 130 function set($name, $value, $unset = false) { 131 132 // Strip out the prefix that we use in the forms 133 $name = str_replace('vbpress_', '', $name); 134 135 if ($unset && isset($this->options[$name])) { 136 unset($this->options[$name]); 137 } else if ( in_array($name, $this->validOptionNames) ) { 138 $this->options[$name] = $value; 139 } 140 } 141 142 /** 143 * Add or update the options in the WordPress database 144 */ 145 function save() { 146 if (!get_option($this->optionName)) { 147 add_option($this->optionName, $this->encode($this->options)); 148 } else { 149 update_option($this->optionName, $this->encode($this->options)); 150 } 151 } 152 153 /* 154 * Removes all vBPress option data from the WordPress database 155 */ 156 function purge() { 157 return (delete_option($this->optionName) ? true : false); 158 } 159 160 /** 161 * Encode vBPress option data 162 */ 163 function encode($data) { 164 // TODO: Can we make the assumption that json_encode is an available function? 165 return json_encode($data); 166 } 167 168 /** 169 * Decode vBPress option data 170 */ 171 function decode($data) { 172 return !empty($data) ? json_decode($data) : null; 69 173 } 70 174 }
Note: See TracChangeset
for help on using the changeset viewer.