| 1 |
<?php |
| 2 |
|
| 3 |
namespace FilterEverything\Filter; |
| 4 |
|
| 5 |
if ( ! defined('ABSPATH') ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Swatches |
| 10 |
{ |
| 11 |
public function __construct() |
| 12 |
{ |
| 13 |
if ( flrt_get_experimental_option('use_color_swatches') === 'on' ) { |
| 14 |
$this->init(); |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
public function init() |
| 19 |
{ //@todo add media uploader for JS https://rudrastyh.com/wordpress/customizable-media-uploader.html |
| 20 |
// add hooks |
| 21 |
add_action( 'admin_init', [ $this, 'admin_init' ], 20 ); |
| 22 |
add_action( 'template_redirect', [ $this, 'frontend_init' ] ); |
| 23 |
} |
| 24 |
|
| 25 |
public function admin_init(){ |
| 26 |
$taxonomies = flrt_get_experimental_option( 'color_swatches_taxonomies', [] ); |
| 27 |
|
| 28 |
if( ! empty( $taxonomies ) ) { |
| 29 |
foreach ( $taxonomies as $taxonomy ) { |
| 30 |
// to check if this taxonomy already has fields |
| 31 |
// register fields for add new term screen |
| 32 |
// register fields for edit term screen |
| 33 |
// save fields |
| 34 |
$this->register_field( 'color', $taxonomy ); |
| 35 |
$this->register_field( 'image', $taxonomy ); |
| 36 |
|
| 37 |
if ( ! $this->field_exists( $taxonomy ) && $taxonomy !== 'product_cat' ) { |
| 38 |
add_filter( "manage_edit-{$taxonomy}_columns", array( $this, 'columns' ) ); |
| 39 |
|
| 40 |
if ( strpos( $taxonomy, 'pa_' ) === 0 ) { |
| 41 |
add_filter( "manage_{$taxonomy}_custom_column", array( $this, 'column_preview_pa' ), 10, 3 ); |
| 42 |
} else { |
| 43 |
add_filter( "manage_{$taxonomy}_custom_column", array( $this, 'column_preview' ), 10, 3 ); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
add_action( 'current_screen', [ $this, 'maybe_enqueue_media_uploaded' ], 20 ); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
public function maybe_enqueue_media_uploaded( $current_screen ){ |
| 54 |
$taxonomies = flrt_get_experimental_option( 'color_swatches_taxonomies', [] ); |
| 55 |
|
| 56 |
if ( in_array( $current_screen->taxonomy, $taxonomies ) ) { |
| 57 |
if ( ! did_action( 'wp_enqueue_media' ) ) { |
| 58 |
wp_enqueue_media(); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
public function frontend_init(){ |
| 65 |
$taxonomies = flrt_get_experimental_option( 'color_swatches_taxonomies', [] ); |
| 66 |
|
| 67 |
if( ! empty( $taxonomies ) ) { |
| 68 |
add_filter( 'wpc_filter_classes', [ $this, 'frontend_filter_classes'], 10, 4 ); |
| 69 |
|
| 70 |
add_filter( 'wpc_filters_radio_term_html', [ $this, 'frontend_display_swatch' ], 10, 4 ); |
| 71 |
add_filter( 'wpc_filters_checkbox_term_html', [ $this, 'frontend_display_swatch' ], 10, 4 ); |
| 72 |
add_filter( 'wpc_filters_label_term_html', [ $this, 'frontend_display_swatch' ], 10, 4 ); |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
public function frontend_filter_classes( $classes, $filter, $default_classes, $terms ){ |
| 78 |
$taxonomies = flrt_get_experimental_option( 'color_swatches_taxonomies', [] ); |
| 79 |
|
| 80 |
if( in_array( $filter['e_name'], $taxonomies ) ) { |
| 81 |
$classes[] = 'wpc-filter-has-swatches'; |
| 82 |
} |
| 83 |
|
| 84 |
return $classes; |
| 85 |
} |
| 86 |
|
| 87 |
public function frontend_display_swatch( $term_html, $link_attributes, $term_object, $filter ) { |
| 88 |
|
| 89 |
$taxonomies = flrt_get_experimental_option( 'color_swatches_taxonomies', [] ); |
| 90 |
|
| 91 |
// A brand term already rendered with its brand logo (wpc_term_brand_logo, |
| 92 |
// priority 5) must keep it — rebuilding here would replace the logo |
| 93 |
// markup with a swatch square |
| 94 |
if ( in_array( $filter['e_name'], flrt_brand_filter_entities() ) && flrt_get_term_brand_image( $term_object->term_id, $filter ) ) { |
| 95 |
return $term_html; |
| 96 |
} |
| 97 |
|
| 98 |
if ( in_array( $filter['e_name'], $taxonomies ) ) { |
| 99 |
|
| 100 |
$image_src = flrt_get_term_swatch_image( $term_object->term_id, $filter ); |
| 101 |
$wrapper_class = ''; |
| 102 |
|
| 103 |
if ( $image_src ) { |
| 104 |
$swatch = '<img src="'.esc_url( $image_src ).'" class="wpc-term-image" />'; |
| 105 |
$wrapper_class = ' wpc-term-swatch-image'; |
| 106 |
} else { |
| 107 |
$maybe_color = flrt_get_term_swatch_color( $term_object->term_id, $filter ); |
| 108 |
if ( $maybe_color ) { |
| 109 |
$swatch = '<span class="wpc-term-swatch" style="background-color: '.esc_attr( $maybe_color ).'"></span>'; |
| 110 |
} else { |
| 111 |
$swatch = '<span class="wpc-term-swatch wpc-no-swatch-yet"></span>'; |
| 112 |
$wrapper_class = ' wpc-term-swatch-no-image'; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
$link_attributes .= ' title="'.esc_attr( $term_object->name ).'"'; |
| 117 |
$term_html = '<a '.$link_attributes.'><span class="wpc-term-swatch-wrapper'.$wrapper_class.'">' . $swatch . '</span> '; |
| 118 |
$term_html .= '<span class="wpc-term-name">' . esc_html( $term_object->name ) . '</span></a>'; |
| 119 |
} |
| 120 |
|
| 121 |
return $term_html; |
| 122 |
} |
| 123 |
|
| 124 |
private function register_field( $field, $taxonomy ) { |
| 125 |
|
| 126 |
if ( $this->field_exists( $taxonomy, $field ) ) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
switch ( $field ){ |
| 131 |
case 'color': |
| 132 |
add_action( "{$taxonomy}_add_form_fields", [ $this, 'add_new_term_color_field' ] ); |
| 133 |
add_action( "{$taxonomy}_edit_form_fields", [ $this, 'edit_term_color_field' ], 10, 2 ); |
| 134 |
|
| 135 |
add_action( "edited_term", [ $this, 'save_color_field' ], 10, 3 ); |
| 136 |
add_action( "created_term", [ $this, 'save_color_field' ], 10, 3 ); |
| 137 |
break; |
| 138 |
|
| 139 |
case 'image': |
| 140 |
add_action( "{$taxonomy}_add_form_fields", [ $this, 'add_new_term_image_field' ] ); |
| 141 |
add_action( "{$taxonomy}_edit_form_fields", [ $this, 'edit_term_image_field' ], 10, 2 ); |
| 142 |
|
| 143 |
add_action( "edited_term", [ $this, 'save_image_field' ], 10, 3 ); |
| 144 |
add_action( "created_term", [ $this, 'save_image_field' ], 10, 3 ); |
| 145 |
break; |
| 146 |
|
| 147 |
default: |
| 148 |
// |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Determines if current taxonomy already has Color and Image fields |
| 155 |
* @param $field |
| 156 |
* @param $taxonomy |
| 157 |
* @return bool |
| 158 |
*/ |
| 159 |
public function field_exists( $taxonomy, $field = '' ) { |
| 160 |
if ( ! $taxonomy ) { |
| 161 |
return false; |
| 162 |
} |
| 163 |
|
| 164 |
if ( class_exists( 'Woo_Variation_Swatches' ) ) { |
| 165 |
if ( strpos( $taxonomy, 'pa_' ) === 0 ) { |
| 166 |
return true; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
if ( $taxonomy === 'product_cat' && $field === 'image' ) { |
| 171 |
return true; |
| 172 |
} |
| 173 |
|
| 174 |
// for other plugins |
| 175 |
|
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
public function add_new_term_color_field( $taxonomy ) { |
| 180 |
?> |
| 181 |
<div class="form-field wpc-color-picker"> |
| 182 |
<label for="wpc_term_color"><?php esc_html_e('Color', 'filter-everything'); ?></label> |
| 183 |
<input type="text" name="wpc_term_color" id="wpc_term_color" /> |
| 184 |
<p id="wpc-color-description"><?php esc_html_e('The selected color will be shown in the filter term.', 'filter-everything'); ?></p> |
| 185 |
</div> |
| 186 |
<?php |
| 187 |
} |
| 188 |
|
| 189 |
public function add_new_term_image_field( $taxonomy ) { |
| 190 |
?> |
| 191 |
<div class="form-field"> |
| 192 |
<label for="wpc_term_img"><?php esc_html_e('Image', 'filter-everything'); ?></label> |
| 193 |
<a href="javascript:void(0);" class="wpc-upload-image-button button wpc-upload"><?php esc_html_e('Upload image', 'filter-everything'); ?></a> |
| 194 |
<a href="javascript:void(0);" class="wpc-remove" style="display:none"><?php esc_html_e('Remove image', 'filter-everything'); ?></a> |
| 195 |
<input type="hidden" name="wpc_term_img" id="wpc_term_img" value=""> |
| 196 |
<p id="wpc-image-description"><?php esc_html_e('The image will be shown in the filter term.', 'filter-everything'); ?></p> |
| 197 |
</div> |
| 198 |
<?php |
| 199 |
} |
| 200 |
|
| 201 |
public function edit_term_color_field( $term, $taxonomy ) { |
| 202 |
|
| 203 |
$key = 'color'; |
| 204 |
if ( strpos( $taxonomy, 'pa_' ) === 0 ) { |
| 205 |
$key = 'product_attribute_color'; |
| 206 |
} |
| 207 |
|
| 208 |
$color = get_term_meta( $term->term_id, $key, true ); |
| 209 |
?> |
| 210 |
<tr class="form-field wpc-color-picker"> |
| 211 |
<th><label for="wpc_term_color"><?php esc_html_e('Color', 'filter-everything'); ?></label></th> |
| 212 |
<td> |
| 213 |
<input name="wpc_term_color" id="wpc_term_color" type="text" value="<?php echo esc_attr( $color ) ?>" /> |
| 214 |
<p id="wpc-color-description"><?php esc_html_e('The selected color will be shown in the filter term.', 'filter-everything'); ?></p> |
| 215 |
</td> |
| 216 |
</tr> |
| 217 |
<?php |
| 218 |
} |
| 219 |
|
| 220 |
public function edit_term_image_field( $term, $taxonomy ) { |
| 221 |
$key = 'image'; |
| 222 |
if ( strpos( $taxonomy, 'pa_' ) === 0 ) { |
| 223 |
$key = 'product_attribute_image'; |
| 224 |
} |
| 225 |
|
| 226 |
$image_id = get_term_meta( $term->term_id, $key, true ); |
| 227 |
|
| 228 |
?> |
| 229 |
<tr class="form-field"> |
| 230 |
<th> |
| 231 |
<label for="wpc_term_img"><?php esc_html_e('Image', 'filter-everything'); ?></label> |
| 232 |
</th> |
| 233 |
<td> |
| 234 |
<?php if( $image = wp_get_attachment_image_url( $image_id, 'medium' ) ) : ?> |
| 235 |
<a href="javascript:void(0);" class="wpc-upload-image-button wpc-upload"> |
| 236 |
<img src="<?php echo esc_url( $image ) ?>" /> |
| 237 |
</a> |
| 238 |
<a href="javascript:void(0);" class="wpc-remove"><?php esc_html_e('Remove image', 'filter-everything'); ?></a> |
| 239 |
<input type="hidden" name="wpc_term_img" id="wpc_term_img" value="<?php echo absint( $image_id ) ?>"> |
| 240 |
<?php else : ?> |
| 241 |
<a href="javascript:void(0);" class="wpc-upload-image-button button wpc-upload"><?php esc_html_e('Upload image', 'filter-everything'); ?></a> |
| 242 |
<a href="javascript:void(0);" class="wpc-remove" style="display:none"><?php esc_html_e('Remove image', 'filter-everything'); ?></a> |
| 243 |
<input type="hidden" name="wpc_term_img" id="wpc_term_img" value=""> |
| 244 |
<?php endif; ?> |
| 245 |
<p id="wpc-image-description"><?php esc_html_e('The image will be shown in the filter term.', 'filter-everything'); ?></p> |
| 246 |
</td> |
| 247 |
</tr><?php |
| 248 |
} |
| 249 |
|
| 250 |
public function save_image_field( $term_id, $tt_id, $taxonomy ){ |
| 251 |
$image_key = 'image'; |
| 252 |
if ( strpos( $taxonomy, 'pa_' ) === 0 ) { |
| 253 |
$image_key = 'product_attribute_image'; |
| 254 |
} |
| 255 |
|
| 256 |
if ( isset( $_POST[ 'wpc_term_img' ] ) ) { |
| 257 |
$image_id = absint( $_POST[ 'wpc_term_img' ] ); |
| 258 |
update_term_meta( $term_id, $image_key, $image_id ); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
public function save_color_field( $term_id, $tt_id, $taxonomy ){ |
| 263 |
$color_key = 'color'; |
| 264 |
if ( strpos( $taxonomy, 'pa_' ) === 0 ) { |
| 265 |
$color_key = 'product_attribute_color'; |
| 266 |
} |
| 267 |
|
| 268 |
if ( isset( $_POST[ 'wpc_term_color' ] ) ) { |
| 269 |
$color = sanitize_text_field( $_POST[ 'wpc_term_color' ] ); |
| 270 |
update_term_meta( $term_id, $color_key, $color ); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
public function columns( $columns ){ |
| 275 |
$new_columns = array(); |
| 276 |
|
| 277 |
if ( isset( $columns[ 'cb' ] ) ) { |
| 278 |
$new_columns[ 'cb' ] = $columns[ 'cb' ]; |
| 279 |
} |
| 280 |
|
| 281 |
$new_columns[ 'wpc-term-meta' ] = ''; |
| 282 |
|
| 283 |
if ( isset( $columns[ 'cb' ] ) ) { |
| 284 |
unset( $columns[ 'cb' ] ); |
| 285 |
} |
| 286 |
|
| 287 |
return array_merge( $new_columns, $columns ); |
| 288 |
} |
| 289 |
|
| 290 |
public function column_preview( $columns, $column, $term_id ) { |
| 291 |
|
| 292 |
if ( 'wpc-term-meta' !== $column ) { |
| 293 |
return $columns; |
| 294 |
} |
| 295 |
|
| 296 |
$this->_display_preview( $term_id, false ); |
| 297 |
|
| 298 |
return $columns; |
| 299 |
} |
| 300 |
|
| 301 |
public function column_preview_pa( $columns, $column, $term_id ) { |
| 302 |
if ( 'wpc-term-meta' !== $column ) { |
| 303 |
return $columns; |
| 304 |
} |
| 305 |
|
| 306 |
$this->_display_preview( $term_id ); |
| 307 |
|
| 308 |
return $columns; |
| 309 |
} |
| 310 |
|
| 311 |
private function _display_preview( $term_id, $is_pa = true ) { |
| 312 |
|
| 313 |
$image_key = 'image'; |
| 314 |
$color_key = 'color'; |
| 315 |
|
| 316 |
if ( $is_pa ) { |
| 317 |
$image_key = 'product_attribute_' . $image_key; |
| 318 |
$color_key = 'product_attribute_' . $color_key; |
| 319 |
} |
| 320 |
|
| 321 |
$image_id = get_term_meta( $term_id, $image_key, true ); |
| 322 |
if ( $image_id ) { |
| 323 |
$maybe_image = wp_get_attachment_image_url( $image_id, 'thumbnail' ); |
| 324 |
if ( $maybe_image ) { |
| 325 |
echo '<img src="'.esc_url( $maybe_image ).'" class="wpc-term-preview" />'."\r\n"; |
| 326 |
return true; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
$color = get_term_meta( $term_id, $color_key, true ); |
| 331 |
if ( $color ) { |
| 332 |
echo '<div class="wpc-term-preview" style="background-color: '.esc_attr( $color ).';"></div>'."\r\n"; |
| 333 |
return true; |
| 334 |
} else { |
| 335 |
echo '<div class="wpc-term-preview" style="background-color: transparent;"></div>'."\r\n"; |
| 336 |
return true; |
| 337 |
} |
| 338 |
|
| 339 |
return false; |
| 340 |
} |
| 341 |
|
| 342 |
} |
| 343 |
|
| 344 |
Container::instance()->getSwatches(); |