Пример #1
0
/**
 * premise get option
 *
 * get options from wordpress with a little more control. This function lets you get values that are
 * saved within arrays by simply passing the name field string. i.e. premise_get_option( 'option[array1][array2]' )
 * which would output the value for array2 of 'option'. 
 * 
 * Alternatively, you can also pass the 'option' key as the first parameter and an array of keys to filter within the 
 * 'option' array as the second parameter. i.e. premise_get_option( 'option', array( 'array1', 'array2' ) ). This would
 * output the same result as premise_get_option( 'option[array1][array2]' ).
 *
 * @since 1.2 uses premise_get_value. ability to submit an option string like option[array1][array2]
 *            to retrieve the value of array2. keys were used for this before.
 * 
 * @param  mixed $option string of option or name field. array of options or name fields
 * @param  array $key    deprecated. Use string i.e. 'option[array1][array2]' better.
 * @return mixed         array of value, or single value, or false if nothing found
 */
function premise_get_option($option = '', $key = '')
{
    if (empty($option)) {
        return false;
    }
    if (is_string($option)) {
        if (!empty($key)) {
            $_key = is_array($key) ? '[\'' . implode('\'][\'', $key) . '\']' : '[\'' . (string) $key . '\']';
            $option .= $_key;
        }
        return premise_get_value($option);
    }
    if (is_array($option)) {
        $options = array();
        foreach ($option as $opt) {
            array_push($options, premise_get_value($opt));
        }
        return $options;
    }
    return false;
}
 /**
  * try to get the option value for a field
  *
  * @since 1.2 added before but documented in this version
  * 
  * @param  string $name name attribute to know what option to look for
  * @return  mixed       returns the value found or an empty string if nothing was found
  */
 protected function get_db_value()
 {
     if (isset($this->args['value']) && !$this->empty_value($this->args['value'])) {
         $val = $this->args['value'];
     } else {
         $name = !empty($this->args['name']) ? $this->args['name'] : $this->get_name();
         if (empty($name)) {
             return '';
         }
         $context = !empty($this->args['context']) ? $this->args['context'] : '';
         $val = premise_get_value($name, $context);
     }
     $val = is_array($val) ? implode(',', $val) : $val;
     if (!$this->empty_value($val)) {
         return esc_attr($val);
     } else {
         return isset($this->args['default']) && !$this->empty_value($this->args['default']) ? esc_attr($this->args['default']) : '';
     }
 }