catalogue = $catalogue; $this->plugin_version = $plugin_version; } /** * @param array $run Authorized completed-run summary. * @param array $report Authorized report projection. * @param array{name:string,email:string,message:string} $contact Validated contact fields. * @param list $selected_refs Explicit local Finding references. * @return array{payload:array,json:string,digest:string,bytes:int,data_classes:list} */ public function prepare( array $run, array $report, array $contact, string $scope, array $selected_refs, string $idempotency_key ): array { $this->validate_context( $run, $report, $contact, $scope, $selected_refs, $idempotency_key ); $report_payload = $this->report_payload( $run, $report, $scope, $selected_refs ); $payload = $this->canonicalize( array( 'schema_version' => self::SCHEMA_VERSION, 'idempotency_key' => strtolower( $idempotency_key ), 'source' => array( 'name' => '320px Site Audit', 'version' => $this->plugin_version, ), 'language' => $run['language'], 'purpose' => 'request_review_and_project_reply', 'contact' => $contact, 'payload_scope' => $scope, 'report' => $report_payload, 'consent' => array( 'confirmed' => true, 'version' => self::CONSENT_VERSION, ), ) ); $json = wp_json_encode( $payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, 64 ); if ( ! is_string( $json ) ) { throw new InvalidArgumentException( 'review_payload_invalid' ); } $bytes = strlen( $json ); if ( $bytes > self::MAX_PAYLOAD_BYTES ) { throw new InvalidArgumentException( 'review_payload_too_large' ); } return array( 'payload' => $payload, 'json' => $json, 'digest' => hash( 'sha256', $json ), 'bytes' => $bytes, 'data_classes' => $this->data_classes( $scope ), ); } /** * @return list */ private function data_classes( string $scope ): array { $classes = array( 'request_transport_metadata', 'contact_name', 'contact_reply_email', 'contact_message', 'consent_record', ); if ( 'summary' === $scope ) { $classes[] = 'report_summary'; } elseif ( 'selected' === $scope ) { $classes[] = 'selected_redacted_findings'; } elseif ( 'full' === $scope ) { $classes[] = 'full_redacted_report'; } return $classes; } /** * @param array $run Run summary. * @param array $report Report projection. * @param list $selected_refs Selected local refs. * @return array|null */ private function report_payload( array $run, array $report, string $scope, array $selected_refs ): ?array { if ( 'contact' === $scope ) { return null; } $base = array( 'profile' => $run['profile'], 'language' => $run['language'], 'completed_at' => $run['completed_at'], 'coverage' => $run['coverage'], 'readiness' => $report['readiness'], 'status_counts' => $report['status_counts'], 'comparison' => $report['comparison'], ); if ( 'summary' === $scope ) { return $base; } $indexes = array(); if ( 'selected' === $scope ) { $wanted = array_fill_keys( $selected_refs, true ); foreach ( $report['finding_refs'] as $index => $reference ) { if ( isset( $wanted[ $reference ] ) ) { $indexes[] = $index; unset( $wanted[ $reference ] ); } } if ( array() !== $wanted ) { throw new InvalidArgumentException( 'review_finding_selection_invalid' ); } } else { $indexes = array_keys( $report['findings'] ); } $findings = array(); foreach ( $indexes as $index ) { $findings[] = $this->finding( $report['findings'][ $index ], $report['finding_meta'][ $index ], $run['language'], 'full' === $scope ); } $base['findings'] = $findings; if ( 'full' === $scope ) { $base['action_plan'] = array_map( static function ( array $action ): array { return array( 'check_id' => (string) $action['check_id'], 'priority_band' => (string) $action['priority_band'], 'effort_band' => (string) $action['effort_band'], 'comparison_state' => (string) $action['comparison_state'], ); }, $report['actions'] ); } return $base; } /** * @param array $finding Finding projection. * @param array $meta Finding report metadata. * @return array */ private function finding( array $finding, array $meta, string $language, bool $include_evidence ): array { $catalogue = $this->catalogue->for_language( $language ); $copy = $catalogue['checks'][ $finding['check_id'] ] ?? null; if ( ! is_array( $copy ) ) { throw new InvalidArgumentException( 'review_finding_copy_missing' ); } $next_step = in_array( $finding['status'], array( 'pass', 'info' ), true ) ? ( 'pass' === $finding['status'] ? $catalogue['ui']['findingNoAction'] : $catalogue['ui']['findingInfoNoAction'] ) : $copy['next_step']; $out = array( 'check_id' => $finding['check_id'], 'title' => $copy['title'], 'scope' => $copy['scope'], 'category' => $finding['category'], 'category_label' => $catalogue['categories'][ $finding['category'] ] ?? $finding['category'], 'status' => $finding['status'], 'status_label' => $catalogue['statuses'][ $finding['status'] ]['label'] ?? $finding['status'], 'severity' => $finding['severity'], 'severity_label' => $catalogue['severities'][ $finding['severity'] ] ?? $finding['severity'], 'confidence' => $finding['confidence'], 'confidence_label' => $catalogue['confidence'][ $finding['confidence'] ] ?? $finding['confidence'], 'priority_band' => $meta['priority_band'], 'effort_band' => $finding['effort_band'], 'comparison_state' => $finding['comparison_state'], 'acknowledgement' => $finding['acknowledgement'], 'coverage' => $finding['coverage'], 'method' => $finding['method'], 'next_step' => $next_step, 'limitation' => $copy['limitation'], ); if ( $include_evidence ) { $out['evidence'] = $finding['evidence']; $out['source_refs'] = $finding['source_refs']; $out['affected_objects'] = $finding['affected_objects']; } return $out; } /** * @param array $run Run summary. * @param array $report Report projection. * @param array{name:string,email:string,message:string} $contact Contact fields. * @param list $selected_refs Selected local refs. */ private function validate_context( array $run, array $report, array $contact, string $scope, array $selected_refs, string $idempotency_key ): void { if ( ! in_array( $scope, self::SCOPES, true ) || 1 !== preg_match( '/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/Di', $idempotency_key ) ) { throw new InvalidArgumentException( 'review_context_invalid' ); } if ( ! isset( $run['status'], $run['profile'], $run['language'], $run['completed_at'], $run['coverage'] ) || 'completed' !== $run['status'] || ! in_array( $run['language'], array( 'en', 'ru' ), true ) || ! is_string( $run['completed_at'] ) ) { throw new InvalidArgumentException( 'review_run_invalid' ); } if ( ! isset( $report['findings'], $report['finding_refs'], $report['finding_meta'], $report['actions'], $report['comparison'], $report['readiness'], $report['status_counts'] ) || ! is_array( $report['findings'] ) || ! is_array( $report['finding_refs'] ) || ! is_array( $report['finding_meta'] ) || count( $report['findings'] ) !== count( $report['finding_refs'] ) || count( $report['findings'] ) !== count( $report['finding_meta'] ) ) { throw new InvalidArgumentException( 'review_report_invalid' ); } if ( array( 'email', 'message', 'name' ) !== $this->sorted_keys( $contact ) || '' === trim( $contact['name'] ) || strlen( $contact['name'] ) > 100 || false === filter_var( $contact['email'], FILTER_VALIDATE_EMAIL ) || strlen( $contact['email'] ) > 254 || strlen( trim( $contact['message'] ) ) < 20 || strlen( $contact['message'] ) > 4000 || 1 === preg_match( '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', implode( '', $contact ) ) ) { throw new InvalidArgumentException( 'review_contact_invalid' ); } if ( count( $selected_refs ) > self::MAX_SELECTED || count( $selected_refs ) !== count( array_unique( $selected_refs ) ) || ( 'selected' === $scope && array() === $selected_refs ) || ( 'selected' !== $scope && array() !== $selected_refs ) ) { throw new InvalidArgumentException( 'review_finding_selection_invalid' ); } foreach ( $selected_refs as $reference ) { if ( ! is_string( $reference ) || 1 !== preg_match( '/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/Di', $reference ) ) { throw new InvalidArgumentException( 'review_finding_selection_invalid' ); } } } /** * @param array $value Value. * @return list */ private function sorted_keys( array $value ): array { $keys = array_keys( $value ); sort( $keys ); return $keys; } /** * Recursively sort object keys while preserving list order. * * @param mixed $value Value. * @return mixed */ private function canonicalize( $value ) { if ( ! is_array( $value ) ) { return $value; } if ( array_values( $value ) === $value ) { return array_map( array( $this, 'canonicalize' ), $value ); } ksort( $value, SORT_STRING ); foreach ( $value as $key => $item ) { $value[ $key ] = $this->canonicalize( $item ); } return $value; } }