Skip to content

Commit

Permalink
Throwing an error if initialize is called twice.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Adams committed May 22, 2012
1 parent e8b06bb commit 0487f87
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions classes/struts/options.php
@@ -1,7 +1,7 @@
<?php

class Struts_Options {
protected $_sections, $_all_options, $_name, $_slug, $_stranded_options;
protected $_sections, $_all_options, $_name, $_slug, $_stranded_options, $_is_initialized;

public function __construct( $slug, $name ) {
$this->sections( array() );
Expand Down Expand Up @@ -63,6 +63,15 @@ public function name( $name = NULL ) {
return $this;
}

public function is_initialized( $is_initialized = NULL ) {
if ( NULL === $is_initialized )
return $this->_is_initialized;

$this->_is_initialized = $is_initialized;

return $this;
}

/***** WordPress setup *****/

public function register_hooks() {
Expand Down Expand Up @@ -113,6 +122,13 @@ public function enqueue_scripts() {
}

public function initialize() {
// We only want to initialize the options once.
if ( $this->is_initialized() ) {
throw new StrutsOptionsAlreadyInitializedException( __( 'Options already initialized. Struts no longer requires manual initialization.', 'struts' ) );
} else {
$this->is_initialized( true );
}

$option_values = get_option( $this->name() );

if ( false === $option_values || empty( $option_values ) ) {
Expand Down Expand Up @@ -310,4 +326,5 @@ public function do_options_html() {

}

class SectionNotFoundException extends Exception { }
class SectionNotFoundException extends Exception {}
class StrutsOptionsAlreadyInitializedException extends Exception {}

0 comments on commit 0487f87

Please sign in to comment.