PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 4.3.0
Adminify – White Label, Admin Menu Editor, Login Customizer v4.3.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 / Libs / adminify-framework / classes / taxonomy-options.class.php

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

279 lines 9.7 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 $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 $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' ] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only check, no state change.
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 // add taxonomy add/edit fields
55 public function add_taxonomy_options() {
56
57 add_action( $this->taxonomy .'_add_form_fields', array( $this, 'render_taxonomy_form_fields' ) );
58 add_action( $this->taxonomy .'_edit_form', array( $this, 'render_taxonomy_form_fields' ) );
59
60 add_action( 'created_'. $this->taxonomy, array( $this, 'save_taxonomy' ) );
61 add_action( 'edited_'. $this->taxonomy, array( $this, 'save_taxonomy' ) );
62
63 }
64
65 // get default value
66 public function get_default( $field ) {
67
68 $default = ( isset( $field['default'] ) ) ? $field['default'] : '';
69 $default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
70
71 return $default;
72
73 }
74
75 // get meta value
76 public function get_meta_value( $field, $term_id = null ) {
77
78 $value = null;
79
80 $term_id = ( ! isset( $term_id ) ) ? get_queried_object_id() : $term_id;
81
82 if ( ! empty( $term_id ) && ! empty( $field['id'] ) ) {
83
84 if ( $this->args['data_type'] !== 'serialize' ) {
85 $meta = get_term_meta( $term_id, $field['id'] );
86 $value = ( isset( $meta[0] ) ) ? $meta[0] : null;
87 } else {
88 $meta = get_term_meta( $term_id, $this->unique, true );
89 $value = ( isset( $meta[$field['id']] ) ) ? $meta[$field['id']] : null;
90 }
91
92 }
93
94 $default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
95 $value = ( isset( $value ) ) ? $value : $default;
96
97 return $value;
98
99 }
100
101 // render taxonomy add/edit form fields
102 public function render_taxonomy_form_fields( $term ) {
103
104 $is_term = ( is_object( $term ) && isset( $term->taxonomy ) ) ? true : false;
105 $term_id = ( $is_term ) ? $term->term_id : 0;
106 $taxonomy = ( $is_term ) ? $term->taxonomy : $term;
107 $classname = ( $is_term ) ? 'edit' : 'add';
108 $errors = ( ! empty( $term_id ) ) ? get_term_meta( $term_id, '_adminify_errors_'. $this->unique, true ) : array();
109 $errors = ( ! empty( $errors ) ) ? $errors : array();
110 $class = ( $this->args['class'] ) ? ' '. $this->args['class'] : '';
111
112 if ( ! empty( $errors ) ) {
113 delete_term_meta( $term_id, '_adminify_errors_'. $this->unique );
114 }
115
116 wp_nonce_field( 'adminify_taxonomy_nonce', 'adminify_taxonomy_nonce'. $this->unique );
117
118 echo '<div class="adminify adminify-taxonomy adminify-show-all adminify-onload adminify-taxonomy-'. esc_attr( $classname ) .'-fields '. esc_attr( $class ) .'">';
119
120 foreach ( $this->sections as $section ) {
121
122 if ( $taxonomy === $this->taxonomy ) {
123
124 $section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="adminify-section-icon '. esc_attr( $section['icon'] ) .'"></i>' : '';
125 $section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : '';
126
127 echo ( $section_title || $section_icon ) ? '<div class="adminify-section-title"><h3>'. wp_kses_post( $section_icon ) . esc_html( $section_title ) .'</h3></div>' : '';
128 echo ( ! empty( $section['description'] ) ) ? '<div class="adminify-field adminify-section-description">'. wp_kses_post( $section['description'] ) .'</div>' : '';
129
130 if ( ! empty( $section['fields'] ) ) {
131 foreach ( $section['fields'] as $field ) {
132
133 if ( ! empty( $field['id'] ) && ! empty( $errors['fields'][$field['id']] ) ) {
134 $field['_error'] = $errors['fields'][$field['id']];
135 }
136
137 if ( ! empty( $field['id'] ) ) {
138 $field['default'] = $this->get_default( $field );
139 }
140
141 ADMINIFY::field( $field, $this->get_meta_value( $field, $term_id ), $this->unique, 'taxonomy' );
142
143 }
144 }
145 }
146
147 }
148
149 echo '</div>';
150
151 }
152
153 // save taxonomy form fields
154 public function save_taxonomy( $term_id ) {
155
156 $count = 1;
157 $data = array();
158 $errors = array();
159 $noncekey = 'adminify_taxonomy_nonce'. $this->unique;
160 $nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : '';
161 $taxonomy = ( ! empty( $_POST[ 'taxonomy' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'taxonomy' ] ) ) : '';
162
163 if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'adminify_taxonomy_nonce' ) ) {
164 return $term_id;
165 }
166
167 // Authorization: a valid nonce proves intent, not permission.
168 if ( ! current_user_can( 'edit_term', $term_id ) ) {
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 ] ) ) ? wp_unslash( $_POST[ $this->unique ] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- each field is sanitized individually by the framework's per-field sanitize handlers.
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 // A sanitize callback was declared but is not callable; never store raw input.
205 if ( is_array( $field_value ) ) {
206 $data[$field_id] = wp_kses_post_deep( $field_value );
207 } else {
208 $data[$field_id] = wp_kses_post( $field_value );
209 }
210
211 }
212
213 // Validate "post" request of field.
214 if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) {
215
216 $has_validated = call_user_func( $field['validate'], $field_value );
217
218 if ( ! empty( $has_validated ) ) {
219
220 $errors['sections'][$count] = true;
221 $errors['fields'][$field_id] = $has_validated;
222 $data[$field_id] = $this->get_meta_value( $field, $term_id );
223
224 }
225
226 }
227
228 }
229
230 }
231
232 }
233
234 $count++;
235
236 }
237
238 }
239
240 $data = apply_filters( "adminify_{$this->unique}_save", $data, $term_id, $this );
241
242 do_action( "adminify_{$this->unique}_save_before", $data, $term_id, $this );
243
244 if ( empty( $data ) ) {
245
246 if ( $this->args['data_type'] !== 'serialize' ) {
247 foreach ( $this->pre_fields as $field ) {
248 if ( ! empty( $field['id'] ) ) {
249 delete_term_meta( $term_id, $field['id'] );
250 }
251 }
252 } else {
253 delete_term_meta( $term_id, $this->unique );
254 }
255
256 } else {
257
258 if ( $this->args['data_type'] !== 'serialize' ) {
259 foreach ( $data as $key => $value ) {
260 update_term_meta( $term_id, $key, $value );
261 }
262 } else {
263 update_term_meta( $term_id, $this->unique, $data );
264 }
265
266 if ( ! empty( $errors ) ) {
267 update_term_meta( $term_id, '_adminify_errors_'. $this->unique, $errors );
268 }
269
270 }
271
272 do_action( "adminify_{$this->unique}_saved", $data, $term_id, $this );
273
274 do_action( "adminify_{$this->unique}_save_after", $data, $term_id, $this );
275
276 }
277 }
278 }
279