PluginProbe
PlugVersions – Easily roll back to previous versions of your plugins. / 0.0.4
PlugVersions – Easily roll back to previous versions of your plugins. v0.0.4
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.4, at admin/pr-admin.php

284 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $destination = str_replace( dirname( $plugin ),'pr-'.$key.'-'.sanitize_option( 'upload_path',$version ).'-ver-'.dirname( $plugin ),sanitize_option( 'upload_path',plugin_dir_path( $path )) );
49 if( !is_dir( $destination ) ){
50 global $wp_filesystem;
51 if( empty( $wp_filesystem ) ){
52 require_once ( ABSPATH .'/wp-admin/includes/file.php' );
53 WP_Filesystem();
54 }
55 $wp_filesystem->mkdir( $destination );
56 }
57 if( !empty( $wp_filesystem ) && $wp_filesystem->is_dir( $destination ) ){
58 $result = copy_dir( plugin_dir_path( $path ),$destination );
59 eos_plugin_revisions_remove_versions( apply_filters( 'max_plugin_revisions',4 ),$plugin_name );
60 }
61 }
62 }
63 }
64 }
65 }
66 return $options;
67 },10,4 );
68
69 add_filter( 'all_plugins',function( $plugins ){
70 /**
71 * Remove plugin revisions from plugins table in the page wp-admin/plugins.php
72 *
73 * @since 0.0.1
74 */
75 $key = eos_plugin_revision_key();
76 if( $key ){
77 foreach( $plugins as $plugin => $arr ){
78 if( false !== strpos( $plugin,'pr-'.$key.'-' ) ){
79 unset( $plugins[$plugin] );
80 $parent_plugin = str_replace( 'pr-'.$key.'-','',$plugin );
81 $parent_pluginArr = explode( '-ver-',$parent_plugin );
82 $parent_plugin = isset( $parent_pluginArr[1] ) ? $parent_pluginArr[1] : $parent_plugin;
83 add_filter( 'plugin_action_links_'.$parent_plugin,function( $actions, $plugin_file, $plugin_data, $context ){
84 if( !current_user_can( 'activate_plugin' ) ) return $actions;
85 $key = eos_plugin_revision_key();
86 $all_dirs = scandir( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) );
87 if( $all_dirs && !empty( $all_dirs ) ){
88 $new_action_links = '';
89 $plugin = dirname( $plugin_file );
90 foreach( $all_dirs as $dir ){
91 if( false !== strpos( $dir,'pr-'.$key.'-' ) && substr( $dir,-strlen( $plugin ),strlen( $plugin ) ) === $plugin ){
92 $ver = str_replace( 'pr-'.$key.'-','',$dir );
93 $verArr = explode( '-ver-',$ver );
94 $ver = $verArr[0];
95 $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> ';
96 }
97 }
98 if( '' !== $new_action_links ){
99 $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>';
100 }
101 }
102 return $actions;
103 },10,4 );
104 }
105 }
106 }
107 return $plugins;
108 } );
109
110 add_action( 'admin_head',function(){
111 /**
112 * Add style to properly show the revisions on the page of plugins
113 *
114 * @since 0.0.1
115 */
116 global $pagenow;
117 if( $pagenow && 'plugins.php' === sanitize_text_field( $pagenow ) ){
118 ?>
119 <style id="plugin-revisions-css" type="text/css">
120 .plugin-revision-wrp{
121 position:relative
122 }
123 .plugin-revisions-vers{
124 display:none;
125 position:absolute;
126 <?php echo is_rtl() ? 'right' : 'left'; ?>:0;
127 top:0
128 }
129 .plugin-revision-wrp:hover .plugin-revisions-vers{
130 display:block;
131 min-width:200px;
132 min-width:max-content;
133 background:#fff;
134 margin-top:15px;
135 padding:10px 10px
136 }
137 .plugin-revision-wrp:hover .plugin-revisions-vers a{
138 display:block;
139 border-bottom:1px dashed;
140 margin-bottom:5px;
141 background-image:url(<?php echo esc_url( PLUGIN_REVISIONS_PLUGIN_URL ); ?>/admin/assets/images/ajax-loader.gif );
142 background-position:-9999px -9999px;
143 background-repeat:no-repeat;
144 background-size:16px 16px
145 }
146 </style>
147 <?php
148 }
149 } );
150
151 add_action( 'admin_footer',function(){
152 /**
153 * Add JS to restore a revision via Ajax
154 *
155 * @since 0.0.1
156 */
157 global $pagenow;
158 if( $pagenow && 'plugins.php' === sanitize_text_field( $pagenow ) ){
159 wp_nonce_field( 'plugin_reviews_restore_version','plugin_reviews_restore_version' );
160 ?>
161 <script id="plugin-revisions-js">
162 function eos_plugin_revisions(){
163 var as = document.getElementsByClassName('plugin-revision-action'),n=0,req = new XMLHttpRequest(),fd=new FormData(),nonce=document.getElementById('plugin_reviews_restore_version').value;
164 for(n;n<as.length;++n){
165 as[n].addEventListener('click',function(e){
166 e.preventDefault();
167 this.style.backgroundPosition = 'center center';
168 for(var k=0;k<as.length;++k){
169 as[k].style.pointerEvents = 'none';
170 }
171 fd.append("dir",this.dataset.dir);
172 fd.append("parent_plugin",this.dataset.parent_plugin);
173 fd.append("nonce",nonce);
174 req.open("POST","<?php echo esc_js( admin_url( 'admin-ajax.php' ) ); ?>" + '?action=eos_plugin_reviews_restore_version',true);
175 req.send(fd);
176 return false;
177 });
178 }
179 req.onload = function(e) {
180 for(n;n<as.length;++n){
181 as[n].style.backgroundPosition = '-9999px -9999px';
182 }
183 if(this.readyState === 4) {
184 window.location.reload();
185 }
186 else{
187 alert('Something went wrong!');
188 }
189 return false;
190 };
191 return false;
192 }
193 eos_plugin_revisions();
194 </script>
195 <?php
196 }
197 if( $pagenow && 'plugin-editor.php' === sanitize_text_field( $pagenow ) ){
198 $key = eos_plugin_revision_key();
199 ?>
200 <script id="plugin-revisions-file-editor-js">
201 function eos_plugin_revisions_fild_editor(){
202 var os=document.getElementById('plugin').getElementsByTagName('option'),n=0,ver='';
203 for(n;n<os.length;++n){
204 if(os[n].value.indexOf("<?php echo esc_js( $key ); ?>") > 0){
205 os[n].innerHTML += ' ' + os[n].value.split('-ver-')[0].split('-')[2];
206 }
207 }
208 }
209 eos_plugin_revisions_fild_editor();
210 </script>
211 <?php
212 }
213 } );
214
215 /**
216 * Remove all plugin revisions
217 *
218 * @since 0.0.1
219 */
220 function eos_plugin_revisions_remove_versions( $N = false,$plugin_name = false ){
221 $key = eos_plugin_revision_key();
222 $all_dirs = eos_plugin_revisions_scandir( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) );
223 global $wp_filesystem;
224 if( empty( $wp_filesystem ) ){
225 require_once ABSPATH . 'wp-admin/includes/file.php';
226 WP_Filesystem();
227 }
228 if( $all_dirs && !empty( $all_dirs ) ){
229 foreach( $all_dirs as $all_dir ){
230 if( substr( $all_dir,-strlen( $plugin_name ),strlen( $plugin_name ) ) !== $plugin_name ){
231 unset( $all_dirs[array_search( $all_dir,$all_dirs )] );
232 }
233 }
234 $n = 0;
235 foreach( $all_dirs as $dir ){
236 if( false !== strpos( $dir,'pr-'.$key.'-' ) ){
237 if( $N && $n < ( count( $all_dirs ) - absint( $N ) ) ){
238 $result = $wp_filesystem->delete( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ).'/'.$dir,true );
239 }
240 }
241 ++$n;
242 }
243 }
244 }
245
246 /**
247 * Remove all revisions of all the plugins
248 *
249 * @since 0.0.1
250 */
251 function eos_plugin_revisions_remove_all_versions(){
252 $key = eos_plugin_revision_key();
253 $all_dirs = eos_plugin_revisions_scandir( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) );
254 global $wp_filesystem;
255 if( empty( $wp_filesystem ) ){
256 require_once ABSPATH . 'wp-admin/includes/file.php';
257 WP_Filesystem();
258 }
259 if( $all_dirs && !empty( $all_dirs ) ){
260 foreach( $all_dirs as $dir ){
261 if( false !== strpos( $dir,'pr-'.$key.'-' ) ){
262 $result = $wp_filesystem->delete( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ).'/'.$dir,true );
263 }
264 }
265 }
266 }
267
268 /**
269 * Revisions scandir
270 *
271 * @since 0.0.1
272 */
273 function eos_plugin_revisions_scandir( $dir ) {
274 $ignored = array('.', '..', '.svn', '.htaccess');
275 $files = array();
276 foreach( scandir( $dir ) as $file ){
277 if( in_array( $file,$ignored ) ) continue;
278 $files[$file] = filemtime( $dir.'/'.$file );
279 }
280 asort( $files );
281 $files = array_keys( $files );
282 return ( $files ) ? $files : false;
283 }
284