self::DATABASE, 'license_key' => wc_clean( $license_key ), 'suffix' => 'tar.gz', ), 'https://download.maxmind.com/app/geoip_download' ); // Needed for the download_url call right below. require_once ABSPATH . 'wp-admin/includes/file.php'; $tmp_archive_path = download_url( $download_uri ); if ( is_wp_error( $tmp_archive_path ) ) { // Transform the error into something more informative. $error_data = $tmp_archive_path->get_error_data(); if ( isset( $error_data['code'] ) ) { switch ( $error_data['code'] ) { case 401: return new WP_Error( 'woocommerce_maxmind_geolocation_database_license_key', __( 'The MaxMind license key is invalid.', 'woocommerce' ) ); } } return new WP_Error( 'woocommerce_maxmind_geolocation_database_download', __( 'Failed to download the MaxMind database.', 'woocommerce' ) ); } // Extract the database from the archive. try { $file = new PharData( $tmp_archive_path ); $tmp_database_path = trailingslashit( dirname( $tmp_archive_path ) ) . trailingslashit( $file->current()->getFilename() ) . self::DATABASE . self::DATABASE_EXTENSION; $file->extractTo( dirname( $tmp_archive_path ), trailingslashit( $file->current()->getFilename() ) . self::DATABASE . self::DATABASE_EXTENSION, true ); } catch ( Exception $exception ) { return new WP_Error( 'woocommerce_maxmind_geolocation_database_archive', $exception->getMessage() ); } finally { // Remove the archive since we only care about a single file in it. unlink( $tmp_archive_path ); } return $tmp_database_path; } }