1, 'proof_id' => $proof_id, 'order_id' => $order_id, 'token' => $token, ], home_url( '/' ) ); } /** * Validate a proof link and render the standalone proof page. * * @param int $proof_id Proof id. * @param int $order_id Order id. * @param string $token Raw token. */ public static function render_proof( int $proof_id, int $order_id, string $token ): void { $proof = $proof_id > 0 ? Proof::get( $proof_id ) : null; $order = $order_id > 0 ? wc_get_order( $order_id ) : null; if ( ! $proof || ! $order instanceof \WC_Order || (int) $proof->order_id !== $order->get_id() ) { self::dead_end(); } $valid = '' !== $token && ( Token::validate( $token, $proof_id, 'respond', false ) || Token::validate( $token, $proof_id, 'view', false ) ); if ( ! $valid ) { self::dead_end(); } Proof::mark_viewed( $proof_id, hash( 'sha256', $token ) ); $file_url = add_query_arg( [ 'proof_id' => $proof_id, 'order_id' => $order_id, 'token' => $token, ], rest_url( 'proofkit/v1/file' ) ); status_header( 200 ); header( 'X-Robots-Tag: noindex, nofollow', true ); header( 'Referrer-Policy: no-referrer', true ); header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0', true ); header( 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' ), true ); self::template( 'proof/view.php', [ 'proof' => $proof, 'order' => $order, 'token' => $token, 'item_name' => self::item_name( $order, (int) $proof->order_item_id ), 'file_url' => $file_url, 'can_respond' => 'sent' === (string) $proof->status, 'notice' => '', 'is_admin' => false, ] ); exit; } /** * Handle the proof response form posted to admin-post.php. */ public static function handle_respond(): void { $proof_id = isset( $_POST['proof_id'] ) && is_scalar( $_POST['proof_id'] ) ? absint( wp_unslash( $_POST['proof_id'] ) ) : 0; $order_id = isset( $_POST['order_id'] ) && is_scalar( $_POST['order_id'] ) ? absint( wp_unslash( $_POST['order_id'] ) ) : 0; $token = isset( $_POST['token'] ) && is_scalar( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : ''; $decision = isset( $_POST['proofkit_decision'] ) && is_scalar( $_POST['proofkit_decision'] ) ? sanitize_key( wp_unslash( $_POST['proofkit_decision'] ) ) : ''; $note = isset( $_POST['note'] ) && is_scalar( $_POST['note'] ) ? wp_kses_post( wp_unslash( (string) $_POST['note'] ) ) : ''; check_admin_referer( 'proofkit_respond_' . $proof_id ); $proof = $proof_id > 0 ? Proof::get( $proof_id ) : null; if ( ! $proof || (int) $proof->order_id !== $order_id || '' === $token || ! Token::validate( $token, $proof_id, 'respond', false ) ) { self::dead_end(); } if ( ! in_array( $decision, [ 'approve', 'changes' ], true ) ) { self::dead_end(); } if ( 'approve' === $decision ) { $result = Proof::approve( $proof_id, $note ); } else { if ( '' === trim( wp_strip_all_tags( $note ) ) ) { self::dead_end( __( 'Please describe the changes you need', 'proofkit' ) ); } $result = Proof::request_changes( $proof_id, $note ); } if ( is_wp_error( $result ) ) { status_header( 409 ); header( 'X-Robots-Tag: noindex, nofollow', true ); header( 'Referrer-Policy: no-referrer', true ); header( 'Cache-Control: no-store', true ); header( 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' ), true ); self::page_shell( __( 'Proof could not be updated', 'proofkit' ), '

' . esc_html( $result->get_error_message() ) . '

' . '

' . esc_html__( 'Please contact the shop if this keeps happening.', 'proofkit' ) . '

' ); exit; } // POST/redirect/GET: the token is now revoked, so re-posting the form // would fail. The confirmation page carries no token and links home. wp_safe_redirect( add_query_arg( 'proofkit_done', 'approve' === $decision ? 'approved' : 'changes', home_url( '/' ) ) ); exit; } /** * Confirmation page after a successful approval or change request. * * Rendered from a clean GET URL with no proof token, so refreshing or * sharing the URL re-posts nothing. * * @param string $flag approved|changes. */ private static function render_done( string $flag ): void { if ( 'changes' === $flag ) { $title = __( 'Change request sent', 'proofkit' ); $message = __( 'Thanks — your change request has been sent to the shop.', 'proofkit' ); } else { $title = __( 'Proof approved', 'proofkit' ); $message = __( 'Thanks — your proof is approved. The shop will use this version for production.', 'proofkit' ); } status_header( 200 ); header( 'X-Robots-Tag: noindex, nofollow', true ); header( 'Referrer-Policy: no-referrer', true ); header( 'Cache-Control: no-store', true ); header( 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' ), true ); self::page_shell( $title, '

' . esc_html( $message ) . '

' . '

' . esc_html__( 'Return to the shop', 'proofkit' ) . '

' ); exit; } /** * 403 dead-end page with no order or proof data. * * @param string $title Optional heading. */ private static function dead_end( string $title = '' ): void { if ( '' === $title ) { $title = __( 'Link expired', 'proofkit' ); } status_header( 403 ); header( 'X-Robots-Tag: noindex, nofollow', true ); header( 'Referrer-Policy: no-referrer', true ); header( 'Cache-Control: no-store', true ); header( 'Content-Type: text/html; charset=' . get_bloginfo( 'charset' ), true ); self::page_shell( $title, '

' . esc_html__( 'This proof link is no longer valid. Please contact the shop and ask for a new link.', 'proofkit' ) . '

' ); exit; } /** * Standalone page shell used for dead ends and confirmations. * * @param string $title Page title. * @param string $body_html Pre-escaped body HTML. */ private static function page_shell( string $title, string $body_html ): void { $shop = get_bloginfo( 'name' ); $charset = get_bloginfo( 'charset' ); ?> > <?php echo esc_html( $title . ' — ' . $shop ); ?>

0 ? $order->get_item( $order_item_id ) : false; if ( $item instanceof \WC_Order_Item_Product && '' !== (string) $item->get_name() ) { return (string) $item->get_name(); } return __( 'your artwork', 'proofkit' ); } /** * Include a plugin template in an isolated scope. * * @param string $template Relative template path. * @param array $vars Template variables. */ private static function template( 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; } )(); } }