PluginProbe
PlugVersions – Easily roll back to previous versions of your plugins. / 0.0.2
PlugVersions – Easily roll back to previous versions of your plugins. v0.0.2
0.0.6.RC-1 0.0.7 0.0.8 0.1.0 0.2.0 0.2.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6
plugversions / admin / pr-admin.php

pr-admin.php in PlugVersions – Easily roll back to previous versions of your plugins. 0.0.2, at admin/pr-admin.php

253 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if( false !== strpos( $dir,'pr-'.$key.'-' ) && substr( $dir,-strlen( $plugin ),strlen( $plugin ) ) === $plugin ){
74 $ver = str_replace( 'pr-'.$key.'-','',$dir );
75 $verArr = explode( '-ver-',$ver );
76 $ver = $verArr[0];
77 $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> ';
78 }
79 }
80 if( '' !== $new_action_links ){
81 $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>';
82 }
83 }
84 return $actions;
85 },10,4 );
86 }
87 }
88 }
89 return $plugins;
90 } );
91
92 add_action( 'admin_head',function(){
93 global $pagenow;
94 if( $pagenow && 'plugins.php' === sanitize_text_field( $pagenow ) ){
95 ?>
96 <style id="plugin-revisions-css" type="text/css">
97 .plugin-revision-wrp{
98 position:relative
99 }
100 .plugin-revisions-vers{
101 display:none;
102 position:absolute;
103 <?php echo is_rtl() ? 'right' : 'left'; ?>:0;
104 top:0
105 }
106 .plugin-revision-wrp:hover .plugin-revisions-vers{
107 display:block;
108 min-width:200px;
109 min-width:max-content;
110 background:#fff;
111 margin-top:15px;
112 padding:10px 10px
113 }
114 .plugin-revision-wrp:hover .plugin-revisions-vers a{
115 display:block;
116 border-bottom:1px dashed;
117 margin-bottom:5px;
118 background-image:url(<?php echo esc_url( PLUGIN_REVISIONS_PLUGIN_URL ); ?>/admin/assets/images/ajax-loader.gif );
119 background-position:-9999px -9999px;
120 background-repeat:no-repeat;
121 background-size:16px 16px
122 }
123 </style>
124 <?php
125 }
126 } );
127 add_action( 'admin_footer',function(){
128 global $pagenow;
129 if( $pagenow && 'plugins.php' === sanitize_text_field( $pagenow ) ){
130 wp_nonce_field( 'plugin_reviews_restore_version','plugin_reviews_restore_version' );
131 ?>
132 <script id="plugin-revisions-js">
133 function eos_plugin_revisions(){
134 var as = document.getElementsByClassName('plugin-revision-action'),n=0,req = new XMLHttpRequest(),fd=new FormData(),nonce=document.getElementById('plugin_reviews_restore_version').value;
135 for(n;n<as.length;++n){
136 as[n].addEventListener('click',function(e){
137 e.preventDefault();
138 this.style.backgroundPosition = 'center center';
139 for(var k=0;k<as.length;++k){
140 as[k].style.pointerEvents = 'none';
141 }
142 fd.append("dir",this.dataset.dir);
143 fd.append("parent_plugin",this.dataset.parent_plugin);
144 fd.append("nonce",nonce);
145 req.open("POST","<?php echo esc_js( admin_url( 'admin-ajax.php' ) ); ?>" + '?action=eos_plugin_reviews_restore_version',true);
146 req.send(fd);
147 return false;
148 });
149 }
150 req.onload = function(e) {
151 for(n;n<as.length;++n){
152 as[n].style.backgroundPosition = '-9999px -9999px';
153 }
154 if(this.readyState === 4) {
155 window.location.reload();
156 }
157 else{
158 alert('Something went wrong!');
159 }
160 return false;
161 };
162 return false;
163 }
164 eos_plugin_revisions();
165 </script>
166 <?php
167 }
168 if( $pagenow && 'plugin-editor.php' === sanitize_text_field( $pagenow ) ){
169 $key = eos_plugin_revision_key();
170 ?>
171 <script id="plugin-revisions-file-editor-js">
172 function eos_plugin_revisions_fild_editor(){
173 var os=document.getElementById('plugin').getElementsByTagName('option'),n=0,ver='';
174 for(n;n<os.length;++n){
175 if(os[n].value.indexOf("<?php echo esc_js( $key ); ?>") > 0){
176 os[n].innerHTML += ' ' + os[n].value.split('-ver-')[0].split('-')[2];
177 }
178 }
179 }
180 eos_plugin_revisions_fild_editor();
181 </script>
182 <?php
183 }
184 } );
185 //Return revision key
186 function eos_plugin_revision_key(){
187 $opts = get_site_option( 'plugin_revisions' );
188 if( $opts && isset( $opts['time' ] ) ){
189 $key = substr( md5( sanitize_text_field( $opts['time' ] ) ),0,8 );
190 return $key;
191 }
192 else{
193 $time = time();
194 $opts['time'] = $time;
195 update_site_option( 'plugin_revisions',$opts );
196 return substr( md5( $time ),0,8 );
197 }
198 return false;
199 }
200
201 //Remove all plugin revisions
202 function eos_plugin_revisions_remove_versions( $N = false,$plugin_name = false ){
203 $key = eos_plugin_revision_key();
204 $all_dirs = eos_plugin_revisions_scandir( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) );
205 global $wp_filesystem;
206 if( empty( $wp_filesystem ) ){
207 require_once ABSPATH . 'wp-admin/includes/file.php';
208 WP_Filesystem();
209 }
210 if( $all_dirs && !empty( $all_dirs ) ){
211 foreach( $all_dirs as $all_dir ){
212 if( substr( $all_dir,-strlen( $plugin_name ),strlen( $plugin_name ) ) !== $plugin_name ){
213 unset( $all_dirs[array_search( $all_dir,$all_dirs )] );
214 }
215 }
216 $n = 0;
217 foreach( $all_dirs as $dir ){
218 if( false !== strpos( $dir,'pr-'.$key.'-' ) ){
219 if( $N && $n < ( count( $all_dirs ) - absint( $N ) ) ){
220 $result = $wp_filesystem->delete( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ).'/'.$dir,true );
221 }
222 }
223 ++$n;
224 }
225 }
226 }
227
228 function eos_plugin_revisions_scandir( $dir ) {
229 $ignored = array('.', '..', '.svn', '.htaccess');
230 $files = array();
231 foreach( scandir( $dir ) as $file ){
232 if( in_array( $file,$ignored ) ) continue;
233 $files[$file] = filemtime( $dir.'/'.$file );
234 }
235 asort( $files );
236 $files = array_keys( $files );
237 return ( $files ) ? $files : false;
238 }
239
240 add_filter( 'site_transient_update_plugins',function( $obj ){
241 if( isset( $obj ) && is_object( $obj ) && isset( $obj->response ) ){
242 $response = $obj->response;
243 $key = eos_plugin_revision_key();
244 foreach( $response as $p => $arr ){
245 if( false !== strpos( $p,'pr-'.$key.'-' ) && isset( $response[$p] ) ){
246 unset( $response[$p] );
247 }
248 }
249 $obj->response = $response;
250 }
251 return $obj;
252 } );
253