self::DB_NAME, 'license_key' => rawurlencode( sanitize_text_field( $license_key ) ), 'suffix' => 'tar.gz', ), 'https://download.maxmind.com/app/geoip_download' ); if ( ! function_exists( 'download_url' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } return download_url( $url ); } /** * Get database base path. * * @return string * @since 2.8.0 */ public function get_db_base_path() { return $this->get_tmp_path( true ) . DIRECTORY_SEPARATOR . self::DB_DIRECTORY; } /** * Extract downloaded database. * * @param string $temp_path The path to the temporary file. * * @return string|WP_Error Path to the database file or an error. * @since 2.8.0 */ public function extract_db_archive( $temp_path ) { try { $phar = new PharData( $temp_path ); $base_path = $this->get_db_base_path(); if ( ! is_dir( $base_path ) ) { wp_mkdir_p( $base_path ); } $phar->extractTo( $base_path, null, true ); $geodb_path = $base_path . DIRECTORY_SEPARATOR . $phar->current()->getFileName() . DIRECTORY_SEPARATOR . $this->get_db_full_name(); } catch ( Exception $exception ) { return new WP_Error( 'wpdef_maxmind_geolocation_database_archive', $exception->getMessage() ); } finally { // Archive file is not needed. wp_delete_file( $temp_path ); } return $geodb_path; } /** * Delete the database. * * @since 2.8.0 */ public function delete_database() { global $wp_filesystem; // Initialize the WP filesystem, no more using 'file-put-contents' function. if ( empty( $wp_filesystem ) ) { require_once ABSPATH . '/wp-admin/includes/file.php'; WP_Filesystem(); } $database_path = $this->get_db_base_path(); if ( $wp_filesystem->exists( $database_path ) ) { $wp_filesystem->delete( $database_path, true ); } } }