1 <?php
2 3 4 5 6 7 8 9 10
11 namespace uix\ui;
12
13 14 15 16 17 18 19
20 class box extends panel implements \uix\data\save, \uix\data\load{
21
22 23 24 25 26 27 28
29 public $type = 'box';
30
31 32 33 34 35 36 37
38 public function init() {
39
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
45 $this->set_data( array( $this->slug => $this->load_data() ) );
46 }
47 }
48
49
50 51 52 53 54 55 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 74 75 76 77
78 public function save_data(){
79 return update_option( $this->store_key(), $this->get_data() );
80 }
81
82 83 84 85 86 87 88
89 public function load_data(){
90 return get_option( $this->store_key(), $this->get_data() );
91 }
92
93 94 95 96 97 98 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 113 114 115 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 125 126 127 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 146 147 148 149 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 167 168 169 170 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 }