UIX Documentation
  • Namespace
  • Class
  • Tree

Namespaces

  • None
  • uix
    • data
    • ui
      • control

Classes

  • uix\data\data
  • uix\ui
  • uix\ui\box
  • uix\ui\control
  • uix\ui\control\autocomplete
  • uix\ui\control\button
  • uix\ui\control\checkbox
  • uix\ui\control\color
  • uix\ui\control\editor
  • uix\ui\control\email
  • uix\ui\control\file
  • uix\ui\control\hidden
  • uix\ui\control\number
  • uix\ui\control\post_relation
  • uix\ui\control\radio
  • uix\ui\control\select
  • uix\ui\control\separator
  • uix\ui\control\slider
  • uix\ui\control\template
  • uix\ui\control\text
  • uix\ui\control\textarea
  • uix\ui\control\toggle
  • uix\ui\footer
  • uix\ui\grid
  • uix\ui\header
  • uix\ui\help
  • uix\ui\metabox
  • uix\ui\modal
  • uix\ui\notice
  • uix\ui\page
  • uix\ui\panel
  • uix\ui\post_type
  • uix\ui\repeat
  • uix\ui\section
  • uix\ui\uix

Interfaces

  • uix\data\load
  • uix\data\save

Functions

  • uix
  • uix_autoload_class
  1 <?php
  2 /**
  3  * UIX Box
  4  *
  5  * @package   ui
  6  * @author    David Cramer
  7  * @license   GPL-2.0+
  8  * @link
  9  * @copyright 2016 David Cramer
 10  */
 11 namespace uix\ui;
 12 
 13 /**
 14  * Unlike metaboxes, the box can be rendered via code and will enqueue assets on the page where its declared.
 15  * A box also has save on a submission. Data is saved as an array structure based on the tree of child objects.
 16  * 
 17  * @package uix\ui
 18  * @author  David Cramer
 19  */
 20 class box extends panel implements \uix\data\save, \uix\data\load{
 21 
 22     /**
 23      * The type of object
 24      *
 25      * @since 1.0.0
 26      * @access public
 27      * @var      string
 28      */
 29     public $type = 'box';
 30 
 31     /**
 32      * Sets the controls data
 33      *
 34      * @since 1.0.0
 35      * @see \uix\uix
 36      * @access public
 37      */
 38     public function init() {
 39         // run parents to setup sanitization filters
 40         $data = uix()->request_vars( 'post' );
 41         if( isset( $data[ 'uixNonce_' . $this->id() ] ) && wp_verify_nonce( $data[ 'uixNonce_' . $this->id() ], $this->id() ) ){
 42             $this->save_data();
 43         }else{
 44             // load data normally
 45             $this->set_data( array( $this->slug => $this->load_data() ) );
 46         }
 47     }
 48 
 49 
 50     /**
 51      * set metabox styles
 52      *
 53      * @since 1.0.0
 54      * @see \uix\ui\uix
 55      * @access public
 56      */
 57     public function set_assets() {
 58 
 59         $this->assets['script']['baldrick'] = array(
 60             'src' => $this->url . 'assets/js/jquery.baldrick' . UIX_ASSET_DEBUG . '.js',
 61             'deps' => array( 'jquery' ),
 62         );
 63         $this->assets['script']['uix-ajax'] = array(
 64             'src' => $this->url . 'assets/js/ajax' . UIX_ASSET_DEBUG . '.js',
 65             'deps' => array( 'baldrick' ),
 66         );
 67         $this->assets['style']['uix-ajax'] =  $this->url . 'assets/css/ajax' . UIX_ASSET_DEBUG . '.css';
 68 
 69         parent::set_assets();
 70     }
 71 
 72     /**
 73      * save data to database
 74      *
 75      * @since 1.0.0
 76      * @access public
 77      */
 78     public function save_data(){
 79         return update_option( $this->store_key(), $this->get_data() );
 80     }
 81 
 82     /**
 83      * Get data
 84      *
 85      * @since 1.0.0
 86      * @access public
 87      * @return mixed $data Requested data of the object
 88      */
 89     public function load_data(){
 90         return get_option( $this->store_key(), $this->get_data() );
 91     }
 92 
 93     /**
 94      * Get Data from all controls of this section
 95      *
 96      * @since 1.0.0
 97      * @see \uix\load
 98      * @return array Array of sections data structured by the controls
 99      */
100     public function get_data(){
101 
102         if( empty( $this->data ) ){
103             $data = parent::get_data();
104             if( !empty( $data[ $this->slug ] ) )
105                 $this->data = $data[ $this->slug ];
106         }
107 
108         return $this->data;
109     }
110 
111     /**
112      * get the objects data store key
113      * @since 1.0.0
114      * @access public
115      * @return string $store_key the defined option name for this UIX object
116      */
117     public function store_key(){
118         if( !empty( $this->struct['store_key'] ) )
119             return $this->struct['store_key'];
120         return sanitize_key( $this->slug );
121     }
122 
123     /**
124      * Sets the wrappers attributes
125      *
126      * @since 1.0.0
127      * @access public
128      */
129     public function set_attributes(){
130 
131         $action = uix()->request_vars('server');
132         $this->attributes += array(
133             'enctype'   =>  'multipart/form-data',
134             'method'    =>  'POST',
135             'class'     =>  'uix-ajax uix-' . $this->type,
136             'data-uix'  =>  $this->slug,
137             'action'    =>  $action['REQUEST_URI'],
138         );
139 
140         parent::set_attributes();
141 
142     }
143 
144     /**
145      * Render the box
146      *
147      * @since 1.0.0
148      * @access public
149      * @return string HMTL of rendered page
150      */
151     public function render(){
152         $output = null;
153 
154         $output .= '<form ' . $this->build_attributes() . '>';
155 
156         $output .= $this->render_header();
157         $output .= parent::render();
158         $output .= wp_nonce_field( $this->id(), 'uixNonce_' . $this->id(), true, false );
159 
160         $output .= '</form>';
161 
162         return $output;
163     }
164 
165     /**
166      * Render the page
167      *
168      * @since 1.0.0
169      * @access public
170      * @return string HMTL of rendered page
171      */
172     public function render_header(){
173 
174         $output = null;
175         if( !empty( $this->child ) ){
176             foreach( $this->child as $child ){
177                 if( $child->type == 'header' )
178                     $output .= $child->render();
179             }
180         }
181 
182         return $output;
183 
184     }
185 }
UIX Documentation API documentation generated by ApiGen