time() ) ); } } ); add_filter( 'upgrader_package_options',function( $options ) { /** * Update revisions after plugin update * * @since 0.0.1 */ if( isset( $options['destination'] ) && isset( $options['hook_extra'] ) ){ $hook_extra = $options['hook_extra']; if( isset( $hook_extra['plugin'] ) ){ $plugin = $hook_extra['plugin']; $plugin_name = dirname( $plugin ); $path = sanitize_option( 'upload_path',$options['destination'].'/'.$plugin ); if( file_exists( $path ) ){ $plugin_data = get_plugin_data( $path ); $version = $plugin_data['Version']; if( !class_exists( 'WP_Upgrader' ) ){ if( file_exists( ABSPATH.'wp-admin/includes/class-wp-upgrader.php' ) ){ require_once ABSPATH.'/wp-admin/includes/class-wp-upgrader.php'; } } if( class_exists( 'WP_Upgrader' ) ){ $upgrader = new WP_Upgrader(); $key = eos_plugin_revision_key(); if( $key ){ $zip_dirname = 'pr-'.$key.'-'.sanitize_option( 'upload_path',$version ).'-ver-'.dirname( $plugin ); $destination = str_replace( dirname( $plugin ), $zip_dirname, sanitize_option( 'upload_path',plugin_dir_path( $path )) ); if( !is_dir( $destination ) ){ global $wp_filesystem; if( empty( $wp_filesystem ) ){ require_once ( ABSPATH .'/wp-admin/includes/file.php' ); WP_Filesystem(); } $wp_filesystem->mkdir( $destination ); } if( !empty( $wp_filesystem ) && $wp_filesystem->is_dir( $destination ) ){ $result = copy_dir( plugin_dir_path( $path ),$destination ); $zip_dirname .= '.zip'; if( eos_pv_create_zip_pclzip( $destination, $zip_dirname ) ) { // If zipped archive created delete the unzipped directory. $wp_filesystem->delete( $destination, true ); } eos_plugin_revisions_remove_versions( apply_filters( 'max_plugin_revisions',4 ),$plugin_name ); } } } } } } return $options; },10,4 ); add_filter( 'all_plugins',function( $plugins ){ /** * Remove plugin revisions from plugins table in the page wp-admin/plugins.php * Add link to restore previous unzipped versions for backward compatibility * * @since 0.0.1 */ $key = eos_plugin_revision_key(); if( $key ){ foreach( $plugins as $plugin => $arr ){ if( false !== strpos( $plugin,'pr-'.$key.'-' ) ){ unset( $plugins[$plugin] ); $parent_plugin = str_replace( 'pr-'.$key.'-','',$plugin ); $parent_pluginArr = explode( '-ver-',$parent_plugin ); $parent_plugin = isset( $parent_pluginArr[1] ) ? $parent_pluginArr[1] : $parent_plugin; add_filter( 'plugin_action_links_'.$parent_plugin,function( $actions, $plugin_file, $plugin_data, $context ){ if( !current_user_can( 'activate_plugin' ) ) return $actions; $key = eos_plugin_revision_key(); $all_dirs = scandir( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) ); if( $all_dirs && !empty( $all_dirs ) ){ $new_action_links = ''; $plugin = dirname( $plugin_file ); foreach( $all_dirs as $dir ){ if( false !== strpos( $dir,'pr-'.$key.'-' ) && substr( $dir,-strlen( $plugin ),strlen( $plugin ) ) === $plugin ){ $ver = str_replace( 'pr-'.$key.'-','',$dir ); $verArr = explode( '-ver-',$ver ); $ver = $verArr[0]; $new_action_links .= ''.sprintf( esc_html__( 'Replace with version: %s','plugversions' ),$ver ).' '; } } if( '' !== $new_action_links ){ $actions['versions'] = ''.esc_html__( 'Revisions','plugin-revisioons' ).''.rtrim( $new_action_links,' ' ).''; } } return $actions; },10,4 ); } } } return $plugins; } ); add_action( 'admin_head',function(){ /** * Add style to properly show the revisions on the page of plugins * * @since 0.0.1 */ global $pagenow; if( $pagenow && 'plugins.php' === sanitize_text_field( $pagenow ) ){ ?> delete( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ).'/'.$dir,true ); } } ++$n; } } } /** * Remove all revisions of all the plugins * * @since 0.0.1 */ function eos_plugin_revisions_remove_all_versions(){ $key = eos_plugin_revision_key(); $all_dirs = eos_plugin_revisions_scandir( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) ); global $wp_filesystem; if( empty( $wp_filesystem ) ){ require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); } if( $all_dirs && !empty( $all_dirs ) ){ foreach( $all_dirs as $dir ){ if( false !== strpos( $dir,'pr-'.$key.'-' ) ){ $result = $wp_filesystem->delete( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ).'/'.$dir,true ); } } } } /** * Revisions scandir * * @since 0.0.1 */ function eos_plugin_revisions_scandir( $dir ) { $ignored = array('.', '..', '.svn', '.htaccess'); $files = array(); foreach( scandir( $dir ) as $file ){ if( in_array( $file,$ignored ) ) continue; $files[$file] = filemtime( $dir.'/'.$file ); } asort( $files ); $files = array_keys( $files ); return ( $files ) ? $files : false; } add_action( 'admin_init', 'eos_pv_restore_revision_links' ); /** * Add links to restore previous revisions from zipped plugins. * * @since 0.0.6 */ function eos_pv_restore_revision_links() { $key = eos_plugin_revision_key(); if( $key ){ $all_plugin_files = scandir( WP_PLUGIN_DIR ); if( $all_plugin_files && is_array( $all_plugin_files ) && ! empty( $all_plugin_files ) ) { require_once PLUGIN_REVISIONS_PLUGIN_DIR . '/admin/classes/class-plugversions-restoring-link.php'; foreach( $all_plugin_files as $plugin ) { if( 'zip' === pathinfo( $plugin, PATHINFO_EXTENSION ) ) { if( false !== strpos( $plugin, 'pr-' . $key .'-' ) && false !== strpos( $plugin, '-ver-' ) ) { $arr = explode( 'ver-', $plugin ); $arr = explode( '-', $arr[0] ); if( 'pr' === $arr[0] && isset( $arr[1] ) && $key === $arr[1] && isset( $arr[2] ) ) { $version = $arr[2]; $plugin_name = str_replace( array( 'pr-' . $key . '-' . $version . '-ver-', '.zip' ), array( '', '' ), $plugin ); if( is_dir( WP_PLUGIN_DIR . '/' . $plugin_name ) ) { $link = new PlugVersions_Restoring_Link( $key, $version, $plugin, $plugin_name ); } } } } } } } }