0, 'sent' => 1, 'draft' => 2, 'approved' => 3, 'superseded' => 4, ]; /** * Whether a ProofKit sync is currently changing order status. * * @var bool */ private static bool $syncing = false; /** * Action forms to print in the admin footer for the current order screen. * * @var array, multipart: bool}> */ private static array $action_forms = []; /** * Hook into WooCommerce and WordPress. */ public static function init(): void { add_filter( 'woocommerce_order_statuses', [ __CLASS__, 'register_statuses' ] ); add_filter( 'wc_order_statuses', [ __CLASS__, 'register_statuses' ] ); add_action( 'init', [ __CLASS__, 'register_post_statuses' ] ); // Status changes made by a proof decision must not trigger WooCommerce's // own "your order is now processing" customer emails; ProofKit sends its // own proof notification and the shop can email manually if needed. foreach ( [ 'customer_processing_order', 'customer_on_hold_order', 'customer_completed_order', 'customer_refunded_order' ] as $proofkit_email_id ) { add_filter( 'woocommerce_email_enabled_' . $proofkit_email_id, [ __CLASS__, 'suppress_sync_email' ], 10, 2 ); } add_filter( 'manage_edit-shop_order_columns', [ __CLASS__, 'add_column' ] ); add_action( 'manage_shop_order_posts_custom_column', [ __CLASS__, 'render_column' ], 10, 2 ); add_filter( 'woocommerce_shop_order_list_table_columns', [ __CLASS__, 'add_column' ] ); add_action( 'woocommerce_shop_order_list_table_custom_column', [ __CLASS__, 'render_column' ], 10, 2 ); add_action( 'restrict_manage_posts', [ __CLASS__, 'render_filter' ] ); add_action( 'woocommerce_order_list_table_restrict_manage_orders', [ __CLASS__, 'render_filter' ] ); add_filter( 'woocommerce_order_list_table_prepare_items_query_args', [ __CLASS__, 'filter_query_args' ] ); add_action( 'pre_get_posts', [ __CLASS__, 'filter_legacy_query' ] ); add_action( 'add_meta_boxes', [ __CLASS__, 'add_meta_boxes' ] ); add_action( 'admin_enqueue_scripts', [ __CLASS__, 'enqueue_admin_styles' ] ); // The order screen wraps its metaboxes in a
. A nested is // invalid HTML: browsers drop the inner tag, so its fields join the // order form and its submit button posts to the wrong action (and the // inner
closes the order form early). Render the real action // forms in the footer, outside that form, and bind controls to them // with the form="..." attribute. add_action( 'admin_footer', [ __CLASS__, 'render_action_forms' ] ); add_action( 'admin_post_proofkit_upload_proof', [ __CLASS__, 'handle_upload_proof' ] ); add_action( 'admin_post_proofkit_send_proof', [ __CLASS__, 'handle_send_proof' ] ); add_action( 'admin_post_proofkit_resend_proof', [ __CLASS__, 'handle_resend_proof' ] ); add_action( 'admin_post_proofkit_download', [ __CLASS__, 'handle_download' ] ); } /** * Add the two order statuses. * * @param array $statuses Existing statuses. * @return array */ public static function register_statuses( array $statuses ): array { $statuses[ self::STATUS_ARTWORK ] = _x( 'Artwork review', 'order status', 'proofkit' ); $statuses[ self::STATUS_PROOF ] = _x( 'Awaiting proof approval', 'order status', 'proofkit' ); return $statuses; } /** * Register the statuses for the legacy order list table. */ public static function register_post_statuses(): void { $statuses = [ self::STATUS_ARTWORK => [ 'label' => _x( 'Artwork review', 'order status', 'proofkit' ), 'public' => false, 'internal' => true, 'exclude_from_search' => true, 'show_in_admin_all_list' => true, 'show_in_admin_status_list' => true, /* translators: %s: number of orders in artwork review. */ 'label_count' => _n_noop( 'Artwork review (%s)', 'Artwork review (%s)', 'proofkit' ), ], self::STATUS_PROOF => [ 'label' => _x( 'Awaiting proof approval', 'order status', 'proofkit' ), 'public' => false, 'internal' => true, 'exclude_from_search' => true, 'show_in_admin_all_list' => true, 'show_in_admin_status_list' => true, /* translators: %s: number of orders awaiting proof approval. */ 'label_count' => _n_noop( 'Awaiting proof approval (%s)', 'Awaiting proof approval (%s)', 'proofkit' ), ], ]; foreach ( $statuses as $status => $args ) { register_post_status( $status, $args ); } } /** * Disable a WooCommerce customer email during a ProofKit status sync. * * @param bool $enabled Whether the email is enabled. * @param mixed $email Email object (WC_Email) or null. * @return bool */ public static function suppress_sync_email( $enabled, $email = null ): bool { return self::$syncing ? false : (bool) $enabled; } /** * Move the order to match a proof status, when it changed. */ public static function sync( int $order_id, string $proof_status ): void { $order = wc_get_order( $order_id ); if ( ! $order ) { return; } $target = Proof::order_status_for( $proof_status ); if ( ! is_string( $target ) || '' === $target ) { return; } if ( str_starts_with( $target, 'wc-' ) ) { $target = substr( $target, 3 ); } if ( '' === $target ) { return; } $closed = in_array( $order->get_status(), [ 'cancelled', 'refunded', 'failed' ], true ); if ( $closed ) { // A closed order must never be revived by a stale or late approval. // Only an approval needs an audit note; other proof updates are ignored. if ( 'approved' === $proof_status ) { $order->add_order_note( __( 'Proof approved after the order was closed; status left unchanged', 'proofkit' ), 0, false ); } } elseif ( $order->get_status() !== $target ) { $note = sprintf( /* translators: 1: proof status label, 2: order status label. */ __( 'ProofKit: proof status is “%1$s”; order moved to “%2$s”.', 'proofkit' ), Proof::status_label( $proof_status ), wc_get_order_status_name( $target ) ); // The third argument is WooCommerce's $manual flag. It does not // suppress email by itself; self::$syncing plus the filters above do. self::$syncing = true; try { $order->update_status( $target, '', true ); } finally { self::$syncing = false; } $order->add_order_note( $note, 0, false ); } $order->update_meta_data( '_proofkit_last_proof_status', $proof_status ); $order->save(); } /** * Order IDs waiting for artwork or proof approval. * * @return int[] */ public static function orders_awaiting( int $limit = 20 ): array { $ids = wc_get_orders( [ 'limit' => $limit, 'status' => [ self::STATUS_ARTWORK, self::STATUS_PROOF ], 'orderby' => 'date', 'order' => 'DESC', 'return' => 'ids', ] ); return array_map( 'intval', is_array( $ids ) ? $ids : [] ); } /** * Add the proof column to order list tables. * * @param array $columns Existing columns. * @return array */ public static function add_column( array $columns ): array { if ( isset( $columns['proofkit_status'] ) ) { return $columns; } $ordered = []; $inserted = false; foreach ( $columns as $key => $label ) { $ordered[ $key ] = $label; if ( 'order_status' === $key ) { $ordered['proofkit_status'] = __( 'Proof', 'proofkit' ); $inserted = true; } } if ( ! $inserted ) { $ordered['proofkit_status'] = __( 'Proof', 'proofkit' ); } return $ordered; } /** * Render the proof column for legacy and HPOS order lists. * * @param string $column Column key. * @param int|string|WP_Post|\WC_Order $order_or_post Order reference. */ public static function render_column( string $column, $order_or_post ): void { if ( 'proofkit_status' !== $column ) { return; } if ( $order_or_post instanceof \WC_Order ) { $order = $order_or_post; } elseif ( $order_or_post instanceof WP_Post ) { $order = wc_get_order( $order_or_post->ID ); } else { $order = wc_get_order( (int) $order_or_post ); } if ( ! $order ) { echo '—'; return; } $status = self::worst_proof_status_for_order( (int) $order->get_id() ); echo '' !== $status ? esc_html( Proof::status_label( $status ) ) : '—'; } /** * Render the order status filter dropdown. * * @param string $post_type Post or order type supplied by the hook. */ public static function render_filter( $post_type = '' ): void { if ( ! self::is_order_list_screen( is_string( $post_type ) ? $post_type : '' ) ) { return; } $current = self::selected_filter_status(); ?> is_main_query() ) { return; } if ( 'shop_order' !== $query->get( 'post_type' ) ) { return; } $status = self::selected_filter_status(); if ( '' !== $status ) { $query->set( 'post_status', $status ); } } /** * Load the meta box styles on order screens only. */ public static function enqueue_admin_styles(): void { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen ) { return; } if ( ! in_array( $screen->id, [ 'shop_order', 'woocommerce_page_wc-orders' ], true ) ) { return; } wp_enqueue_style( 'proofkit-admin-order', PROOFKIT_URL . 'assets/css/admin-order.css', [], PROOFKIT_VERSION ); } /** * Register the proof meta box for legacy and HPOS order screens. */ public static function add_meta_boxes(): void { $screens = [ 'shop_order' ]; if ( function_exists( 'wc_get_page_screen_id' ) ) { $screens[] = wc_get_page_screen_id( 'shop-order' ); } foreach ( array_unique( $screens ) as $screen ) { add_meta_box( 'proofkit_order_proofs', __( 'Artwork proofs', 'proofkit' ), [ __CLASS__, 'metabox' ], $screen, 'normal', 'high' ); } } /** * Render the order metabox. * * @param WP_Post|\WC_Order $order Order screen object. */ public static function metabox( WP_Post|\WC_Order $order ): void { if ( $order instanceof WP_Post ) { $order = wc_get_order( $order->ID ); } if ( ! $order instanceof \WC_Order ) { echo '

