-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstaging-setup.php
More file actions
462 lines (366 loc) · 14.2 KB
/
Copy pathstaging-setup.php
File metadata and controls
462 lines (366 loc) · 14.2 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
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
<?php
require_once __DIR__ . '/wp-load.php';
// PHP-level suppression
ini_set('display_errors', '0');
ini_set('log_errors', '0');
error_reporting(0);
function extract_subdomains($host) {
$host = strtolower($host);
$parts = explode('.', $host);
if (count($parts) < 3) {
// Δεν υπάρχουν subdomains
return '';
}
// Παίρνουμε όλα εκτός από τα 2 τελευταία (domain + tld)
return implode('.', array_slice($parts, 0, -2));
}
$host = parse_url( get_option('siteurl'), PHP_URL_HOST );
$subdomains = extract_subdomains($host);
// We don't want the code to run if is LIVE site (aka if is there is no subdomain or if it is 'www')
if ( !$subdomains || $subdomains === 'www' ) {
echo 'SOS: You cannot run this script in LIVE site!';
echo "\n";
return;
}
$functions = [
'dc_staging_install_mu_plugin_to_restrict_outgoing_emails',
'dc_staging_add_staging_prefix_to_site_title',
'dc_staging_enable_coming_soon',
'dc_staging_enable_no_index',
'dc_staging_enable_debug_mode',
'dc_staging_deactivate_plugins',
'dc_staging_disable_custom_admin_monitor',
'dc_staging_disable_order_auto_sync_to_erp',
'dc_staging_change_admin_email',
'dc_staging_change_wc_email_recipients',
];
// Your logic
echo "\n";
foreach ($functions as $function) {
ob_start();
try {
call_user_func( $function );
} catch ( Throwable $e ) {
echo "FAIL: Exception in {$function}: {$e->getMessage()}\nTrace:\n{$e->getTraceAsString()}";
}
$echo = ob_get_clean();
if( trim($echo) ){
echo "-> WP: $echo";
echo "\n";
echo "\n";
}
}
function dc_staging_add_staging_prefix_to_site_title(){
$site_title = get_option('blogname');
if( strpos($site_title, 'STAGING') === 0 ){
echo 'Site name already has the STAGING prefix.';
return;
}
update_option('blogname', "STAGING $site_title");
if( strpos(get_option('blogname'), 'STAGING') === 0 ){
echo 'Site name now has the STAGING prefix!';
}else{
echo 'FAIL: Could not add STAGING prefix to site name.';
}
}
function dc_staging_enable_coming_soon(){
// Fully qualified class name
$fqcn = '\Automattic\WooCommerce\Admin\API\LaunchYourStore';
$method = 'initialize_coming_soon';
if ( !class_exists( $fqcn ) || !method_exists( $fqcn , $method ) ) {
echo "FAIL: Coming Soon not enabled. Use plugin or htaccess for this scope";
return;
}
update_option( 'woocommerce_coming_soon', 'yes' );
update_option( 'woocommerce_store_pages_only', 'no' );
update_option( 'woocommerce_feature_site_visibility_badge_enabled', 'yes' );
if ( get_option( 'woocommerce_coming_soon') == 'yes' && get_option( 'woocommerce_store_pages_only') == 'no' && get_option( 'woocommerce_feature_site_visibility_badge_enabled') == 'yes' ) {
echo "Coming Soon initialized successfully";
} else {
echo "FAIL: Coming Soon not enabled. Use plugin or htaccess for this scope";
}
}
function dc_staging_enable_no_index(){
update_option( 'blog_public', '0' );
if ( get_option( 'blog_public', 0) == 0 ) {
echo "NO-INDEX enabled successfully";
} else {
echo "FAIL: NO-INDEX failed to enable";
}
}
function dc_staging_enable_debug_mode() {
$config_file = ABSPATH . 'wp-config.php';
// WP_DEBUG must be exactly true
$wp_debug_needs_update = !defined('WP_DEBUG') || WP_DEBUG !== true;
// WP_DEBUG_LOG must be non-falsy (true or string). Update only if missing or false.
$wp_debug_log_needs_update = !defined('WP_DEBUG_LOG') || !WP_DEBUG_LOG;
if (!$wp_debug_needs_update && !$wp_debug_log_needs_update) {
echo 'WP_DEBUG & WP_DEBUG_LOG are already enabled!';
return;
}
if (!file_exists($config_file) || !is_writable($config_file)) {
echo "FAIL: wp-config.php is not writable, WP_DEBUG & WP_DEBUG_LOG cannot be enabled";
return;
}
$content = file_get_contents($config_file);
$is_inside_block_comment = false;
// Process file with callback to safely replace defines outside comments
$content = preg_replace_callback(
'/(\/\*|\*\/|\/\/.*$|define\s*\(\s*[\'"](WP_DEBUG|WP_DEBUG_LOG)[\'"].*?\))/m',
function($matches) use (&$is_inside_block_comment, &$wp_debug_needs_update, &$wp_debug_log_needs_update) {
$match = $matches[0];
// Track start of block comment
if (strpos($match, '/*') !== false) {
$is_inside_block_comment = true;
return $match;
}
// Track end of block comment
if (strpos($match, '*/') !== false) {
$is_inside_block_comment = false;
return $match;
}
// Ignore anything inside block comment
if ($is_inside_block_comment) {
return $match;
}
// Ignore single-line comments
if (strpos($match, '//') === 0) {
return $match;
}
// Replace defines if needed
if ((stripos($match, "'WP_DEBUG'") !== false || stripos($match, '"WP_DEBUG"') !== false) && $wp_debug_needs_update) {
$wp_debug_needs_update = false;
return "define('WP_DEBUG', true)";
}
if ((stripos($match, "'WP_DEBUG_LOG'") !== false || stripos($match, '"WP_DEBUG_LOG"') !== false) && $wp_debug_log_needs_update) {
$wp_debug_log_needs_update = false;
return "define('WP_DEBUG_LOG', true)";
}
return $match;
},
$content
);
// If defines are still missing, insert after <?php
if ($wp_debug_needs_update || $wp_debug_log_needs_update) {
$content = preg_replace_callback(
'/<\?php/',
function ($matches) use ($wp_debug_needs_update, $wp_debug_log_needs_update) {
$insert = $matches[0] . "\n";
if ($wp_debug_needs_update) {
$insert .= "define('WP_DEBUG', true);\n";
}
if ($wp_debug_log_needs_update) {
$insert .= "define('WP_DEBUG_LOG', true);\n";
}
return $insert;
},
$content,
1 // only the first <?php
);
}
// Write the modified content back
$putting_result = file_put_contents($config_file, $content);
if($putting_result) {
echo 'WP_DEBUG & WP_DEBUG_LOG are successfully enabled!';
}else{
echo "FAIL: wp-config.php is not writable, WP_DEBUG & WP_DEBUG_LOG cannot be enabled";
}
}
function dc_staging_deactivate_plugins(){
require_once ABSPATH . 'wp-admin/includes/plugin.php';
$plugins_to_deactivate = [
'wp-rocket/wp-rocket.php',
'litespeed-cache/litespeed-cache.php',
'wp-fastest-cache/wpFastestCache.php',
'redis-cache/redis-cache.php',
'wp-all-import-pro/wp-all-import-pro.php',
'wp-all-export-pro/wp-all-export-pro.php',
'wpai-acf-add-on/wpai-acf-add-on.php',
'wpai-woocommerce-add-on/wpai-woocommerce-add-on.php',
'wpae-woocommerce-add-on/wpae-woocommerce-add-on.php',
'wpae-acf-add-on/wpae-acf-add-on-pro.php',
'dc-skroutz-bestprice-feed/dc-skroutz-bestprice-feed.php',
'xml-feed-for-skroutz-for-woocommerce/xml-feed-for-skroutz-for-woocommerce.php',
'dc-skroutz-sc/dc-skroutz-sc.php',
'webexpert-skroutz-xml-feed/webexpert-skroutz-xml-feed.php',
'webexpert-woocommerce-skroutz-smart-cart/webexpert-woocommerce-skroutz-smart-cart.php',
'woo-xml-feed-for-skroutzgr-bestpricegr/wooshop-skroutzxml.php',
'skroutz-analytics-woocommerce/wc-skroutz-analytics.php',
'skroutz-marketplace-xml-for-woocommerce/wocommerce-skroutz-smart-cart.php',
'pixelyoursite/facebook-pixel-master.php',
'pixelyoursite-pro/pixelyoursite-pro.php',
'pixelyoursite-super-pack/pixelyoursite-super-pack.php',
'microsoft-clarity/clarity.php',
'moosend-email-marketing/index.php',
'webappick-product-feed-for-woocommerce/woo-feed.php',
'webappick-product-feed-for-woocommerce-pro/webappick-product-feed-for-woocommerce-pro.php',
'facebook-for-woocommerce/facebook-for-woocommerce.php',
'enhanced-e-commerce-for-woocommerce-store/enhanced-ecommerce-google-analytics.php',
'mainwp-child/mainwp-child.php',
'mainwp-child-reports/mainwp-child-reports.php',
'google-pagespeed-insights/google-pagespeed-insights.php',
'rac/recoverabandoncart.php',
'woocommerce-apg-sms-notifications/apg-sms.php',
'glami-feed-generator-pixel-for-woocommerce-main/glami-feed-generator-pixel-for-woocommerce.php',
'woo-product-feed-pro/woocommerce-sea.php',
'product-catalog-feed-pro/product-catalog-feed-pro.php',
'dc-shopflix-sc/dc-shopflix-sc.php',
'smartsupp-live-chat/smartsupp.php',
'chaty/cht-icons.php',
'tawkto-live-chat/tawkto.php',
'tidio-live-chat/tidio-elements.php',
'chat-viber/chat-viber-lite.php',
'wp-whatsapp/whatsapp.php',
'facebook-messenger-customer-chat/facebook-messenger-customer-chat.php',
'burst-statistics/burst.php',
'google-listings-and-ads/google-listings-and-ads.php',
'woocommerce-google-analytics-integration/woocommerce-google-analytics-integration.php',
'google-analytics-for-wordpress/googleanalytics.php',
'woo_xml_feed/wc_xml_feed.php',
'google-analytics-dashboard-for-wp/gadwp.php'
];
$plugins_names = array_map( fn($plg) => $plg['Name'] , get_plugins() );
foreach($plugins_to_deactivate as $file_path){
if( $name = $plugins_names[$file_path] ?? null ){
try {
deactivate_plugins( $file_path );
echo "$name plugin deactivated successfully!";
} catch ( Throwable $e ) {
echo "FAIL: Exception deactivating $name plugin: {$e->getMessage()}\nTrace:\n{$e->getTraceAsString()}";
}
echo "\n";
}
}
echo "Plugins' deactivation finished!";
}
function dc_staging_disable_custom_admin_monitor(){
delete_option('dc_ca_feeds_list');
delete_option('dc_ca_wpai_imports_to_check');
if ( get_option( 'dc_ca_feeds_list', null) === null && get_option( 'dc_ca_wpai_imports_to_check', null) === null ) {
echo "Custom Admin plugin's Monitor has been disabled successfully";
} else {
echo "FAIL: Custom Admin plugin's Monitor failed to be disabled!";
}
}
function dc_staging_disable_order_auto_sync_to_erp(){
$b4e_settings = get_option('b4e_settings');
if( !$b4e_settings ){
return; // return without message
}
if( empty( $b4e_settings['orders_options_status_trigger'] ) ){
echo 'Order auto sync to ERP is already disabled!';
return;
}
if( $b4e_settings['orders_options_status_trigger'] == 'off' ){
echo 'Order auto sync to ERP is already disabled!';
return;
}
$b4e_settings['orders_options_status_trigger'] = 'off';
update_option('b4e_settings', $b4e_settings);
// if success
$b4e_settings = get_option('b4e_settings');
if( $b4e_settings['orders_options_status_trigger'] == 'off' ){
echo 'Order auto sync to ERP has been disabled!';
}
}
function dc_staging_change_admin_email(){
update_option( 'admin_email', 'dichadev@gmail.com' );
if ( get_option( 'admin_email') == 'dichadev@gmail.com' ) {
echo "Admin email is now dichadev@gmail.com";
} else {
echo "FAIL: Fail to change admin email to dichadev@gmail.com";
}
}
function dc_staging_change_wc_email_recipients() {
global $wpdb;
$admin_email = get_option('admin_email');
// Find all WooCommerce email options ending with "woocommerce_*_order_settings"
// If the relevant option is not set, by default recipient is the admin email
$options = $wpdb->get_results(
"SELECT option_name FROM $wpdb->options WHERE option_name LIKE 'woocommerce\_%\_order\_settings'"
);
if ( empty( $options ) ) {
echo "All the woo emails that accepts recipient has been changed to $admin_email";
return;
}
foreach ( $options as $option ) {
$settings = get_option( $option->option_name );
// Proceed only if option is an array AND has a non-empty recipient, change it
if ( is_array( $settings ) && ! empty( $settings['recipient'] ) ) {
$email_slug = preg_replace('/^woocommerce_(.+)_settings$/', '$1', $option->option_name);
// Update recipients
$settings['recipient'] = $admin_email;
// Save changes
update_option( $option->option_name, $settings );
// if success
$settings = get_option($option->option_name);
if( $settings['recipient'] != $admin_email ){
$has_fail = true;
echo "FAIL: Woo email $email_slug — recipient hasn't been set to $admin_email!";
}
}
}
if( !empty($has_fail) ) { // == true
echo "All the other woo emails that accepts recipient has been changed to $admin_email";
}else{
echo "All the woo emails that accepts recipient has been changed to $admin_email";
}
}
function dc_staging_install_mu_plugin_to_restrict_outgoing_emails(){
$plugin_code = <<<'PHP'
<?php
/**
* Plugin Name: DC Staging Email Restriction
* Description: Allow outgoing emails only to @dicha.gr and dichadev@gmail.com (including aliases).
*/
add_filter('wp_mail', 'dc_staging_remove_outside_dicha_email_addresses', 999);
function dc_staging_remove_outside_dicha_email_addresses( $mail_data ){
$to = !is_array( $mail_data['to'] ) ? explode(',', $mail_data['to'] ) : $mail_data['to'];
$mail_data['to'] = array_filter( $to, 'dc_staging_is_email_address_allowed' );
return $mail_data;
}
add_filter('pre_wp_mail', 'dc_staging_restrict_outgoing_emails', 999, 2);
function dc_staging_restrict_outgoing_emails( $default_value, $mail_data ){
if ( empty($mail_data['to']) ) {
do_action('wp_mail_failed', new WP_Error('wp_mail_failed','Outgoing emails are restricted. Only @dicha.gr addresses and dichadev@gmail.com (including its aliases) are allowed.', $mail_data));
return false;
}
return $default_value;
}
function dc_staging_is_email_address_allowed( $email_address ) {
$email_address = strtolower(trim($email_address));
// Allow *@dicha.gr using preg_match
if ( preg_match('/@dicha\.gr$/', $email_address) ) {
return true;
}
if ( preg_match('/^(.*?)@(gmail\.com|googlemail\.com)$/', $email_address, $matches) ) {
$local_part = $matches[1];
// Remove dots & +anything (because they are alias)
$local_part = str_replace('.', '', $local_part);
$local_part = preg_replace('/\+.*/', '', $local_part);
if ( $local_part === 'dichadev' ) { // only allow aliases of dichadev@gmail.com
return true;
}
}
return false;
}
add_action('admin_notices', 'dc_staging_email_restriction_notice');
function dc_staging_email_restriction_notice() {
?>
<div class="notice notice-info">
<p><strong>Outgoing emails are restricted.</strong> Only <code>@dicha.gr</code> addresses and <code>dichadev@gmail.com</code> (including its aliases) are allowed.</p>
</div>
<?php
}
PHP;
wp_mkdir_p(WPMU_PLUGIN_DIR);
// Path to MU-Plugin
$mu_plugin_path = trailingslashit(WPMU_PLUGIN_DIR) . 'restrict-outgoing-emails.php';
// Write file
$data_written = file_put_contents($mu_plugin_path, $plugin_code);
if($data_written){
echo 'MU Plugin that restricts outgoing emails has been installed! Only @dicha.gr addresses and dichadev@gmail.com (including its aliases) are allowed.';
}else{
echo 'FAIL: Could not install MU plugin that restricts outgoing emails!';
}
}