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