| 1 |
<?php if ( ! defined( 'ABSPATH' ) ) { |
| 2 |
die; } // Cannot access directly. |
| 3 |
/** |
| 4 |
* |
| 5 |
* Taxonomy Options Class |
| 6 |
* |
| 7 |
* @since 1.0.0 |
| 8 |
* @version 1.0.0 |
| 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 = []; |
| 18 |
public $sections = []; |
| 19 |
public $taxonomies = []; |
| 20 |
public $args = [ |
| 21 |
'taxonomy' => 'category', |
| 22 |
'data_type' => 'serialize', |
| 23 |
'class' => '', |
| 24 |
'enqueue_webfont' => true, |
| 25 |
'async_webfont' => false, |
| 26 |
'output_css' => true, |
| 27 |
'defaults' => [], |
| 28 |
]; |
| 29 |
|
| 30 |
// run taxonomy construct |
| 31 |
public function __construct( $key, $params ) { |
| 32 |
$this->unique = $key; |
| 33 |
$this->args = apply_filters( "adminify_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this ); |
| 34 |
$this->sections = apply_filters( "adminify_{$this->unique}_sections", $params['sections'], $this ); |
| 35 |
$this->taxonomies = ( is_array( $this->args['taxonomy'] ) ) ? $this->args['taxonomy'] : array_filter( (array) $this->args['taxonomy'] ); |
| 36 |
$this->taxonomy = ( ! empty( $_REQUEST['taxonomy'] ) ) ? sanitize_text_field( wp_unslash( $_REQUEST['taxonomy'] ) ) : ''; |
| 37 |
$this->pre_fields = $this->pre_fields( $this->sections ); |
| 38 |
|
| 39 |
if ( ! empty( $this->taxonomies ) && in_array( $this->taxonomy, $this->taxonomies ) ) { |
| 40 |
add_action( 'admin_init', [ $this, 'add_taxonomy_options' ] ); |
| 41 |
} |
| 42 |
|
| 43 |
// wp enqeueu for typography and output css |
| 44 |
parent::__construct(); |
| 45 |
} |
| 46 |
|
| 47 |
// instance |
| 48 |
public static function instance( $key, $params ) { |
| 49 |
return new self( $key, $params ); |
| 50 |
} |
| 51 |
|
| 52 |
public function pre_fields( $sections ) { |
| 53 |
$result = []; |
| 54 |
|
| 55 |
foreach ( $sections as $key => $section ) { |
| 56 |
if ( ! empty( $section['fields'] ) ) { |
| 57 |
foreach ( $section['fields'] as $field ) { |
| 58 |
$result[] = $field; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return $result; |
| 64 |
} |
| 65 |
|
| 66 |
// add taxonomy add/edit fields |
| 67 |
public function add_taxonomy_options() { |
| 68 |
add_action( $this->taxonomy . '_add_form_fields', [ $this, 'render_taxonomy_form_fields' ] ); |
| 69 |
add_action( $this->taxonomy . '_edit_form', [ $this, 'render_taxonomy_form_fields' ] ); |
| 70 |
|
| 71 |
add_action( 'created_' . $this->taxonomy, [ $this, 'save_taxonomy' ] ); |
| 72 |
add_action( 'edited_' . $this->taxonomy, [ $this, 'save_taxonomy' ] ); |
| 73 |
} |
| 74 |
|
| 75 |
// get default value |
| 76 |
public function get_default( $field ) { |
| 77 |
$default = ( isset( $field['default'] ) ) ? $field['default'] : ''; |
| 78 |
$default = ( isset( $this->args['defaults'][ $field['id'] ] ) ) ? $this->args['defaults'][ $field['id'] ] : $default; |
| 79 |
|
| 80 |
return $default; |
| 81 |
} |
| 82 |
|
| 83 |
// get meta value |
| 84 |
public function get_meta_value( $field, $term_id = null ) { |
| 85 |
$value = null; |
| 86 |
|
| 87 |
$term_id = ( ! isset( $term_id ) ) ? get_queried_object_id() : $term_id; |
| 88 |
|
| 89 |
if ( ! empty( $term_id ) && ! empty( $field['id'] ) ) { |
| 90 |
if ( $this->args['data_type'] !== 'serialize' ) { |
| 91 |
$meta = get_term_meta( $term_id, $field['id'] ); |
| 92 |
$value = ( isset( $meta[0] ) ) ? $meta[0] : null; |
| 93 |
} else { |
| 94 |
$meta = get_term_meta( $term_id, $this->unique, true ); |
| 95 |
$value = ( isset( $meta[ $field['id'] ] ) ) ? $meta[ $field['id'] ] : null; |
| 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 |
// render taxonomy add/edit form fields |
| 106 |
public function render_taxonomy_form_fields( $term ) { |
| 107 |
$is_term = ( is_object( $term ) && isset( $term->taxonomy ) ) ? true : false; |
| 108 |
$term_id = ( $is_term ) ? $term->term_id : 0; |
| 109 |
$taxonomy = ( $is_term ) ? $term->taxonomy : $term; |
| 110 |
$classname = ( $is_term ) ? 'edit' : 'add'; |
| 111 |
$errors = ( ! empty( $term_id ) ) ? get_term_meta( $term_id, '_adminify_errors_' . $this->unique, true ) : []; |
| 112 |
$errors = ( ! empty( $errors ) ) ? $errors : []; |
| 113 |
$class = ( $this->args['class'] ) ? ' ' . $this->args['class'] : ''; |
| 114 |
|
| 115 |
if ( ! empty( $errors ) ) { |
| 116 |
delete_term_meta( $term_id, '_adminify_errors_' . $this->unique ); |
| 117 |
} |
| 118 |
|
| 119 |
wp_nonce_field( 'adminify_taxonomy_nonce', 'adminify_taxonomy_nonce' . $this->unique ); |
| 120 |
|
| 121 |
echo '<div class="adminify adminify-taxonomy adminify-show-all adminify-onload adminify-taxonomy-' . esc_attr( $classname ) . '-fields ' . esc_attr( $class ) . '">'; |
| 122 |
|
| 123 |
foreach ( $this->sections as $section ) { |
| 124 |
if ( $taxonomy === $this->taxonomy ) { |
| 125 |
$section_icon = ( ! empty( $section['icon'] ) ) ? '<i class="adminify-section-icon ' . wp_kses_post( $section['icon'] ) . '"></i>' : ''; |
| 126 |
$section_title = ( ! empty( $section['title'] ) ) ? $section['title'] : ''; |
| 127 |
|
| 128 |
echo ( $section_title || $section_icon ) ? '<div class="adminify-section-title"><h3>' . wp_kses_post( $section_icon . $section_title ) . '</h3></div>' : ''; |
| 129 |
echo ( ! empty( $section['description'] ) ) ? '<div class="adminify-field adminify-section-description">' . wp_kses_post( $section['description'] ) . '</div>' : ''; |
| 130 |
|
| 131 |
if ( ! empty( $section['fields'] ) ) { |
| 132 |
foreach ( $section['fields'] as $field ) { |
| 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 |
echo '</div>'; |
| 148 |
} |
| 149 |
|
| 150 |
// save taxonomy form fields |
| 151 |
public function save_taxonomy( $term_id ) { |
| 152 |
$count = 1; |
| 153 |
$data = []; |
| 154 |
$errors = []; |
| 155 |
$noncekey = 'adminify_taxonomy_nonce' . $this->unique; |
| 156 |
$nonce = ( ! empty( $_POST[ $noncekey ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ $noncekey ] ) ) : ''; |
| 157 |
$taxonomy = ( ! empty( $_POST['taxonomy'] ) ) ? sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) ) : ''; |
| 158 |
|
| 159 |
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || ! wp_verify_nonce( $nonce, 'adminify_taxonomy_nonce' ) ) { |
| 160 |
return $term_id; |
| 161 |
} |
| 162 |
|
| 163 |
if ( ! empty( $_POST[ $this->unique ] ) ) { |
| 164 |
$request = sanitize_text_field( wp_unslash( $_POST[ $this->unique ] ) ); |
| 165 |
} else { |
| 166 |
$request = []; |
| 167 |
} |
| 168 |
|
| 169 |
if ( ! empty( $request ) ) { |
| 170 |
foreach ( $this->sections as $section ) { |
| 171 |
if ( ! empty( $section['fields'] ) ) { |
| 172 |
foreach ( $section['fields'] as $field ) { |
| 173 |
if ( ! empty( $field['id'] ) ) { |
| 174 |
$field_id = $field['id']; |
| 175 |
$field_value = isset( $request[ $field_id ] ) ? $request[ $field_id ] : ''; |
| 176 |
|
| 177 |
// Sanitize "post" request of field. |
| 178 |
if ( ! isset( $field['sanitize'] ) ) { |
| 179 |
if ( is_array( $field_value ) ) { |
| 180 |
$data[ $field_id ] = wp_kses_post_deep( $field_value ); |
| 181 |
} else { |
| 182 |
$data[ $field_id ] = wp_kses_post( $field_value ); |
| 183 |
} |
| 184 |
} elseif ( isset( $field['sanitize'] ) && is_callable( $field['sanitize'] ) ) { |
| 185 |
$data[ $field_id ] = call_user_func( $field['sanitize'], $field_value ); |
| 186 |
} else { |
| 187 |
$data[ $field_id ] = $field_value; |
| 188 |
} |
| 189 |
|
| 190 |
// Validate "post" request of field. |
| 191 |
if ( isset( $field['validate'] ) && is_callable( $field['validate'] ) ) { |
| 192 |
$has_validated = call_user_func( $field['validate'], $field_value ); |
| 193 |
|
| 194 |
if ( ! empty( $has_validated ) ) { |
| 195 |
$errors['sections'][ $count ] = true; |
| 196 |
$errors['fields'][ $field_id ] = $has_validated; |
| 197 |
$data[ $field_id ] = $this->get_meta_value( $field, $term_id ); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
$count++; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
$data = apply_filters( "adminify_{$this->unique}_save", $data, $term_id, $this ); |
| 209 |
|
| 210 |
do_action( "adminify_{$this->unique}_save_before", $data, $term_id, $this ); |
| 211 |
|
| 212 |
if ( empty( $data ) ) { |
| 213 |
if ( $this->args['data_type'] !== 'serialize' ) { |
| 214 |
foreach ( $data as $key => $value ) { |
| 215 |
delete_term_meta( $term_id, $key ); |
| 216 |
} |
| 217 |
} else { |
| 218 |
delete_term_meta( $term_id, $this->unique ); |
| 219 |
} |
| 220 |
} else { |
| 221 |
if ( $this->args['data_type'] !== 'serialize' ) { |
| 222 |
foreach ( $data as $key => $value ) { |
| 223 |
update_term_meta( $term_id, $key, $value ); |
| 224 |
} |
| 225 |
} else { |
| 226 |
update_term_meta( $term_id, $this->unique, $data ); |
| 227 |
} |
| 228 |
|
| 229 |
if ( ! empty( $errors ) ) { |
| 230 |
update_term_meta( $term_id, '_adminify_errors_' . $this->unique, $errors ); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
do_action( "adminify_{$this->unique}_saved", $data, $term_id, $this ); |
| 235 |
|
| 236 |
do_action( "adminify_{$this->unique}_save_after", $data, $term_id, $this ); |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|