class-setup.php
4.02 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* Class BackUpWordPress_Setup
*/
class HMBKP_Setup {
/**
* Defines the minimum version of WordPress required by BWP.
*/
const MIN_WP_VERSION = '3.9';
/**
* Defines the minimum version of PHP required by BWP.
*/
const MIN_PHP_VERSION = '5.3.2';
/**
* Setup the plugin defaults on activation
*/
public static function activate() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
// loads the translation files for the Error message in the wp_die call.
load_plugin_textdomain( 'backupwordpress', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
if ( ! self::meets_requirements() ) {
wp_die( self::get_notice_message(), __( 'BackUpWordPress', 'backupwordpress' ), array( 'back_link' => true ) );
}
// Run deactivate on activation in-case it was deactivated manually
self::deactivate();
}
/**
* Cleanup on plugin deactivation
*
* Removes options and clears all cron schedules
*/
public static function deactivate() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
self::delete_schedules();
self::delete_transients();
}
/**
* Deletes the backup schedule database entries and WP Cron entries.
*/
public static function delete_schedules() {
// Delete Cron schedules.
global $wpdb;
$schedules = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s", 'hmbkp_schedule_%' ) );
foreach ( array_map( array( 'self', 'trim_prefix' ), $schedules ) as $item ) {
wp_clear_scheduled_hook( 'hmbkp_schedule_hook', array( 'id' => $item ) );
}
}
public static function trim_prefix( $item ) {
return ltrim( $item, 'hmbkp_schedule_' );
}
/**
* Deletes the plugin's transients from the database.
*/
public static function delete_transients() {
// Delete all transients
$transients = array(
'hmbkp_plugin_data',
'hmbkp_directory_filesizes',
'hmbkp_directory_filesizes_running',
'hmbkp_wp_cron_test_beacon',
'hm_backdrop',
);
array_map( 'delete_transient', $transients );
}
/**
* Deactivate BackUpWordPress.
*/
public static function self_deactivate() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
if ( ! function_exists( 'deactivate_plugins' ) ) {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
deactivate_plugins( dirname( dirname( __FILE__ ) ) . '/backupwordpress.php' );
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
/**
* Determine if this WordPress install meets the minimum requirements for BWP to run.
*
* @return bool
*/
public static function meets_requirements() {
if ( false === self::is_supported_php_version() ) {
return false;
}
if ( false === self::is_supported_wp_version() ) {
return false;
}
return true;
}
/**
* Checks the current PHP version against the required version.
*
* @return bool 'Operator' parameter specified, returns a boolean.
*/
protected static function is_supported_php_version() {
return version_compare( phpversion(), self::MIN_PHP_VERSION, '>=' );
}
/**
* Checks the current WordPress version against the required version.
*
* @return bool 'Operator' parameter specified, returns a boolean.
*/
protected static function is_supported_wp_version() {
return version_compare( get_bloginfo( 'version' ), self::MIN_WP_VERSION, '>=' );
}
/**
* Displays a user friendly message in the WordPress admin.
*/
public static function display_admin_notices() {
echo '<div class="error"><p>' . self::get_notice_message() . '</p></div>';
}
/**
* Returns a localized user friendly error message.
*
* @return string
*/
public static function get_notice_message() {
return sprintf(
__( 'BackUpWordPress requires PHP version %1$s or later and WordPress version %2$s or later to run. It has not been activated. %3$s%4$s%5$sLearn more%6$s', 'backupwordpress' ),
self::MIN_PHP_VERSION,
self::MIN_WP_VERSION,
'<a href="',
'https://bwp.hmn.md/unsupported-php-version-error/',
'">',
'</a>'
);
}
}