assets
1 year ago
loader.php
1 year ago
ppf-admin.php
1 year ago
ppf-class.php
1 year ago
ppf-plugin-addon.php
1 year ago
ppf-plugin.php
1 year ago
ppf-settings.php
1 year ago
ppf-subclass.php
1 year ago
ppf-settings.php
222 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Settings Class |
| 5 | * |
| 6 | * Peter's Plugins Foundation 09 |
| 7 | * |
| 8 | * @package PPF09 |
| 9 | * @author Peter Raschendorfer |
| 10 | * @license GPL2+ |
| 11 | */ |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; // Exit if accessed directly |
| 15 | } |
| 16 | |
| 17 | if ( !class_exists( 'PPF09_Settings' ) ) { |
| 18 | |
| 19 | |
| 20 | abstract class PPF09_Settings extends PPF09_SubClass { |
| 21 | |
| 22 | /** |
| 23 | * name of settings in databse (meta_key) |
| 24 | * |
| 25 | * @since PPF01 |
| 26 | * @var string |
| 27 | * @access protected |
| 28 | * was private prior to PPF04 |
| 29 | */ |
| 30 | protected $_key; |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * current settings |
| 35 | * |
| 36 | * @since PPF01 |
| 37 | * @var array |
| 38 | * @access protected |
| 39 | * was private prior to PPF04 |
| 40 | */ |
| 41 | protected $_settings; |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * settings defaults |
| 46 | * |
| 47 | * @since PPF01 |
| 48 | * @var array |
| 49 | * @access protected |
| 50 | * was private prior to PPF04 |
| 51 | */ |
| 52 | protected $_defaults; |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * Init the Class |
| 57 | * |
| 58 | * @since PPF01 |
| 59 | * @access public |
| 60 | */ |
| 61 | public function __construct( $_core, $defaults ) { |
| 62 | |
| 63 | parent::__construct( $_core, false ); |
| 64 | |
| 65 | $this->_key = $this->get_option_name(); |
| 66 | $this->_defaults = $defaults; |
| 67 | $this->load(); |
| 68 | |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * Sub-Class init |
| 74 | * do nothing |
| 75 | * |
| 76 | * @since PPF01 |
| 77 | * @access public |
| 78 | */ |
| 79 | public function init() { |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /** |
| 84 | * get option name for settings |
| 85 | * |
| 86 | * @since PPF01 |
| 87 | * @access public |
| 88 | * @return string option name |
| 89 | */ |
| 90 | public function get_option_name() { |
| 91 | |
| 92 | return str_replace( '-', '_', $this->core()->get_plugin_slug() ) . '_settings'; |
| 93 | |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /** |
| 98 | * load settings from database |
| 99 | * settings are automatically loaded, but reload can be forced from outside |
| 100 | * |
| 101 | * @since PPF01 |
| 102 | * @access public |
| 103 | */ |
| 104 | public function load() { |
| 105 | |
| 106 | $this->_settings = get_option( $this->_key, array() ); |
| 107 | $this->_settings = wp_parse_args( $this->_settings, $this->_defaults ); |
| 108 | $this->sanitize_settings(); |
| 109 | |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * sanitize settings |
| 115 | * this function can be used to sanitize settings after they are loaded |
| 116 | * |
| 117 | * @since PPF01 |
| 118 | * @access public |
| 119 | */ |
| 120 | public function sanitize_settings() { |
| 121 | |
| 122 | // Just to be defined here |
| 123 | |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /** |
| 128 | * get a settings value |
| 129 | * |
| 130 | * @since PPF01 |
| 131 | * @param string $key settings key |
| 132 | * @access public |
| 133 | */ |
| 134 | public function get( $key ) { |
| 135 | |
| 136 | // as of PPF04 we check if the key exists |
| 137 | if ( array_key_exists( $key, $this->_settings ) ) { |
| 138 | |
| 139 | return $this->_settings[$key]; |
| 140 | |
| 141 | } else { |
| 142 | |
| 143 | return null; |
| 144 | |
| 145 | } |
| 146 | |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /** |
| 151 | * set a settings value |
| 152 | * |
| 153 | * @since PPF01 |
| 154 | * @param string $key settings key |
| 155 | * @param string $value new value |
| 156 | * @access public |
| 157 | */ |
| 158 | public function set( $key, $value ) { |
| 159 | |
| 160 | return $this->_settings[$key] = $value; |
| 161 | |
| 162 | } |
| 163 | |
| 164 | |
| 165 | /** |
| 166 | * save settings to database |
| 167 | * |
| 168 | * @since PPF01 |
| 169 | * @access public |
| 170 | */ |
| 171 | public function save() { |
| 172 | |
| 173 | update_option( $this->_key, $this->_settings ); |
| 174 | |
| 175 | } |
| 176 | |
| 177 | |
| 178 | /** |
| 179 | * get option key name for HTML form fields |
| 180 | * |
| 181 | * @since PPF01 |
| 182 | * @param string $key settings key |
| 183 | * @access public |
| 184 | * @return string option key name |
| 185 | */ |
| 186 | public function get_option_key_name( $key ) { |
| 187 | |
| 188 | return $this->get_option_name() . '[' . $key . ']'; |
| 189 | |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /** |
| 194 | * get the defaults |
| 195 | * |
| 196 | * @since PPF01 |
| 197 | * @access public |
| 198 | * @return array defaults |
| 199 | */ |
| 200 | public function get_defaults() { |
| 201 | |
| 202 | return $this->_defaults; |
| 203 | |
| 204 | } |
| 205 | |
| 206 | |
| 207 | /** |
| 208 | * remove |
| 209 | * |
| 210 | * @since PPF01 |
| 211 | * @access public |
| 212 | */ |
| 213 | public function remove() { |
| 214 | |
| 215 | delete_option( $this->_key ); |
| 216 | |
| 217 | } |
| 218 | |
| 219 | |
| 220 | } |
| 221 | |
| 222 | } |