<?php
/**
 *	Custom colors term meta for category
 *	@author Justin Tadlock
 *	see http://themehybrid.com/weblog/introduction-to-wordpress-term-meta
 *-----------------------------------------------------------------*/
// Registering meta.
function magazinex_register_meta() {
    	register_meta( 'term', 'color', 'magazinex_sanitize_hex' );
}
add_action( 'init', 'magazinex_register_meta' );

// Sanitize.
function magazinex_sanitize_hex( $color ) {
    	$color = ltrim( $color, '#' );
    	return preg_match( '/([A-Fa-f0-9]{3}){1,2}$/', $color ) ? $color : '';
}

// Getting color term meta
function magazinex_get_term_color( $term_id, $hash = false ) {
    	$color = get_term_meta( $term_id, 'color', true );
    	$color = magazinex_sanitize_hex( $color );

    	return $hash && $color ? "#{$color}" : $color;
}

// Adding form fields for Add New category
add_action( 'category_add_form_fields', 'magazinex_new_term_color_field' );
function magazinex_new_term_color_field() { // add new form
    	wp_nonce_field( get_stylesheet_directory(). '/includes/term-meta-color.php', 'magazinex_term_color_nonce' ); ?>

    	<div class="form-field magazinex-term-color-wrap">
        		<label for="magazinex-term-color"><?php esc_html_e( 'Color', 'magazinex-lite' ); ?></label>
        		<input type="text" name="magazinex_term_color" id="magazinex-term-color" value="" class="magazinex-color-field" data-default-color="#ffffff" />
    	</div>
<?php }

// Adding form fields for Edit category
add_action( 'category_edit_form_fields', 'magazinex_edit_term_color_field' );
function magazinex_edit_term_color_field( $term ) {
    	$default = '#ffffff';
    	$color   = magazinex_get_term_color( $term->term_id, true );

    	if ( ! $color ) {
        		$color = $default; } ?>

    	<tr class="form-field magazinex-term-color-wrap">
        	<th scope="row"><label for="magazinex-term-color"><?php esc_html_e( 'Color', 'magazinex-lite' ); ?></label></th>
        		<td>
            			<?php wp_nonce_field( get_stylesheet_directory(). '/includes/term-meta-color.php', 'magazinex_term_color_nonce' ); ?>
            			<input type="text" name="magazinex_term_color" id="magazinex-term-color" value="<?php echo esc_attr( $color ); ?>" class="magazinex-color-field" data-default-color="<?php echo esc_attr( $default ); ?>" />
        		</td>
    	</tr>
<?php }

// Saving color term meta
function magazinex_save_term_color( $term_id ) {
    	if ( ! isset( $_POST['magazinex_term_color_nonce'] ) || ! wp_verify_nonce( $_POST['magazinex_term_color_nonce'], get_stylesheet_directory(). '/includes/term-meta-color.php' ) ) {
        		return;
	}
    	$old_color = magazinex_get_term_color( $term_id );
    	$new_color = isset( $_POST['magazinex_term_color'] ) ? magazinex_sanitize_hex( $_POST['magazinex_term_color'] ) : '';

    	if ( $old_color && '' === $new_color ) {
        		delete_term_meta( $term_id, 'color' );
	}
	if ( $old_color !== $new_color ) {
        		update_term_meta( $term_id, 'color', $new_color );
	}
}
add_action( 'edit_category',   'magazinex_save_term_color' );
add_action( 'create_category', 'magazinex_save_term_color' );

// Adding a colors term meta amin column
function magazinex_edit_term_columns( $columns ) {
    	$columns['color'] = esc_html__( 'Color', 'magazinex-lite' );
    	return $columns;
}
add_filter( 'manage_edit-category_columns', 'magazinex_edit_term_columns' );

// Handle the output for the column
function magazinex_manage_term_custom_column( $out, $column, $term_id ) {
    	if ( 'color' === $column ) {
        		$color = magazinex_get_term_color( $term_id, true );
        		if ( ! $color ) {
            			$color = '#ffffff';
		}
    	$out = sprintf( '<span class="color-block" style="background:%s;">&nbsp;</span>', esc_attr( $color ) );
    	}
    	return $out;
}
add_filter( 'manage_category_custom_column', 'magazinex_manage_term_custom_column', 10, 3 );

// JS and CSS include
add_action( 'admin_enqueue_scripts', 'magazinex_admin_enqueue_scripts' );

function magazinex_admin_enqueue_scripts( $hook_suffix ) {
    	if ( 'edit-tags.php' != $hook_suffix && 'category' != get_current_screen()->taxonomy
	|| 'term.php' != $hook_suffix && 'category' != get_current_screen()->taxonomy ) { // WP older 4.5 works?
        		return;
		}
    	wp_enqueue_style( 'wp-color-picker' );
    	wp_enqueue_script( 'wp-color-picker' );

    	add_action( 'admin_head',   'magazinex_term_colors_print_styles' );
    	add_action( 'admin_footer', 'magazinex_term_colors_print_scripts' );
}

function magazinex_term_colors_print_styles() { ?>
    	<style type="text/css">
        		.column-color { width: 50px; }
        		.column-color .color-block { display: inline-block; width: 28px; height: 28px; border: 1px solid #ddd; }
    	</style>
<?php }
function magazinex_term_colors_print_scripts() { ?>
    	<script type="text/javascript">
        		jQuery( document ).ready( function( $ ) {
            			$( '.magazinex-color-field' ).wpColorPicker();
        		} );
    	</script>
<?php }