-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvanced-backwpup-s3-destinations.php
272 lines (240 loc) · 12 KB
/
advanced-backwpup-s3-destinations.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* Plugin name: Advanced S3 Destinations for BackWPup
* Plugin URI: https://gitlab.com/noplanman/advanced-backwpup-s3-destinations
* Description: Easily add custom S3 destinations for <a href="https://backwpup.com">BackWPup</a>.
* Version: 2.0.0
* Author: Armando Lüscher
* Author URI: https://noplanman.ch
* Text Domain: advanced-backwpup-s3-destinations
* Network: true
* Requires at least: 6.0
* Requires PHP: 8.0
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
/**
* Copyright 2023 Armando Lüscher (email: [email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, 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
*/
namespace NPM\AdvancedBackWPupS3Destinations;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* Make sure BackWPup is installed and activated.
*/
add_action( 'admin_init', static function () {
if ( current_user_can( 'activate_plugins' )
&& ! is_plugin_active( 'backwpup/backwpup.php' )
&& ! is_plugin_active( 'backwpup-pro/backwpup.php' )
) {
deactivate_plugins( plugin_basename( __FILE__ ) );
add_action( 'admin_notices', 'NPM\AdvancedBackWPupS3Destinations\admin_notice_missing_plugin' );
add_action( 'network_admin_notices', 'NPM\AdvancedBackWPupS3Destinations\admin_notice_missing_plugin' );
}
} );
/**
* Admin notice to alert about missing BackWPup plugin.
*/
function admin_notice_missing_plugin(): void {
?>
<div class="notice notice-error is-dismissible">
<p>
<strong><?php _e( 'BackWPup or BackWPup Pro must be installed and activated!', 'advanced-backwpup-s3-destinations' ); ?></strong>
</p>
<p>
<em><?php _e( 'Advanced S3 Destinations for BackWPup has been deactivated.', 'advanced-backwpup-s3-destinations' ); ?></em>
</p>
</div>
<?php
unset( $_GET['activate'] );
}
/**
* Fetch the saved S3 destinations.
*/
function get_s3_destinations(): array {
return json_decode( get_site_option( 'advanced_backwpup_s3_destinations', '[]' ), true ) ?: [];
}
/**
* Save the passed S3 destinations.
*/
function save_s3_destinations( array $s3_destinations ): void {
update_site_option( 'advanced_backwpup_s3_destinations', json_encode( $s3_destinations ) );
}
/**
* Add direct link to "S3 Destinations" on plugin page.
*/
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), static function ( array $links ): array {
$links[] = '<a href="' . esc_url( admin_url( 'admin.php?page=backwpupsettings#backwpup-tab-s3-destinations' ) ) . '">' . __( 'Settings', 'advanced-backwpup-s3-destinations' ) . '</a>';
return $links;
} );
/**
* Add a new tab to the "BackWPup -> Settings" page.
*/
add_filter( 'backwpup_page_settings_tab', static function ( array $tabs ): array {
$tabs['s3-destinations'] = esc_html__( 'S3 Destinations', 'advanced-backwpup-s3-destinations' );
return $tabs;
} );
/**
* Output the form on the settings page.
*/
add_action( 'backwpup_page_settings_tab_content', static function (): void {
// Fetch saved destinations.
$s3_destinations = get_s3_destinations();
// Always set an extra one to allow expansion.
$s3_destinations['new-s3-destination'] = $default = [
'label' => __( 'New S3 destination', 'advanced-backwpup-s3-destinations' ),
'endpoint' => '',
'region' => '',
'multipart' => false,
'only_path_style_bucket' => false,
'version' => '',
'signature' => '',
];
?>
<div class="table ui-tabs-hide" id="backwpup-tab-s3-destinations">
<h3 class="title"><?php _e( 'Custom S3 destinations', 'advanced-backwpup-s3-destinations' ); ?></h3>
<label for="s3_destinations_append">
<input name="s3_destinations_append" type="checkbox" id="s3_destinations_append" value="1" <?php checked( get_site_option( 'advanced_backwpup_s3_destinations_append', true ) ); ?> />
<?php esc_html_e( 'Append to the default S3 destinations list', 'advanced-backwpup-s3-destinations' ); ?>
</label>
<?php foreach ( $s3_destinations as $id => $s3_destination ):
$name = "s3_destinations[{$id}]";
// Fill in defaults for missing fields.
$s3_destination += $default;
?>
<div class="card">
<table class="form-table">
<tr>
<th scope="row">
<label for="s3_destination_id_<?php echo $id; ?>"><?php esc_html_e( 'Unique ID', 'advanced-backwpup-s3-destinations' ); ?><span style="color:#f00">*</span></label>
</th>
<td>
<input type="text" name="<?php echo $name; ?>[id]" id="s3_destination_id_<?php echo $id; ?>" value="<?php echo $id; ?>" class="regular-text"/>
</td>
</tr>
<tr>
<th scope="row">
<label for="s3_destination_label_<?php echo $id; ?>"><?php esc_html_e( 'Label', 'advanced-backwpup-s3-destinations' ); ?><span style="color:#f00">*</span></label>
</th>
<td>
<input type="text" name="<?php echo $name; ?>[label]" id="s3_destination_label_<?php echo $id; ?>" value="<?php echo $s3_destination['label']; ?>" class="regular-text"/>
</td>
</tr>
<tr>
<th scope="row">
<label for="s3_destination_endpoint_<?php echo $id; ?>"><?php esc_html_e( 'Endpoint', 'advanced-backwpup-s3-destinations' ); ?><span style="color:#f00">*</span></label>
</th>
<td>
<input type="url" name="<?php echo $name; ?>[endpoint]" id="s3_destination_endpoint_<?php echo $id; ?>" value="<?php echo $s3_destination['endpoint']; ?>" placeholder="https://..." class="regular-text"/>
</td>
</tr>
<tr>
<th scope="row">
<label for="s3_destination_region_<?php echo $id; ?>"><?php esc_html_e( 'Region', 'advanced-backwpup-s3-destinations' ); ?></label>
</th>
<td>
<input type="text" name="<?php echo $name; ?>[region]" id="s3_destination_region_<?php echo $id; ?>" value="<?php echo $s3_destination['region']; ?>" class="regular-text"/>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e( 'Multipart', 'advanced-backwpup-s3-destinations' ); ?></th>
<td>
<fieldset>
<legend class="screen-reader-text">
<span><?php _e( 'Multipart', 'advanced-backwpup-s3-destinations' ); ?></span>
</legend>
<label for="s3_destination_multipart_<?php echo $id; ?>">
<input name="<?php echo $name; ?>[multipart]" type="checkbox" id="s3_destination_multipart_<?php echo $id; ?>" value="1" <?php checked( $s3_destination['multipart'] ); ?> />
<?php esc_html_e( 'Destination supports multipart', 'advanced-backwpup-s3-destinations' ); ?>
</label>
</fieldset>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e( 'Pathstyle-Only Bucket', 'advanced-backwpup-s3-destinations' ); ?></th>
<td>
<fieldset>
<legend class="screen-reader-text">
<span><?php _e( 'Pathstyle-Only Bucket', 'advanced-backwpup-s3-destinations' ); ?></span>
</legend>
<label for="s3_destination_only_path_style_bucket_<?php echo $id; ?>">
<input name="<?php echo $name; ?>[only_path_style_bucket]" type="checkbox" id="s3_destination_only_path_style_bucket_<?php echo $id; ?>" value="1" <?php checked( $s3_destination['only_path_style_bucket'] ); ?> />
<?php esc_html_e( 'Destination provides only Pathstyle buckets', 'advanced-backwpup-s3-destinations' ); ?>
<p class="description"><?php esc_html_e( 'Example: https://s3.example.com/bucket-name', 'advanced-backwpup-s3-destinations' ); ?></p>
</label>
</fieldset>
</td>
</tr>
<tr>
<th scope="row">
<label for="s3_destination_version_<?php echo $id; ?>"><?php esc_html_e( 'Version', 'advanced-backwpup-s3-destinations' ); ?></label>
</th>
<td>
<input type="text" name="<?php echo $name; ?>[version]" id="s3_destination_version_<?php echo $id; ?>" value="<?php echo $s3_destination['version']; ?>" placeholder="latest"/>
<p class="description"><?php _e( 'The S3 version for the API like "2006-03-01"', 'advanced-backwpup-s3-destinations' ); ?></p>
</td>
</tr>
<tr>
<th scope="row">
<label for="s3_destination_signature_<?php echo $id; ?>"><?php esc_html_e( 'Signature', 'advanced-backwpup-s3-destinations' ); ?></label>
</th>
<td>
<input type="text" name="<?php echo $name; ?>[signature]" id="s3_destination_signature_<?php echo $id; ?>" value="<?php echo $s3_destination['signature']; ?>" placeholder="v4"/>
<p class="description"><?php _e( 'The signature for the API like "v4"', 'advanced-backwpup-s3-destinations' ); ?></p>
</td>
</tr>
</table>
</div>
<?php endforeach; ?>
</div>
<?php
} );
/**
* Handle S3 destinations saving.
*/
add_action( 'backwpup_page_settings_save', static function (): void {
update_site_option( 'advanced_backwpup_s3_destinations_append', ! empty( $_POST['s3_destinations_append'] ) );
if ( empty( $_POST['s3_destinations'] ) || ! is_array( $_POST['s3_destinations'] ) ) {
return;
}
$s3_destinations = [];
foreach ( $_POST['s3_destinations'] as $s3_destination ) {
$s3_destination = array_filter( array_map( 'trim', (array) $s3_destination ) );
// Skip invalid entries.
if ( empty( $s3_destination['id'] )
|| empty( $s3_destination['label'] )
|| empty( $s3_destination['endpoint'] )
) {
continue;
}
$id = sanitize_title( $s3_destination['id'] );
unset( $s3_destination['id'] );
// Checkboxes to boolean.
$s3_destination['multipart'] = ! empty( $s3_destination['multipart'] );
$s3_destination['only_path_style_bucket'] = ! empty( $s3_destination['only_path_style_bucket'] );
$s3_destinations[ $id ] = $s3_destination;
}
save_s3_destinations( $s3_destinations );
} );
/**
* Display S3 destinations in the job.
*/
add_filter( 'backwpup_s3_destination', static function ( array $destinations ): array {
if ( ! get_site_option( 'advanced_backwpup_s3_destinations_append', true ) ) {
$destinations = [];
}
return array_merge( $destinations, get_s3_destinations() );
} );