| 1 |
<?php |
| 2 |
/** |
| 3 |
* It includes the code for the backend. |
| 4 |
|
| 5 |
* @package Plugversions |
| 6 |
*/ |
| 7 |
defined( 'PLUGIN_REVISIONS_PLUGIN_DIR' ) || exit; // Exit if not accessed from Plugversions. |
| 8 |
|
| 9 |
if( wp_doing_ajax() ){ |
| 10 |
require_once PLUGIN_REVISIONS_PLUGIN_DIR . '/admin/pr-ajax-admin.php'; |
| 11 |
} |
| 12 |
|
| 13 |
register_activation_hook( __FILE__, function() { |
| 14 |
/** |
| 15 |
* Actions triggered after plugin activation or after a new site of a multisite installation is created |
| 16 |
* |
| 17 |
* @since 0.0.1 |
| 18 |
*/ |
| 19 |
if( ! eos_plugin_revision_key() ){ |
| 20 |
update_site_option( 'plugin_revisions',array( 'time' => time() ) ); |
| 21 |
} |
| 22 |
} ); |
| 23 |
|
| 24 |
add_filter( 'upgrader_package_options',function( $options ) { |
| 25 |
/** |
| 26 |
* Update revisions after plugin update |
| 27 |
* |
| 28 |
* @since 0.0.1 |
| 29 |
*/ |
| 30 |
if( isset( $options['destination'] ) && isset( $options['hook_extra'] ) ){ |
| 31 |
$hook_extra = $options['hook_extra']; |
| 32 |
if( isset( $hook_extra['plugin'] ) ){ |
| 33 |
$plugin = $hook_extra['plugin']; |
| 34 |
$plugin_name = dirname( $plugin ); |
| 35 |
$path = sanitize_option( 'upload_path',$options['destination'].'/'.$plugin ); |
| 36 |
if( file_exists( $path ) ){ |
| 37 |
$plugin_data = get_plugin_data( $path ); |
| 38 |
$version = $plugin_data['Version']; |
| 39 |
if( !class_exists( 'WP_Upgrader' ) ){ |
| 40 |
if( file_exists( ABSPATH.'wp-admin/includes/class-wp-upgrader.php' ) ){ |
| 41 |
require_once ABSPATH.'/wp-admin/includes/class-wp-upgrader.php'; |
| 42 |
} |
| 43 |
} |
| 44 |
if( class_exists( 'WP_Upgrader' ) ){ |
| 45 |
$upgrader = new WP_Upgrader(); |
| 46 |
$key = eos_plugin_revision_key(); |
| 47 |
if( $key ){ |
| 48 |
$zip_dirname = 'pr-'.$key.'-'.sanitize_option( 'upload_path',$version ).'-ver-'.dirname( $plugin ); |
| 49 |
$destination = str_replace( dirname( $plugin ), $zip_dirname, sanitize_option( 'upload_path',plugin_dir_path( $path )) ); |
| 50 |
if( !is_dir( $destination ) ){ |
| 51 |
global $wp_filesystem; |
| 52 |
if( empty( $wp_filesystem ) ){ |
| 53 |
require_once ( ABSPATH .'/wp-admin/includes/file.php' ); |
| 54 |
WP_Filesystem(); |
| 55 |
} |
| 56 |
$wp_filesystem->mkdir( $destination ); |
| 57 |
} |
| 58 |
if( !empty( $wp_filesystem ) && $wp_filesystem->is_dir( $destination ) ){ |
| 59 |
$result = copy_dir( plugin_dir_path( $path ),$destination ); |
| 60 |
$zip_dirname .= '.zip'; |
| 61 |
if( eos_pv_create_zip_pclzip( $destination, $zip_dirname ) ) { |
| 62 |
// If zipped archive created delete the unzipped directory. |
| 63 |
$wp_filesystem->delete( $destination, true ); |
| 64 |
} |
| 65 |
eos_plugin_revisions_remove_versions( apply_filters( 'max_plugin_revisions',4 ),$plugin_name ); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
return $options; |
| 73 |
},10,4 ); |
| 74 |
|
| 75 |
add_filter( 'all_plugins',function( $plugins ){ |
| 76 |
/** |
| 77 |
* Remove plugin revisions from plugins table in the page wp-admin/plugins.php |
| 78 |
* Add link to restore previous unzipped versions for backward compatibility |
| 79 |
* |
| 80 |
* @since 0.0.1 |
| 81 |
*/ |
| 82 |
$key = eos_plugin_revision_key(); |
| 83 |
if( $key ){ |
| 84 |
foreach( $plugins as $plugin => $arr ){ |
| 85 |
if( false !== strpos( $plugin,'pr-'.$key.'-' ) ){ |
| 86 |
unset( $plugins[$plugin] ); |
| 87 |
$parent_plugin = str_replace( 'pr-'.$key.'-','',$plugin ); |
| 88 |
$parent_pluginArr = explode( '-ver-',$parent_plugin ); |
| 89 |
$parent_plugin = isset( $parent_pluginArr[1] ) ? $parent_pluginArr[1] : $parent_plugin; |
| 90 |
add_filter( 'plugin_action_links_'.$parent_plugin,function( $actions, $plugin_file, $plugin_data, $context ){ |
| 91 |
if( !current_user_can( 'activate_plugin' ) ) return $actions; |
| 92 |
$key = eos_plugin_revision_key(); |
| 93 |
$all_dirs = scandir( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) ); |
| 94 |
if( $all_dirs && !empty( $all_dirs ) ){ |
| 95 |
$new_action_links = ''; |
| 96 |
$plugin = dirname( $plugin_file ); |
| 97 |
foreach( $all_dirs as $dir ){ |
| 98 |
if( false !== strpos( $dir,'pr-'.$key.'-' ) && substr( $dir,-strlen( $plugin ),strlen( $plugin ) ) === $plugin ){ |
| 99 |
$ver = str_replace( 'pr-'.$key.'-','',$dir ); |
| 100 |
$verArr = explode( '-ver-',$ver ); |
| 101 |
$ver = $verArr[0]; |
| 102 |
$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> '; |
| 103 |
} |
| 104 |
} |
| 105 |
if( '' !== $new_action_links ){ |
| 106 |
$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>'; |
| 107 |
} |
| 108 |
} |
| 109 |
return $actions; |
| 110 |
},10,4 ); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
return $plugins; |
| 115 |
} ); |
| 116 |
|
| 117 |
add_action( 'admin_head',function(){ |
| 118 |
/** |
| 119 |
* Add style to properly show the revisions on the page of plugins |
| 120 |
* |
| 121 |
* @since 0.0.1 |
| 122 |
*/ |
| 123 |
global $pagenow; |
| 124 |
if( $pagenow && 'plugins.php' === sanitize_text_field( $pagenow ) ){ |
| 125 |
?> |
| 126 |
<style id="plugin-revisions-css" type="text/css"> |
| 127 |
.plugin-revision-wrp{ |
| 128 |
position:relative |
| 129 |
} |
| 130 |
.plugin-revisions-vers{ |
| 131 |
display:none; |
| 132 |
position:absolute; |
| 133 |
<?php echo is_rtl() ? 'right' : 'left'; ?>:0; |
| 134 |
top:0 |
| 135 |
} |
| 136 |
.plugin-revision-wrp:hover .plugin-revisions-vers{ |
| 137 |
display:block; |
| 138 |
min-width:200px; |
| 139 |
min-width:max-content; |
| 140 |
background:#fff; |
| 141 |
margin-top:15px; |
| 142 |
padding:10px 10px |
| 143 |
} |
| 144 |
.plugin-revision-wrp:hover .plugin-revisions-vers a{ |
| 145 |
display:block; |
| 146 |
border-bottom:1px dashed; |
| 147 |
margin-bottom:5px; |
| 148 |
background-image:url(<?php echo esc_url( PLUGIN_REVISIONS_PLUGIN_URL ); ?>/admin/assets/images/ajax-loader.gif ); |
| 149 |
background-position:-9999px -9999px; |
| 150 |
background-repeat:no-repeat; |
| 151 |
background-size:16px 16px |
| 152 |
} |
| 153 |
</style> |
| 154 |
<?php |
| 155 |
} |
| 156 |
} ); |
| 157 |
|
| 158 |
add_action( 'admin_footer',function(){ |
| 159 |
/** |
| 160 |
* Add JS to restore a revision via Ajax |
| 161 |
* |
| 162 |
* @since 0.0.1 |
| 163 |
*/ |
| 164 |
global $pagenow; |
| 165 |
if( $pagenow && 'plugins.php' === sanitize_text_field( $pagenow ) ){ |
| 166 |
wp_nonce_field( 'plugin_reviews_restore_version','plugin_reviews_restore_version' ); |
| 167 |
?> |
| 168 |
<script id="plugin-revisions-js"> |
| 169 |
function eos_plugin_revisions(){ |
| 170 |
var as = document.getElementsByClassName('plugin-revision-action'),n=0,req = new XMLHttpRequest(),fd=new FormData(),nonce=document.getElementById('plugin_reviews_restore_version').value; |
| 171 |
for(n;n<as.length;++n){ |
| 172 |
as[n].addEventListener('click',function(e){ |
| 173 |
e.preventDefault(); |
| 174 |
this.style.backgroundPosition = 'center center'; |
| 175 |
for(var k=0;k<as.length;++k){ |
| 176 |
as[k].style.pointerEvents = 'none'; |
| 177 |
} |
| 178 |
fd.append("dir",this.dataset.dir); |
| 179 |
fd.append("parent_plugin",this.dataset.parent_plugin); |
| 180 |
fd.append("nonce",nonce); |
| 181 |
req.open("POST","<?php echo esc_js( admin_url( 'admin-ajax.php' ) ); ?>" + '?action=eos_plugin_reviews_restore_version',true); |
| 182 |
req.send(fd); |
| 183 |
return false; |
| 184 |
}); |
| 185 |
} |
| 186 |
req.onload = function(e) { |
| 187 |
for(n;n<as.length;++n){ |
| 188 |
as[n].style.backgroundPosition = '-9999px -9999px'; |
| 189 |
} |
| 190 |
if(this.readyState === 4) { |
| 191 |
window.location.reload(); |
| 192 |
} |
| 193 |
else{ |
| 194 |
alert('Something went wrong!'); |
| 195 |
} |
| 196 |
return false; |
| 197 |
}; |
| 198 |
return false; |
| 199 |
} |
| 200 |
eos_plugin_revisions(); |
| 201 |
</script> |
| 202 |
<?php |
| 203 |
} |
| 204 |
if( $pagenow && 'plugin-editor.php' === sanitize_text_field( $pagenow ) ){ |
| 205 |
$key = eos_plugin_revision_key(); |
| 206 |
?> |
| 207 |
<script id="plugin-revisions-file-editor-js"> |
| 208 |
function eos_plugin_revisions_fild_editor(){ |
| 209 |
var os=document.getElementById('plugin').getElementsByTagName('option'),n=0,ver=''; |
| 210 |
for(n;n<os.length;++n){ |
| 211 |
if(os[n].value.indexOf("<?php echo esc_js( $key ); ?>") > 0){ |
| 212 |
os[n].innerHTML += ' ' + os[n].value.split('-ver-')[0].split('-')[2]; |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
eos_plugin_revisions_fild_editor(); |
| 217 |
</script> |
| 218 |
<?php |
| 219 |
} |
| 220 |
} ); |
| 221 |
|
| 222 |
/** |
| 223 |
* Remove all plugin revisions |
| 224 |
* |
| 225 |
* @since 0.0.1 |
| 226 |
*/ |
| 227 |
function eos_plugin_revisions_remove_versions( $N = false,$plugin_name = false ){ |
| 228 |
$key = eos_plugin_revision_key(); |
| 229 |
$all_dirs = eos_plugin_revisions_scandir( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) ); |
| 230 |
global $wp_filesystem; |
| 231 |
if( empty( $wp_filesystem ) ){ |
| 232 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 233 |
WP_Filesystem(); |
| 234 |
} |
| 235 |
if( $all_dirs && !empty( $all_dirs ) ){ |
| 236 |
foreach( $all_dirs as $all_dir ){ |
| 237 |
if( substr( $all_dir,-strlen( $plugin_name ),strlen( $plugin_name ) ) !== $plugin_name ){ |
| 238 |
unset( $all_dirs[array_search( $all_dir,$all_dirs )] ); |
| 239 |
} |
| 240 |
} |
| 241 |
$n = 0; |
| 242 |
foreach( $all_dirs as $dir ){ |
| 243 |
if( false !== strpos( $dir,'pr-'.$key.'-' ) ){ |
| 244 |
if( $N && $n < ( count( $all_dirs ) - absint( $N ) ) ){ |
| 245 |
$result = $wp_filesystem->delete( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ).'/'.$dir,true ); |
| 246 |
} |
| 247 |
} |
| 248 |
++$n; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Remove all revisions of all the plugins |
| 255 |
* |
| 256 |
* @since 0.0.1 |
| 257 |
*/ |
| 258 |
function eos_plugin_revisions_remove_all_versions(){ |
| 259 |
$key = eos_plugin_revision_key(); |
| 260 |
$all_dirs = eos_plugin_revisions_scandir( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) ); |
| 261 |
global $wp_filesystem; |
| 262 |
if( empty( $wp_filesystem ) ){ |
| 263 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 264 |
WP_Filesystem(); |
| 265 |
} |
| 266 |
if( $all_dirs && !empty( $all_dirs ) ){ |
| 267 |
foreach( $all_dirs as $dir ){ |
| 268 |
if( false !== strpos( $dir,'pr-'.$key.'-' ) ){ |
| 269 |
$result = $wp_filesystem->delete( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ).'/'.$dir,true ); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Revisions scandir |
| 277 |
* |
| 278 |
* @since 0.0.1 |
| 279 |
*/ |
| 280 |
function eos_plugin_revisions_scandir( $dir ) { |
| 281 |
$ignored = array('.', '..', '.svn', '.htaccess'); |
| 282 |
$files = array(); |
| 283 |
foreach( scandir( $dir ) as $file ){ |
| 284 |
if( in_array( $file,$ignored ) ) continue; |
| 285 |
$files[$file] = filemtime( $dir.'/'.$file ); |
| 286 |
} |
| 287 |
asort( $files ); |
| 288 |
$files = array_keys( $files ); |
| 289 |
return ( $files ) ? $files : false; |
| 290 |
} |
| 291 |
|
| 292 |
add_action( 'admin_init', 'eos_pv_restore_revision_links' ); |
| 293 |
/** |
| 294 |
* Add links to restore previous revisions from zipped plugins. |
| 295 |
* |
| 296 |
* @since 0.0.6 |
| 297 |
*/ |
| 298 |
function eos_pv_restore_revision_links() { |
| 299 |
$key = eos_plugin_revision_key(); |
| 300 |
if( $key ){ |
| 301 |
$all_plugin_files = scandir( WP_PLUGIN_DIR ); |
| 302 |
if( $all_plugin_files && is_array( $all_plugin_files ) && ! empty( $all_plugin_files ) ) { |
| 303 |
require_once PLUGIN_REVISIONS_PLUGIN_DIR . '/admin/classes/class-plugversions-restoring-link.php'; |
| 304 |
foreach( $all_plugin_files as $plugin ) { |
| 305 |
if( 'zip' === pathinfo( $plugin, PATHINFO_EXTENSION ) ) { |
| 306 |
if( false !== strpos( $plugin, 'pr-' . $key .'-' ) && false !== strpos( $plugin, '-ver-' ) ) { |
| 307 |
$arr = explode( 'ver-', $plugin ); |
| 308 |
$arr = explode( '-', $arr[0] ); |
| 309 |
if( 'pr' === $arr[0] && isset( $arr[1] ) && $key === $arr[1] && isset( $arr[2] ) ) { |
| 310 |
$version = $arr[2]; |
| 311 |
$plugin_name = str_replace( array( 'pr-' . $key . '-' . $version . '-ver-', '.zip' ), array( '', '' ), $plugin ); |
| 312 |
if( is_dir( WP_PLUGIN_DIR . '/' . $plugin_name ) ) { |
| 313 |
$link = new PlugVersions_Restoring_Link( $key, $version, $plugin, $plugin_name ); |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
|