| 1 |
<?php |
| 2 |
namespace Manage\Modules\Api\Classes; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly. |
| 6 |
} |
| 7 |
|
| 8 |
class Temp_Backup { |
| 9 |
|
| 10 |
const BACKUP_DIR_NAME = 'manage-temp-backup'; |
| 11 |
const PLUGINS = 'plugins'; |
| 12 |
const THEMES = 'themes'; |
| 13 |
|
| 14 |
public static function get_backup_dir( $type ): string { |
| 15 |
return WP_CONTENT_DIR . DIRECTORY_SEPARATOR . self::BACKUP_DIR_NAME . DIRECTORY_SEPARATOR . $type; |
| 16 |
} |
| 17 |
|
| 18 |
public static function create_plugin_backup( $plugin_slug ) { |
| 19 |
return self::create_backup( $plugin_slug, self::PLUGINS ); |
| 20 |
} |
| 21 |
|
| 22 |
public static function create_theme_backup( $theme_slug ) { |
| 23 |
return self::create_backup( $theme_slug, self::THEMES ); |
| 24 |
} |
| 25 |
|
| 26 |
private static function create_backup( $slug, $type ) { |
| 27 |
$source_dir = self::get_source_directory( $slug, $type ); |
| 28 |
|
| 29 |
if ( ! file_exists( $source_dir ) ) { |
| 30 |
$error_code = $type . '_not_found'; |
| 31 |
$error_message = ucfirst( $type ) . ' directory not found.'; |
| 32 |
return new \WP_Error( $error_code, $error_message, [ 'status' => \WP_Http::NOT_FOUND ] ); |
| 33 |
} |
| 34 |
|
| 35 |
$backup_dir_result = self::create_backup_dir( $type ); |
| 36 |
if ( is_wp_error( $backup_dir_result ) ) { |
| 37 |
return $backup_dir_result; |
| 38 |
} |
| 39 |
|
| 40 |
$backup_dir = self::get_backup_dir( $type ); |
| 41 |
$backup_item_dir = $backup_dir . DIRECTORY_SEPARATOR . $slug; |
| 42 |
|
| 43 |
if ( file_exists( $backup_item_dir ) ) { |
| 44 |
$remove_result = self::remove_backup( $backup_item_dir, $type ); |
| 45 |
if ( is_wp_error( $remove_result ) ) { |
| 46 |
return $remove_result; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
$copy_result = self::copy_directory( $source_dir, $backup_item_dir ); |
| 51 |
if ( is_wp_error( $copy_result ) ) { |
| 52 |
return $copy_result; |
| 53 |
} |
| 54 |
|
| 55 |
return true; |
| 56 |
} |
| 57 |
|
| 58 |
private static function get_source_directory( $slug, $type ): string { |
| 59 |
if ( self::PLUGINS === $type ) { |
| 60 |
return WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $slug; |
| 61 |
} |
| 62 |
|
| 63 |
return get_theme_root() . DIRECTORY_SEPARATOR . $slug; |
| 64 |
} |
| 65 |
|
| 66 |
private static function create_backup_dir( $type ) { |
| 67 |
$subfolder_dir = self::get_backup_dir( $type ); |
| 68 |
if ( ! file_exists( $subfolder_dir ) ) { |
| 69 |
if ( ! wp_mkdir_p( $subfolder_dir ) ) { |
| 70 |
return new \WP_Error( 'backup_subfolder_creation_failed', 'Failed to create backup subfolder: ' . $type, [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ] ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
private static function remove_backup( $backup_item_dir, $type ) { |
| 78 |
if ( ! file_exists( $backup_item_dir ) ) { |
| 79 |
$error_code = 'backup_not_found'; |
| 80 |
$error_message = ucfirst( $type ) . ' backup not found.'; |
| 81 |
return new \WP_Error( $error_code, $error_message, [ 'status' => \WP_Http::NOT_FOUND ] ); |
| 82 |
} |
| 83 |
|
| 84 |
$remove_result = self::remove_directory( $backup_item_dir ); |
| 85 |
if ( is_wp_error( $remove_result ) ) { |
| 86 |
return $remove_result; |
| 87 |
} |
| 88 |
|
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
public static function backup_exists( $slug, $type ): bool { |
| 93 |
if ( empty( $slug ) ) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
if ( ! in_array( $type, [ self::PLUGINS, self::THEMES ], true ) ) { |
| 98 |
$type = self::PLUGINS; |
| 99 |
} |
| 100 |
|
| 101 |
$backup_dir = self::get_backup_dir( $type ); |
| 102 |
$backup_item_dir = $backup_dir . DIRECTORY_SEPARATOR . $slug; |
| 103 |
|
| 104 |
return file_exists( $backup_item_dir ); |
| 105 |
} |
| 106 |
|
| 107 |
public static function restore_plugin_backup( $plugin_slug ) { |
| 108 |
return self::restore_backup( $plugin_slug, self::PLUGINS ); |
| 109 |
} |
| 110 |
|
| 111 |
public static function restore_theme_backup( $theme_slug ) { |
| 112 |
return self::restore_backup( $theme_slug, self::THEMES ); |
| 113 |
} |
| 114 |
|
| 115 |
private static function restore_backup( $slug, $type ) { |
| 116 |
$destination_dir = self::get_source_directory( $slug, $type ); |
| 117 |
|
| 118 |
if ( ! self::backup_exists( $slug, $type ) ) { |
| 119 |
$error_code = $type . '_backup_not_found'; |
| 120 |
$error_message = ucfirst( $type ) . ' backup not found.'; |
| 121 |
return new \WP_Error( $error_code, $error_message, [ 'status' => \WP_Http::NOT_FOUND ] ); |
| 122 |
} |
| 123 |
|
| 124 |
$backup_dir = self::get_backup_dir( $type ); |
| 125 |
$backup_source_dir = $backup_dir . DIRECTORY_SEPARATOR . $slug; |
| 126 |
|
| 127 |
if ( file_exists( $destination_dir ) ) { |
| 128 |
$remove_result = self::remove_directory( $destination_dir ); |
| 129 |
if ( is_wp_error( $remove_result ) ) { |
| 130 |
return $remove_result; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
$copy_result = self::copy_directory( $backup_source_dir, $destination_dir ); |
| 135 |
if ( is_wp_error( $copy_result ) ) { |
| 136 |
return $copy_result; |
| 137 |
} |
| 138 |
|
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
private static function copy_directory( $source, $destination ) { |
| 143 |
if ( ! file_exists( $source ) ) { |
| 144 |
return new \WP_Error( 'source_not_found', 'Source directory not found.', [ 'status' => \WP_Http::NOT_FOUND ] ); |
| 145 |
} |
| 146 |
|
| 147 |
if ( ! is_dir( $source ) ) { |
| 148 |
return new \WP_Error( 'source_not_directory', 'Source is not a directory.', [ 'status' => \WP_Http::BAD_REQUEST ] ); |
| 149 |
} |
| 150 |
|
| 151 |
if ( ! wp_mkdir_p( $destination ) ) { |
| 152 |
return new \WP_Error( 'destination_creation_failed', 'Failed to create destination directory.', [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ] ); |
| 153 |
} |
| 154 |
|
| 155 |
$iterator = new \RecursiveIteratorIterator( |
| 156 |
new \RecursiveDirectoryIterator( $source, \RecursiveDirectoryIterator::SKIP_DOTS ), |
| 157 |
\RecursiveIteratorIterator::SELF_FIRST |
| 158 |
); |
| 159 |
|
| 160 |
foreach ( $iterator as $item ) { |
| 161 |
$target = $destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName(); |
| 162 |
|
| 163 |
if ( $item->isDir() ) { |
| 164 |
if ( ! wp_mkdir_p( $target ) ) { |
| 165 |
return new \WP_Error( 'directory_copy_failed', 'Failed to create directory: ' . $target, [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ] ); |
| 166 |
} |
| 167 |
} else { |
| 168 |
if ( ! copy( $item, $target ) ) { |
| 169 |
return new \WP_Error( 'file_copy_failed', 'Failed to copy file: ' . $item, [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ] ); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return true; |
| 175 |
} |
| 176 |
|
| 177 |
private static function remove_directory( $directory ) { |
| 178 |
if ( ! file_exists( $directory ) ) { |
| 179 |
return true; |
| 180 |
} |
| 181 |
|
| 182 |
if ( ! is_dir( $directory ) ) { |
| 183 |
return new \WP_Error( 'not_directory', 'Path is not a directory.', [ 'status' => \WP_Http::BAD_REQUEST ] ); |
| 184 |
} |
| 185 |
|
| 186 |
$iterator = new \RecursiveIteratorIterator( |
| 187 |
new \RecursiveDirectoryIterator( $directory, \RecursiveDirectoryIterator::SKIP_DOTS ), |
| 188 |
\RecursiveIteratorIterator::CHILD_FIRST |
| 189 |
); |
| 190 |
|
| 191 |
foreach ( $iterator as $item ) { |
| 192 |
if ( $item->isDir() ) { |
| 193 |
if ( ! rmdir( $item ) ) { |
| 194 |
return new \WP_Error( 'directory_removal_failed', 'Failed to remove directory: ' . $item, [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ] ); |
| 195 |
} |
| 196 |
} else { |
| 197 |
if ( ! unlink( $item ) ) { |
| 198 |
return new \WP_Error( 'file_removal_failed', 'Failed to remove file: ' . $item, [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ] ); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
if ( ! rmdir( $directory ) ) { |
| 204 |
return new \WP_Error( 'directory_removal_failed', 'Failed to remove main directory: ' . $directory, [ 'status' => \WP_Http::INTERNAL_SERVER_ERROR ] ); |
| 205 |
} |
| 206 |
|
| 207 |
return true; |
| 208 |
} |
| 209 |
} |
| 210 |
|