| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package GDPRess |
| 4 |
* @author Daan van den Bergh |
| 5 |
* https://daan.dev |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace GDPRess\Admin; |
| 9 |
|
| 10 |
use GDPRess\Helper; |
| 11 |
use GDPRess\Download as DownloadHelper; |
| 12 |
|
| 13 |
class CacheManager { |
| 14 |
|
| 15 |
/** @var string $settings_page */ |
| 16 |
private $settings_page = ''; |
| 17 |
|
| 18 |
/** @var string $settings_tab */ |
| 19 |
private $settings_tab = ''; |
| 20 |
|
| 21 |
/** @var bool $settings_updated */ |
| 22 |
private $settings_updated = false; |
| 23 |
|
| 24 |
/** |
| 25 |
* Set Fields. |
| 26 |
* |
| 27 |
* @return void |
| 28 |
* |
| 29 |
* @throws \SodiumException |
| 30 |
*/ |
| 31 |
public function __construct() { |
| 32 |
$this->settings_page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : ''; |
| 33 |
$this->settings_tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : Settings::GDPRESS_ADMIN_SECTION_MANAGE; |
| 34 |
$this->settings_updated = isset( $_GET['settings-updated'] ); |
| 35 |
|
| 36 |
$this->maybe_manage_cached_files(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Filters & Hooks. |
| 41 |
* |
| 42 |
* @return void |
| 43 |
* |
| 44 |
* @throws \SodiumException |
| 45 |
*/ |
| 46 |
private function maybe_manage_cached_files() { |
| 47 |
if ( Settings::GDPRESS_ADMIN_PAGE !== $this->settings_page ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
if ( Settings::GDPRESS_ADMIN_SECTION_MANAGE !== $this->settings_tab ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
if ( ! $this->settings_updated ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$this->manage_cached_files(); |
| 60 |
|
| 61 |
// Clear the default 'Settings saved.' message. |
| 62 |
delete_transient( 'settings_errors' ); |
| 63 |
|
| 64 |
// Set our own. |
| 65 |
add_settings_error( 'general', 'settings_updated', __( 'Selected files downloaded successfully.', 'gdpr-press' ), 'success' ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Download all not excluded files. We don't have to check if $requests actually exist because the Submit button is only made available when it does. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
* |
| 73 |
* @throws \SodiumException |
| 74 |
*/ |
| 75 |
private function manage_cached_files() { |
| 76 |
if ( ! function_exists( 'download_url' ) ) { |
| 77 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 78 |
} |
| 79 |
|
| 80 |
$downloader = new DownloadHelper(); |
| 81 |
|
| 82 |
foreach ( Helper::requests() as $type => $requests ) { |
| 83 |
foreach ( $requests as $request ) { |
| 84 |
if ( $this->maybe_delete_excluded_file( $type, $request ) ) { |
| 85 |
continue; |
| 86 |
} |
| 87 |
|
| 88 |
$url = $downloader->download_file( $request['name'], $type, $request['href'] ); |
| 89 |
|
| 90 |
Helper::set_local_url( $type, $url ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Write everything to the database. |
| 96 |
*/ |
| 97 |
Helper::set_local_url( '', '', true ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Deletes the local file for the excluded resource if it exists. |
| 102 |
* |
| 103 |
* @param string $type |
| 104 |
* @param array $request |
| 105 |
* |
| 106 |
* @return bool |
| 107 |
*/ |
| 108 |
private function maybe_delete_excluded_file( $type, $request ) { |
| 109 |
if ( ! Helper::is_excluded( $type, $request['href'] ) ) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
if ( Helper::is_google_fonts_request( $request['href'] ) ) { |
| 114 |
$file_path = Helper::get_local_path_google_font( $request['name'] ); |
| 115 |
} else { |
| 116 |
$file_path = Helper::get_local_path( $request['href'], $type ); |
| 117 |
} |
| 118 |
|
| 119 |
if ( file_exists( $file_path ) ) { |
| 120 |
wp_delete_file( $file_path ); |
| 121 |
} |
| 122 |
|
| 123 |
return true; |
| 124 |
} |
| 125 |
} |
| 126 |
|