' . esc_html__( 'Order not found.', 'proofkit' ) . '

'; return; } if ( ! current_user_can( 'manage_woocommerce' ) ) { echo '

' . esc_html__( 'You do not have permission to view artwork proofs.', 'proofkit' ) . '

'; return; } $order_id = (int) $order->get_id(); $items = $order->get_items(); $all_proofs = Proof::get_for_order( $order_id ); $proofs_by_item = []; foreach ( $all_proofs as $proof ) { if ( is_object( $proof ) ) { $proofs_by_item[ (int) $proof->order_item_id ][] = $proof; } } ?>

$item ) : ?> version <=> (int) $b->version; } ); $latest = Proof::latest_for_item( $order_id, $item_id ); ?>

get_name() ) ); ?>

id === (int) $proof->id; $replaced_by = 'superseded' === (string) $proof->status ? self::superseded_by( (int) $proof->id ) : 0; $status = Proof::status_label( (string) $proof->status ); if ( $replaced_by > 0 ) { $status .= ' ' . sprintf( /* translators: %d: replacement proof version. */ __( '(replaced by v%d)', 'proofkit' ), $replaced_by ); } $sha = (string) $proof->sha256; $sha_short = strlen( $sha ) > 12 ? substr( $sha, 0, 12 ) . '…' : $sha; ?>
version ) ); ?> created_at ) ); ?> file_name ); ?> size_bytes ) ); ?> status ) : ?> $order_id, 'proof_id' => (int) $proof->id, ], false, 'proofkit_download_proof' ); ?> —
id ); ?>

