| @@ -9,28 +9,58 @@ | ||
| 9 | 9 | |
| 10 | 10 | add_action( 'wp_ajax_eos_plugin_reviews_restore_version','eos_plugin_reviews_restore_version' ); |
| 11 | 11 | /** |
| 12 | 12 | * Restore plugin version |
| 13 | + * This function is triggered by an Ajax request to restore a plugin version. | |
| 14 | + * It verifies the nonce for security, checks user permissions, and restores the plugin version | |
| 15 | + * from a specified directory. It unzips the plugin version if it is in zip format, renames the | |
| 16 | + * plugin directory, and activates the plugin. | |
| 13 | 17 | * |
| 14 | 18 | * @since 0.0.1 |
| 15 | 19 | */ |
| 16 | 20 | function eos_plugin_reviews_restore_version(){ |
| 17 | - if( isset( $_POST['nonce'] ) && isset( $_POST['dir'] ) && isset( $_POST['parent_plugin'] ) && wp_verify_nonce( sanitize_text_field( $_POST['nonce'] ),'plugin_reviews_restore_version' ) ){ | |
| 21 | + if( isset( $_POST['nonce'] ) && isset( $_POST['dir'] ) && isset( $_POST['parent_plugin'] ) && wp_verify_nonce( sanitize_text_field( $_POST['nonce'] ),'plugin_reviews_restore_version' ) && current_user_can( 'manage_options' ) ) { | |
| 18 | 22 | $key = eos_plugin_revision_key(); |
| 19 | 23 | if( $key ){ |
| 24 | + global $wp_filesystem; | |
| 25 | + if( empty( $wp_filesystem ) || ! function_exists( 'unzip_file' ) ) { | |
| 26 | + require_once ABSPATH .'/wp-admin/includes/file.php'; | |
| 27 | + WP_Filesystem(); | |
| 28 | + } | |
| 20 | 29 | $time = time(); |
| 21 | - $dir = sanitize_text_field( $_POST['dir'] ); | |
| 30 | + $dir = sanitize_text_field( $_POST['dir'] ); // $dir is the path of the plugin versoin that will be restored. | |
| 22 | 31 | $plugin = sanitize_text_field( $_POST['parent_plugin'] ); |
| 23 | 32 | $plugin_data = get_plugin_data( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) . '/' . $plugin ); |
| 24 | 33 | $version = $plugin_data['Version']; |
| 25 | 34 | $plugin_version = dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) . '/pr-' . $key . '-' . sanitize_option( 'upload_path',$version ) . '-ver-' . $time . dirname( $plugin ); |
| 26 | - $r1 = rename( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) . '/' . dirname( $plugin ), $plugin_version ); | |
| 27 | - $r2 = rename( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) . '/' . $dir , dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) . '/' . dirname( $plugin ) ); | |
| 28 | - $r3 = rename( $plugin_version,str_replace( $time,'',$plugin_version ) ); | |
| 35 | + // $plugin_version is the path assigned to the plugin that will be replaced. | |
| 36 | + | |
| 37 | + $r_unzip = true; | |
| 38 | + if( 'zip' === pathinfo( $dir, PATHINFO_EXTENSION ) ) { | |
| 39 | + if( ! function_exists( 'unzip_file' ) ) { | |
| 40 | + die(); | |
| 41 | + exit; | |
| 42 | + } | |
| 43 | + $r_unzip = unzip_file( | |
| 44 | + dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) . '/' . $dir, | |
| 45 | + dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) | |
| 46 | + ); // Unzip the chosen version. | |
| 47 | + if( $r_unzip ) { | |
| 48 | + wp_delete_file( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) . '/' . $dir ); // Delete the zip after unzipping it. | |
| 49 | + } | |
| 50 | + } | |
| 51 | + $r1 = $wp_filesystem->move( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) . '/' . dirname( $plugin ), $plugin_version ); // Rename the actual plugin that will be replaced giving a unique title. | |
| 52 | + $r2 = $wp_filesystem->move( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) . '/' . str_replace( '.zip', '', $dir ) , dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) . '/' . dirname( $plugin ) ); // Rename the chosen plugin version with the official plugin name. | |
| 53 | + $replaced_version_path = str_replace( $time,'',$plugin_version ); // Remove the time() from the title in the replaced plugin. | |
| 54 | + $r3 = $wp_filesystem->move( $plugin_version, $replaced_version_path ); | |
| 55 | + $r_zip = eos_pv_create_zip_pclzip( $replaced_version_path, '/pr-' . $key . '-' . sanitize_option( 'upload_path', $version ) . '-ver-' . dirname( $plugin ) ); | |
| 56 | + if( $r_zip ) { | |
| 57 | + $wp_filesystem->delete( $replaced_version_path, true ); | |
| 58 | + } | |
| 29 | 59 | do_action( 'activate_plugin',$plugin ); |
| 30 | 60 | do_action( "activate_{$plugin}" ); |
| 31 | 61 | do_action( 'activated_plugin', $plugin ); |
| 32 | - echo (bool) ( $r1 && $r2 && $r3 ); | |
| 62 | + echo (bool) ( $r_unzip && $r1 && $r2 && $r3 && $r_zip ); // Echo true if everything was successfull. | |
| 33 | 63 | } |
| 34 | 64 | } |
| 35 | 65 | die(); |
| 36 | 66 | exit; |