From 15f0d2566d3394abfba8b8b531626a6b2737088a Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 8 Mar 2019 18:20:17 +0000 Subject: [PATCH] Use filesystem and hardcoded upload dir --- includes/class-wc-geolocation.php | 39 ++++++++++++++----------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/includes/class-wc-geolocation.php b/includes/class-wc-geolocation.php index d365cfaf47c..ebf24268deb 100644 --- a/includes/class-wc-geolocation.php +++ b/includes/class-wc-geolocation.php @@ -237,13 +237,13 @@ class WC_Geolocation { * @return string */ public static function get_local_database_path( $deprecated = '2' ) { - $upload_dir = wp_get_upload_dir(); - - return apply_filters( 'woocommerce_geolocation_local_database_path', $upload_dir['basedir'] . '/GeoLite2-Country.mmdb', $deprecated ); + return apply_filters( 'woocommerce_geolocation_local_database_path', WP_CONTENT_DIR . '/uploads/GeoLite2-Country.mmdb', $deprecated ); } /** * Update geoip database. + * + * Extract files with PharData. Tool built into PHP since 5.3. */ public static function update_database() { $logger = wc_get_logger(); @@ -255,31 +255,28 @@ class WC_Geolocation { require_once ABSPATH . 'wp-admin/includes/file.php'; - $upload_dir = wp_upload_dir(); - $tmp_database_path = download_url( self::GEOLITE2_DB ); + $database = 'GeoLite2-Country.mmdb'; + $target_database_path = self::get_local_database_path(); + $tmp_database_path = download_url( self::GEOLITE2_DB ); if ( ! is_wp_error( $tmp_database_path ) ) { try { - // GeoLite2 database name. - $database = 'GeoLite2-Country.mmdb'; - $dest_path = trailingslashit( $upload_dir['basedir'] ) . $database; + WP_Filesystem(); + + global $wp_filesystem; + + // Make sure target dir exists. + $wp_filesystem->mkdir( dirname( $target_database_path ) ); // Extract files with PharData. Tool built into PHP since 5.3. $file = new PharData( $tmp_database_path ); // phpcs:ignore PHPCompatibility.Classes.NewClasses.phardataFound $file_path = trailingslashit( $file->current()->getFileName() ) . $database; + $file->extractTo( dirname( $tmp_database_path ), $file_path, true ); - // Extract under uploads directory. - $file->extractTo( $upload_dir['basedir'], $file_path, true ); - - // Remove old database. - @unlink( $dest_path ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged, WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink - - // Copy database and delete tmp directories. - @rename( trailingslashit( $upload_dir['basedir'] ) . $file_path, $dest_path ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged, WordPress.VIP.FileSystemWritesDisallow.file_ops_rename - @rmdir( trailingslashit( $upload_dir['basedir'] ) . $file->current()->getFileName() ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged, WordPress.VIP.FileSystemWritesDisallow.directory_rmdir - - // Set correct file permission. - @chmod( $dest_path, 0644 ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged, WordPress.VIP.FileSystemWritesDisallow.chmod_chmod + // Move file and delete temp. + $wp_filesystem->move( trailingslashit( dirname( $tmp_database_path ) ) . $file_path, $target_database_path, true ); + $wp_filesystem->unlink( trailingslashit( dirname( $tmp_database_path ) ) . $file->current()->getFileName() ); + $wp_filesystem->unlink( $tmp_database_path ); } catch ( Exception $e ) { $logger->notice( $e->getMessage(), array( 'source' => 'geolocation' ) ); @@ -287,8 +284,6 @@ class WC_Geolocation { wp_clear_scheduled_hook( 'woocommerce_geoip_updater' ); wp_schedule_event( strtotime( 'first tuesday of next month' ), 'monthly', 'woocommerce_geoip_updater' ); } - - @unlink( $tmp_database_path ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged, WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink } else { $logger->notice( 'Unable to download GeoIP Database: ' . $tmp_database_path->get_error_message(),