PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 2.0.0
Adminify – White Label, Admin Menu Editor, Login Customizer v2.0.0
4.3.1 4.3.0 4.2.26 4.2.25 4.2.24 4.2.23 4.2.22 4.2.21 4.2.20 4.2.19 4.2.18 4.2.17 4.2.16 4.2.15 4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 4.1.17 All 164 releases
adminify / lib / adminify-framework / classes / taxonomy-options.class.php

taxonomy-options.class.php in Adminify – White Label, Admin Menu Editor, Login Customizer 2.0.0, at lib/adminify-framework/classes/taxonomy-options.class.php

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