Overview

Namespaces

  • None
  • PHP

Classes

  • Sidecar
  • Sidecar_Admin_Page
  • Sidecar_Admin_Tab
  • Sidecar_Field
  • Sidecar_Form
  • Sidecar_Form_Settings
  • Sidecar_Plugin_Base
  • Sidecar_Plugin_Settings
  • Sidecar_Settings_Base
  • Sidecar_Shortcode
  • Sidecar_Singleton_Base

Functions

  • body
  • format_gmt_string
  • headers
  • output_css
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download
  1: <?php
  2: 
  3: class Sidecar_Plugin_Settings extends Sidecar_Settings_Base {
  4: 
  5:   /**
  6:    * @var string
  7:    */
  8:   var $option_name;
  9: 
 10:   /**
 11:    * @var string
 12:    */
 13:   var $installed_version;
 14: 
 15:   /**
 16:    * @var bool
 17:    */
 18:   var $configured = false;
 19: 
 20:   /**
 21:    * @var bool
 22:    */
 23:   protected $_is_encrypted;
 24: 
 25:   /**
 26:    * @var object Mirrors $_parent so semantics are easier to understand while debugging.
 27:    */
 28:   var $_plugin;
 29: 
 30:   /**
 31:    * @param Sidecar_Plugin_Base|Sidecar_Settings_Base $plugin
 32:    * @param string $option_name
 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:    * Register a form
 43:    * @param string $form_name
 44:    */
 45:   function register_form_settings( $form_name ) {
 46:     $this->register_setting( $form_name, 'Sidecar_Form_Settings' );
 47:   }
 48: 
 49:   /**
 50:    * Register a setting
 51:    * @param string $setting_name
 52:    * @param bool|string $setting_class
 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:    * @param string $setting_name
 61:    *
 62:    * @return Sidecar_Settings_Base|Sidecar_Form_Settings
 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:    * Autosave dirty settings on shutdown
 81:    */
 82:   function shutdown() {
 83:     if ( $this->is_dirty() ) {
 84:       $this->save_settings();
 85:     }
 86:   }
 87: 
 88:   /**
 89:    * Removes settings from the wp_options table in the WordPress MySQL database.
 90:    */
 91:   function delete_settings() {
 92:     delete_option( $plugin->option_name );
 93:   }
 94: 
 95:   /**
 96:    * Accepts an array of $form objects and assigns to the internal array.
 97:    *
 98:    * @param array $forms
 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:    * Accepts an array of name/value pairs and assigns the settings.
111:    *
112:    * @param array $forms_values
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:    * Accept an object that might have been serialized and stored in wp_options table in the WordPress MySQL database.
124:    *
125:    * @param string|object $data
126:    * @return array
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:          * @todo Add logic to compare with current value to ensure dirty is not set unnecessarily.
145:          */
146:         $this->set_dirty( true );
147: 
148:       }
149: 
150:     }
151: 
152:   }
153: 
154:   /**
155:    * Load settings from the wp_options table in the WordPress MySQL database.
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:    * Format the settings as an object that can be serialized to the wp_options table in the WordPress MySQL database.
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:    * Save settings to the wp_options table in the WordPress MySQL database.
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:    * Get a string representing the encryption status
187:    * @return bool
188:    */
189:   function is_encrypted() {
190:     return $this->_is_encrypted;
191:   }
192: 
193:   /**
194:    * @param bool $is_encrypted
195:    */
196:   function set_encrypted( $is_encrypted ) {
197:     $this->_is_encrypted = $is_encrypted;
198:   }
199: 
200:   /**
201:    * Call decryption method if specified in plugin.
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:    * Call decryption method if specified in plugin.
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:    * @param $is_dirty
224:    */
225:   function set_dirty( $is_dirty ) {
226:     $this->_is_dirty = $is_dirty;
227:   }
228: 
229: }
230: 
API documentation generated by ApiGen 2.8.0