Use filesystem and hardcoded upload dir

This commit is contained in:
Mike Jolley 2019-03-08 18:20:17 +00:00
parent f0de2a6d23
commit 15f0d2566d
1 changed files with 17 additions and 22 deletions

View File

@ -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(),