PluginProbe
PlugVersions – Easily roll back to previous versions of your plugins. / 0.2.1
PlugVersions – Easily roll back to previous versions of your plugins. v0.2.1
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 / plugversions.php

plugversions.php in PlugVersions – Easily roll back to previous versions of your plugins. 0.2.1, at plugversions.php

136 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: PlugVersions
4 Description: it retains up to three versions when you update a plugin. It works also with premium and custom plugins.
5 Author: Jose Mortellaro
6 Author URI: https://josemortellaro.com
7 Plugin URI: https://josemortellaro.com
8 Domain Path: /languages/
9 Text Domain: plugversions
10 License: GPLv2 or later
11 License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
12 Version: 0.2.1
13 Requires at least: 4.9
14 Tested up to: 6.8
15 Requires PHP: 7.4
16 Tags: plugin, versions, revisions, restore, rollback, backup, management
17 */
18 /* This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
26 */
27 defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
28
29 //Definitions.
30 define( 'PLUGIN_REVISIONS_PLUGIN_DIR', untrailingslashit( dirname( __FILE__ ) ) );
31 define( 'PLUGIN_REVISIONS_PLUGIN_URL', untrailingslashit( plugins_url( '', __FILE__ ) ) );
32
33 if( is_admin() ){
34 require_once PLUGIN_REVISIONS_PLUGIN_DIR . '/admin/pr-admin.php';
35 }
36
37 add_filter( 'site_transient_update_plugins', function( $obj ) {
38 /**
39 * Remove plugin revisions from the update notifications
40 * This filter is applied to the site transient update plugins object.
41 * It checks if the object is set and is an object, then it iterates through the response array.
42 * If it finds a key that matches the plugin revision key, it removes that entry from the response.
43 *
44 * @since 0.0.1
45 */
46 if( isset( $obj ) && is_object( $obj ) && isset( $obj->response ) ) {
47 $response = $obj->response;
48 $key = eos_plugin_revision_key();
49 foreach( $response as $p => $arr ){
50 if( false !== strpos( $p,'pr-'.$key.'-' ) && isset( $response[$p] ) ) {
51 unset( $response[$p] );
52 }
53 }
54 $obj->response = $response;
55 }
56 return $obj;
57 } );
58
59 /**
60 * Return revision key
61 * This key is used to identify the plugin revisions.
62 * It is based on the current time and is sanitized to ensure it is safe for use.
63 * If the key does not exist, it creates a new one and saves it in the site options.
64 *
65 * @since 0.0.1
66 */
67 function eos_plugin_revision_key(){
68 $opts = get_site_option( 'plugin_revisions', array() );
69 if( $opts && isset( $opts['time' ] ) ){
70 $key = substr( md5( sanitize_text_field( $opts['time' ] ) ), 0, 8 );
71 return $key;
72 }
73 else{
74 $time = time();
75 $opts['time'] = $time;
76 update_site_option( 'plugin_revisions',$opts );
77 return substr( md5( $time ), 0, 8 );
78 }
79 return false;
80 }
81
82 /**
83 * Create a zip file using PclZip.
84 * This function creates a zip file from a specified directory using the PclZip library.
85 * It checks if the PclZip class exists, retrieves all files from the directory,
86 * and creates a zip file at the specified destination.
87 * If the zip creation fails, it returns false; otherwise, it returns true.
88 *
89 * @param string $destination The path where the zip file will be created.
90 * @param string $zip_dirname The directory name to be zipped.
91 * @return bool True on success, false on failure.
92 *
93 * @since 0.0.6
94 *
95 */
96 function eos_pv_create_zip_pclzip( $destination, $zip_dirname ) {
97 if( file_exists( ABSPATH . 'wp-admin/includes/class-pclzip.php' ) ) {
98 require_once( ABSPATH . 'wp-admin/includes/class-pclzip.php' );
99 if( class_exists( 'PclZip' ) ) {
100 $files = eos_pv_get_files( $destination );
101 if( $files ) {
102 $zip = new PclZip( str_replace( '/.zip', '.zip', $destination . '.zip' ) );
103 if ( defined( 'PCLZIP_OPT_REMOVE_PATH' ) ) {
104 $zip_created = $zip->create( $files, PCLZIP_OPT_REMOVE_PATH, WP_PLUGIN_DIR );
105 if( 0 == $zip_created ) {
106 // If the zip creation fails, return false
107 return false;
108 }
109 return true;
110 }
111 }
112 }
113 return false;
114 }
115 }
116
117 /**
118 * This function retrieves all files from a given directory path, including subdirectories.
119 * It uses the RecursiveDirectoryIterator and RecursiveIteratorIterator classes to traverse the directory structure.
120 * The function returns an array of file paths, with backslashes replaced by forward slashes for consistency.
121 *
122 * @since 0.0.6
123 */
124 function eos_pv_get_files( $path ) {
125 if( ! class_exists( 'RecursiveIteratorIterator' ) ) return false;
126 $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $path ), RecursiveIteratorIterator::LEAVES_ONLY );
127 $files_paths = array();
128 foreach ( $files as $name => $file ) {
129 if ( ! $file->isDir() ) {
130 $file_path = str_replace( '\\', '/', $file->getRealPath() );
131 $files_paths[] = $file_path;
132 }
133 }
134 return $files_paths;
135 }
136