<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Rusnetim_Taxonomy_Category {
public function __construct() {
add_action( 'init', [ $this, 'register_taxonomy' ] );
add_action( 'rusnetim_category_add_form_fields', [ $this, 'add_term_fields' ] );
add_action( 'rusnetim_category_edit_form_fields', [ $this, 'edit_term_fields' ], 10, 2 );
add_action( 'created_rusnetim_category', [ $this, 'save_term_fields' ] );
add_action( 'edited_rusnetim_category', [ $this, 'save_term_fields' ] );
}
public function register_taxonomy() {
$labels = array(
'name' => esc_html__( 'Marker Categories', 'rusnet-interactive-map' ),
'singular_name' => esc_html__( 'Category', 'rusnet-interactive-map' ),
'search_items' => esc_html__( 'Search Categories', 'rusnet-interactive-map' ),
'all_items' => esc_html__( 'All Categories', 'rusnet-interactive-map' ),
'parent_item' => esc_html__( 'Parent Category', 'rusnet-interactive-map' ),
'parent_item_colon' => esc_html__( 'Parent Category:', 'rusnet-interactive-map' ),
'edit_item' => esc_html__( 'Edit Category', 'rusnet-interactive-map' ),
'update_item' => esc_html__( 'Update Category', 'rusnet-interactive-map' ),
'add_new_item' => esc_html__( 'Add New Category', 'rusnet-interactive-map' ),
'new_item_name' => esc_html__( 'Category Name', 'rusnet-interactive-map' ),
'menu_name' => esc_html__( 'Categories', 'rusnet-interactive-map' ),
);
register_taxonomy(
'rusnetim_category',
array( 'rusnetim_marker' ),
array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'show_in_menu' => 'edit.php?post_type=rusnetim_marker',
'query_var' => true,
'rewrite' => false,
'public' => false,
'show_in_quick_edit' => true,
'meta_box_cb' => 'post_categories_meta_box',
)
);
}
public function add_term_fields( $taxonomy ) {
$defaults = Rusnetim_Options::get_all();
$my_iconset = $defaults['my_iconset'];
?>
<div class="form-field term-group">
<label for="category-icon"><?php esc_html_e( 'Icon (preset or myset:slug)', 'rusnet-interactive-map' ); ?></label>
<div style="display: flex; gap: 5px; align-items: center;">
<input type="text" id="category-icon" name="rusnetim_category_icon" class="widefat" value="" placeholder="islands#dotIcon" style="flex:1;">
<button type="button" class="button rusnetim-upload-icon-btn"><?php esc_html_e( 'Select Image', 'rusnet-interactive-map' ); ?></button>
</div>
<p class="description">
<?php esc_html_e( 'Enter icon preset, myset:slug, or URL to custom icon image. You can also upload your own image using the button.', 'rusnet-interactive-map' ); ?>
<a href="https://tech.yandex.com/maps/doc/jsapi/2.1/ref/reference/option.presetStorage-docpage/" target="_blank"><?php esc_html_e( 'Documentation', 'rusnet-interactive-map' ); ?></a>
</p>
</div>
<div class="form-field term-group">
<label for="category-color"><?php esc_html_e( 'Color', 'rusnet-interactive-map' ); ?></label>
<input type="text" id="category-color" name="rusnetim_category_color" class="rusnetim-color-field" value="" data-default-color="">
<p class="description"><?php esc_html_e( 'Select color for category markers. If empty, global color will be used.', 'rusnet-interactive-map' ); ?></p>
</div>
<div class="form-field term-group">
<label for="category-icon-width"><?php esc_html_e( 'Icon width (px)', 'rusnet-interactive-map' ); ?></label>
<input type="number" id="category-icon-width" name="rusnetim_category_icon_width" value="" min="0" step="1" />
<p class="description"><?php esc_html_e( 'If custom icon is used, you can specify dimensions.', 'rusnet-interactive-map' ); ?></p>
</div>
<div class="form-field term-group">
<label for="category-icon-height"><?php esc_html_e( 'Icon height (px)', 'rusnet-interactive-map' ); ?></label>
<input type="number" id="category-icon-height" name="rusnetim_category_icon_height" value="" min="0" step="1" />
</div>
<?php
do_action( 'rusnetim_category_add_form_fields_after', $taxonomy );
}
public function edit_term_fields( $term, $taxonomy = '' ) {
if ( func_num_args() == 1 ) {
$taxonomy = 'rusnetim_category';
}
$icon = get_term_meta( $term->term_id, 'rusnetim_category_icon', true );
$color = get_term_meta( $term->term_id, 'rusnetim_category_color', true );
$icon_width = get_term_meta( $term->term_id, 'rusnetim_category_icon_width', true );
$icon_height = get_term_meta( $term->term_id, 'rusnetim_category_icon_height', true );
$defaults = Rusnetim_Options::get_all();
$my_iconset = $defaults['my_iconset'];
?>
<tr class="form-field term-group-wrap">
<th scope="row"><label for="category-icon"><?php esc_html_e( 'Icon (preset or custom image)', 'rusnet-interactive-map' ); ?></label></th>
<td>
<div id="category-icon-wrapper">
<div style="display: flex; gap: 5px; align-items: center;">
<input type="text" id="category-icon" name="rusnetim_category_icon" class="widefat" value="<?php echo esc_attr( $icon ); ?>" placeholder="islands#dotIcon" style="flex:1;" />
<button type="button" class="button rusnetim-upload-icon-btn"><?php esc_html_e( 'Select Image', 'rusnet-interactive-map' ); ?></button>
</div>
<div id="category-icon-preview" style="margin-top: 10px;"></div>
<button type="button" id="category-icon-remove" class="button" style="display: none;"><?php esc_html_e( 'Remove', 'rusnet-interactive-map' ); ?></button>
<p class="description">
<?php esc_html_e( 'Enter icon preset, myset:slug, or upload your own image. If using custom image, you can specify dimensions below.', 'rusnet-interactive-map' ); ?>
<a href="https://tech.yandex.com/maps/doc/jsapi/2.1/ref/reference/option.presetStorage-docpage/" target="_blank"><?php esc_html_e( 'Documentation', 'rusnet-interactive-map' ); ?></a>
</p>
</div>
</td>
</tr>
<tr class="form-field term-group-wrap">
<th scope="row"><label for="category-color"><?php esc_html_e( 'Color', 'rusnet-interactive-map' ); ?></label></th>
<td>
<input type="text" id="category-color" name="rusnetim_category_color" class="rusnetim-color-field" value="<?php echo esc_attr( $color ); ?>" data-default-color="">
<p class="description"><?php esc_html_e( 'Select color for category markers. If empty, global color will be used.', 'rusnet-interactive-map' ); ?></p>
</td>
</tr>
<tr class="form-field term-group-wrap">
<th scope="row"><label for="category-icon-width"><?php esc_html_e( 'Icon width (px)', 'rusnet-interactive-map' ); ?></label></th>
<td>
<input type="number" id="category-icon-width" name="rusnetim_category_icon_width" value="<?php echo esc_attr( $icon_width ); ?>" min="0" step="1" />
<p class="description"><?php esc_html_e( 'If custom icon is used, you can specify dimensions.', 'rusnet-interactive-map' ); ?></p>
</td>
</tr>
<tr class="form-field term-group-wrap">
<th scope="row"><label for="category-icon-height"><?php esc_html_e( 'Icon height (px)', 'rusnet-interactive-map' ); ?></label></th>
<td>
<input type="number" id="category-icon-height" name="rusnetim_category_icon_height" value="<?php echo esc_attr( $icon_height ); ?>" min="0" step="1" />
</td>
</tr>
<?php
do_action( 'rusnetim_category_edit_form_fields_after', $term, $taxonomy );
}
public function save_term_fields( $term_id ) {
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'update-tag_' . $term_id ) ) {
return;
}
if ( isset( $_POST['rusnetim_category_icon'] ) ) {
$icon = sanitize_text_field( wp_unslash( $_POST['rusnetim_category_icon'] ) );
update_term_meta( $term_id, 'rusnetim_category_icon', $icon );
}
if ( isset( $_POST['rusnetim_category_color'] ) ) {
$color = sanitize_hex_color( wp_unslash( $_POST['rusnetim_category_color'] ) );
if ( $color ) {
update_term_meta( $term_id, 'rusnetim_category_color', $color );
} else {
delete_term_meta( $term_id, 'rusnetim_category_color' );
}
}
if ( isset( $_POST['rusnetim_category_icon_width'] ) ) {
$w = intval( wp_unslash( $_POST['rusnetim_category_icon_width'] ) );
update_term_meta( $term_id, 'rusnetim_category_icon_width', $w ?: '' );
}
if ( isset( $_POST['rusnetim_category_icon_height'] ) ) {
$h = intval( wp_unslash( $_POST['rusnetim_category_icon_height'] ) );
update_term_meta( $term_id, 'rusnetim_category_icon_height', $h ?: '' );
}
do_action( 'rusnetim_save_category_meta', $term_id );
}
}