get_id(); try { self::maybe_create_proofs( $order->get_id() ); } finally { self::$defer_artwork_status = false; } } ); add_action( 'woocommerce_order_status_changed', [ __CLASS__, 'hold_for_artwork_after_payment' ], 10, 4 ); // The payment gateway's own status email ("processing") is wrong for // an order that is about to be held for artwork review. add_filter( 'woocommerce_email_enabled_customer_processing_order', [ __CLASS__, 'suppress_hold_email' ], 10, 2 ); add_filter( 'woocommerce_email_enabled_customer_on_hold_order', [ __CLASS__, 'suppress_hold_email' ], 10, 2 ); // Supported render path for block themes and any page builder. add_shortcode( 'proofkit_upload', [ __CLASS__, 'render_shortcode' ] ); // Persist a successful upload for block themes, where the upload // control may render outside the add-to-cart form. add_action( 'wp_ajax_proofkit_store_upload', [ __CLASS__, 'ajax_store_upload' ] ); add_action( 'wp_ajax_nopriv_proofkit_store_upload', [ __CLASS__, 'ajax_store_upload' ] ); add_action( 'wp_enqueue_scripts', static function (): void { self::enqueue_assets(); } ); } /** * Render the upload control on enabled single product pages. */ public static function render_field(): void { if ( ! function_exists( 'is_product' ) || ! is_product() ) { return; } $product_id = (int) get_the_ID(); if ( $product_id <= 0 || ! self::is_enabled_product( $product_id ) ) { return; } self::render_upload_control( $product_id ); } /** * Render the upload control as `[proofkit_upload product_id="123"]`. * * Block themes cannot render the classic * `woocommerce_before_add_to_cart_button` hook reliably, so this shortcode * is the supported render path there. It reuses the same template and * assets as the classic single-product hook. * * @param array|string $atts Shortcode attributes. * @return string Buffered upload control, or '' when unavailable. */ public static function render_shortcode( $atts = [] ): string { $atts = shortcode_atts( [ 'product_id' => 0 ], (array) $atts, 'proofkit_upload' ); $product_id = absint( $atts['product_id'] ); if ( $product_id <= 0 ) { $product_id = (int) get_the_ID(); } if ( $product_id <= 0 || ! self::is_enabled_product( $product_id ) ) { return ''; } ob_start(); self::render_upload_control( $product_id ); return (string) ob_get_clean(); } /** * Store a verified upload token in the WooCommerce session. * * Called after a successful REST upload. The session entry lets the token * reach add-to-cart in block themes where the shortcode control renders * outside the add-to-cart form. */ public static function ajax_store_upload(): void { if ( ! check_ajax_referer( 'wp_rest', 'nonce', false ) ) { wp_send_json_error( [ 'message' => __( 'Your session has expired. Please reload the page and try again.', 'proofkit' ) ], 403 ); } $product_id = isset( $_POST['product_id'] ) && is_scalar( $_POST['product_id'] ) ? absint( wp_unslash( (string) $_POST['product_id'] ) ) : 0; $raw_token = isset( $_POST['token'] ) && is_string( $_POST['token'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['token'] ) ) ) : ''; if ( $product_id <= 0 || ! self::is_enabled_product( $product_id ) || '' === $raw_token ) { wp_send_json_error( [ 'message' => __( 'The uploaded file could not be attached. Please try again.', 'proofkit' ) ], 400 ); } $descriptor = self::descriptor_from_token( $raw_token ); if ( [] === $descriptor || ! self::descriptor_is_stored( $descriptor ) ) { wp_send_json_error( [ 'message' => __( 'We could not find your uploaded artwork. Please upload the file again.', 'proofkit' ) ], 400 ); } if ( ! self::remember_upload( $product_id, $raw_token, $descriptor ) ) { wp_send_json_error( [ 'message' => __( 'Your artwork could not be attached to this session. Please reload the page and try again.', 'proofkit' ) ], 500 ); } wp_send_json_success( [ 'stored' => true ] ); } /** * Block add-to-cart when a required artwork upload is missing or invalid. * * @param bool $passed Whether validation passed. * @param int $product_id Product id. * @param int $qty Quantity. */ public static function validate_add_to_cart( bool $passed, int $product_id, int $qty ): bool { if ( ! self::is_enabled_product( $product_id ) || ! self::is_required_product( $product_id ) ) { return $passed; } $descriptor = self::posted_descriptor(); if ( [] === $descriptor ) { $session = self::session_upload( $product_id ); $descriptor = $session['descriptor'] ?? []; } if ( [] === $descriptor ) { wc_add_notice( __( 'Please upload your artwork before adding this product to the cart.', 'proofkit' ), 'error' ); return false; } if ( ! self::descriptor_is_stored( $descriptor ) ) { wc_add_notice( __( 'We could not find your uploaded artwork. Please upload the file again.', 'proofkit' ), 'error' ); return false; } if ( ! self::descriptor_within_product_limits( $descriptor, $product_id ) ) { wc_add_notice( __( 'This artwork does not meet this product\'s file type or size limits. Please upload a different file.', 'proofkit' ), 'error' ); return false; } return $passed; } /** * Stop classic checkout when a required item has no usable artwork. * * Hooked to both `woocommerce_checkout_process` and * `woocommerce_check_cart_items`; the static flag prevents duplicate * notices when both fire in the same request. */ public static function validate_checkout(): void { if ( self::$checkout_notice_added ) { return; } if ( ! self::cart_needs_artwork() ) { return; } self::$checkout_notice_added = true; wc_add_notice( __( 'Please upload your artwork before placing this order.', 'proofkit' ), 'error' ); } /** * Attach the verified descriptor and its signed token to the cart item. * * @param array $cart_item Cart item data. */ public static function add_item_data( array $cart_item ): array { $product_id = isset( $cart_item['product_id'] ) ? absint( $cart_item['product_id'] ) : 0; $raw = self::posted_token(); if ( '' === $raw && $product_id > 0 ) { $session = self::session_upload( $product_id ); $raw = $session['token'] ?? ''; } if ( '' === $raw ) { return $cart_item; } $descriptor = self::descriptor_from_token( $raw ); if ( [] === $descriptor || ! self::descriptor_is_stored( $descriptor ) ) { return $cart_item; } $cart_item['proofkit'] = $descriptor; // Keep the raw signed token so checkout can re-verify it after the // session round-trip (descriptors alone are not proof of validity). $cart_item['proofkit_token'] = $raw; if ( $product_id > 0 ) { self::forget_upload( $product_id ); } return $cart_item; } /** * Copy a verified Store API upload token into cart item data. * * @param array $cart_item_data Cart item data. * @param \WP_REST_Request|array $request Store API request. * @return array */ public static function store_api_add_to_cart_data( $cart_item_data, $request = null ) { if ( ! is_array( $cart_item_data ) ) { return $cart_item_data; } $product_id = self::request_product_id( $request, $cart_item_data ); $raw = self::token_from_request( $request ); if ( '' === $raw && $product_id > 0 ) { $session = self::session_upload( $product_id ); $raw = $session['token'] ?? ''; } if ( '' === $raw ) { return $cart_item_data; } $descriptor = self::descriptor_from_token( $raw ); if ( [] === $descriptor || ! self::descriptor_is_stored( $descriptor ) ) { return $cart_item_data; } $cart_item_data['proofkit'] = $descriptor; $cart_item_data['proofkit_token'] = $raw; if ( $product_id > 0 ) { self::forget_upload( $product_id ); } return $cart_item_data; } /** * Block Store API add-to-cart for required products without valid artwork. * * @param \WC_Product|int $product Product or product id. * @param \WP_REST_Request|array $request Store API request. * @throws \Exception When required artwork is missing or invalid. */ public static function store_api_validate_add_to_cart( $product, $request = null ): void { $product_id = $product instanceof \WC_Product ? (int) $product->get_id() : absint( $product ); if ( ! self::is_required_product( $product_id ) ) { return; } $raw = self::token_from_request( $request ); $descriptor = '' !== $raw ? self::descriptor_from_token( $raw ) : []; if ( [] === $descriptor && $product_id > 0 ) { $session = self::session_upload( $product_id ); $descriptor = $session['descriptor'] ?? []; } if ( [] === $descriptor || ! self::descriptor_is_stored( $descriptor ) ) { // The Store API converts an exception into a 400 response. throw new \Exception( esc_html__( 'Please upload your artwork before adding this product to the cart.', 'proofkit' ) ); } if ( ! self::descriptor_within_product_limits( $descriptor, $product_id ) ) { throw new \Exception( esc_html__( 'This artwork does not meet this product\'s file type or size limits. Please upload a different file.', 'proofkit' ) ); } } /** * Add the same required-artwork error to Store API cart/checkout errors. * * @param \WP_Error $errors Existing errors. * @param \WC_Cart|null $cart Cart object. * @return \WP_Error */ public static function store_api_cart_errors( $errors, $cart = null ) { if ( ! $errors instanceof \WP_Error ) { $errors = new \WP_Error(); } if ( self::cart_needs_artwork( $cart instanceof \WC_Cart ? $cart : null ) ) { $errors->add( 'proofkit_artwork_required', __( 'Please upload your artwork before placing this order.', 'proofkit' ) ); } return $errors; } /** * Show the artwork name and size in cart and checkout line items. * * @param array $item_data Existing item data. * @param array $cart_item Cart item. */ public static function item_data( array $item_data, array $cart_item ): array { if ( empty( $cart_item['proofkit']['name'] ) ) { return $item_data; } $value = esc_html( (string) $cart_item['proofkit']['name'] ); $size = isset( $cart_item['proofkit']['size'] ) ? (int) $cart_item['proofkit']['size'] : 0; if ( $size > 0 ) { $value .= ' (' . esc_html( Util::human_size( $size ) ) . ')'; } $item_data[] = [ 'name' => __( 'Artwork', 'proofkit' ), 'value' => $value, ]; return $item_data; } /** * Persist the artwork descriptor on the order line item. * * Only a descriptor that still points at a verifiable stored file is * written. Missing or invalid artwork is handled (and blocked) by * maybe_create_proofs() instead of being dropped silently here. * * @param \WC_Order_Item_Product $item Order line item. * @param string $cart_item_key Cart key. * @param array $values Cart item values. * @param \WC_Order $order Order. */ public static function create_order_line_item( \WC_Order_Item_Product $item, string $cart_item_key, array $values, \WC_Order $order ): void { if ( empty( $values['proofkit'] ) || ! is_array( $values['proofkit'] ) ) { return; } $descriptor = self::normalize( $values['proofkit'] ); if ( [] === $descriptor || ! self::descriptor_is_stored( $descriptor ) ) { return; } $item->add_meta_data( '_proofkit_artwork', $descriptor, true ); } /** * Create draft proofs for a new order and move it to artwork review. * * Required items fail closed: the order is blocked in artwork review with * a non-customer note rather than reaching production without artwork. * Never throws and never blocks the payment request itself. * * @param int $order_id Order id. */ public static function maybe_create_proofs( int $order_id ): void { $order = wc_get_order( absint( $order_id ) ); if ( ! $order instanceof \WC_Order ) { return; } $created = []; $blocked = false; foreach ( $order->get_items() as $item ) { if ( ! $item instanceof \WC_Order_Item_Product ) { continue; } // A proof may already exist (for example an admin upload). if ( self::latest_proof( $order->get_id(), (int) $item->get_id() ) ) { continue; } $required = self::is_required_product( (int) $item->get_product_id() ); $stored = $item->get_meta( '_proofkit_artwork', true ); if ( ! is_array( $stored ) || empty( $stored ) ) { if ( $required ) { $blocked = true; self::flag_blocked_item( $order, $item, __( 'no artwork was attached to the order', 'proofkit' ) ); } continue; } $descriptor = self::normalize( $stored ); if ( [] === $descriptor || ! self::descriptor_is_stored( $descriptor ) ) { if ( $required ) { $blocked = true; self::flag_blocked_item( $order, $item, __( 'the attached artwork could not be verified', 'proofkit' ) ); } else { $order->add_order_note( __( 'Artwork upload could not be verified', 'proofkit' ), 0, false ); } continue; } $proof_id = Proof::create( [ 'order_id' => $order->get_id(), 'order_item_id' => $item->get_id(), 'file' => $descriptor, 'created_by' => 0, ] ); if ( is_wp_error( $proof_id ) ) { if ( $required ) { $blocked = true; self::flag_blocked_item( $order, $item, sprintf( /* translators: %s: error message */ __( 'the proof could not be created: %s', 'proofkit' ), $proof_id->get_error_message() ) ); } else { $order->add_order_note( sprintf( /* translators: %s: error message */ __( 'Artwork proof could not be created: %s', 'proofkit' ), $proof_id->get_error_message() ), 0, false ); } continue; } $created[] = (int) $proof_id; } if ( $blocked ) { self::hold_for_artwork( $order, __( 'Artwork required. The order is held until the customer supplies valid artwork.', 'proofkit' ) ); return; } if ( [] === $created ) { return; } self::hold_for_artwork( $order, __( 'Artwork received. A proof will be prepared for the customer.', 'proofkit' ) ); } /** * Move an order to artwork review, or defer that move past the payment step. * * The Store API fires its "order processed" action before the gateway runs, * so changing the status there would immediately be overwritten (COD sets * "processing", bank transfer sets "on-hold"). In that pass we only mark * the order; hold_for_artwork_after_payment() applies the status once the * gateway has finished. * * @param \WC_Order $order Order to hold. * @param string $note Order note explaining the hold. */ private static function hold_for_artwork( \WC_Order $order, string $note ): void { if ( self::$defer_artwork_status ) { $order->update_meta_data( '_proofkit_hold_artwork', 'yes' ); $order->update_meta_data( '_proofkit_hold_artwork_note', $note ); $order->save(); return; } // Only unpaid/in-progress orders may be moved to artwork review. if ( in_array( $order->get_status(), [ 'pending', 'on-hold', 'processing' ], true ) ) { $order->update_status( Order::STATUS_ARTWORK, $note, false ); return; } $order->save(); } /** * Apply the deferred artwork hold once payment has set the order status. * * @param int $order_id Order id. * @param string $from Previous status (without wc- prefix). * @param string $to New status (without wc- prefix). * @param \WC_Order $order Order object. */ public static function hold_for_artwork_after_payment( $order_id, $from, $to, $order ): void { if ( self::$applying_hold || ! $order instanceof \WC_Order ) { return; } if ( ! in_array( (string) $to, [ 'pending', 'processing', 'on-hold' ], true ) ) { return; } if ( 'yes' !== (string) $order->get_meta( '_proofkit_hold_artwork' ) ) { return; } $note = (string) $order->get_meta( '_proofkit_hold_artwork_note' ); if ( '' === $note ) { $note = __( 'Artwork received. A proof will be prepared for the customer.', 'proofkit' ); } $order->delete_meta_data( '_proofkit_hold_artwork' ); $order->delete_meta_data( '_proofkit_hold_artwork_note' ); self::$applying_hold = true; try { $order->update_status( Order::STATUS_ARTWORK, $note, false ); } finally { self::$applying_hold = false; self::$holding_order_id = 0; } } /** * Suppress status emails for an order that is being held for artwork. * * @param bool $enabled Whether the email is enabled. * @param mixed $email Email object (WC_Email) or null. * @return bool */ public static function suppress_hold_email( $enabled, $email = null ): bool { if ( ! self::$holding_order_id || ! is_object( $email ) || ! isset( $email->object ) ) { return (bool) $enabled; } $order = $email->object; if ( $order instanceof \WC_Order && $order->get_id() === self::$holding_order_id ) { return false; } return (bool) $enabled; } /** * Register and localise the upload assets on product and shortcode pages. */ private static function enqueue_assets(): void { $product_id = 0; if ( function_exists( 'is_product' ) && is_product() ) { $product_id = (int) get_queried_object_id(); if ( $product_id <= 0 || ! self::is_enabled_product( $product_id ) ) { return; } } elseif ( ! self::page_has_upload_shortcode() ) { return; } wp_enqueue_style( 'proofkit-frontend', PROOFKIT_URL . 'assets/css/frontend.css', [], PROOFKIT_VERSION ); wp_enqueue_script( 'proofkit-upload', PROOFKIT_URL . 'assets/js/upload.js', [], PROOFKIT_VERSION, true ); $max_bytes = $product_id > 0 ? self::product_max_bytes( $product_id ) : (int) Settings::max_bytes(); $exts = $product_id > 0 ? self::product_allowed_extensions( $product_id ) : array_values( (array) Settings::allowed_extensions() ); wp_localize_script( 'proofkit-upload', 'proofkitUpload', [ 'restUrl' => esc_url_raw( rest_url() ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'maxBytes' => (int) $max_bytes, 'allowedExts' => array_values( $exts ), 'i18n' => [ 'choose' => __( 'Choose artwork', 'proofkit' ), 'uploading' => __( 'Uploading…', 'proofkit' ), 'uploaded' => __( 'Artwork uploaded.', 'proofkit' ), 'tooLarge' => __( 'This file is larger than the allowed size.', 'proofkit' ), 'badType' => __( 'This file type is not allowed.', 'proofkit' ), 'remove' => __( 'Remove', 'proofkit' ), 'required' => __( 'Please upload your artwork before adding this product to the cart.', 'proofkit' ), 'noFile' => __( 'No file selected.', 'proofkit' ), 'emptyFile' => __( 'That file is empty.', 'proofkit' ), 'uploadFailed' => __( 'Upload failed. Please try again.', 'proofkit' ), 'networkError' => __( 'The upload could not reach the server. Please try again.', 'proofkit' ), 'wait' => __( 'Please wait for the artwork upload to finish.', 'proofkit' ), 'notUploaded' => __( 'Your artwork was not uploaded. Try again or remove the file before continuing.', 'proofkit' ), ], 'ajaxUrl' => esc_url_raw( admin_url( 'admin-ajax.php' ) ), 'sessionAction' => 'proofkit_store_upload', ] ); } /** * Whether the current singular post contains the upload shortcode. */ private static function page_has_upload_shortcode(): bool { if ( ! function_exists( 'has_shortcode' ) ) { return false; } $post = get_post(); return $post instanceof \WP_Post && has_shortcode( (string) $post->post_content, 'proofkit_upload' ); } /** * Render the shared product upload template. * * @param int $product_id Product id. */ private static function render_upload_control( int $product_id ): void { $allowed_exts = self::product_allowed_extensions( $product_id ); self::render( 'product/upload.php', [ 'product_id' => $product_id, 'required' => self::is_required_product( $product_id ), 'instructions' => self::product_instructions( $product_id ), 'max_bytes' => self::product_max_bytes( $product_id ), 'allowed_exts' => $allowed_exts, 'accept' => self::accept_attr( $allowed_exts ), ] ); } /** * Whether the cart holds a required item without valid artwork. * * @param \WC_Cart|null $cart Optional cart object. */ private static function cart_needs_artwork( ?\WC_Cart $cart = null ): bool { if ( null === $cart ) { if ( ! function_exists( 'WC' ) || ! WC()->cart instanceof \WC_Cart ) { return false; } $cart = WC()->cart; } $items = $cart->get_cart(); if ( ! is_array( $items ) ) { return false; } foreach ( $items as $item ) { if ( ! is_array( $item ) ) { continue; } $product_id = isset( $item['product_id'] ) ? absint( $item['product_id'] ) : 0; if ( ! self::is_required_product( $product_id ) ) { continue; } if ( ! self::cart_item_has_valid_artwork( $item ) ) { return true; } } return false; } /** * Whether a cart item carries a usable, verified descriptor. * * @param array $item Cart item. */ private static function cart_item_has_valid_artwork( array $item ): bool { $descriptor = isset( $item['proofkit'] ) && is_array( $item['proofkit'] ) ? self::normalize( $item['proofkit'] ) : []; $raw = isset( $item['proofkit_token'] ) && is_string( $item['proofkit_token'] ) ? trim( $item['proofkit_token'] ) : ''; if ( '' !== $raw ) { $verified = self::descriptor_from_token( $raw ); if ( [] !== $verified ) { $descriptor = $verified; } } if ( [] === $descriptor ) { return false; } return self::descriptor_is_stored( $descriptor ); } /** * Session key used to carry an upload for one product between requests. * * @param int $product_id Product id. */ private static function upload_session_key( int $product_id ): string { return 'proofkit_upload_' . self::session_product_id( $product_id ); } /** * Use the parent product id so variations share one upload slot. * * @param int $product_id Product or variation id. */ private static function session_product_id( int $product_id ): int { if ( $product_id <= 0 || ! function_exists( 'wc_get_product' ) ) { return $product_id; } $product = wc_get_product( $product_id ); if ( $product instanceof \WC_Product_Variation ) { return (int) $product->get_parent_id(); } return $product_id; } /** * WooCommerce session, when it is available on this request. */ private static function session(): ?\WC_Session { if ( ! function_exists( 'WC' ) ) { return null; } $wc = WC(); if ( ! is_object( $wc ) || ! isset( $wc->session ) || ! $wc->session instanceof \WC_Session ) { return null; } return $wc->session; } /** * Persist a verified token and descriptor for a product in the session. * * @param int $product_id Product id. * @param string $token Signed upload token. * @param array $descriptor Verified descriptor. */ private static function remember_upload( int $product_id, string $token, array $descriptor ): bool { $session = self::session(); if ( null === $session || $product_id <= 0 || '' === $token ) { return false; } $session->set( self::upload_session_key( $product_id ), [ 'token' => $token, 'descriptor' => $descriptor, 'at' => time(), ] ); return true; } /** * Delete a stored upload for one product from the session. * * @param int $product_id Product id. */ private static function forget_upload( int $product_id ): void { $session = self::session(); if ( null === $session || $product_id <= 0 ) { return; } $key = self::upload_session_key( $product_id ); if ( method_exists( $session, 'forget' ) ) { $session->forget( $key ); return; } $session->set( $key, null ); } /** * Read, re-verify and return a session upload for a product. * * The stored descriptor is never trusted: it is derived again from the * signed token. Stale or unverifiable entries are deleted. * * @param int $product_id Product id. * @return array{token:string,descriptor:array}|array */ private static function session_upload( int $product_id ): array { $session = self::session(); if ( null === $session || $product_id <= 0 ) { return []; } $stored = $session->get( self::upload_session_key( $product_id ) ); if ( ! is_array( $stored ) || ! isset( $stored['token'] ) || ! is_string( $stored['token'] ) ) { if ( null !== $stored ) { self::forget_upload( $product_id ); } return []; } $at = isset( $stored['at'] ) ? (int) $stored['at'] : 0; if ( $at <= 0 || ( time() - $at ) > 72 * HOUR_IN_SECONDS ) { self::forget_upload( $product_id ); return []; } $token = trim( sanitize_text_field( $stored['token'] ) ); $descriptor = self::descriptor_from_token( $token ); if ( [] === $descriptor || ! self::descriptor_is_stored( $descriptor ) ) { self::forget_upload( $product_id ); return []; } return [ 'token' => $token, 'descriptor' => $descriptor, ]; } /** * Read the raw signed upload token posted with the add-to-cart form. */ private static function posted_token(): string { // The value is HMAC-signed and verified by signature, and WooCommerce // has already checked the add-to-cart nonce before this runs. // phpcs:disable WordPress.Security.NonceVerification.Missing if ( ! isset( $_POST['proofkit_upload_token'] ) ) { return ''; } $raw = wp_unslash( $_POST['proofkit_upload_token'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitised below. if ( ! is_string( $raw ) ) { return ''; } // phpcs:enable WordPress.Security.NonceVerification.Missing return trim( sanitize_text_field( $raw ) ); } /** * Read and verify the signed upload token posted with the add-to-cart form. */ private static function posted_descriptor(): array { $raw = self::posted_token(); return '' !== $raw ? self::descriptor_from_token( $raw ) : []; } /** * Resolve the product id from a Store API request or cart item data. * * @param \WP_REST_Request|array|mixed $request Request. * @param array $cart_item_data Cart item data. */ private static function request_product_id( $request, array $cart_item_data = [] ): int { if ( isset( $cart_item_data['product_id'] ) ) { return absint( $cart_item_data['product_id'] ); } $candidates = []; if ( $request instanceof \WP_REST_Request ) { $candidates[] = $request->get_param( 'id' ); $candidates[] = $request->get_param( 'product_id' ); } elseif ( is_array( $request ) ) { $candidates[] = $request['id'] ?? null; $candidates[] = $request['product_id'] ?? null; } foreach ( $candidates as $candidate ) { if ( is_numeric( $candidate ) && (int) $candidate > 0 ) { return absint( $candidate ); } } return 0; } /** * Read the upload token from a Store API request in either supported shape. * * @param \WP_REST_Request|array|mixed $request Request. */ private static function token_from_request( $request ): string { $raw = null; if ( $request instanceof \WP_REST_Request ) { $raw = $request->get_param( 'proofkit_upload_token' ); if ( ! is_string( $raw ) || '' === trim( $raw ) ) { $data = $request->get_param( 'data' ); if ( is_array( $data ) && isset( $data['proofkit_upload_token'] ) ) { $raw = $data['proofkit_upload_token']; } } } elseif ( is_array( $request ) ) { if ( isset( $request['proofkit_upload_token'] ) ) { $raw = $request['proofkit_upload_token']; } if ( ( ! is_string( $raw ) || '' === trim( $raw ) ) && isset( $request['data'] ) && is_array( $request['data'] ) ) { $raw = $request['data']['proofkit_upload_token'] ?? null; } } if ( ! is_string( $raw ) ) { return ''; } return trim( sanitize_text_field( wp_unslash( $raw ) ) ); } /** * Verify a signed upload token and return a normalised descriptor. * * @param string $raw Signed upload token. */ private static function descriptor_from_token( string $raw ): array { if ( '' === $raw ) { return []; } $descriptor = Token::verify_upload( $raw ); if ( is_wp_error( $descriptor ) || ! is_array( $descriptor ) ) { return []; } return self::normalize( $descriptor ); } /** * Keep only the descriptor keys the plugin trusts. * * @param array $descriptor Raw descriptor. */ private static function normalize( array $descriptor ): array { $path = isset( $descriptor['path'] ) ? (string) $descriptor['path'] : ''; $sha256 = isset( $descriptor['sha256'] ) ? (string) $descriptor['sha256'] : ''; if ( '' === $path || '' === $sha256 ) { return []; } return [ 'path' => $path, 'name' => isset( $descriptor['name'] ) ? (string) $descriptor['name'] : '', 'mime' => isset( $descriptor['mime'] ) ? (string) $descriptor['mime'] : '', 'size' => isset( $descriptor['size'] ) ? (int) $descriptor['size'] : 0, 'sha256' => $sha256, ]; } /** * Confirm the stored file exists and still matches its hash. * * @param array $descriptor Normalised descriptor. */ private static function descriptor_is_stored( array $descriptor ): bool { if ( empty( $descriptor['path'] ) || empty( $descriptor['sha256'] ) ) { return false; } return Upload::exists( (string) $descriptor['path'] ) && Upload::verify( (string) $descriptor['path'], (string) $descriptor['sha256'] ); } /** * Confirm a descriptor still fits the product's effective limits. * * @param array $descriptor Normalised descriptor. * @param int $product_id Product id. */ private static function descriptor_within_product_limits( array $descriptor, int $product_id ): bool { $size = isset( $descriptor['size'] ) ? (int) $descriptor['size'] : 0; if ( $size > 0 && $size > self::product_max_bytes( $product_id ) ) { return false; } $name = isset( $descriptor['name'] ) ? (string) $descriptor['name'] : ''; $ext = strtolower( (string) pathinfo( $name, PATHINFO_EXTENSION ) ); $allowed = self::product_allowed_extensions( $product_id ); return '' !== $ext && in_array( $ext, $allowed, true ); } /** * Latest proof for an order item, if the Proof API is available. * * @param int $order_id Order id. * @param int $order_item_id Order item id. */ private static function latest_proof( int $order_id, int $order_item_id ): ?object { if ( ! is_callable( [ Proof::class, 'latest_for_item' ] ) ) { return null; } $proof = Proof::latest_for_item( $order_id, $order_item_id ); return is_object( $proof ) ? $proof : null; } /** * Add a non-customer note and an error log entry for a blocked item. * * @param \WC_Order $order Order. * @param \WC_Order_Item_Product $item Order item. * @param string $reason Human-readable reason. */ private static function flag_blocked_item( \WC_Order $order, \WC_Order_Item_Product $item, string $reason ): void { $order->add_order_note( sprintf( /* translators: 1: item name, 2: reason the artwork is unusable */ __( 'Artwork required but unavailable for %1$s: %2$s. The order is blocked until this is resolved.', 'proofkit' ), (string) $item->get_name(), $reason ), 0, false ); self::log( sprintf( 'Order %1$d item %2$d blocked: %3$s', $order->get_id(), $item->get_id(), $reason ), [ 'level' => 'error', 'order_id' => $order->get_id(), 'order_item_id' => $item->get_id(), ] ); } /** * Whether artwork is enabled for a product (guarded for the stub harness). * * @param int $product_id Product id. */ private static function is_enabled_product( int $product_id ): bool { if ( $product_id <= 0 || ! class_exists( Product::class ) || ! is_callable( [ Product::class, 'is_enabled' ] ) ) { return false; } return (bool) Product::is_enabled( $product_id ); } /** * Whether artwork is required for a product (guarded for the stub harness). * * @param int $product_id Product id. */ private static function is_required_product( int $product_id ): bool { if ( $product_id <= 0 || ! class_exists( Product::class ) || ! is_callable( [ Product::class, 'is_required' ] ) ) { return false; } return (bool) Product::is_required( $product_id ); } /** * Product instructions with a guarded fallback. * * @param int $product_id Product id. */ private static function product_instructions( int $product_id ): string { if ( ! class_exists( Product::class ) || ! is_callable( [ Product::class, 'instructions' ] ) ) { return ''; } return (string) Product::instructions( $product_id ); } /** * Effective product maximum size, clamped to the global ceiling. * * @param int $product_id Product id. */ private static function product_max_bytes( int $product_id ): int { $global = max( 1, (int) Settings::max_bytes() ); if ( $product_id > 0 && class_exists( Product::class ) && is_callable( [ Product::class, 'max_bytes' ] ) ) { $product = (int) Product::max_bytes( $product_id ); if ( $product > 0 ) { return min( $global, $product ); } } return $global; } /** * Effective product extension list, intersected with the global list. * * @param int $product_id Product id. * @return string[] */ private static function product_allowed_extensions( int $product_id ): array { $global = array_values( (array) Settings::allowed_extensions() ); if ( $product_id > 0 && class_exists( Product::class ) && is_callable( [ Product::class, 'allowed_extensions' ] ) ) { $product = array_values( (array) Product::allowed_extensions( $product_id ) ); if ( [] !== $product ) { $intersection = array_values( array_intersect( $global, $product ) ); if ( [] !== $intersection ) { return $intersection; } } } return $global; } /** * Build an accept attribute from an extension list. * * @param array $exts Extensions without dots. */ private static function accept_attr( array $exts ): string { $parts = []; foreach ( $exts as $ext ) { $ext = ltrim( sanitize_key( (string) $ext ), '.' ); if ( '' !== $ext ) { $parts[] = '.' . $ext; } } return implode( ',', $parts ); } /** * Include a plugin template in an isolated scope. Theme overrides are not * supported in v1. * * @param string $template Relative template path. * @param array $vars Template variables. */ private static function render( string $template, array $vars ): void { $file = PROOFKIT_DIR . 'templates/' . ltrim( $template, '/' ); if ( ! is_readable( $file ) ) { return; } ( static function () use ( $file, $vars ): void { extract( $vars, EXTR_SKIP ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract include $file; } )(); } /** * Log through Util without letting logging break checkout. * * @param string $message Message. * @param array $context Structured context. */ private static function log( string $message, array $context = [] ): void { if ( ! is_callable( [ Util::class, 'log' ] ) ) { return; } try { Util::log( $message, $context ); } catch ( \Throwable ) { return; } } }