| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class for handling what to show when clicking on support in the menu in wp-admin |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace WPSynchro\Pages; |
| 8 |
|
| 9 |
use WPSynchro\Utilities\CommonFunctions; |
| 10 |
use WPSynchro\Utilities\DebugInformation; |
| 11 |
use WPSynchro\Utilities\Licensing\Licensing; |
| 12 |
use WPSynchro\Utilities\PluginDirs; |
| 13 |
|
| 14 |
class AdminSupport |
| 15 |
{ |
| 16 |
private $show_delete_settings_notice = false; |
| 17 |
|
| 18 |
/** |
| 19 |
* Called from WP menu to show support |
| 20 |
*/ |
| 21 |
public static function render() |
| 22 |
{ |
| 23 |
$instance = new self(); |
| 24 |
// Handle post |
| 25 |
if ($_SERVER['REQUEST_METHOD'] == 'POST') { |
| 26 |
$instance->handlePOST(); |
| 27 |
} |
| 28 |
$instance->handleGET(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Handle the update of data from support screen |
| 33 |
*/ |
| 34 |
private function handlePOST() |
| 35 |
{ |
| 36 |
// Check if it is delete settings |
| 37 |
if (isset($_POST['deletesettings']) && $_POST['deletesettings'] == 1) { |
| 38 |
$nonce = $_POST['nonce'] ?? ''; |
| 39 |
if (!wp_verify_nonce($nonce, 'wpsynchro_delete_all_data')) { |
| 40 |
echo "<div class='notice wpsynchro-notice'><p>" . __('Security token is no longer valid - Go back and try again.', 'wpsynchro') . '</p></div>'; |
| 41 |
return; |
| 42 |
} |
| 43 |
$this->cleanUpPluginMigration(); |
| 44 |
$this->show_delete_settings_notice = true; |
| 45 |
return; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Show WP Synchro support screen |
| 51 |
*/ |
| 52 |
private function handleGET() |
| 53 |
{ |
| 54 |
$debug_obj = new DebugInformation(); |
| 55 |
$debug_json = $debug_obj->getJSONDebugInformation(); |
| 56 |
|
| 57 |
if (CommonFunctions::isPremiumVersion()) { |
| 58 |
// Licensing |
| 59 |
$licensing = new Licensing(); |
| 60 |
} |
| 61 |
|
| 62 |
// Nonces |
| 63 |
$delete_all_settings_nonce = wp_create_nonce('wpsynchro_delete_all_data'); |
| 64 |
|
| 65 |
// Data for JS |
| 66 |
$data_for_js = [ |
| 67 |
'delete_all_settings_nonce' => $delete_all_settings_nonce, |
| 68 |
'show_delete_settings_notice' => $this->show_delete_settings_notice, |
| 69 |
'isPro' => CommonFunctions::isPremiumVersion() && $licensing->verifyLicense(), |
| 70 |
'debugJson' => $debug_json, |
| 71 |
]; |
| 72 |
wp_localize_script('wpsynchro_admin_js', 'wpsynchro_support_data', $data_for_js); |
| 73 |
|
| 74 |
// Print content |
| 75 |
echo '<div id="wpsynchro-support" class="wpsynchro"></div>'; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Clean up WP Synchro migration (used in setup) |
| 80 |
*/ |
| 81 |
public function cleanUpPluginMigration() |
| 82 |
{ |
| 83 |
// Setup |
| 84 |
$plugins_dirs = new PluginDirs(); |
| 85 |
$log_dir = $plugins_dirs->getUploadsFilePath(); |
| 86 |
|
| 87 |
// Clean files |
| 88 |
@array_map('unlink', glob("$log_dir*.log")); |
| 89 |
@array_map('unlink', glob("$log_dir*.sql")); |
| 90 |
@array_map('unlink', glob("$log_dir*.txt")); |
| 91 |
@array_map('unlink', glob("$log_dir*.tmp")); |
| 92 |
|
| 93 |
// Delete from database |
| 94 |
$options_to_keep = [ |
| 95 |
'wpsynchro_license_key', |
| 96 |
'wpsynchro_dbversion', |
| 97 |
'wpsynchro_accesskey', |
| 98 |
'wpsynchro_allowed_methods', |
| 99 |
'wpsynchro_muplugin_enabled', |
| 100 |
'wpsynchro_uploads_dir_secret' |
| 101 |
]; |
| 102 |
|
| 103 |
global $wpdb; |
| 104 |
$wpdb->query('delete FROM ' . $wpdb->options . " WHERE option_name like 'wpsynchro_%' and option_name not in ('" . implode("','", $options_to_keep) . "') "); |
| 105 |
} |
| 106 |
} |
| 107 |
|