| @@ -1,26 +1,66 @@ | ||
| 1 | 1 | <?php |
| 2 | -defined( 'PLUGIN_REVISIONS_PLUGIN_DIR' ) || exit; | |
| 2 | +/** | |
| 3 | + * It includes the code for the Ajax activities. | |
| 3 | 4 | |
| 5 | + * @package Plugversions | |
| 6 | + */ | |
| 7 | + | |
| 8 | +defined( 'PLUGIN_REVISIONS_PLUGIN_DIR' ) || exit; // Exit if not accessed from Plugversions. | |
| 9 | + | |
| 4 | 10 | add_action( 'wp_ajax_eos_plugin_reviews_restore_version','eos_plugin_reviews_restore_version' ); |
| 5 | -//Restore plugin version | |
| 11 | +/** | |
| 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. | |
| 17 | + * | |
| 18 | + * @since 0.0.1 | |
| 19 | + */ | |
| 6 | 20 | function eos_plugin_reviews_restore_version(){ |
| 7 | - 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' ) ) { | |
| 8 | 22 | $key = eos_plugin_revision_key(); |
| 9 | 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 | + } | |
| 10 | 29 | $time = time(); |
| 11 | - $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. | |
| 12 | 31 | $plugin = sanitize_text_field( $_POST['parent_plugin'] ); |
| 13 | - $plugin_data = get_plugin_data( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ).'/'.$plugin ); | |
| 32 | + $plugin_data = get_plugin_data( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) . '/' . $plugin ); | |
| 14 | 33 | $version = $plugin_data['Version']; |
| 15 | - $plugin_version = dirname( PLUGIN_REVISIONS_PLUGIN_DIR ).'/pr-'.$key.'-'.sanitize_option( 'upload_path',$version ).'-ver-'.$time.dirname( $plugin ); | |
| 16 | - $r1 = rename( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ).'/'.dirname( $plugin ),$plugin_version ); | |
| 17 | - $r2 = rename( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ).'/'.$dir,dirname( PLUGIN_REVISIONS_PLUGIN_DIR ).'/'.dirname( $plugin ) ); | |
| 18 | - $r3 = rename( $plugin_version,str_replace( $time,'',$plugin_version ) ); | |
| 34 | + $plugin_version = dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) . '/pr-' . $key . '-' . sanitize_option( 'upload_path',$version ) . '-ver-' . $time . dirname( $plugin ); | |
| 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 | + } | |
| 19 | 59 | do_action( 'activate_plugin',$plugin ); |
| 20 | 60 | do_action( "activate_{$plugin}" ); |
| 21 | 61 | do_action( 'activated_plugin', $plugin ); |
| 22 | - echo (bool) ( $r1 && $r2 && $r3 ); | |
| 62 | + echo (bool) ( $r_unzip && $r1 && $r2 && $r3 && $r_zip ); // Echo true if everything was successfull. | |
| 23 | 63 | } |
| 24 | 64 | } |
| 25 | 65 | die(); |
| 26 | 66 | exit; |