files(); if (empty($files['file'])) { return 'No file uploaded'; } try { // Validate the REAL bytes on disk (magic-byte MIME sniffing, // double-extension and dangerous-extension checks). The // client-supplied $file['type'] / $file['size'] are attacker // controlled and must never be trusted. $this->validatedFile = (new AttachmentValidatorService())->validate( $files['file'], null, AttachmentValidatorService::attachmentTypes() ); } catch (InvalidArgumentException $e) { return $e->getMessage(); } return null; } /** * The verified file array to hand to wp_handle_upload(), available after a * successful validateFile(). Falls back to the raw request file if, for any * reason, validation was not run. */ public function validatedFile(): ?array { if ($this->validatedFile !== null) { return $this->validatedFile; } $files = $this->files(); return $files['file'] ?? null; } }