div{flex:1}.usedkit-intake .button-hero{margin:18px 8px 0 0}.usedkit-intake .check{font-weight:400}'); }); /** The most recent other product of the same model, to copy from; null if none. */ function usedkit_intake_source($title, $except = 0) { $key = function_exists('usedkit_plain_key') ? usedkit_plain_key($title) : ''; $recent = wc_get_products(array('limit' => 300, 'status' => array('publish', 'private', 'draft'), 'orderby' => 'date', 'order' => 'DESC', 'exclude' => $except ? array($except) : array())); foreach ($recent as $p) { if (mb_strtolower(trim($p->get_name())) === mb_strtolower(trim($title))) { return $p; } } if ($key === '') { return null; } $keys = function_exists('usedkit_model_keys') ? usedkit_model_keys($recent, false) : array(); foreach ($recent as $p) { if (($keys[$p->get_id()] ?? '') === $key) { return $p; } } return null; } /** Copy what describes the model, not the unit, from one product to another. */ function usedkit_intake_copy($from, $to) { $to->set_description($from->get_description()); $to->set_short_description($from->get_short_description()); $to->set_category_ids($from->get_category_ids()); $to->set_tag_ids($from->get_tag_ids()); $to->set_shipping_class_id($from->get_shipping_class_id()); $to->set_weight($from->get_weight()); $to->set_length($from->get_length()); $to->set_width($from->get_width()); $to->set_height($from->get_height()); $to->set_tax_class($from->get_tax_class()); $to->set_attributes(array_map(function ($a) { return clone $a; }, $from->get_attributes())); foreach (array('_usedkit_specs', '_usedkit_margin', '_usedkit_match_key') as $key) { if ($from->get_meta($key) !== '') { $to->update_meta_data($key, $from->get_meta($key)); } } $brands = wp_get_post_terms($from->get_id(), 'product_brand', array('fields' => 'ids')); return is_array($brands) ? $brands : array(); } function usedkit_intake_page() { if (!current_user_can('edit_products')) { return; } $done = null; $error = ''; if (isset($_POST['usedkit_intake']) && check_admin_referer('usedkit_intake')) { $result = usedkit_intake_save(); is_wp_error($result) ? $error = $result->get_error_message() : $done = wc_get_product($result); } $titles = array_unique(array_map(function ($p) { return $p->get_name(); }, wc_get_products(array('limit' => 200, 'orderby' => 'date', 'order' => 'DESC', 'status' => array('publish', 'private', 'draft'))))); $s = usedkit_settings(); $cats = get_terms(array('taxonomy' => 'product_cat', 'hide_empty' => false)); ?>
set_name($title); $brands = array(); if (!empty($_POST['copy']) && ($from = usedkit_intake_source($title))) { $brands = usedkit_intake_copy($from, $p); } if ($cat = absint($_POST['cat'] ?? 0)) { $p->set_category_ids(array($cat)); } $p->set_regular_price($price); $p->set_manage_stock(true); $p->set_stock_quantity(1); $p->set_sold_individually(true); $p->set_status(sanitize_key(wp_unslash($_POST['usedkit_intake'] ?? '')) === 'publish' && current_user_can('publish_products') ? 'publish' : 'draft'); $meta = array( '_usedkit_cost' => wc_format_decimal(sanitize_text_field(wp_unslash($_POST['cost'] ?? ''))), '_usedkit_condition' => in_array($_POST['grade'] ?? '', usedkit_grade_keys(), true) ? sanitize_key($_POST['grade']) : '', '_usedkit_condition_note' => sanitize_textarea_field(wp_unslash($_POST['note'] ?? '')), '_usedkit_included' => sanitize_text_field(wp_unslash($_POST['included'] ?? '')), '_usedkit_seller' => sanitize_text_field(wp_unslash($_POST['seller'] ?? '')), '_usedkit_serial' => sanitize_text_field(wp_unslash($_POST['serial'] ?? '')), ); foreach ($meta as $key => $value) { if ($value !== '') { $p->update_meta_data($key, $value); } } if ($meta['_usedkit_cost'] !== '' || $meta['_usedkit_seller'] !== '') { $p->update_meta_data('_usedkit_bought', current_time('Y-m-d')); } $id = $p->save(); if ($brands) { wp_set_object_terms($id, $brands, 'product_brand'); } // The photos: the first is the product image, the rest its gallery. $images = usedkit_intake_photos($id); if ($images) { $p = wc_get_product($id); $p->set_image_id(array_shift($images)); $p->set_gallery_image_ids($images); $p->save(); } // phpcs:enable return $id; } /** Upload the posted photos to the media library, attached to the product. */ function usedkit_intake_photos($product_id) { if (empty($_FILES['photos']['name']) || !is_array($_FILES['photos']['name'])) { // phpcs:ignore WordPress.Security.NonceVerification return array(); } require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; $files = $_FILES['photos']; // phpcs:ignore WordPress.Security.NonceVerification, WordPress.Security.ValidatedSanitizedInput -- handed to media_handle_upload, which validates $ids = array(); foreach (array_keys($files['name']) as $i) { if ((int) $files['error'][$i] !== UPLOAD_ERR_OK) { continue; } $_FILES['usedkit_photo'] = array('name' => $files['name'][$i], 'type' => $files['type'][$i], 'tmp_name' => $files['tmp_name'][$i], 'error' => $files['error'][$i], 'size' => $files['size'][$i]); $id = media_handle_upload('usedkit_photo', $product_id); if (!is_wp_error($id)) { $ids[] = $id; } } unset($_FILES['usedkit_photo']); return $ids; }