| 1 |
<?php |
| 2 |
|
| 3 |
namespace webaware\disable_emails; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
|
| 7 |
if (!defined('ABSPATH')) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
const OPT_SETTINGS = 'disable_emails'; |
| 12 |
|
| 13 |
const INDICATOR_NONE = 'none'; |
| 14 |
const INDICATOR_TOOLBAR = 'toolbar'; |
| 15 |
const INDICATOR_NOTICE = 'notice'; |
| 16 |
const INDICATOR_NOTICE_AND_TB = 'notice_toolbar'; |
| 17 |
|
| 18 |
/** |
| 19 |
* get current plugin settings, use defaults if settings not yet saved |
| 20 |
* @return array |
| 21 |
*/ |
| 22 |
function get_plugin_settings() { |
| 23 |
$defaults = [ |
| 24 |
'wp_mail' => 1, |
| 25 |
'wp_mail_from' => 1, |
| 26 |
'wp_mail_from_name' => 1, |
| 27 |
'wp_mail_content_type' => 1, |
| 28 |
'wp_mail_charset' => 1, |
| 29 |
'phpmailer_init' => 1, |
| 30 |
'buddypress' => 1, |
| 31 |
'events_manager' => 1, |
| 32 |
'indicator' => INDICATOR_TOOLBAR, |
| 33 |
]; |
| 34 |
|
| 35 |
return wp_parse_args(get_option(OPT_SETTINGS, []), $defaults); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* can current user activate/deactivate the must-use plugin? |
| 40 |
* @return bool |
| 41 |
*/ |
| 42 |
function has_mu_plugin_permission() { |
| 43 |
return current_user_can(is_multisite() ? 'manage_network_plugins' : 'activate_plugins'); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* install, update, or remove the must-use plugin |
| 48 |
* @param string $action |
| 49 |
* @return bool |
| 50 |
* @throws Exception |
| 51 |
*/ |
| 52 |
function mu_plugin_manage($action) { |
| 53 |
if (!has_mu_plugin_permission()) { |
| 54 |
throw new Exception(__('No permission to manage Disable Emails must-use plugin.', 'disable-emails')); |
| 55 |
} |
| 56 |
|
| 57 |
$has_mu_plugin = defined('DISABLE_EMAILS_MU_PLUGIN'); |
| 58 |
|
| 59 |
$wpmu_plugin_dir = rtrim(wp_normalize_path(WPMU_PLUGIN_DIR), '/'); |
| 60 |
if (!is_dir($wpmu_plugin_dir)) { |
| 61 |
// folder does not exist, create it now |
| 62 |
wp_mkdir_p($wpmu_plugin_dir); |
| 63 |
if (!is_dir($wpmu_plugin_dir)) { |
| 64 |
throw new Exception(__('Unable to create folder for Disable Emails must-use plugin.', 'disable-emails')); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
$source = wp_normalize_path(DISABLE_EMAILS_PLUGIN_ROOT . 'mu-plugin/disable-emails-mu.php'); |
| 69 |
$target = "$wpmu_plugin_dir/disable-emails-mu.php"; |
| 70 |
|
| 71 |
switch ($action) { |
| 72 |
|
| 73 |
case 'activate': |
| 74 |
case 'update': |
| 75 |
if (!copy($source, $target)) { |
| 76 |
throw new Exception(__('Unable to install Disable Emails must-use plugin.', 'disable-emails')); |
| 77 |
} |
| 78 |
$has_mu_plugin = true; |
| 79 |
break; |
| 80 |
|
| 81 |
case 'deactivate': |
| 82 |
if (!unlink($target)) { |
| 83 |
throw new Exception(__('Unable to uninstall Disable Emails must-use plugin.', 'disable-emails')); |
| 84 |
} |
| 85 |
$has_mu_plugin = false; |
| 86 |
break; |
| 87 |
|
| 88 |
default: |
| 89 |
throw new Exception(__('Invalid action for Disable Emails must-use plugin.', 'disable-emails')); |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
return $has_mu_plugin; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* get message for current active status |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
function get_status_message() { |
| 101 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 102 |
|
| 103 |
if (defined('DISABLE_EMAILS_MU_PLUGIN') && is_multisite()) { |
| 104 |
/* translators: shown when emails are disabled for all sites in all networks in a multisite, with the must-use plugin */ |
| 105 |
$msg = __('Emails are disabled for all sites.', 'disable-emails'); |
| 106 |
} |
| 107 |
elseif (is_plugin_active_for_network(DISABLE_EMAILS_PLUGIN_NAME)) { |
| 108 |
/* translators: shown when emails are disabled for all sites in a multisite network, by network-activating the plugin */ |
| 109 |
$msg = __('Emails are disabled on this network.', 'disable-emails'); |
| 110 |
} |
| 111 |
else { |
| 112 |
/* translators: shown when emails are disabled for the current site */ |
| 113 |
$msg = __('Emails are disabled.', 'disable-emails'); |
| 114 |
} |
| 115 |
|
| 116 |
return $msg; |
| 117 |
} |
| 118 |
|