| 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' ] ) ) : ''; |
| 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>'. $section_icon . $section_title .'</h3></div>' : ''; |
| 128 |
echo ( ! empty( $section['description'] ) ) ? '<div class="adminify-field adminify-section-description">'. $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 |
// XSS ok. |
| 168 |
// No worries, This "POST" requests is sanitizing in the below foreach. |
| 169 |
$request = ( ! empty( $_POST[ $this->unique ] ) ) ? $_POST[ $this->unique ] : array(); |
| 170 |
|
| 171 |
if ( ! empty( $request ) ) { |
| 172 |
|
| 173 |
foreach ( $this->sections as $section ) { |
| 174 |
|
| 175 |
if ( ! empty( $section['fields'] ) ) { |
| 176 |
|
| 177 |
foreach ( $section['fields'] as $field ) { |
| 178 |
|
| 179 |
if ( ! empty( $field['id'] ) ) { |
| 180 |
|
| 181 |
$field_id = $field['id']; |
| 182 |
$field_value = isset( $request[$field_id] ) ? $request[$field_id] : ''; |
| 183 |
|
| 184 |
// Sanitize "post" request of field. |
| 185 |
if ( ! isset( $field['sanitize'] ) ) { |
| 186 |
|
| 187 |
if( is_array( $field_value ) ) { |
| 188 |
$data[$field_id] = wp_kses_post_deep( $field_value ); |
| 189 |
} else { |
| 190 |
$data[$field_id] = wp_kses_post( $field_value ); |
| 191 |
} |
| 192 |
|
| 193 |
} else if( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) { |
| 194 |
|
| 195 |
$data[$field_id] = call_user_func( $field['sanitize'], $field_value ); |
| 196 |
|
| 197 |
} else { |
| 198 |
|
| 199 |
$data[$field_id] = $field_value; |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
// Validate "post" request of field. |
| 204 |
if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) { |
| 205 |
|
| 206 |
$has_validated = call_user_func( $field['validate'], $field_value ); |
| 207 |
|
| 208 |
if ( ! empty( $has_validated ) ) { |
| 209 |
|
| 210 |
$errors['sections'][$count] = true; |
| 211 |
$errors['fields'][$field_id] = $has_validated; |
| 212 |
$data[$field_id] = $this->get_meta_value( $field, $term_id ); |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
$count++; |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
$data = apply_filters( "adminify_{$this->unique}_save", $data, $term_id, $this ); |
| 231 |
|
| 232 |
do_action( "adminify_{$this->unique}_save_before", $data, $term_id, $this ); |
| 233 |
|
| 234 |
if ( empty( $data ) ) { |
| 235 |
|
| 236 |
if ( $this->args['data_type'] !== 'serialize' ) { |
| 237 |
foreach ( $this->pre_fields as $field ) { |
| 238 |
if ( ! empty( $field['id'] ) ) { |
| 239 |
delete_term_meta( $term_id, $field['id'] ); |
| 240 |
} |
| 241 |
} |
| 242 |
} else { |
| 243 |
delete_term_meta( $term_id, $this->unique ); |
| 244 |
} |
| 245 |
|
| 246 |
} else { |
| 247 |
|
| 248 |
if ( $this->args['data_type'] !== 'serialize' ) { |
| 249 |
foreach ( $data as $key => $value ) { |
| 250 |
update_term_meta( $term_id, $key, $value ); |
| 251 |
} |
| 252 |
} else { |
| 253 |
update_term_meta( $term_id, $this->unique, $data ); |
| 254 |
} |
| 255 |
|
| 256 |
if ( ! empty( $errors ) ) { |
| 257 |
update_term_meta( $term_id, '_adminify_errors_'. $this->unique, $errors ); |
| 258 |
} |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
do_action( "adminify_{$this->unique}_saved", $data, $term_id, $this ); |
| 263 |
|
| 264 |
do_action( "adminify_{$this->unique}_save_after", $data, $term_id, $this ); |
| 265 |
|
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|