1: <?php
2:
3: class Sidecar_Plugin_Settings extends Sidecar_Settings_Base {
4:
5: 6: 7:
8: var $option_name;
9:
10: 11: 12:
13: var $installed_version;
14:
15: 16: 17:
18: var $configured = false;
19:
20: 21: 22:
23: protected $_is_encrypted;
24:
25: 26: 27:
28: var $_plugin;
29:
30: 31: 32: 33:
34: function __construct( $plugin, $option_name ) {
35: parent::__construct( $plugin );
36: $this->_plugin = $plugin;
37: $this->option_name = $option_name;
38: add_action( 'shutdown', array( $this, 'shutdown' ) );
39: }
40:
41: 42: 43: 44:
45: function register_form_settings( $form_name ) {
46: $this->register_setting( $form_name, 'Sidecar_Form_Settings' );
47: }
48:
49: 50: 51: 52: 53:
54: function register_setting( $setting_name, $setting_class = false ) {
55: if ( class_exists( $setting_class ) )
56: $this->offsetSet( $setting_name, new $setting_class( $this, $setting_name ) );
57: }
58:
59: 60: 61: 62: 63:
64: function get_setting( $setting_name ) {
65: if ( ! $this->offsetExists( $setting_name ) ) {
66: $setting_value = false;
67: } else {
68: $setting_value = $this->offsetGet( $setting_name );
69: }
70:
71: if ( method_exists( $this->_plugin, $method_name = "get_setting_{$setting_name}" ) ) {
72: $setting_value = call_user_func( array( $this->_plugin, $method_name ), $setting_value );
73: }
74:
75: return $setting_value;
76:
77: }
78:
79: 80: 81:
82: function shutdown() {
83: if ( $this->is_dirty() ) {
84: $this->save_settings();
85: }
86: }
87:
88: 89: 90:
91: function delete_settings() {
92: delete_option( $plugin->option_name );
93: }
94:
95: 96: 97: 98: 99:
100: function set_values( $forms ) {
101: if ( is_array( $forms ) ) {
102: $is_dirty = $forms !== $this->getArrayCopy();
103: $this->exchangeArray( $forms );
104: if ( $is_dirty )
105: $this->set_dirty( true );
106: }
107: }
108:
109: 110: 111: 112: 113:
114: function set_values_deep( $forms_values ) {
115: if ( is_array( $forms_values ) )
116: foreach( $forms_values as $form_name => $form_values ) {
117: $this->get_setting( $form_name )->set_values( $form_values );
118: }
119: $this->set_dirty( true );
120: }
121:
122: 123: 124: 125: 126: 127:
128: function set_data( $data ) {
129:
130: if ( $data ) {
131: if ( is_string( $data ) )
132: $data = unserialize( $data );
133:
134: if ( ! empty( $data ) && is_object( $data ) ) {
135:
136: $this->configured = isset( $data->configured ) ? $data->configured : false;
137: $this->installed_version = isset( $data->installed_version ) ? $data->installed_version : 'unknown';
138:
139: if ( ! empty( $data->values ) && is_array( $data->values ) ) {
140: $this->set_values_deep( $data->values );
141: }
142:
143: 144: 145:
146: $this->set_dirty( true );
147:
148: }
149:
150: }
151:
152: }
153:
154: 155: 156:
157: function load_settings() {
158: $this->set_data( get_option( $this->option_name ) );
159: if ( $this->is_encrypted() )
160: $this->decrypt_settings();
161: $this->set_dirty( false );
162: }
163:
164: 165: 166:
167: function get_data() {
168: return (object)array(
169: 'installed_version' => $this->installed_version,
170: 'configured' => $this->configured,
171: 'values' => $this->get_values_deep(),
172: );
173: }
174:
175: 176: 177:
178: function save_settings() {
179: if ( ! $this->is_encrypted() )
180: $this->encrypt_settings();
181: update_option( $this->option_name, $this->get_data() );
182: $this->set_dirty( false );
183: }
184:
185: 186: 187: 188:
189: function is_encrypted() {
190: return $this->_is_encrypted;
191: }
192:
193: 194: 195:
196: function set_encrypted( $is_encrypted ) {
197: $this->_is_encrypted = $is_encrypted;
198: }
199:
200: 201: 202: 203:
204: function decrypt_settings() {
205: if ( method_exists( $this->_plugin, 'decrypt_settings' ) ) {
206: call_user_func( array( $this->_plugin, 'decrypt_settings' ), $this );
207: }
208: $this->set_encrypted( false );
209: }
210:
211: 212: 213: 214:
215: function encrypt_settings() {
216: if ( method_exists( $this->_plugin, 'encrypt_settings' ) ) {
217: call_user_func( array( $this->_plugin, 'encrypt_settings' ), $this );
218: }
219: $this->set_encrypted( true );
220: }
221:
222: 223: 224:
225: function set_dirty( $is_dirty ) {
226: $this->_is_dirty = $is_dirty;
227: }
228:
229: }
230: