| 1 |
<?php |
| 2 |
defined( 'PLUGIN_REVISIONS_PLUGIN_DIR' ) || exit; |
| 3 |
|
| 4 |
if( wp_doing_ajax() ){ |
| 5 |
require_once PLUGIN_REVISIONS_PLUGIN_DIR.'/admin/pr-ajax-admin.php'; |
| 6 |
} |
| 7 |
|
| 8 |
//Actions triggered after plugin activation or after a new site of a multisite installation is created |
| 9 |
register_activation_hook( __FILE__,function(){ |
| 10 |
if( !eos_plugin_revision_key() ){ |
| 11 |
update_site_option( 'plugin_revisions',array( 'time' => time() ) ); |
| 12 |
} |
| 13 |
} ); |
| 14 |
|
| 15 |
add_filter( 'upgrader_package_options',function( $options){ |
| 16 |
if( isset( $options['destination'] ) && isset( $options['hook_extra'] ) ){ |
| 17 |
$hook_extra = $options['hook_extra']; |
| 18 |
if( isset( $hook_extra['plugin'] ) ){ |
| 19 |
$plugin = $hook_extra['plugin']; |
| 20 |
$plugin_name = dirname( $plugin ); |
| 21 |
$path = sanitize_option( 'upload_path',$options['destination'].'/'.$plugin ); |
| 22 |
if( file_exists( $path ) ){ |
| 23 |
$plugin_data = get_plugin_data( $path ); |
| 24 |
$version = $plugin_data['Version']; |
| 25 |
if( !class_exists( 'WP_Upgrader' ) ){ |
| 26 |
if( file_exists( ABSPATH.'wp-admin/includes/class-wp-upgrader.php' ) ){ |
| 27 |
require_once ABSPATH.'/wp-admin/includes/class-wp-upgrader.php'; |
| 28 |
} |
| 29 |
} |
| 30 |
if( class_exists( 'WP_Upgrader' ) ){ |
| 31 |
$upgrader = new WP_Upgrader(); |
| 32 |
$key = eos_plugin_revision_key(); |
| 33 |
if( $key ){ |
| 34 |
$destination = str_replace( dirname( $plugin ),'pr-'.$key.'-'.sanitize_option( 'upload_path',$version ).'-ver-'.dirname( $plugin ),sanitize_option( 'upload_path',plugin_dir_path( $path )) ); |
| 35 |
if( !is_dir( $destination ) ){ |
| 36 |
global $wp_filesystem; |
| 37 |
if( empty( $wp_filesystem ) ){ |
| 38 |
require_once ( ABSPATH .'/wp-admin/includes/file.php' ); |
| 39 |
WP_Filesystem(); |
| 40 |
} |
| 41 |
$wp_filesystem->mkdir( $destination ); |
| 42 |
} |
| 43 |
if( !empty( $wp_filesystem ) && $wp_filesystem->is_dir( $destination ) ){ |
| 44 |
$result = copy_dir( plugin_dir_path( $path ),$destination ); |
| 45 |
eos_plugin_revisions_remove_versions( apply_filters( 'max_plugin_revisions',4 ),$plugin_name ); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
return $options; |
| 53 |
},10,4 ); |
| 54 |
|
| 55 |
add_filter( 'all_plugins',function( $plugins ){ |
| 56 |
//Remove plugin revisions from plugins table in the page wp-admin/plugins.php |
| 57 |
$key = eos_plugin_revision_key(); |
| 58 |
if( $key ){ |
| 59 |
foreach( $plugins as $plugin => $arr ){ |
| 60 |
if( false !== strpos( $plugin,'pr-'.$key.'-' ) ){ |
| 61 |
unset( $plugins[$plugin] ); |
| 62 |
$parent_plugin = str_replace( 'pr-'.$key.'-','',$plugin ); |
| 63 |
$parent_pluginArr = explode( '-ver-',$parent_plugin ); |
| 64 |
$parent_plugin = isset( $parent_pluginArr[1] ) ? $parent_pluginArr[1] : $parent_plugin; |
| 65 |
add_filter( 'plugin_action_links_'.$parent_plugin,function( $actions, $plugin_file, $plugin_data, $context ){ |
| 66 |
if( !current_user_can( 'activate_plugin' ) ) return $actions; |
| 67 |
$key = eos_plugin_revision_key(); |
| 68 |
$all_dirs = scandir( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) ); |
| 69 |
if( $all_dirs && !empty( $all_dirs ) ){ |
| 70 |
$new_action_links = ''; |
| 71 |
$plugin = dirname( $plugin_file ); |
| 72 |
foreach( $all_dirs as $dir ){ |
| 73 |
error_log('dir: '.$dir); |
| 74 |
error_log('plugin: '.$plugin); |
| 75 |
if( false !== strpos( $dir,'pr-'.$key.'-' ) && substr( $dir,-strlen( $plugin ),strlen( $plugin ) ) === $plugin ){ |
| 76 |
$ver = str_replace( 'pr-'.$key.'-','',$dir ); |
| 77 |
$verArr = explode( '-ver-',$ver ); |
| 78 |
$ver = $verArr[0]; |
| 79 |
$new_action_links .= '<a class="plugin-revision-action" href="#" data-parent_plugin="'.esc_attr( $plugin_file ).'" data-dir="'.esc_attr( $dir ).'">'.sprintf( esc_html__( 'Replace with version: %s','plugversions' ),$ver ).'</a> '; |
| 80 |
} |
| 81 |
} |
| 82 |
if( '' !== $new_action_links ){ |
| 83 |
$actions['versions'] = '<span class="plugin-revision-wrp"><a href="#">'.esc_html__( 'Revisions','plugin-revisioons' ).'</a><span class="plugin-revisions-vers">'.rtrim( $new_action_links,' ' ).'</span></span>'; |
| 84 |
} |
| 85 |
} |
| 86 |
return $actions; |
| 87 |
},10,4 ); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
return $plugins; |
| 92 |
} ); |
| 93 |
|
| 94 |
add_action( 'admin_head',function(){ |
| 95 |
global $pagenow; |
| 96 |
if( $pagenow && 'plugins.php' === sanitize_text_field( $pagenow ) ){ |
| 97 |
?> |
| 98 |
<style id="plugin-revisions-css" type="text/css"> |
| 99 |
.plugin-revision-wrp{ |
| 100 |
position:relative |
| 101 |
} |
| 102 |
.plugin-revisions-vers{ |
| 103 |
display:none; |
| 104 |
position:absolute; |
| 105 |
<?php echo is_rtl() ? 'right' : 'left'; ?>:0; |
| 106 |
top:0 |
| 107 |
} |
| 108 |
.plugin-revision-wrp:hover .plugin-revisions-vers{ |
| 109 |
display:block; |
| 110 |
min-width:200px; |
| 111 |
min-width:max-content; |
| 112 |
background:#fff; |
| 113 |
margin-top:15px; |
| 114 |
padding:10px 10px |
| 115 |
} |
| 116 |
.plugin-revision-wrp:hover .plugin-revisions-vers a{ |
| 117 |
display:block; |
| 118 |
border-bottom:1px dashed; |
| 119 |
margin-bottom:5px; |
| 120 |
background-image:url(<?php echo esc_url( PLUGIN_REVISIONS_PLUGIN_URL ); ?>/admin/assets/images/ajax-loader.gif ); |
| 121 |
background-position:-9999px -9999px; |
| 122 |
background-repeat:no-repeat; |
| 123 |
background-size:16px 16px |
| 124 |
} |
| 125 |
</style> |
| 126 |
<?php |
| 127 |
} |
| 128 |
} ); |
| 129 |
add_action( 'admin_footer',function(){ |
| 130 |
global $pagenow; |
| 131 |
if( $pagenow && 'plugins.php' === sanitize_text_field( $pagenow ) ){ |
| 132 |
wp_nonce_field( 'plugin_reviews_restore_version','plugin_reviews_restore_version' ); |
| 133 |
?> |
| 134 |
<script id="plugin-revisions-js"> |
| 135 |
function eos_plugin_revisions(){ |
| 136 |
var as = document.getElementsByClassName('plugin-revision-action'),n=0,req = new XMLHttpRequest(),fd=new FormData(),nonce=document.getElementById('plugin_reviews_restore_version').value; |
| 137 |
for(n;n<as.length;++n){ |
| 138 |
as[n].addEventListener('click',function(e){ |
| 139 |
e.preventDefault(); |
| 140 |
this.style.backgroundPosition = 'center center'; |
| 141 |
for(var k=0;k<as.length;++k){ |
| 142 |
as[k].style.pointerEvents = 'none'; |
| 143 |
} |
| 144 |
fd.append("dir",this.dataset.dir); |
| 145 |
fd.append("parent_plugin",this.dataset.parent_plugin); |
| 146 |
fd.append("nonce",nonce); |
| 147 |
req.open("POST","<?php echo esc_js( admin_url( 'admin-ajax.php' ) ); ?>" + '?action=eos_plugin_reviews_restore_version',true); |
| 148 |
req.send(fd); |
| 149 |
return false; |
| 150 |
}); |
| 151 |
} |
| 152 |
req.onload = function(e) { |
| 153 |
for(n;n<as.length;++n){ |
| 154 |
as[n].style.backgroundPosition = '-9999px -9999px'; |
| 155 |
} |
| 156 |
if(this.readyState === 4) { |
| 157 |
window.location.reload(); |
| 158 |
} |
| 159 |
else{ |
| 160 |
alert('Something went wrong!'); |
| 161 |
} |
| 162 |
return false; |
| 163 |
}; |
| 164 |
return false; |
| 165 |
} |
| 166 |
eos_plugin_revisions(); |
| 167 |
</script> |
| 168 |
<?php |
| 169 |
} |
| 170 |
if( $pagenow && 'plugin-editor.php' === sanitize_text_field( $pagenow ) ){ |
| 171 |
$key = eos_plugin_revision_key(); |
| 172 |
?> |
| 173 |
<script id="plugin-revisions-file-editor-js"> |
| 174 |
function eos_plugin_revisions_fild_editor(){ |
| 175 |
var os=document.getElementById('plugin').getElementsByTagName('option'),n=0,ver=''; |
| 176 |
for(n;n<os.length;++n){ |
| 177 |
if(os[n].value.indexOf("<?php echo esc_js( $key ); ?>") > 0){ |
| 178 |
os[n].innerHTML += ' ' + os[n].value.split('-ver-')[0].split('-')[2]; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
eos_plugin_revisions_fild_editor(); |
| 183 |
</script> |
| 184 |
<?php |
| 185 |
} |
| 186 |
} ); |
| 187 |
//Return revision key |
| 188 |
function eos_plugin_revision_key(){ |
| 189 |
$opts = get_site_option( 'plugin_revisions' ); |
| 190 |
if( $opts && isset( $opts['time' ] ) ){ |
| 191 |
$key = substr( md5( sanitize_text_field( $opts['time' ] ) ),0,8 ); |
| 192 |
return $key; |
| 193 |
} |
| 194 |
else{ |
| 195 |
$time = time(); |
| 196 |
$opts['time'] = $time; |
| 197 |
update_site_option( 'plugin_revisions',$opts ); |
| 198 |
return substr( md5( $time ),0,8 ); |
| 199 |
} |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
//Remove all plugin revisions |
| 204 |
function eos_plugin_revisions_remove_versions( $N = false,$plugin_name = false ){ |
| 205 |
$key = eos_plugin_revision_key(); |
| 206 |
$all_dirs = eos_plugin_revisions_scandir( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) ); |
| 207 |
global $wp_filesystem; |
| 208 |
if( empty( $wp_filesystem ) ){ |
| 209 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 210 |
WP_Filesystem(); |
| 211 |
} |
| 212 |
if( $all_dirs && !empty( $all_dirs ) ){ |
| 213 |
foreach( $all_dirs as $all_dir ){ |
| 214 |
if( substr( $all_dir,-strlen( $plugin_name ),strlen( $plugin_name ) ) !== $plugin_name ){ |
| 215 |
unset( $all_dirs[array_search( $all_dir,$all_dirs )] ); |
| 216 |
} |
| 217 |
} |
| 218 |
$n = 0; |
| 219 |
foreach( $all_dirs as $dir ){ |
| 220 |
if( false !== strpos( $dir,'pr-'.$key.'-' ) ){ |
| 221 |
if( $N && $n < ( count( $all_dirs ) - absint( $N ) ) ){ |
| 222 |
$result = $wp_filesystem->delete( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ).'/'.$dir,true ); |
| 223 |
} |
| 224 |
} |
| 225 |
++$n; |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
function eos_plugin_revisions_scandir( $dir ) { |
| 231 |
$ignored = array('.', '..', '.svn', '.htaccess'); |
| 232 |
$files = array(); |
| 233 |
foreach( scandir( $dir ) as $file ){ |
| 234 |
if( in_array( $file,$ignored ) ) continue; |
| 235 |
$files[$file] = filemtime( $dir.'/'.$file ); |
| 236 |
} |
| 237 |
asort( $files ); |
| 238 |
$files = array_keys( $files ); |
| 239 |
return ( $files ) ? $files : false; |
| 240 |
} |
| 241 |
|