Skip to content

Commit

Permalink
v0.5.2
Browse files Browse the repository at this point in the history
-add: method enqueue_js
-change: typhinting and type-checks in methods
  • Loading branch information
RalfAlbert committed Oct 22, 2011
1 parent 4aec56f commit 1c97231
Showing 1 changed file with 50 additions and 9 deletions.
59 changes: 50 additions & 9 deletions class-easy_settings_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @package WordPress
* @subpackage Settings-API class
* @author Ralf Albert
* @version 0.5.1
* @version 0.5.2
* @license GPL
*/

Expand Down Expand Up @@ -120,6 +120,7 @@ class Easy_Settings_API
*/
public function __construct( array $settings = null ) {

// check if $settings was set. if not, just create an object of this class
if( null !== $settings ) {
$this->setup( $settings );
$this->init();
Expand All @@ -137,7 +138,7 @@ public function __construct( array $settings = null ) {
* @since 0.1
* @access public
*/
public function setup( $settings = null) {
public function setup( array $settings = null) {
if( null === $settings )
return false;

Expand Down Expand Up @@ -231,13 +232,18 @@ public function init() {

/**
*
* Return options from database
* Return options from database or $this->options if already set
* @param string $options_name
* @return array $options
* @since 0.4
* @access protected
*/
protected function get_options( $options_name ){
protected function get_options( $options_name = '' ){
// we can't get options from database if we do not know which option to retrive
// but if $this->options is already set, return this options
if( '' == $options_name && empty( $this->options ) )
return false;

if( empty( $this->options ) )
$this->options = get_option( $options_name );

Expand Down Expand Up @@ -304,12 +310,20 @@ public function get_settings() {
/**
*
* Setter for $settings
* Set $_settings if $settings is given and $defaults not
* Sanitize $settings
* Merge $settings and $defaults if both are given
*
* @param array $settings
* @return array $_settings
* @since 0.3
* @access public
*/
public function set_settings( array $settings, $defaults = array() ){
public function set_settings( array $settings = null, $defaults = array() ){
// no settings, no action
if( null === $settings )
return false;

// Sanitize the users data!
// There is only one thing that makes you sleep well:
// Better than security is more security
Expand Down Expand Up @@ -374,7 +388,7 @@ public function register_settings() {
* @since 0.1
* @access public
*/
public function create_setting( $args = array() ) {
public function create_setting( array $args = array() ) {
if( null === $this->output )
$this->output = $this->get_output();

Expand Down Expand Up @@ -436,7 +450,7 @@ public function display_section( array $section ) {
* @since 0.1
* @access public
*/
public function display_settings_field( array $args ) {
public function display_settings_field( array $args = array() ) {
// get outpot-object
if( null === $this->output )
$this->output = $this->get_output();
Expand All @@ -457,7 +471,7 @@ public function display_settings_field( array $args ) {

// set standard for multi checkbox
if( ( isset( $std ) && is_array( $std ) ) &&
( $type == 'mcheckbox' || $type == 'mselect') &&
( isset( $type) && ( 'mcheckbox' == $type || 'mselect' == $type) ) &&
! isset( $this->options[$id] ) ) {

foreach( $std as $key ) {
Expand Down Expand Up @@ -492,7 +506,34 @@ public function display_settings_field( array $args ) {
}

}


public function enqueue_js( array $src = null ){
// no $src, no action
if( empty( $src ) )
return false;

// just set $js_src, optionspage wasn't added yet. e.g. for external calls
if( empty( $this->_settings['admin_page'] ) ){
$this->_settings['js_src'] = $src;
return sizeof( $this->_settings['js_src'] );
}

//optionspage was already added. set js_src if it isn't set yet
if( empty( $this->_settings['js_src'] ) )
$this->_settings['js_src'] = $src;

$name = $this->_settings('page_slug');

foreach( $this->_settings['js_src'] as $key => $val ){
if( ! is_string($key) )
$key = $name.$key;
//TODO: pruefen ob $src eine datei und lesbar ist
add_action( 'load-'.$name, $src );
}

return true;
}

/* --------------- sanitizing --------------- */
/**
*
Expand Down

0 comments on commit 1c97231

Please sign in to comment.