is_status_request() ) { // A lookup form elsewhere holds a nonce: keep it out of page caches, but it needs no noindex. if ( $this->has_lookup_block() ) { self::do_not_cache(); nocache_headers(); } return; } self::do_not_cache(); nocache_headers(); header( 'X-Robots-Tag: noindex, nofollow', true ); header( 'Referrer-Policy: no-referrer', true ); // Keep the token out of outbound Referer headers. add_filter( 'wp_robots', 'wp_robots_no_robots' ); } public function maybe_print(): void { // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( empty( $_GET['actr_print'] ) || ! $this->is_status_request() ) { return; } $receipt = $this->receipt_from_request(); if ( ! $receipt ) { return; } wp_register_style( 'actr-print', ACTR_URL . 'assets/css/print.css', array(), ACTR_VERSION ); $this->load_template( 'receipt-print.php', array( 'receipt' => $receipt, 'snapshot' => SnapshotService::decode( $receipt ), 'status' => StatusPipeline::get( $receipt['status_key'] ), 'verified' => SnapshotService::verify( $receipt ), ) ); exit; } /** * Tells page-cache plugins not to store this page. Also called while rendering, which covers * blocks placed in templates, where protect_page() cannot see them. */ public static function do_not_cache(): void { if ( ! defined( 'DONOTCACHEPAGE' ) ) { define( 'DONOTCACHEPAGE', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- standard constant read by page-cache plugins. } } public function render_shortcode(): string { return $this->render_status_or_lookup(); } /** * Status for a valid private link, otherwise the lookup form. */ public function render_status_or_lookup(): string { wp_enqueue_style( 'actr-frontend' ); self::do_not_cache(); $receipt = $this->receipt_from_request(); ob_start(); if ( $receipt ) { $this->load_template( 'status.php', array( 'receipt' => $receipt, 'status' => StatusPipeline::get( $receipt['status_key'] ), 'statuses' => StatusPipeline::all(), 'timeline' => EventRepository::public_timeline( (int) $receipt['id'] ), 'print_url' => ReceiptService::status_url( $receipt, array( 'actr_print' => 1 ) ), 'verified' => SnapshotService::verify( $receipt ), ) ); } else { $this->load_template( 'lookup-form.php', $this->lookup_vars( true, true ) ); } return (string) ob_get_clean(); } /** * The lookup form on its own (lookup block). It never shows a submission. * * @param bool $show_title Show the heading and intro line. */ public function render_lookup( bool $show_title = true ): string { wp_enqueue_style( 'actr-frontend' ); self::do_not_cache(); ob_start(); $this->load_template( 'lookup-form.php', $this->lookup_vars( $show_title, false ) ); return (string) ob_get_clean(); } /** * @param bool $show_title Heading and intro line. * @param bool $is_link_page True on the status page, where ?ref= means a private link was opened. */ private function lookup_vars( bool $show_title, bool $is_link_page ): array { // phpcs:disable WordPress.Security.NonceVerification.Recommended $error = ''; if ( isset( $_GET['actr_error'] ) ) { $error = 'limit' === $_GET['actr_error'] ? __( 'Too many attempts. Please wait a few minutes and try again.', 'action-receipts' ) : __( 'We could not find a submission with those details. Please check and try again.', 'action-receipts' ); } elseif ( $is_link_page && isset( $_GET['ref'] ) ) { $error = __( 'This link is not valid. Please look up your submission below.', 'action-receipts' ); } $ref_value = isset( $_GET['ref'] ) ? sanitize_text_field( wp_unslash( $_GET['ref'] ) ) : ''; // phpcs:enable ++self::$instances; return array( 'action_url' => admin_url( 'admin-post.php' ), 'action' => self::LOOKUP_ACTION, 'error' => $error, 'ref_value' => $ref_value, 'return_to' => (int) get_queried_object_id(), 'show_title' => $show_title, 'uid' => 1 === self::$instances ? 'actr' : 'actr' . self::$instances, ); } public function handle_lookup(): void { $page_id = isset( $_POST['actr_page'] ) ? absint( $_POST['actr_page'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing $settings = Settings::get(); $page_id = $page_id ? $page_id : (int) $settings['status_page_id']; $back = $page_id ? get_permalink( $page_id ) : home_url( '/' ); if ( ! isset( $_POST['actr_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['actr_nonce'] ) ), self::LOOKUP_ACTION ) ) { wp_safe_redirect( add_query_arg( 'actr_error', '1', $back ) ); exit; } if ( $this->rate_limited() ) { wp_safe_redirect( add_query_arg( 'actr_error', 'limit', $back ) ); exit; } $reference = isset( $_POST['actr_ref'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_POST['actr_ref'] ) ) ) : ''; $email = isset( $_POST['actr_email'] ) ? strtolower( sanitize_email( wp_unslash( $_POST['actr_email'] ) ) ) : ''; $receipt = '' !== $reference ? ReceiptRepository::find_by_reference( $reference ) : null; $match = $receipt && '' !== $email && '' !== (string) $receipt['email'] && hash_equals( strtolower( (string) $receipt['email'] ), $email ); if ( ! $match ) { wp_safe_redirect( add_query_arg( 'actr_error', '1', $back ) ); exit; } $url = ReceiptService::status_url( $receipt ); wp_safe_redirect( $url ? $url : $back ); exit; } private function receipt_from_request(): ?array { // phpcs:disable WordPress.Security.NonceVerification.Recommended $reference = isset( $_GET['ref'] ) ? strtoupper( sanitize_text_field( wp_unslash( $_GET['ref'] ) ) ) : ''; $token = isset( $_GET['k'] ) ? sanitize_text_field( wp_unslash( $_GET['k'] ) ) : ''; // phpcs:enable if ( '' === $reference || '' === $token ) { return null; } $receipt = ReceiptRepository::find_by_reference( $reference ); if ( ! $receipt || ! TokenService::verify( $receipt['reference'], $receipt['token_salt'], $token ) ) { return null; } return $receipt; } private function is_status_request(): bool { $settings = Settings::get(); $page_id = (int) $settings['status_page_id']; if ( $page_id && is_page( $page_id ) ) { return true; } $post = get_post(); return $post && is_singular() && ( has_shortcode( (string) $post->post_content, self::SHORTCODE ) || has_block( self::STATUS_BLOCK, $post ) ); } private function has_lookup_block(): bool { $post = get_post(); return $post && is_singular() && has_block( self::LOOKUP_BLOCK, $post ); } private function rate_limited(): bool { $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : 'unknown'; $key = 'actr_rl_' . md5( $ip ); $hit = (int) get_transient( $key ); if ( $hit >= self::RATE_LIMIT ) { return true; } set_transient( $key, $hit + 1, self::RATE_WINDOW_MIN * MINUTE_IN_SECONDS ); return false; } /** * Themes can override templates in /action-receipts/ inside the theme folder. */ private function load_template( string $file, array $vars ): void { $template = locate_template( 'action-receipts/' . $file ); if ( ! $template ) { $template = ACTR_DIR . 'templates/' . $file; } extract( $vars, EXTR_SKIP ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract include $template; } }