| @@ -1,19 +1,33 @@ | ||
| 1 | 1 | <?php |
| 2 | -defined( 'PLUGIN_REVISIONS_PLUGIN_DIR' ) || exit; | |
| 2 | +/** | |
| 3 | + * It includes the code for the backend. | |
| 3 | 4 | |
| 5 | + * @package Plugversions | |
| 6 | + */ | |
| 7 | +defined( 'PLUGIN_REVISIONS_PLUGIN_DIR' ) || exit; // Exit if not accessed from Plugversions. | |
| 8 | + | |
| 4 | 9 | if( wp_doing_ajax() ){ |
| 5 | - require_once PLUGIN_REVISIONS_PLUGIN_DIR.'/admin/pr-ajax-admin.php'; | |
| 10 | + require_once PLUGIN_REVISIONS_PLUGIN_DIR . '/admin/pr-ajax-admin.php'; | |
| 6 | 11 | } |
| 7 | 12 | |
| 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() ){ | |
| 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() ){ | |
| 11 | 20 | update_site_option( 'plugin_revisions',array( 'time' => time() ) ); |
| 12 | 21 | } |
| 13 | 22 | } ); |
| 14 | 23 | |
| 15 | -add_filter( 'upgrader_package_options',function( $options){ | |
| 24 | +add_filter( 'upgrader_package_options',function( $options ) { | |
| 25 | + /** | |
| 26 | + * Update revisions after plugin update | |
| 27 | + * | |
| 28 | + * @since 0.0.1 | |
| 29 | + */ | |
| 16 | 30 | if( isset( $options['destination'] ) && isset( $options['hook_extra'] ) ){ |
| 17 | 31 | $hook_extra = $options['hook_extra']; |
| 18 | 32 | if( isset( $hook_extra['plugin'] ) ){ |
| 19 | 33 | $plugin = $hook_extra['plugin']; |
| @@ -52,9 +66,13 @@ | ||
| 52 | 66 | return $options; |
| 53 | 67 | },10,4 ); |
| 54 | 68 | |
| 55 | 69 | add_filter( 'all_plugins',function( $plugins ){ |
| 56 | - //Remove plugin revisions from plugins table in the page wp-admin/plugins.php | |
| 70 | + /** | |
| 71 | + * Remove plugin revisions from plugins table in the page wp-admin/plugins.php | |
| 72 | + * | |
| 73 | + * @since 0.0.1 | |
| 74 | + */ | |
| 57 | 75 | $key = eos_plugin_revision_key(); |
| 58 | 76 | if( $key ){ |
| 59 | 77 | foreach( $plugins as $plugin => $arr ){ |
| 60 | 78 | if( false !== strpos( $plugin,'pr-'.$key.'-' ) ){ |
| @@ -89,8 +107,13 @@ | ||
| 89 | 107 | return $plugins; |
| 90 | 108 | } ); |
| 91 | 109 | |
| 92 | 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 | + */ | |
| 93 | 116 | global $pagenow; |
| 94 | 117 | if( $pagenow && 'plugins.php' === sanitize_text_field( $pagenow ) ){ |
| 95 | 118 | ?> |
| 96 | 119 | <style id="plugin-revisions-css" type="text/css"> |
| @@ -123,9 +146,15 @@ | ||
| 123 | 146 | </style> |
| 124 | 147 | <?php |
| 125 | 148 | } |
| 126 | 149 | } ); |
| 150 | + | |
| 127 | 151 | add_action( 'admin_footer',function(){ |
| 152 | + /** | |
| 153 | + * Add JS to restore a revision via Ajax | |
| 154 | + * | |
| 155 | + * @since 0.0.1 | |
| 156 | + */ | |
| 128 | 157 | global $pagenow; |
| 129 | 158 | if( $pagenow && 'plugins.php' === sanitize_text_field( $pagenow ) ){ |
| 130 | 159 | wp_nonce_field( 'plugin_reviews_restore_version','plugin_reviews_restore_version' ); |
| 131 | 160 | ?> |
| @@ -181,25 +210,14 @@ | ||
| 181 | 210 | </script> |
| 182 | 211 | <?php |
| 183 | 212 | } |
| 184 | 213 | } ); |
| 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 | 214 | |
| 201 | -//Remove all plugin revisions | |
| 215 | +/** | |
| 216 | + * Remove all plugin revisions | |
| 217 | + * | |
| 218 | + * @since 0.0.1 | |
| 219 | + */ | |
| 202 | 220 | function eos_plugin_revisions_remove_versions( $N = false,$plugin_name = false ){ |
| 203 | 221 | $key = eos_plugin_revision_key(); |
| 204 | 222 | $all_dirs = eos_plugin_revisions_scandir( dirname( PLUGIN_REVISIONS_PLUGIN_DIR ) ); |
| 205 | 223 | global $wp_filesystem; |
| @@ -224,8 +242,35 @@ | ||
| 224 | 242 | } |
| 225 | 243 | } |
| 226 | 244 | } |
| 227 | 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 | + */ | |
| 228 | 273 | function eos_plugin_revisions_scandir( $dir ) { |
| 229 | 274 | $ignored = array('.', '..', '.svn', '.htaccess'); |
| 230 | 275 | $files = array(); |
| 231 | 276 | foreach( scandir( $dir ) as $file ){ |
| @@ -235,18 +280,4 @@ | ||
| 235 | 280 | asort( $files ); |
| 236 | 281 | $files = array_keys( $files ); |
| 237 | 282 | return ( $files ) ? $files : false; |
| 238 | 283 | } |
| 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 | -} ); | |