forked from ArRolin/codestar-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaxonomy.class.php
executable file
·225 lines (143 loc) · 5.77 KB
/
taxonomy.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access pages directly.
/**
*
* Taxonomy Class
*
* @since 1.0.0
* @version 1.0.0
*
*/
class CSFramework_Taxonomy extends CSFramework_Abstract{
/**
*
* taxonomy options
* @access public
* @var array
*
*/
public $options = array();
/**
*
* instance
* @access private
* @var class
*
*/
private static $instance = null;
// run taxonomy construct
public function __construct( $options ) {
$this->options = apply_filters( 'cs_taxonomy_options', $options );
if( ! empty( $this->options ) ) {
$this->addAction( 'admin_init', 'add_taxonomy_fields' );
}
}
// instance
public static function instance( $options = array() ) {
if ( is_null( self::$instance ) && CS_ACTIVE_TAXONOMY ) {
self::$instance = new self( $options );
}
return self::$instance;
}
// add taxonomy add/edit fields
public function add_taxonomy_fields() {
foreach ( $this->options as $option ) {
$opt_taxonomy = $option['taxonomy'];
$get_taxonomy = cs_get_var( 'taxonomy' );
if( $get_taxonomy == $opt_taxonomy ) {
$this->addAction( $opt_taxonomy .'_add_form_fields', 'render_taxonomy_form_fields' );
$this->addAction( $opt_taxonomy .'_edit_form', 'render_taxonomy_form_fields' );
$this->addAction( 'created_'. $opt_taxonomy, 'save_taxonomy' );
$this->addAction( 'edited_'. $opt_taxonomy, 'save_taxonomy' );
$this->addAction( 'delete_'. $opt_taxonomy, 'delete_taxonomy' );
}
}
}
// render taxonomy add/edit form fields
public function render_taxonomy_form_fields( $term ) {
global $cs_errors;
$form_edit = ( is_object( $term ) && isset( $term->taxonomy ) ) ? true : false;
$taxonomy = ( $form_edit ) ? $term->taxonomy : $term;
$classname = ( $form_edit ) ? 'edit' : 'add';
$cs_errors = get_transient( 'cs-taxonomy-transient' );
wp_nonce_field( 'cs-taxonomy', 'cs-taxonomy-nonce' );
echo '<div class="cs-framework cs-taxonomy cs-taxonomy-'. $classname .'-fields">';
foreach( $this->options as $option ) {
if( $taxonomy == $option['taxonomy'] ) {
$tax_value = ( $form_edit ) ? get_term_meta( $term->term_id, $option['id'], true ) : '';
foreach ( $option['fields'] as $field ) {
$default = ( isset( $field['default'] ) ) ? $field['default'] : '';
$elem_id = ( isset( $field['id'] ) ) ? $field['id'] : '';
$elem_value = ( is_array( $tax_value ) && isset( $tax_value[$elem_id] ) ) ? $tax_value[$elem_id] : $default;
echo cs_add_element( $field, $elem_value, $option['id'] );
}
}
}
echo '</div>';
}
// save taxonomy form fields
public function save_taxonomy( $term_id ) {
if ( wp_verify_nonce( cs_get_var( 'cs-taxonomy-nonce' ), 'cs-taxonomy' ) ) {
$errors = array();
$taxonomy = cs_get_var( 'taxonomy' );
foreach ( $this->options as $request_value ) {
if( $taxonomy == $request_value['taxonomy'] ) {
$request_key = $request_value['id'];
$request = cs_get_var( $request_key, array() );
// ignore _nonce
if( isset( $request['_nonce'] ) ) {
unset( $request['_nonce'] );
}
if( isset( $request_value['fields'] ) ) {
foreach( $request_value['fields'] as $field ) {
if( isset( $field['type'] ) && isset( $field['id'] ) ) {
$field_value = cs_get_vars( $request_key, $field['id'] );
// sanitize options
if( isset( $field['sanitize'] ) && $field['sanitize'] !== false ) {
$sanitize_type = $field['sanitize'];
} else if ( ! isset( $field['sanitize'] ) ) {
$sanitize_type = $field['type'];
}
if( has_filter( 'cs_sanitize_'. $sanitize_type ) ) {
$request[$field['id']] = apply_filters( 'cs_sanitize_' . $sanitize_type, $field_value, $field, $request_value['fields'] );
}
// validate options
if ( isset( $field['validate'] ) && has_filter( 'cs_validate_'. $field['validate'] ) ) {
$validate = apply_filters( 'cs_validate_' . $field['validate'], $field_value, $field, $request_value['fields'] );
if( ! empty( $validate ) ) {
$meta_value = get_term_meta( $term_id, $request_key, true );
$errors[$field['id']] = array( 'code' => $field['id'], 'message' => $validate, 'type' => 'error' );
$default_value = isset( $field['default'] ) ? $field['default'] : '';
$request[$field['id']] = ( isset( $meta_value[$field['id']] ) ) ? $meta_value[$field['id']] : $default_value;
}
}
}
}
}
$request = apply_filters( 'cs_save_taxonomy', $request, $request_key, $term_id );
if( empty( $request ) ) {
delete_term_meta( $term_id, $request_key );
} else {
if( get_term_meta( $term_id, $request_key, true ) ) {
update_term_meta( $term_id, $request_key, $request );
} else {
add_term_meta( $term_id, $request_key, $request );
}
}
}
}
set_transient( 'cs-taxonomy-transient', $errors, 10 );
}
}
// delete taxonomy
public function delete_taxonomy( $term_id ) {
$taxonomy = cs_get_var( 'taxonomy' );
if( ! empty( $taxonomy ) ) {
foreach ( $this->options as $request_value ) {
if( $taxonomy == $request_value['taxonomy'] ) {
$request_key = $request_value['id'];
delete_term_meta( $term_id, $request_key );
}
}
}
}
}