PluginProbe
Easy Hotel – Powerful Hotel Booking / trunk
Easy Hotel – Powerful Hotel Booking vtrunk
2.0.8 2.0.7 2.0.6 2.0.5 2.0.4 2.0.3 2.0.2 2.0.1 2.0.0 1.9.9 1.9.8 1.9.7 1.9.6 1.9.5 1.9.4 1.9.3 1.9.2 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 All 110 releases
easy-hotel / admin / includes / framework / classes / taxonomy-options.class.php

taxonomy-options.class.php in Easy Hotel – Powerful Hotel Booking trunk, at admin/includes/framework/classes/taxonomy-options.class.php

274 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
2 /**
3 *
4 * Taxonomy Options Class
5 *
6 * @since 1.0.0
7 * @version 1.0.0
8 *
9 */
10 if ( ! class_exists( 'ESHB_Taxonomy_Options' ) ) {
11 class ESHB_Taxonomy_Options extends ESHB_Abstract{
12
13 // constans
14 public $unique = '';
15 public $taxonomy = '';
16 public $abstract = 'taxonomy';
17 public $sections = array();
18 public $pre_fields = array();
19 public $taxonomies = array();
20 public $args = array(
21 'taxonomy' => 'category',
22 'data_type' => 'serialize',
23 'class' => '',
24 'enqueue_webfont' => true,
25 'async_webfont' => false,
26 'output_css' => true,
27 'defaults' => array(),
28 );
29
30 // run taxonomy construct
31 public function __construct( $key, $params ) {
32
33 // Verify nonce for security
34 if (isset($_POST['eshb_save_meta'])) {
35 wp_verify_nonce( sanitize_text_field(wp_unslash($_POST['eshb_save_meta'])), 'eshb_save_meta_box_nonce');
36 }
37
38 $this->unique = $key;
39 $this->args = apply_filters( "eshb_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
40 $this->sections = apply_filters( "eshb_{$this->unique}_sections", $params['sections'], $this );
41 $this->taxonomies = ( is_array( $this->args['taxonomy'] ) ) ? $this->args['taxonomy'] : array_filter( (array) $this->args['taxonomy'] );
42 $this->taxonomy = ( ! empty( $_REQUEST[ 'taxonomy' ] ) ) ? sanitize_text_field( wp_unslash( $_REQUEST[ 'taxonomy' ] ) ) : '';
43 $this->pre_fields = $this->pre_fields( $this->sections );
44
45 if ( ! empty( $this->taxonomies ) && in_array( $this->taxonomy, $this->taxonomies ) ) {
46 add_action( 'admin_init', array( $this, 'add_taxonomy_options' ) );
47 }
48
49 // wp enqeueu for typography and output css
50 parent::__construct();
51
52 }
53
54 // instance
55 public static function instance( $key, $params ) {
56 return new self( $key, $params );
57 }
58
59 // add taxonomy add/edit fields
60 public function add_taxonomy_options() {
61
62 add_action( $this->taxonomy .'_add_form_fields', array( $this, 'render_taxonomy_form_fields' ) );
63 add_action( $this->taxonomy .'_edit_form', array( $this, 'render_taxonomy_form_fields' ) );
64
65 add_action( 'created_'. $this->taxonomy, array( $this, 'save_taxonomy' ) );
66 add_action( 'edited_'. $this->taxonomy, array( $this, 'save_taxonomy' ) );
67
68 }
69
70 // get default value
71 public function get_default( $field ) {
72
73 $default = ( isset( $field['default'] ) ) ? $field['default'] : '';
74 $default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
75
76 return $default;
77
78 }
79
80 // get meta value
81 public function get_meta_value( $field, $term_id = null ) {
82
83 $value = null;
84
85 $term_id = ( ! isset( $term_id ) ) ? get_queried_object_id() : $term_id;
86
87 if ( ! empty( $term_id ) && ! empty( $field['id'] ) ) {
88
89 if ( $this->args['data_type'] !== 'serialize' ) {
90 $meta = get_term_meta( $term_id, $field['id'] );
91 $value = ( isset( $meta[0] ) ) ? $meta[0] : null;
92 } else {
93 $meta = get_term_meta( $term_id, $this->unique, true );
94 $value = ( isset( $meta[$field['id']] ) ) ? $meta[$field['id']] : null;
95 }
96
97 }
98
99 $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
100 $value = ( isset( $value ) ) ? $value : $default;
101
102 return $value;
103
104 }
105
106 // render taxonomy add/edit form fields
107 public function render_taxonomy_form_fields( $term ) {
108
109 $is_term = ( is_object( $term ) && isset( $term->taxonomy ) ) ? true : false;
110 $term_id = ( $is_term ) ? $term->term_id : 0;
111 $taxonomy = ( $is_term ) ? $term->taxonomy : $term;
112 $classname = ( $is_term ) ? 'edit' : 'add';
113 $errors = ( ! empty( $term_id ) ) ? get_term_meta( $term_id, '_eshb_errors_'. $this->unique, true ) : array();
114 $errors = ( ! empty( $errors ) ) ? $errors : array();
115 $class = ( $this->args['class'] ) ? ' '. $this->args['class'] : '';
116
117 if ( ! empty( $errors ) ) {
118 delete_term_meta( $term_id, '_eshb_errors_'. $this->unique );
119 }
120
121 wp_nonce_field( 'eshb_taxonomy_nonce', 'eshb_taxonomy_nonce'. $this->unique );
122
123 echo '<div class="csf csf-taxonomy csf-show-all csf-onload csf-taxonomy-'. esc_attr( $classname ) .'-fields '. esc_attr( $class ) .'">';
124
125 foreach ( $this->sections as $section ) {
126
127 if ( $taxonomy === $this->taxonomy ) {
128
129 $section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="csf-section-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
130 $section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
131
132 echo ( $section_title || $section_icon ) ? '<div class="csf-section-title"><h3>'. wp_kses_post($section_icon) . esc_html($section_title) .'</h3></div>' : '';
133 echo ( ! empty( $section['description'] ) ) ? '<div class="csf-field csf-section-description">'. esc_html($section['description']) .'</div>' : '';
134
135 if ( ! empty( $section['fields'] ) ) {
136 foreach ( $section['fields'] as $field ) {
137
138 if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][$field['id']] ) ) {
139 $field['_error'] = $errors['fields'][$field['id']];
140 }
141
142 if ( ! empty( $field['id'] ) ) {
143 $field['default'] = $this->get_default( $field );
144 }
145
146 ESHB::field( $field, $this->get_meta_value( $field, $term_id ), $this->unique, 'taxonomy' );
147
148 }
149 }
150 }
151
152 }
153
154 echo '</div>';
155
156 }
157
158 // save taxonomy form fields
159 public function save_taxonomy( $term_id ) {
160
161 $count = 1;
162 $data = array();
163 $errors = array();
164 $noncekey = 'eshb_taxonomy_nonce'. $this->unique;
165 $nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : '';
166 $taxonomy = ( ! empty( $_POST[ 'taxonomy' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'taxonomy' ] ) ) : '';
167
168 if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'eshb_taxonomy_nonce' ) ) {
169 return $term_id;
170 }
171
172 // XSS ok.
173 // No worries, This "POST" requests is sanitizing in the below foreach.
174 $request = ( ! empty( $_POST[ $this->unique ] ) ) ? map_deep( wp_unslash($_POST[ $this->unique ]), 'sanitize_text_field') : array();
175
176 if ( ! empty( $request ) ) {
177
178 foreach ( $this->sections as $section ) {
179
180 if ( ! empty( $section['fields'] ) ) {
181
182 foreach ( $section['fields'] as $field ) {
183
184 if ( ! empty( $field['id'] ) ) {
185
186 $field_id = $field['id'];
187 $field_value = isset( $request[$field_id] ) ? $request[$field_id] : '';
188
189 // Sanitize "post" request of field.
190 if ( ! isset( $field['sanitize'] ) ) {
191
192 if( is_array( $field_value ) ) {
193 $data[$field_id] = wp_kses_post_deep( $field_value );
194 } else {
195 $data[$field_id] = wp_kses_post( $field_value );
196 }
197
198 } else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) {
199
200 $data[$field_id] = call_user_func( $field['sanitize'], $field_value );
201
202 } else {
203
204 $data[$field_id] = $field_value;
205
206 }
207
208 // Validate "post" request of field.
209 if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
210
211 $has_validated = call_user_func( $field['validate'], $field_value );
212
213 if ( ! empty( $has_validated ) ) {
214
215 $errors['sections'][$count] = true;
216 $errors['fields'][$field_id] = $has_validated;
217 $data[$field_id] = $this->get_meta_value( $field, $term_id );
218
219 }
220
221 }
222
223 }
224
225 }
226
227 }
228
229 $count++;
230
231 }
232
233 }
234
235 $data = apply_filters( "eshb_{$this->unique}_save", $data, $term_id, $this );
236
237 do_action( "eshb_{$this->unique}_save_before", $data, $term_id, $this );
238
239 if ( empty( $data ) ) {
240
241 if ( $this->args['data_type'] !== 'serialize' ) {
242 foreach ( $this->pre_fields as $field ) {
243 if ( ! empty( $field['id'] ) ) {
244 delete_term_meta( $term_id, $field['id'] );
245 }
246 }
247 } else {
248 delete_term_meta( $term_id, $this->unique );
249 }
250
251 } else {
252
253 if ( $this->args['data_type'] !== 'serialize' ) {
254 foreach ( $data as $key => $value ) {
255 update_term_meta( $term_id, $key, $value );
256 }
257 } else {
258 update_term_meta( $term_id, $this->unique, $data );
259 }
260
261 if ( ! empty( $errors ) ) {
262 update_term_meta( $term_id, '_eshb_errors_'. $this->unique, $errors );
263 }
264
265 }
266
267 do_action( "eshb_{$this->unique}_saved", $data, $term_id, $this );
268
269 do_action( "eshb_{$this->unique}_save_after", $data, $term_id, $this );
270
271 }
272 }
273 }
274