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

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