init();
}
}
public function init()
{ //@todo add media uploader for JS https://rudrastyh.com/wordpress/customizable-media-uploader.html
// add hooks
add_action( 'admin_init', [ $this, 'admin_init' ], 20 );
add_action( 'template_redirect', [ $this, 'frontend_init' ] );
}
public function admin_init(){
$taxonomies = flrt_get_experimental_option( 'color_swatches_taxonomies', [] );
if( ! empty( $taxonomies ) ) {
foreach ( $taxonomies as $taxonomy ) {
// to check if this taxonomy already has fields
// register fields for add new term screen
// register fields for edit term screen
// save fields
$this->register_field( 'color', $taxonomy );
$this->register_field( 'image', $taxonomy );
if ( ! $this->field_exists( $taxonomy ) && $taxonomy !== 'product_cat' ) {
add_filter( "manage_edit-{$taxonomy}_columns", array( $this, 'columns' ) );
if ( strpos( $taxonomy, 'pa_' ) === 0 ) {
add_filter( "manage_{$taxonomy}_custom_column", array( $this, 'column_preview_pa' ), 10, 3 );
} else {
add_filter( "manage_{$taxonomy}_custom_column", array( $this, 'column_preview' ), 10, 3 );
}
}
}
add_action( 'current_screen', [ $this, 'maybe_enqueue_media_uploaded' ], 20 );
}
}
public function maybe_enqueue_media_uploaded( $current_screen ){
$taxonomies = flrt_get_experimental_option( 'color_swatches_taxonomies', [] );
if ( in_array( $current_screen->taxonomy, $taxonomies ) ) {
if ( ! did_action( 'wp_enqueue_media' ) ) {
wp_enqueue_media();
}
}
}
public function frontend_init(){
$taxonomies = flrt_get_experimental_option( 'color_swatches_taxonomies', [] );
if( ! empty( $taxonomies ) ) {
add_filter( 'wpc_filter_classes', [ $this, 'frontend_filter_classes'], 10, 4 );
add_filter( 'wpc_filters_radio_term_html', [ $this, 'frontend_display_swatch' ], 10, 4 );
add_filter( 'wpc_filters_checkbox_term_html', [ $this, 'frontend_display_swatch' ], 10, 4 );
add_filter( 'wpc_filters_label_term_html', [ $this, 'frontend_display_swatch' ], 10, 4 );
}
}
public function frontend_filter_classes( $classes, $filter, $default_classes, $terms ){
$taxonomies = flrt_get_experimental_option( 'color_swatches_taxonomies', [] );
if( in_array( $filter['e_name'], $taxonomies ) ) {
$classes[] = 'wpc-filter-has-swatches';
}
return $classes;
}
public function frontend_display_swatch( $term_html, $link_attributes, $term_object, $filter ) {
$taxonomies = flrt_get_experimental_option( 'color_swatches_taxonomies', [] );
// A brand term already rendered with its brand logo (wpc_term_brand_logo,
// priority 5) must keep it — rebuilding here would replace the logo
// markup with a swatch square
if ( in_array( $filter['e_name'], flrt_brand_filter_entities() ) && flrt_get_term_brand_image( $term_object->term_id, $filter ) ) {
return $term_html;
}
if ( in_array( $filter['e_name'], $taxonomies ) ) {
$image_src = flrt_get_term_swatch_image( $term_object->term_id, $filter );
$wrapper_class = '';
if ( $image_src ) {
$swatch = '';
$wrapper_class = ' wpc-term-swatch-image';
} else {
$maybe_color = flrt_get_term_swatch_color( $term_object->term_id, $filter );
if ( $maybe_color ) {
$swatch = '';
} else {
$swatch = '';
$wrapper_class = ' wpc-term-swatch-no-image';
}
}
$link_attributes .= ' title="'.esc_attr( $term_object->name ).'"';
$term_html = '' . $swatch . ' ';
$term_html .= '' . esc_html( $term_object->name ) . '';
}
return $term_html;
}
private function register_field( $field, $taxonomy ) {
if ( $this->field_exists( $taxonomy, $field ) ) {
return false;
}
switch ( $field ){
case 'color':
add_action( "{$taxonomy}_add_form_fields", [ $this, 'add_new_term_color_field' ] );
add_action( "{$taxonomy}_edit_form_fields", [ $this, 'edit_term_color_field' ], 10, 2 );
add_action( "edited_term", [ $this, 'save_color_field' ], 10, 3 );
add_action( "created_term", [ $this, 'save_color_field' ], 10, 3 );
break;
case 'image':
add_action( "{$taxonomy}_add_form_fields", [ $this, 'add_new_term_image_field' ] );
add_action( "{$taxonomy}_edit_form_fields", [ $this, 'edit_term_image_field' ], 10, 2 );
add_action( "edited_term", [ $this, 'save_image_field' ], 10, 3 );
add_action( "created_term", [ $this, 'save_image_field' ], 10, 3 );
break;
default:
//
break;
}
}
/**
* Determines if current taxonomy already has Color and Image fields
* @param $field
* @param $taxonomy
* @return bool
*/
public function field_exists( $taxonomy, $field = '' ) {
if ( ! $taxonomy ) {
return false;
}
if ( class_exists( 'Woo_Variation_Swatches' ) ) {
if ( strpos( $taxonomy, 'pa_' ) === 0 ) {
return true;
}
}
if ( $taxonomy === 'product_cat' && $field === 'image' ) {
return true;
}
// for other plugins
return false;
}
public function add_new_term_color_field( $taxonomy ) {
?>