version ) ); ?>

status ) : ?> $order_id, 'order_item_id' => (int) $item_id, 'proof_id' => (int) $latest->id, ] ); ?> status ) : ?> $order_id, 'order_item_id' => (int) $item_id, 'proof_id' => (int) $latest->id, ] ); ?>

$order_id ], true ); ?>



$hidden Hidden fields to submit. * @param bool $multipart Whether the form accepts files. * @param string $nonce_action Nonce action; defaults to $action. * @return string Form id for the form="..." attribute. */ private static function action_form( string $action, int $order_id, array $hidden, bool $multipart = false, string $nonce_action = '' ): string { $nonce_action = '' !== $nonce_action ? $nonce_action : $action; $key = $action . '|' . $nonce_action . '|' . $order_id . '|' . (string) wp_json_encode( $hidden ); $id = 'proofkit-form-' . sanitize_key( $action ) . '-' . $order_id . '-' . substr( md5( $key ), 0, 8 ); if ( ! isset( self::$action_forms[ $id ] ) ) { self::$action_forms[ $id ] = [ 'action' => $action, 'nonce' => $nonce_action, 'hidden' => $hidden, 'multipart' => $multipart, ]; } return $id; } /** * Print the queued action forms outside the order screen's own form. * * Controls in the metabox point at these forms with the form attribute, so * no invalid nested form is emitted and WooCommerce's order form stays intact. */ public static function render_action_forms(): void { if ( [] === self::$action_forms ) { return; } foreach ( self::$action_forms as $id => $definition ) { printf( '
', esc_attr( $id ), esc_url( admin_url( 'admin-post.php' ) ), $definition['multipart'] ? ' enctype="multipart/form-data"' : '' ); printf( '', esc_attr( $definition['action'] ) ); foreach ( $definition['hidden'] as $name => $value ) { printf( '', esc_attr( (string) $name ), esc_attr( (string) $value ) ); } wp_nonce_field( $definition['nonce'], '_wpnonce', false ); echo '
'; } self::$action_forms = []; } /** * Render success and failure messages after an order action. * * The query flags are only honoured when the redirect nonce is valid, so * a crafted URL cannot show a false success or consume a pending warning. */ private static function render_admin_notices( int $order_id ): void { $nonce = isset( $_GET['proofkit_notice_nonce'] ) && is_string( $_GET['proofkit_notice_nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['proofkit_notice_nonce'] ) ) : ''; if ( '' === $nonce || ! wp_verify_nonce( $nonce, 'proofkit_admin_notice_' . $order_id ) ) { return; } $notice = isset( $_GET['proofkit_notice'] ) && is_string( $_GET['proofkit_notice'] ) ? sanitize_key( wp_unslash( $_GET['proofkit_notice'] ) ) : ''; $notices = [ 'proof_uploaded' => __( 'Proof uploaded as a new draft.', 'proofkit' ), 'proof_sent' => __( 'Proof sent to the customer.', 'proofkit' ), 'proof_resent' => __( 'Proof email resent to the customer.', 'proofkit' ), ]; if ( isset( $notices[ $notice ] ) ) { echo '

' . esc_html( $notices[ $notice ] ) . '

'; } $warning = isset( $_GET['proofkit_warning'] ) && is_string( $_GET['proofkit_warning'] ) ? sanitize_key( wp_unslash( $_GET['proofkit_warning'] ) ) : ''; if ( '' !== $warning ) { $warning_key = 'proofkit_admin_warning_' . get_current_user_id(); $warning_message = get_transient( $warning_key ); delete_transient( $warning_key ); if ( is_string( $warning_message ) && '' !== $warning_message ) { echo '

' . esc_html( $warning_message ) . '

'; } } $error = isset( $_GET['proofkit_error'] ) && is_string( $_GET['proofkit_error'] ) ? sanitize_key( wp_unslash( $_GET['proofkit_error'] ) ) : ''; if ( '' === $error ) { return; } $key = 'proofkit_admin_notice_' . get_current_user_id(); $message = get_transient( $key ); delete_transient( $key ); // The transient is single-use; a reload must not show a stale error. if ( ! is_string( $message ) || '' === $message ) { return; } echo '

' . esc_html( $message ) . '

'; } /** * Render the last ten order-level proof events. * * @param array $proofs Proofs for the order. */ private static function render_event_log( array $proofs ): void { $recent = []; foreach ( $proofs as $proof ) { if ( ! is_object( $proof ) ) { continue; } $sha = (string) ( $proof->sha256 ?? '' ); $sha = strlen( $sha ) > 12 ? substr( $sha, 0, 12 ) . '…' : $sha; foreach ( Proof::events( (int) $proof->id ) as $event ) { if ( is_object( $event ) || is_array( $event ) ) { $recent[] = [ 'event' => (object) $event, 'version' => (int) $proof->version, 'sha256' => $sha, ]; } } } if ( ! $recent ) { return; } usort( $recent, static function ( $a, $b ) { return (int) ( $b['event']->id ?? 0 ) <=> (int) ( $a['event']->id ?? 0 ); } ); $recent = array_slice( $recent, 0, 10 ); echo '
' . esc_html__( 'Recent proof activity (last 10)', 'proofkit' ) . '
'; } /** * Display name for the proof uploader. */ private static function uploader_name( object $proof ): string { $user_id = (int) $proof->created_by; if ( $user_id > 0 ) { $user = get_userdata( $user_id ); if ( $user ) { return (string) $user->display_name; } } return __( 'Customer', 'proofkit' ); } /** * Latest change-request note recorded for a proof. */ private static function changes_note( int $proof_id ): string { $note = ''; foreach ( Proof::events( $proof_id ) as $event ) { if ( ! is_object( $event ) && ! is_array( $event ) ) { continue; } $event = (object) $event; if ( 'changes_requested' !== ( $event->event ?? '' ) ) { continue; } $meta = $event->meta ?? ''; if ( is_string( $meta ) && '' !== $meta ) { $decoded = json_decode( $meta, true ); if ( is_array( $decoded ) && isset( $decoded['note'] ) ) { $note = (string) $decoded['note']; } } elseif ( is_array( $meta ) && isset( $meta['note'] ) ) { $note = (string) $meta['note']; } } return trim( $note ); } /** * Version that replaced a superseded proof, from its newest superseded event. */ private static function superseded_by( int $proof_id ): int { $version = 0; foreach ( Proof::events( $proof_id ) as $event ) { if ( ! is_object( $event ) && ! is_array( $event ) ) { continue; } $event = (object) $event; if ( 'superseded' !== ( $event->event ?? '' ) ) { continue; } $meta = $event->meta ?? ''; if ( is_string( $meta ) && '' !== $meta ) { $decoded = json_decode( $meta, true ); if ( is_array( $decoded ) && isset( $decoded['replaced_by_version'] ) ) { $version = absint( $decoded['replaced_by_version'] ); } } elseif ( is_array( $meta ) && isset( $meta['replaced_by_version'] ) ) { $version = absint( $meta['replaced_by_version'] ); } } return $version; } /** * Human label for a proof event. */ private static function event_label( string $event ): string { switch ( $event ) { case 'uploaded': return __( 'Uploaded', 'proofkit' ); case 'proof_sent': return __( 'Proof sent', 'proofkit' ); case 'viewed': return __( 'Viewed by customer', 'proofkit' ); case 'approved': return __( 'Approved', 'proofkit' ); case 'changes_requested': return __( 'Changes requested', 'proofkit' ); case 'superseded': return Proof::status_label( 'superseded' ); case 'downloaded': return __( 'Downloaded', 'proofkit' ); case 'download_blocked': return __( 'Download blocked', 'proofkit' ); case 'purged': return __( 'Purged', 'proofkit' ); default: return $event; } } /** * Human label for an event actor. */ private static function actor_label( string $actor ): string { switch ( $actor ) { case 'admin': return __( 'Staff', 'proofkit' ); case 'customer': return __( 'Customer', 'proofkit' ); case 'system': return __( 'System', 'proofkit' ); default: return $actor; } } /** * Worst current proof status across an order's line items. * * All proofs are loaded in one query; only the latest version per item * contributes, so a superseded older version cannot mask a live state. */ private static function worst_proof_status_for_order( int $order_id ): string { $proofs = Proof::get_for_order( $order_id ); if ( ! $proofs ) { return ''; } $latest_by_item = []; foreach ( $proofs as $proof ) { if ( ! is_object( $proof ) ) { continue; } $item_id = (int) ( $proof->order_item_id ?? 0 ); if ( ! isset( $latest_by_item[ $item_id ] ) || (int) $proof->version > (int) $latest_by_item[ $item_id ]->version ) { $latest_by_item[ $item_id ] = $proof; } } $worst = ''; $worst_rank = PHP_INT_MAX; foreach ( $latest_by_item as $proof ) { $status = (string) ( $proof->status ?? '' ); $rank = self::PROOF_STATUS_PRECEDENCE[ $status ] ?? PHP_INT_MAX; if ( '' === $worst || $rank < $worst_rank ) { $worst = $status; $worst_rank = $rank; } } return $worst; } /** * Whether the current request is an order list screen. */ private static function is_order_list_screen( string $post_type ): bool { if ( 'shop_order' === $post_type ) { return true; } $screen = get_current_screen(); if ( ! $screen ) { return false; } $ids = [ 'edit-shop_order', 'woocommerce_page_wc-orders' ]; if ( function_exists( 'wc_get_page_screen_id' ) ) { $ids[] = wc_get_page_screen_id( 'shop-order' ); } return in_array( $screen->id, array_unique( $ids ), true ); } /** * Validated proof-status filter from the request. */ private static function selected_filter_status(): string { // Read-only list-table filter; it is echoed back into the filter // control and never written to the database. // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( ! isset( $_GET['proofkit_proof_status'] ) || ! is_string( $_GET['proofkit_proof_status'] ) ) { return ''; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended $value = sanitize_key( wp_unslash( $_GET['proofkit_proof_status'] ) ); return in_array( $value, [ self::STATUS_ARTWORK, self::STATUS_PROOF ], true ) ? $value : ''; } /** * Handle an admin proof upload. */ public static function handle_upload_proof(): void { self::authorize(); check_admin_referer( 'proofkit_upload_proof' ); $order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : 0; $item_id = isset( $_POST['order_item_id'] ) ? absint( $_POST['order_item_id'] ) : 0; $order = wc_get_order( $order_id ); if ( ! $order ) { self::fail( $order_id, 'missing_order', __( 'That order could not be found.', 'proofkit' ) ); } $item = $item_id > 0 ? $order->get_item( $item_id ) : null; if ( ! $item instanceof \WC_Order_Item_Product ) { self::fail( $order_id, 'missing_item', __( 'Choose a line item that belongs to this order.', 'proofkit' ) ); } $product_id = (int) $item->get_product_id(); if ( $product_id <= 0 ) { self::fail( $order_id, 'missing_product', __( 'That line item has no product to validate artwork against.', 'proofkit' ) ); } if ( empty( $_FILES['proofkit_file'] ) || ! is_array( $_FILES['proofkit_file'] ) ) { self::fail( $order_id, 'missing_file', __( 'Choose an artwork file to upload.', 'proofkit' ) ); } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- validated by Upload::validate(). $file = $_FILES['proofkit_file']; $valid = Upload::validate( $file, $product_id ); if ( is_wp_error( $valid ) ) { self::fail( $order_id, $valid->get_error_code(), $valid->get_error_message() ); } $descriptor = Upload::store( $file, $product_id ); if ( is_wp_error( $descriptor ) ) { self::fail( $order_id, $descriptor->get_error_code(), $descriptor->get_error_message() ); } if ( ! is_array( $descriptor ) ) { self::fail( $order_id, 'store_failed', __( 'The file could not be stored.', 'proofkit' ) ); } $proof_id = Proof::create( [ 'order_id' => $order_id, 'order_item_id' => $item_id, 'file' => $descriptor, 'created_by' => get_current_user_id(), ] ); if ( is_wp_error( $proof_id ) ) { self::fail( $order_id, $proof_id->get_error_code(), $proof_id->get_error_message() ); } self::redirect( $order_id, [ 'proofkit_notice' => 'proof_uploaded' ] ); } /** * Handle sending a proof to the customer. */ public static function handle_send_proof(): void { self::authorize(); check_admin_referer( 'proofkit_send_proof' ); $order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : 0; $proof_id = isset( $_POST['proof_id'] ) ? absint( $_POST['proof_id'] ) : 0; $proof = Proof::get( $proof_id ); $order = wc_get_order( $order_id ); if ( ! $order || ! $proof || (int) $proof->order_id !== $order_id ) { self::fail( $order_id, 'missing_proof', __( 'That proof does not belong to this order.', 'proofkit' ) ); } if ( isset( $_POST['order_item_id'] ) && (int) $proof->order_item_id !== absint( $_POST['order_item_id'] ) ) { self::fail( $order_id, 'missing_proof', __( 'That proof does not belong to the requested line item.', 'proofkit' ) ); } if ( ! $order->get_item( (int) $proof->order_item_id ) ) { self::fail( $order_id, 'missing_proof', __( 'The proof’s line item no longer belongs to this order.', 'proofkit' ) ); } $result = Proof::send( $proof_id ); if ( is_wp_error( $result ) ) { if ( 'proofkit_email_failed' === $result->get_error_code() ) { self::warn( $order_id, $result->get_error_code(), __( 'The proof link is live, but the email could not be sent. Fix email delivery and use Resend.', 'proofkit' ) ); } else { self::fail( $order_id, $result->get_error_code(), $result->get_error_message() ); } } self::redirect( $order_id, [ 'proofkit_notice' => 'proof_sent' ] ); } /** * Reissue the respond link and resend the proof email. */ public static function handle_resend_proof(): void { self::authorize(); check_admin_referer( 'proofkit_resend_proof' ); $order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : 0; $proof_id = isset( $_POST['proof_id'] ) ? absint( $_POST['proof_id'] ) : 0; $proof = Proof::get( $proof_id ); $order = wc_get_order( $order_id ); if ( ! $order || ! $proof || (int) $proof->order_id !== $order_id ) { self::fail( $order_id, 'missing_proof', __( 'That proof does not belong to this order.', 'proofkit' ) ); } if ( isset( $_POST['order_item_id'] ) && (int) $proof->order_item_id !== absint( $_POST['order_item_id'] ) ) { self::fail( $order_id, 'missing_proof', __( 'That proof does not belong to the requested line item.', 'proofkit' ) ); } if ( ! $order->get_item( (int) $proof->order_item_id ) ) { self::fail( $order_id, 'missing_proof', __( 'The proof’s line item no longer belongs to this order.', 'proofkit' ) ); } $result = Proof::resend( $proof_id ); if ( is_wp_error( $result ) ) { if ( 'proofkit_email_failed' === $result->get_error_code() ) { self::warn( $order_id, $result->get_error_code(), __( 'The proof link is live, but the email could not be sent. Fix email delivery and use Resend.', 'proofkit' ) ); } else { self::fail( $order_id, $result->get_error_code(), $result->get_error_message() ); } } self::redirect( $order_id, [ 'proofkit_notice' => 'proof_resent' ] ); } /** * Stream a proof file to an authorised admin. */ public static function handle_download(): void { self::authorize(); check_admin_referer( 'proofkit_download_proof' ); $order_id = isset( $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : 0; $proof_id = isset( $_POST['proof_id'] ) ? absint( $_POST['proof_id'] ) : 0; $proof = Proof::get( $proof_id ); if ( ! $proof || (int) $proof->order_id !== $order_id ) { self::fail( $order_id, 'missing_proof', __( 'That proof does not belong to this order.', 'proofkit' ) ); } if ( 'superseded' === (string) $proof->status ) { self::fail( $order_id, 'superseded_proof', __( 'Superseded proofs cannot be downloaded for production.', 'proofkit' ) ); } $relative = (string) $proof->file_path; if ( '' === $relative || ! Upload::exists( $relative ) ) { Proof::record_event( $proof_id, $order_id, 'download_blocked', 'admin', [ 'reason' => 'file_missing' ] ); self::log_error( 'Admin download blocked: stored file is missing.', [ 'order_id' => $order_id, 'proof_id' => $proof_id ] ); self::fail( $order_id, 'missing_file', __( 'The stored artwork file is missing. Production download is blocked.', 'proofkit' ) ); } if ( ! Upload::verify( $relative, (string) $proof->sha256 ) ) { Proof::record_event( $proof_id, $order_id, 'download_blocked', 'admin', [ 'reason' => 'hash_mismatch' ] ); self::log_error( 'Admin download blocked: checksum mismatch.', [ 'order_id' => $order_id, 'proof_id' => $proof_id ] ); self::fail( $order_id, 'hash_mismatch', __( 'The stored file does not match its recorded checksum. Production download is blocked until the mismatch is resolved.', 'proofkit' ) ); } // Log before streaming so the audit trail exists even if the client disconnects. Proof::record_event( $proof_id, $order_id, 'downloaded', 'admin' ); Upload::send_file( $relative, (string) $proof->file_name, (string) $proof->mime, 'attachment' ); } /** * Require the shop-management capability. */ private static function authorize(): void { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'You do not have permission to manage artwork proofs.', 'proofkit' ), '', [ 'response' => 403 ] ); } } /** * Store a warning message and redirect back to the order. */ private static function warn( int $order_id, string $code, string $message ): void { if ( $order_id > 0 ) { set_transient( 'proofkit_admin_warning_' . get_current_user_id(), $message, 120 ); } self::redirect( $order_id, [ 'proofkit_warning' => sanitize_key( $code ) ] ); } /** * Log an operational failure at error level, regardless of WP_DEBUG. * * @param array $context Structured context. */ private static function log_error( string $message, array $context = [] ): void { if ( function_exists( 'wc_get_logger' ) ) { wc_get_logger()->error( 'ProofKit: ' . $message, array_merge( [ 'source' => 'proofkit' ], $context ) ); return; } Util::log( $message, $context ); } /** * Store a failure message and redirect back to the order. */ private static function fail( int $order_id, string $code, string $message ): void { if ( $order_id > 0 ) { set_transient( 'proofkit_admin_notice_' . get_current_user_id(), $message, 120 ); } self::redirect( $order_id, [ 'proofkit_error' => sanitize_key( $code ) ] ); } /** * Redirect back to the order edit screen with a query flag. * * @param array $args Query args to add. */ private static function redirect( int $order_id, array $args ): void { $args['proofkit_notice_nonce'] = wp_create_nonce( 'proofkit_admin_notice_' . $order_id ); wp_safe_redirect( add_query_arg( $args, self::order_edit_url( $order_id ) ) ); exit; } /** * Edit URL for an order, with a legacy fallback. */ private static function order_edit_url( int $order_id ): string { $order = $order_id > 0 ? wc_get_order( $order_id ) : false; return $order ? $order->get_edit_order_url() : admin_url( 'edit.php?post_type=shop_order' ); } }