| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
// Exit if accessed directly |
| 5 |
if( !defined( 'ABSPATH' ) ) |
| 6 |
exit; |
| 7 |
|
| 8 |
/** |
| 9 |
* Helper method to check if user is in the plugins page. |
| 10 |
* |
| 11 |
* @author |
| 12 |
* @since 1.4.0 |
| 13 |
* |
| 14 |
* @return bool |
| 15 |
*/ |
| 16 |
function wpdbbkp_is_plugins_page() { |
| 17 |
if(function_exists('get_current_screen')){ |
| 18 |
$screen = get_current_screen(); |
| 19 |
if(is_object($screen)){ |
| 20 |
if($screen->id == 'plugins' || $screen->id == 'plugins-network'){ |
| 21 |
return true; |
| 22 |
} |
| 23 |
} |
| 24 |
} |
| 25 |
return false; |
| 26 |
} |
| 27 |
|
| 28 |
function wpdbbkp_get_current_url(){ |
| 29 |
|
| 30 |
$link = "http"; |
| 31 |
|
| 32 |
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on'){ |
| 33 |
$link = "https"; |
| 34 |
} |
| 35 |
|
| 36 |
$link .= "://"; |
| 37 |
$link .= $_SERVER['HTTP_HOST']; |
| 38 |
$link .= $_SERVER['REQUEST_URI']; |
| 39 |
|
| 40 |
return $link; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* display deactivation logic on plugins page |
| 45 |
* |
| 46 |
* @since 1.4.0 |
| 47 |
*/ |
| 48 |
|
| 49 |
|
| 50 |
function wpdbbkp_add_deactivation_feedback_modal() { |
| 51 |
|
| 52 |
|
| 53 |
if( !is_admin() && !wpdbbkp_is_plugins_page()) { |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$current_user = wp_get_current_user(); |
| 58 |
if( !($current_user instanceof WP_User) ) { |
| 59 |
$email = ''; |
| 60 |
} else { |
| 61 |
$email = trim( $current_user->user_email ); |
| 62 |
} |
| 63 |
|
| 64 |
require_once WPDB_PATH."includes/admin/deactivate-feedback.php"; |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* send feedback via email |
| 70 |
* |
| 71 |
* @since 1.4.0 |
| 72 |
*/ |
| 73 |
function wpdbbkp_send_feedback() { |
| 74 |
|
| 75 |
if ( ! isset( $_POST['wpdbbkp_security_nonce'] ) ){ |
| 76 |
return; |
| 77 |
} |
| 78 |
if ( !wp_verify_nonce( $_POST['wpdbbkp_security_nonce'], 'wpdbbkp-pub-nonce' ) ){ |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
if( ! current_user_can( 'manage_options' ) ) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
if( isset( $_POST['data'] ) ) { |
| 87 |
parse_str( $_POST['data'], $form ); |
| 88 |
} |
| 89 |
|
| 90 |
$text = ''; |
| 91 |
if( isset( $form['wpdbbkp_disable_text'] ) ) { |
| 92 |
$text = implode( "\n\r", $form['wpdbbkp_disable_text'] ); |
| 93 |
} |
| 94 |
|
| 95 |
$headers = array(); |
| 96 |
|
| 97 |
$from = isset( $form['wpdbbkp_disable_from'] ) ? $form['wpdbbkp_disable_from'] : ''; |
| 98 |
if( $from ) { |
| 99 |
$headers[] = "From: $from"; |
| 100 |
$headers[] = "Reply-To: $from"; |
| 101 |
} |
| 102 |
|
| 103 |
$subject = isset( $form['wpdbbkp_disable_reason'] ) ? $form['wpdbbkp_disable_reason'] : '(no reason given)'; |
| 104 |
|
| 105 |
$subject = $subject.' - Backup for WP Publisher'; |
| 106 |
|
| 107 |
if($subject == 'technical - Backup for WP Publisher'){ |
| 108 |
|
| 109 |
$text = trim($text); |
| 110 |
|
| 111 |
if(!empty($text)){ |
| 112 |
|
| 113 |
$text = 'technical issue description: '.$text; |
| 114 |
|
| 115 |
}else{ |
| 116 |
|
| 117 |
$text = 'no description: '.$text; |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
$success = wp_mail( 'team@magazine3.in', $subject, $text, $headers ); |
| 123 |
|
| 124 |
wp_die(); |
| 125 |
} |
| 126 |
add_action( 'wp_ajax_wpdbbkp_send_feedback', 'wpdbbkp_send_feedback' ); |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
add_action( 'admin_enqueue_scripts', 'wpdbbkp_enqueue_makebetter_email_js' ); |
| 131 |
|
| 132 |
function wpdbbkp_enqueue_makebetter_email_js(){ |
| 133 |
|
| 134 |
if( !is_admin() && !wpdbbkp_is_plugins_page()) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
wp_register_script( 'wpdbbkp-make-better-js', WPDB_PLUGIN_URL . '/assets/js/make-better-admin.js', array( 'jquery' ), WPDB_VERSION , true); |
| 140 |
wp_localize_script('wpdbbkp-make-better-js', 'wpdbbkp_pub_script_vars', array( |
| 141 |
'nonce' => wp_create_nonce( 'wpdbbkp-pub-nonce' ),) |
| 142 |
); |
| 143 |
wp_enqueue_script( 'wpdbbkp-make-better-js'); |
| 144 |
wp_enqueue_style( 'wpdbbkp-make-better-css', WPDB_PLUGIN_URL . '/assets/css/make-better-admin.css', false , WPDB_VERSION); |
| 145 |
} |
| 146 |
|
| 147 |
add_filter('admin_footer', 'wpdbbkp_add_deactivation_feedback_modal'); |
| 148 |
|
| 149 |
|
| 150 |
/* |
| 151 |
* Read the contents of a file using the WordPress filesystem API. |
| 152 |
* |
| 153 |
* @param string $file_path The path to the file. |
| 154 |
* @return string|false The file contents or false on failure. |
| 155 |
*/ |
| 156 |
function wpdbbkp_read_file_contents($file_path) { |
| 157 |
global $wp_filesystem; |
| 158 |
|
| 159 |
// Initialize the WordPress filesystem, no more using file_get_contents function |
| 160 |
if (empty($wp_filesystem)) { |
| 161 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 162 |
WP_Filesystem(); |
| 163 |
} |
| 164 |
|
| 165 |
// Check if the file exists and is readable |
| 166 |
if ($wp_filesystem->exists($file_path) && $wp_filesystem->is_readable($file_path)) { |
| 167 |
return $wp_filesystem->get_contents($file_path); |
| 168 |
} |
| 169 |
|
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Write data to a file using the WordPress File System API for smaller files and PHP native functions for large files. |
| 175 |
* |
| 176 |
* @param string $filename The path to the file to write data to. |
| 177 |
* @param string $data The data to write. |
| 178 |
* @param bool $append Whether to append the data to the file (default: false). |
| 179 |
*/ |
| 180 |
function wpdbbkp_write_file_contents( $filename, $data, $append = false ) { |
| 181 |
global $wp_filesystem; |
| 182 |
|
| 183 |
if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 184 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 185 |
} |
| 186 |
|
| 187 |
if ( ! WP_Filesystem() ) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
// Ensure the directory is writable. |
| 192 |
$dir = dirname( $filename ); |
| 193 |
if ( ! $wp_filesystem->exists( $dir ) ) { |
| 194 |
$wp_filesystem->mkdir( $dir ); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
$file_size = $wp_filesystem->size( $filename ); |
| 199 |
|
| 200 |
// Use the WordPress File System API for small files. |
| 201 |
if ( $file_size < 104857600 ) { // 100 MB as threshold for large files. |
| 202 |
// Check if the file exists; create it if it does not. |
| 203 |
if ( ! $wp_filesystem->exists( $filename ) ) { |
| 204 |
$wp_filesystem->put_contents( $filename, '', FS_CHMOD_FILE ); |
| 205 |
} |
| 206 |
|
| 207 |
// Read current contents if appending. |
| 208 |
$current_content = $append ? $wp_filesystem->get_contents( $filename ) : ''; |
| 209 |
|
| 210 |
// Write the new data. |
| 211 |
$wp_filesystem->put_contents( $filename, $current_content . $data, FS_CHMOD_FILE ); |
| 212 |
} else { |
| 213 |
// Use native PHP functions for very large files. |
| 214 |
$mode = $append ? 'a' : 'w'; |
| 215 |
//phpcs:ignore -- using native PHP functions for large files. |
| 216 |
$file = fopen( $filename, $mode ); |
| 217 |
|
| 218 |
if ( $file ) { |
| 219 |
//phpcs:ignore -- using native PHP functions for large files. |
| 220 |
fwrite( $file, $data ); |
| 221 |
//phpcs:ignore -- using native PHP functions for large files. |
| 222 |
fclose( $file ); |
| 223 |
} else { |
| 224 |
error_log( "Failed to open file for writing: $filename" ); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Check if a directory is writable using the WordPress File System API. |
| 231 |
* |
| 232 |
* @param string $dir The path to the directory. |
| 233 |
* @return bool True if the directory is writable, false otherwise. |
| 234 |
*/ |
| 235 |
function wpdbbkp_is_writable( $dir ) { |
| 236 |
global $wp_filesystem; |
| 237 |
|
| 238 |
if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 239 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 240 |
} |
| 241 |
|
| 242 |
if ( ! WP_Filesystem() ) { |
| 243 |
return false; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
return $wp_filesystem->is_writable( $dir ); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
/** |
| 252 |
* Check if a file exists using the WordPress File System API. |
| 253 |
* |
| 254 |
* @param string $file The path to the file. |
| 255 |
* @return bool True if the file exists, false otherwise. |
| 256 |
*/ |
| 257 |
function wpdbbkp_file_exists( $file ) { |
| 258 |
global $wp_filesystem; |
| 259 |
|
| 260 |
if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 261 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 262 |
} |
| 263 |
|
| 264 |
if ( ! WP_Filesystem() ) { |
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
return $wp_filesystem->exists( $file ); |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Filters an array of backup files to ensure only unique filenames are included. |
| 273 |
* |
| 274 |
* @param array $backups Array of backup files. |
| 275 |
* @return array Filtered array of backup files with unique filenames. |
| 276 |
*/ |
| 277 |
function wpdbbkp_filter_unique_filenames( $backups ) { |
| 278 |
$unique_filenames = []; |
| 279 |
$filtered_backups = []; |
| 280 |
|
| 281 |
if ( ! empty( $backups ) ) { |
| 282 |
foreach ( $backups as $backup ) { |
| 283 |
if ( isset( $backup['filename'] ) && ! in_array( $backup['filename'], $unique_filenames ) ) { |
| 284 |
$unique_filenames[] = $backup['filename']; |
| 285 |
$filtered_backups[] = $backup; |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
return $filtered_backups; |
| 291 |
} |
| 292 |
|
| 293 |
|