array('GET', 'POST'), 'callback' => array(__CLASS__, 'handle'), 'permission_callback' => function () { return current_user_can('manage_woocommerce'); }, )); } private static function barcode($id) { foreach (self::KEYS as $key) { $value = get_post_meta($id, $key, true); if (is_scalar($value) && trim((string) $value) !== '') { return trim((string) $value); } } return null; } public static function handle($request) { if (!function_exists('wc_get_product')) { return new WP_Error('unavailable', 'WooCommerce unavailable.', array('status' => 409)); } if ($request->get_method() === 'GET') { $ids = $request->get_param('product_ids'); if (!$ids) { $codes = explode(',', (string) $request->get_param('barcodes')); if (count($codes) > 50 || !count($codes)) { return new WP_Error('invalid', 'Invalid codes.', array('status' => 400)); } foreach ($codes as $code) { if (!preg_match('/^\d{8,14}$/D', $code)) { return new WP_Error('invalid', 'Invalid code.', array('status' => 400)); } } $matches = get_posts(array('post_type' => array('product', 'product_variation'), 'post_status' => 'any', 'posts_per_page' => 101, 'fields' => 'ids', 'meta_query' => array(array('key' => self::KEYS, 'compare_key' => 'IN', 'value' => $codes, 'compare' => 'IN')))); if (count($matches) >= 101) { return new WP_Error('unavailable', 'Too many matching barcodes.', array('status' => 409)); } $variants = array(); foreach ($matches as $id) { foreach (self::KEYS as $key) { $value = get_post_meta($id, $key, true); if (is_scalar($value) && in_array(trim((string) $value), $codes, true)) { $variants[] = array('id' => (string) $id, 'barcode' => trim((string) $value)); } } } return array('variants' => $variants); } if (!is_string($ids) || !preg_match('/^[1-9]\d*(?:,[1-9]\d*)*$/D', $ids)) { return new WP_Error('invalid', 'Invalid product IDs.', array('status' => 400)); } $ids = explode(',', $ids); if (count($ids) > 100) { return new WP_Error('invalid', 'Too many products.', array('status' => 400)); } $variants = array(); foreach ($ids as $id) { $product = wc_get_product((int) $id); if (!$product || !current_user_can('edit_post', (int) $id)) { return new WP_Error('forbidden', 'Product unavailable.', array('status' => 403)); } $children = $product->is_type('variable') ? $product->get_children() : array((int) $id); foreach ($children as $child) { if (count($variants) >= 5000) { return new WP_Error('unavailable', 'Too many variations.', array('status' => 409)); } $variant = wc_get_product($child); if (!$variant) { return new WP_Error('unavailable', 'Variation unavailable.', array('status' => 409)); } $options = array(); if ($variant->is_type('variation')) { foreach ($variant->get_attributes() as $name => $value) { $options[] = array('name' => wc_attribute_label($name), 'value' => (string) $value); } } $variants[] = array('productId' => (string) $id, 'id' => (string) $child, 'barcode' => self::barcode($child), 'sku' => $variant->get_sku(), 'selectedOptions' => $options); } } return array('variants' => $variants); } $body = $request->get_json_params(); $entries = isset($body['entries']) ? $body['entries'] : null; if (!is_array($entries) || !count($entries) || count($entries) > 100) { return new WP_Error('invalid', 'Invalid entries.', array('status' => 400)); } foreach ($entries as $entry) { $id = isset($entry['variantId']) ? (int) $entry['variantId'] : 0; $product = wc_get_product($id); $parent = $product ? ($product->get_parent_id() ?: $id) : 0; if (!$product || (string) $parent !== $entry['productId'] || !current_user_can('edit_post', $id)) { return new WP_Error('forbidden', 'Product unavailable.', array('status' => 403)); } $code = isset($entry['barcode']) ? $entry['barcode'] : ''; if (!is_string($code) || !preg_match('/^(?:\d{8}|\d{12}|\d{13}|\d{14})$/D', $code)) { return new WP_Error('invalid', 'Invalid GTIN.', array('status' => 400)); } $sum = 0; $digits = strrev(substr($code, 0, -1)); for ($i = 0; $i < strlen($digits); $i++) { $sum += (int) $digits[$i] * ($i % 2 === 0 ? 3 : 1); } if ((10 - $sum % 10) % 10 !== (int) substr($code, -1)) { return new WP_Error('invalid', 'Invalid GTIN.', array('status' => 400)); } } $outcomes = array(); foreach ($entries as $entry) { $id = (int) $entry['variantId']; $product = wc_get_product($id); $key = method_exists($product, 'set_global_unique_id') ? '_global_unique_id' : '_wpm_gtin_code'; $existing_key = null; $populated_key = null; foreach (self::KEYS as $candidate) { if ($existing_key === null && metadata_exists('post', $id, $candidate)) { $existing_key = $candidate; } if (trim((string) get_post_meta($id, $candidate, true)) !== '') { $populated_key = $candidate; break; } } $key = $populated_key ?: ($existing_key ?: $key); try { if ($key === '_global_unique_id' && method_exists($product, 'set_global_unique_id')) { $product->set_global_unique_id($entry['barcode']); $product->save(); } else { update_post_meta($id, $key, $entry['barcode']); } wc_delete_product_transients($id); } catch (Throwable $error) { // A later save must not hide variations already written by this request. } $outcomes[] = array('variantId' => (string) $id, 'ok' => self::barcode($id) === $entry['barcode']); } return array('ok' => count(array_filter($outcomes, function ($outcome) { return !$outcome['ok']; })) === 0, 'outcomes' => $outcomes); } }