binary_data = $binary_data; $this->mime_type = $mime_type; } /** * Retrieves the binary data of the blob. * * @since 0.5.0 * * @return string The binary data. */ public function get_binary_data(): string { return $this->binary_data; } /** * Retrieves the MIME type of the blob. * * @since 0.5.0 * * @return string The MIME type. */ public function get_mime_type(): string { return $this->mime_type; } /** * Creates a new blob instance from a file. * * @since 0.5.0 * * @param string $file The file path or URL. * @param string $mime_type Optional. MIME type, to override the automatic detection. Default empty string. * @return Blob The blob instance. * * @throws InvalidArgumentException Thrown if the file could not be read or if the MIME type cannot be determined. */ public static function from_file( string $file, string $mime_type = '' ): self { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $blob = file_get_contents( $file ); if ( ! $blob ) { throw new InvalidArgumentException( sprintf( 'Could not read file %s.', htmlspecialchars( $file ) // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped ) ); } if ( ! $mime_type ) { $file_type = wp_check_filetype( $file ); if ( ! $file_type['type'] ) { throw new InvalidArgumentException( sprintf( 'Could not determine MIME type of file %s.', htmlspecialchars( $file ) // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped ) ); } $mime_type = $file_type['type']; } return new self( $blob, $mime_type ); } }