/
wp-safe-updates.php
159 lines (131 loc) · 4.67 KB
/
wp-safe-updates.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* Plugin name: WP Safe Updates
* Plugin URI: https://github.com/anttiviljami/wp-safe-updates
* Description: Test WordPress plugin updates safely before applying them on the live site.
* Version: 1.2.2
* Author: @anttiviljami
* Author URI: https://github.com/anttiviljami
* License: GPLv3
* Text Domain: wp-safe-updates
*/
/** Copyright 2017 Antti Kuosmanen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 3, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( ! class_exists( 'Safe_Updates' ) ) :
class Safe_Updates {
public static $instance;
public $alt_heap;
public $update_logic;
public static $dropin_file = __DIR__ . '/db.php.txt';
public static $dropin_target = WP_CONTENT_DIR . '/db.php';
public static function init() {
if ( is_null( self::$instance ) ) {
self::$instance = new Safe_Updates();
}
return self::$instance;
}
private function __construct() {
global $wpdb;
// load textdomain for translations
add_action( 'plugins_loaded', array( $this, 'load_our_textdomain' ) );
if ( function_exists( 'currheap' ) && is_a( $wpdb, 'SafeUpdatesDB' ) ) {
// plugin is already configured
require_once 'lib/class-alternative-heap.php';
require_once 'lib/class-update-logic.php';
$this->alt_heap = Alternative_Heap::init();
$this->update_logic = Update_Logic::init();
if ( false !== currheap() ) {
add_action( 'plugins_loaded', array( $this, 'make_sure_we_are_first' ) );
}
} else {
// show a notice to prompt the user to configure WP Safe Updates
// @TODO: offer to do this automatically
add_action( 'admin_notices', array( $this, 'not_configured_notice' ) );
}
// configure custom dropin file
register_activation_hook( __FILE__, array( 'Safe_Updates', 'install_custom_dropin' ) );
// clear all heaps on uninstall and clean up files
register_uninstall_hook( __FILE__, array( 'Safe_Updates', 'uninstall_cleanup' ) );
}
/**
* When in an alternative heap, make sure this plugin is loaded first
*/
public function make_sure_we_are_first() {
$active_plugins = get_option( 'active_plugins' );
$this_plugin = basename( dirname( __FILE__ ) ) . '/' . basename( __FILE__ );
$priority = array_search( $this_plugin, $active_plugins, true );
if ( 0 !== $priority ) {
array_splice( $active_plugins, $priority, 1 );
array_unshift( $active_plugins, $this_plugin );
update_option( 'active_plugins', $active_plugins );
}
}
/**
* Shows a notice to prompt the user to configure WP Safe Updates
*/
public function not_configured_notice() {
?>
<div class="notice notice-warning is-dismissible">
<p>
<?php
echo wp_sprintf(
wp_kses(
// translators: %1$s: dropin filename %1$s dropin target
__( 'WP Safe Updates is not yet active. Please copy and rename the %1$s file from this plugin to %2$s',
'wp-safe-updates' ),
'<code>'
),
'<code>db.php.txt</code>',
'<code>wp-content/db.php</code>'
);
?>
<button type="button" class="notice-dismiss"></button>
</p>
</div>
<?php
}
/**
* Configure our custom db.php dropin
*/
public static function install_custom_dropin() {
if ( ! file_exists( self::$dropin_target ) ) {
copy( self::$dropin_file, self::$dropin_target );
}
}
/**
* Delete all alternative heap directories and tables on uninstall
*/
public static function uninstall_cleanup() {
require_once 'lib/class-alternative-heap.php';
$alt_heap = Alternative_Heap::init();
// Deleting all tmp plugins directories...
$alt_heap->delete_alt_plugins_dirs();
// Deleting all tmp tables...
$alt_heap->delete_tmp_wp_tables();
if (
file_exists( self::$dropin_target ) &&
hash_file( 'md5', self::$dropin_target ) === hash_file( 'md5', self::$dropin_file )
) {
unlink( self::$dropin_target );
}
}
/**
* Load our textdomain
*/
public static function load_our_textdomain() {
load_plugin_textdomain( 'wp-safe-updates', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
}
}
endif;
// init the plugin
$safe_updates = Safe_Updates::init();