-
Notifications
You must be signed in to change notification settings - Fork 2
/
wp-staging-hooks.php
516 lines (457 loc) · 17.4 KB
/
wp-staging-hooks.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<?php
/*
Plugin Name: WP Staging Hooks
Plugin URI: https://wp-staging.com
Description: Extend WP STAGING by using actions and filters.
Author: WP STAGING
Version: 1.0.4
Author URI: https://wp-staging.com
*/
/*
* Copyright (c) 2023 WP STAGING. All rights reserved.
* 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.
*
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
*/
/*
* Note: This plugin does not contain all available hooks.
* Always check the official docs before using one of these hooks:
* https://wp-staging.com/docs/actions-and-filters/
*/
class wpstagingHooks
{
/**
* Uncomment the actions / filters below to activate them
*/
function __construct()
{
/*
* Keep certain plugins activated while wp staging requests are executed.
* Necessary if you want to use third party plugin function while using one of wp staging hooks or filters
*/
//update_option('wpstg_optimizer_excluded', array('wp-mail-smtp'));
/*
* Run an action after after successfull cloning on the prod site
*/
//add_action( 'wpstg_cloning_complete', array($this, 'sendMail'), 10 );
//add_action( 'wpstg_cloning_complete', array($this, 'executeSql'), 10 );
/*
* Run an action after after successfull pushing on the prod site
*/
//add_action( 'wpstg_pushing_complete', array($this, 'pushingComplete') );
/*
* Cloning: Run an action after after successfull cloning on the staging site
*/
//add_action( 'wpstg.clone_first_run', array($this, 'wpstg_execute_after_cloning' ), 10);
/*
* Exclude Tables From Search & Replace operation / Cloning and Pushing
*/
//add_action( 'wpstg_searchreplace_excl_tables', array($this, 'excludeTablesSR'), 10 );
/*
* Cloning: Exclude Rows From Search & Replace in wp_options
*/
//add_action( 'wpstg_clone_searchreplace_excl_rows', array($this, 'excludeRowsSR'), 10 );
/*
* Cloning: Exclude Rows From Search & Replace in wp_options
*/
//add_action( 'wpstg_clone_searchreplace_excl', array($this, 'excludeStringsSR'), 10 );
/*
* Cloning: Change Search & Replace Parameters
*/
//add_action( 'wpstg_clone_searchreplace_params', array($this, 'setSRparams'), 10 );
/*
* Cloning: Exclude files from cloning
*/
//add_action( 'wpstg_clone_excluded_files', array($this, 'wpstg_clone_excluded_files'), 10 );
/*
* Cloning: Exclude Folders
*/
//add_action( 'wpstg_clone_excl_folders', array($this, 'excludeFolders'), 10 );
/*
* Cloning: Exclude Folders (Multisites)
*/
//add_action( 'wpstg_clone_mu_excl_folders', array($this, 'multisiteExcludeFoldersCloning'), 10 );
/*
* Cloning: Do not Modify Table Prefix from option_name in wp_options
*/
//add_action( 'wpstg_excl_option_name_custom', array($this, 'wpstg_excl_option_name_custom'), 10 );
/*
* Cloning: Change target hostname
*/
// add_filter( "wpstg_cloning_target_hostname", array($this, 'set_cloning_target_hostname'), 10 );
/*
* Cloning: Change target destination dir
*/
// add_filter( "wpstg_cloning_target_dir", array($this, 'set_cloning_target_directory'), 10 );
/*
* Cloning: Copy specific database rows during cloning
*/
// add_filter( "wpstg.cloning.database.queryRows", array($this, 'query_cloning_database_rows'), 10 );
/*
* Pushing: Change Search & Replace parameters
*/
// add_filter( 'wpstg_push_searchreplace_params', array($this, 'wpstg_push_custom_params'), 10 );
/*
* Pushing: Exclude tables from pushing
*/
//add_action( 'wpstg_push_excluded_tables', array($this, 'wpstg_push_excluded_tables'), 10 );
/*
* Pushing: Exclude folders from pushing
*/
//add_action( 'wpstg_push_excl_folders_custom', array($this, 'wpstg_push_directories_excl'), 10 );
/*
* Pushing: Exclude files from pushing
*/
//add_action( 'wpstg_push_excluded_files', array($this, 'wpstg_push_excluded_files'), 10 );
/*
* Pushing: Preserve data in wp_options and exclude it from pushing
*/
//add_action( 'wpstg_preserved_options', array($this, 'wpstg_push_options_excl'), 10 );
/*
* Pushing: Copy specific database rows during push
*/
//add_filter( "wpstg.pushing.database.queryRows", array($this, 'query_pushing_database_rows'), 10 );
/*
* Backup: Exclude file extension from backup
*/
//add_filter( 'wpstg.export.files.ignore.file_extension', [$this, 'wpstg_export_files_ignore_extensions']);
/*
* Backup: Exclude files bigger than the given size from backup
*/
//add_filter( 'wpstg.export.files.ignore.file_bigger_than', [$this, 'wpstg_backup_files_ignore_files_bigger_than']);
/*
* Backup: Exclude files with particular extensions larger than the given file size from backup
*/
//add_filter( 'wpstg.export.files.ignore.file_extension_bigger_than', [$this, 'wpstg_backup_files_ignore_files_w_extension_bigger_than']);
/*
* Backup: Exclude folders from being included in a backup
*/
//add_filter( 'wpstg.backup.exclude.directories', [$this, 'wpstg_backup_exclude_directories']);
/*
* Backup: Exclude files from being restored
*/
//add_filter( 'wpstg.backup.restore.exclude.other.files', [$this, 'wpstg_backup_restore_exclude_other_files']);
}
/**
* Change target hostname of staging site
* @param string $dest
* @return string
*/
public function set_cloning_target_hostname($dest)
{
$dest = "https://example.com";
return $dest;
}
/**
* Change target directory of staging site
* @param string $dest
* @return string
*/
public function set_cloning_target_directory($dest)
{
$dest = "/custompath/";
return $dest;
}
/**
* Send out an email when the cloning proces has been finished successfully
*/
public function sendMail()
{
wp_mail('[email protected]', 'WP Staging cloning process has been finished', 'body sample text');
}
/**
* Execute custom sql query after cloning on staging site
*/
public function executeSql($args)
{
global $wpdb;
$extDb = true; // set to false to use the default $wpdb db object and to use SQL on the production databae. Set to false to execute SQL on external database
// External database object
if ($extDb && !empty($args->databaseUser) && !empty($args->databasePassword) && !empty($args->databaseDatabase) && !empty($args->databaseServer)) {
$db = new \wpdb($args->databaseUser, str_replace("\\\\", "\\", $args->databasePassword), $args->databaseDatabase, $args->databaseServer);
} else {
$db = $wpdb;
}
// Prefix of the staging site
$prefix = $args->prefix;
$sql = "INSERT INTO {$prefix}options (option_name,option_value) VALUES ('test2', 'value')";
error_log('Execute SQL: ' . $sql);
// Add value testvalue into prefix_options or execute any other sql query here
$db->query($sql);
}
/**
* Send out an email when the pushing proces has been finished successfully
*/
public function pushingComplete()
{
wp_mail('[email protected]', 'WP Staging cloning process has been finished', 'body sample text');
}
/**
* Exclude certain Tables From Search & Replace operation
*
* Use this if the search & replace process eats up a lot of your available memory
* and the cloning or pushing process failed with a ‘memory exhausted error‘.
* You can also use this to improve the speed of the cloning and pushing process.
*
* Exclude tables which do not need any search & replacement of strings!
* These can be tables which contains visitor stats, IP addresses or similar.
* After excluding these tables you can increase the DB Search & Replace limit in
* WP Staging settings to a higher value to get better performance.
*/
public function excludeTablesSR($tables)
{
$addTables = array('_posts', '_postmeta');
return array_merge($tables, $addTables);
}
/**
* Exclude certain rows in table wp_options from Search & Replace operation
*/
public function excludeRowsSR($default)
{
$rows = array('siteurl', 'home');
return array_merge($default, $rows);
}
/**
* Exclude certain strings from Search & Replace operations
*/
public function excludeStringsSR()
{
return array('blog.localhost.com', 'blog1.localhost.com');
}
/**
* Cloning: Add new Search & Replace rules on top of existing ones or change existing ones entirely
*/
public function setSRparams($args)
{
// Add new strings to search & replace array
$args['search_for'][] = '%2F%2Fwww.example.com%2Fstaging%2F';
$args['search_for'][] = '//www.example2.com/staging/';
$args['replace_with'][] = '%2F%2Fwww.example.com%2F';
$args['replace_with'][] = '//www.example2.com/';
// Default values - You can change these
$args['replace_guids'] = 'off';
$args['dry_run'] = 'off';
$args['case_insensitive'] = false;
$args['replace_guids'] = 'off';
$args['replace_mails'] = 'off';
$args['skip_transients'] = 'on';
return $args;
}
/**
* Cloning: Exclude files from cloning
* Example: You can use a wildcard pattern like *.log to exclude all log files
*/
function wpstg_clone_excluded_files($default)
{
$files = array('custom-file', '*LOG-*', '*.logs');
return array_merge($default, $files);
}
/**
* Cloning: Exclude Folders
* These paths must be the path relative to the wordpress root folder
* These exclude rules will not be applied to wp-admin, wp-includes
*/
public function excludeFolders($defaultFolders)
{
$folders = [
'/wp-content/plugins/wordpress-seo', // Relative path to your wordpress root path. Remember to add leading slash
'**/node_modules', // Wildcard path
'*.zip' // Extension
];
return array_merge($defaultFolders, $folders);
}
/**
* Excluded paths relative to the wordpress root folder when cloning multisites
* These exclude rules will not be applied to wp-admin, wp-includes
*/
public function multisiteExcludePathsCloning($defaultFolders)
{
$folders = [
'/wp-content/plugins/wordpress-seo', // Relative path to your wordpress root path, remember to add leading slash
'**/node_modules', // Wildcard path
'*.zip' // Extension
];
return array_merge($defaultFolders, $folders);
}
/**
* Cloning: Do not Modify Table Prefix for particular rows in option_name
*/
public function wpstg_excl_option_name_custom($args)
{
$cols = array('wp_mail_smtp', 'wp_mail_smtp_version');
return array_merge($args, $cols);
}
/**
* Pushing: Change Search & Replace parameters
*/
public function wpstg_push_custom_params($args)
{
// Add new strings to search & replace array
$args['search_for'][] = '%2F%2Fwww.example.com%2Fstaging%2F';
$args['search_for'][] = '//www.example2.com/staging/';
$args['search_for'][] = '\/\/www.example2.com\/staging';
$args['replace_with'][] = '%2F%2Fwww.example.com%2F';
$args['replace_with'][] = '//www.example2.com/';
$args['replace_with'][] = '\/\/www.example2.com';
// Default values - Can be changed
$args['replace_guids'] = 'off';
$args['dry_run'] = 'off';
$args['case_insensitive'] = false;
$args['replace_guids'] = 'off';
$args['replace_mails'] = 'off';
$args['skip_transients'] = 'on';
return $args;
}
/**
* Pushing: Change Search & Replace parameters
*/
function wpstg_push_excluded_tables($tables)
{
$customTables = array('_options', '_posts');
return array_merge($tables, $customTables);
}
/**
* Pushing: Exclude folders from pushing
*/
function wpstg_push_directories_excl($default)
{
$dirs = array('custom-folder', 'custom-folder2');
return array_merge($default, $dirs);
}
/**
* Pushing: Exclude files from pushing
* Example: You can use a wildcard pattern like *.log to exclude all log files
*/
function wpstg_push_excluded_files($default)
{
$files = array('custom-file', '*LOG-*', '*.logs');
return array_merge($default, $files);
}
/**
* Pushing: Preserve data in wp_options and exclude it from pushing
* The example below preserves the value of the ‘siteurl’ on the live site.
* Any number of additional options may be added.
*/
function wpstg_push_options_excl($options)
{
$options[] = 'siteurl';
return $options;
}
/**
* Cloning: This function will be executed after cloning on staging site
*/
function wpstg_execute_after_cloning()
{
// add some code
}
/**
* Cloning: Query specific database rows
*/
function query_cloning_database_rows($filters)
{
// Use prefix of production site database.
$myFilters = [
// copy posts where post type is not 'coupon'
'wp_posts' => [
'post_type' => [
'operator' => '<>',
'value' => 'coupon'
]
],
// query wp_postmeta table depending upon options selected for wp_posts table
'wp_postmeta' => [
'join' => [
'table' => 'wp_posts',
'primaryKey' => 'ID',
'foreignKey' => 'post_id',
]
]
];
return array_merge($filters, $myFilters);
}
/**
* Pushing: Query specific database rows
*/
function query_pushing_database_rows($filters)
{
// Use prefix of staging site database i.e. wpstg0
$filters = [
// copy options where option_name is not 'wpstg_is_staging_site'
'wpstg0_options' => [
'option_name' => [
'operator' => '<>',
'value' => 'wpstg_is_staging_site'
]
],
// copy posts where post_title LIKE 'Hello%' AND post_status is 'publish'
'wpstg0_posts' => [
'post_title' => [
'operator' => 'LIKE',
'value' => 'Hello%'
],
'post_status' => 'publish'
],
// query wpstg0_postmeta table depending upon options selected for wpstg0_posts table
'wpstg0_postmeta' => [
'join' => [
'table' => 'wp_posts',
'primaryKey' => 'ID',
'foreignKey' => 'post_id',
]
]
];
return $filters;
}
/**
* Backup: Exclude files bigger than the given size from backup
* Allow user to exclude certain file extensions from being backup exported
*/
function wpstg_backup_files_ignore_extensions($default_excluded){
return array_merge($default_excluded, ['zip', 'gz']);
}
/**
* Backup: Exclude files bigger than the given size from backup
* Allow user to exclude files bigger than given size from being backup exported
*/
function wpstg_backup_files_ignore_files_bigger_than($default){
return 10 * MB_IN_BYTES;
}
/**
* Backup: Exclude files with particular extensions larger than the given file size from backup
* Allow users to exclude files with extensions larger than the given size from being exported
*/
function wpstg_backup_files_ignore_files_w_extension_bigger_than($default){
$ignoreFiles = [
'gz' => 10 * MB_IN_BYTES,
'tar' => 20 * MB_IN_BYTES
];
return array_merge($default, $ignoreFiles);
}
/**
* Backup: Exclude folders from being included in a backup
* E.g. This examples excludes the folder WP_CONTENT_DIR . '/cache'.
* Path needs to be an absolute one.
*/
function wpstg_backup_exclude_directories($excludedDirectories){
$customExcludedDirectories = [
WP_CONTENT_DIR . '/cache'
];
return array_merge($excludedDirectories, $customExcludedDirectories);
}
/**
* Backup: Exclude files from being restored
*/
function wpstg_backup_restore_exclude_other_files($excludedFiles){
$customExcludedFiles = [
'wp-content/db.php',
'wp-content/object-cache.php',
];
return array_merge($excludedFiles, $customExcludedFiles);
}
}
// Launch it
new wpstagingHooks();