array('label'=>__('Front', 'printloom'), 'prefix'=>''), 'back' => array('label'=>__('Back', 'printloom'), 'prefix'=>'back_'), 'sleeve_left' => array('label'=>__('Left sleeve', 'printloom'), 'prefix'=>'sleeve_left_'), 'sleeve_right' => array('label'=>__('Right sleeve', 'printloom'), 'prefix'=>'sleeve_right_'), ); } /** Sides that can be designed. Area keys are stored in the product's _aiph_area meta (percent of the mockup). */ function aiph_side_defs() { return (array) apply_filters('aiph_side_defs', array( 'front' => array('label'=>__('Front', 'printloom'), 'prefix'=>'', 'flag'=>'', 'image'=>'', 'area'=>array('x','y','w','h'), 'size'=>array('pw','ph')), )); } /** Sides enabled for a product. */ function aiph_product_sides($id) { $sides = (array) apply_filters('aiph_product_sides', array('front'), $id); return array_values(array_intersect($sides, array_keys(aiph_side_defs()))); } function aiph_side_prefix($side) { $d = aiph_known_sides(); return isset($d[$side]) ? $d[$side]['prefix'] : ''; } /** Download "kinds" for every side, e.g. preview, back_artwork. */ function aiph_file_kinds() { $kinds = array(); foreach (aiph_known_sides() as $def) foreach (array('preview','artwork','vector') as $k) $kinds[] = $def['prefix'].$k; // Add-ons that store more files per side (e.g. a print PDF) add their kinds so cleanup removes them too. return (array) apply_filters('aiph_file_kinds', $kinds); } function aiph_record_sides($record) { return !empty($record['sides']) && is_array($record['sides']) ? array_values(array_intersect($record['sides'], array_keys(aiph_known_sides()))) : array('front'); } function aiph_sides_label($sides) { $defs = aiph_known_sides(); $names = array(); foreach ($sides as $s) if (isset($defs[$s])) $names[] = $defs[$s]['label']; /* translators: %s: list of printed sides, e.g. "Front + Back" */ return sprintf(__('Custom design: %s', 'printloom'), implode(' + ', $names ?: array($defs['front']['label']))); } /** Area settings with defaults for every side. */ function aiph_settings($id) { $defaults = array('x'=>32, 'y'=>28, 'w'=>36, 'h'=>46, 'bx'=>32, 'by'=>24, 'bw'=>36, 'bh'=>50, 'lx'=>35, 'ly'=>30, 'lw'=>30, 'lh'=>30, 'rx'=>35, 'ry'=>30, 'rw'=>30, 'rh'=>30, 'export'=>1800, 'pw'=>0, 'ph'=>0, 'bpw'=>0, 'bph'=>0, 'lpw'=>0, 'lph'=>0, 'rpw'=>0, 'rph'=>0); $saved = get_post_meta($id, '_aiph_area', true); return array_merge($defaults, is_array($saved) ? $saved : array()); } /** Real print size (shop setting): unit and resolution for print files. */ function aiph_print_unit() { return aiph_opt('unit') === 'cm' ? 'cm' : 'in'; } function aiph_print_dpi() { return max(72, min(600, (int) aiph_opt('dpi'))); } function aiph_size_keys() { return array('pw','ph','bpw','bph','lpw','lph','rpw','rph'); } /** Print size of one side as array(w, h) in the shop unit, or null when not set. */ function aiph_side_print($id, $side) { $d = aiph_side_defs(); if (empty($d[$side]['size'])) return null; $s = aiph_settings($id); list($w, $h) = $d[$side]['size']; return ((float) $s[$w] > 0 && (float) $s[$h] > 0) ? array((float) $s[$w], (float) $s[$h]) : null; } /** "12 × 16 in" */ function aiph_size_text($w, $h, $unit) { return wc_format_localized_decimal(round($w, 2)).' × '.wc_format_localized_decimal(round($h, 2)).' '.$unit; } // ---- Product edit screen --------------------------------------------------------- /** Real print size inputs for one side in the old field layout (kept for add-ons built for 1.0.0). */ function aiph_size_fields($s, $def) { $u = aiph_print_unit(); list($wk, $hk) = $def['size']; $max = $u === 'cm' ? 500 : 200; /* translators: 1: side name, 2: unit (in or cm) */ woocommerce_wp_text_input(array('id'=>'_aiph_'.$wk,'label'=>sprintf(__('%1$s print width (%2$s)', 'printloom'), $def['label'], $u),'value'=>$s[$wk] ?: '','type'=>'number','placeholder'=>$u === 'cm' ? '30' : '12','class'=>'short aiph-size','custom_attributes'=>array('min'=>0,'max'=>$max,'step'=>'0.01'),'description'=>__('Real size of the print area. Leave empty to skip.', 'printloom'),'desc_tip'=>true)); /* translators: 1: side name, 2: unit (in or cm) */ woocommerce_wp_text_input(array('id'=>'_aiph_'.$hk,'label'=>sprintf(__('%1$s print height (%2$s)', 'printloom'), $def['label'], $u),'value'=>$s[$hk] ?: '','type'=>'number','placeholder'=>$u === 'cm' ? '40' : '16','class'=>'short aiph-size','custom_attributes'=>array('min'=>0,'max'=>$max,'step'=>'0.01'))); } // The fields are shown in the PrintLoom product data tab (product-panel.php). add_action('woocommerce_admin_process_product_object', function ($product) { // WooCommerce checks this nonce before the hook too; checking it here keeps the handler safe on its own. $nonce = isset($_POST['woocommerce_meta_nonce']) ? sanitize_text_field(wp_unslash($_POST['woocommerce_meta_nonce'])) : ''; if (!wp_verify_nonce($nonce, 'woocommerce_save_data') || !current_user_can('edit_product', $product->get_id())) return; $product->update_meta_data('_aiph_enabled', isset($_POST['_aiph_enabled']) ? 'yes' : 'no'); $s = aiph_settings($product->get_id()); $sizes = aiph_size_keys(); $max = aiph_print_unit() === 'cm' ? 500 : 200; foreach ($s as $key=>$v) { if (!isset($_POST['_aiph_'.$key])) continue; if (in_array($key, $sizes, true)) { $s[$key] = round(max(0, min($max, (float) wc_format_decimal(sanitize_text_field(wp_unslash($_POST['_aiph_'.$key]))))), 2); continue; } if ($key==='export') { $s[$key] = max(600, min(3600, absint(wp_unslash($_POST['_aiph_'.$key])))); continue; } $s[$key] = round(max(0, min(100, (float) wc_format_decimal(sanitize_text_field(wp_unslash($_POST['_aiph_'.$key]))))), 1); // one decimal keeps the box close to the print size proportions } foreach (array(array('x','y','w','h'), array('bx','by','bw','bh'), array('lx','ly','lw','lh'), array('rx','ry','rw','rh')) as $keys) { list($x, $y, $w, $h) = $keys; $s[$x] = min(99, $s[$x]); $s[$y] = min(99, $s[$y]); $s[$w] = max(1, min($s[$w], 100-$s[$x])); $s[$h] = max(1, min($s[$h], 100-$s[$y])); } $product->update_meta_data('_aiph_area', $s); do_action('aiph_product_save', $product); }); // ---- Cart and orders: notes for the print shop -------------------------------------- add_filter('woocommerce_get_item_data', function ($data, $item) { if (!empty($item['aiph_design']) && !empty($item['aiph_note'])) $data[] = array('key'=>__('Notes', 'printloom'), 'value'=>$item['aiph_note']); return $data; }, 30, 2); add_action('woocommerce_checkout_create_order_line_item', function ($item, $cart_key, $values) { if (!empty($values['aiph_design']) && !empty($values['aiph_note'])) $item->add_meta_data(__('Notes', 'printloom'), $values['aiph_note']); }, 30, 3);