';
}
/* A badge on the product photo: on the first image of the product page's gallery. */
add_filter('woocommerce_single_product_image_thumbnail_html', function ($html, $attachment_id) {
static $done = array();
global $product;
if (!$product || !usedkit_condition_on() || usedkit_settings()['cond_page'] !== 'photo' || isset($done[$product->get_id()])
|| ($g = usedkit_condition($product)) === '') {
return $html;
}
$done[$product->get_id()] = true;
return preg_replace('/^(\s*
]*>)/', '$1' . wp_kses(usedkit_grade_pill($g, 'usedkit-on-photo'), usedkit_allowed_html()), $html, 1);
}, 10, 2);
/* In product listings: a pill, text or a small scale under the title, or a badge on the photo. */
add_action('woocommerce_after_shop_loop_item_title', function () {
global $product;
if (!$product || !usedkit_condition_on() || ($g = usedkit_condition($product)) === '') {
return;
}
$style = usedkit_settings()['cond_loop'];
if ($style === 'pill') {
echo wp_kses(usedkit_grade_pill($g), usedkit_allowed_html());
} elseif ($style === 'text') {
/* translators: %s: the item's condition grade, e.g. Good */
$text = sprintf(__('Condition: %s', 'usedkit'), usedkit_grade_names()[$g]);
printf('%s', esc_attr($g), esc_html($text));
} elseif ($style === 'scale') {
printf('%s%s', esc_attr($g),
wp_kses(usedkit_scale_html($g), usedkit_allowed_html()), esc_html(usedkit_grade_names()[$g]));
}
}, 6);
add_action('woocommerce_before_shop_loop_item_title', function () {
global $product;
if (!$product || !usedkit_condition_on() || usedkit_settings()['cond_loop'] !== 'photo' || ($g = usedkit_condition($product)) === '') {
return;
}
echo wp_kses(usedkit_grade_pill($g, 'usedkit-on-photo'), usedkit_allowed_html());
}, 9);
/* itemCondition in the product's structured data, on every offer. Ungraded items in a
* used shop are still used. */
add_filter('woocommerce_structured_data_product', function ($markup, $product) {
if (!usedkit_condition_on() || empty($markup['offers'])) {
return $markup;
}
$g = usedkit_condition($product);
$url = usedkit_grade_schema($g === '' ? 'good' : $g);
foreach ($markup['offers'] as $i => $offer) {
if (is_array($offer)) {
$markup['offers'][$i]['itemCondition'] = $url;
}
}
return $markup;
}, 10, 2);
/* The filter: ?condition=excellent,good on any product listing, and a select beside
* the sorting that sets it. */
function usedkit_condition_filter() {
$raw = sanitize_text_field(wp_unslash($_GET['condition'] ?? '')); // phpcs:ignore WordPress.Security.NonceVerification
return array_values(array_intersect(explode(',', $raw), usedkit_grade_keys()));
}
add_action('woocommerce_product_query', function ($q) {
if (!usedkit_condition_on() || !($grades = usedkit_condition_filter())) {
return;
}
$meta = (array) $q->get('meta_query');
$meta[] = array('key' => '_usedkit_condition', 'value' => $grades, 'compare' => 'IN');
$q->set('meta_query', $meta);
});
add_action('woocommerce_before_shop_loop', function () {
if (!usedkit_condition_on()) {
return;
}
$now = usedkit_condition_filter();
$now = count($now) === 1 ? $now[0] : '';
echo '';
}, 25);
/* [usedkit_conditions]: the grades and what each means, for a shop's "Our grading" page. */
add_shortcode('usedkit_conditions', function () {
$out = '