PluginProbe
PlugVersions – Easily roll back to previous versions of your plugins. / 0.4.0
PlugVersions – Easily roll back to previous versions of your plugins. v0.4.0
0.3.1 0.4.0 0.3.0 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.4.0, at plugversions.php

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