-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
717 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
* Plugin Name: BC Security | ||
* Plugin URI: https://github.com/chesio/bc-security | ||
* Description: Helps keeping WordPress websites secure. Plugin requires PHP 5.6 or newer to run. | ||
* Version: 0.2.0 | ||
* Version: 0.3.0 | ||
* Author: Česlav Przywara <[email protected]> | ||
* Author URI: https://www.chesio.com | ||
* Requires at least: 4.7 | ||
|
@@ -44,6 +44,8 @@ | |
// Construct plugin instance. | ||
$bc_security = new \BlueChip\Security\Plugin($GLOBALS['wpdb']); | ||
// Register activation hook. | ||
register_activation_hook(BC_SECURITY_PLUGIN_FILE, [$bc_security, 'install']); | ||
register_activation_hook(BC_SECURITY_PLUGIN_FILE, [$bc_security, 'activate']); | ||
// Register deactivation hook. | ||
register_deactivation_hook(BC_SECURITY_PLUGIN_FILE, [$bc_security, 'deactivate']); | ||
// Load the plugin. | ||
$bc_security->load(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
/** | ||
* @package BC_Security | ||
*/ | ||
namespace BlueChip\Security\Helpers; | ||
|
||
/** | ||
* Various is::xxx() helpers. | ||
*/ | ||
class Is | ||
{ | ||
/** | ||
* Return true, if current user is an admin. | ||
* | ||
* @param \WP_User $user | ||
* @return bool | ||
*/ | ||
public static function admin(\WP_User $user) | ||
{ | ||
return is_multisite() ? user_can($user, 'manage_network') : user_can($user, 'manage_options'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
/** | ||
* @package BC_Security | ||
*/ | ||
namespace BlueChip\Security\Modules; | ||
|
||
/** | ||
* @internal Surely the ugliest English "word" that I came up with in a long time... | ||
*/ | ||
interface Deactivateable | ||
{ | ||
/** | ||
* Deactivate module (clean caches, URL redirects etc.) | ||
*/ | ||
public function deactivate(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
classes/BlueChip/Security/Modules/Notifications/AdminPage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
/** | ||
* @package BC_Security | ||
*/ | ||
|
||
namespace BlueChip\Security\Modules\Notifications; | ||
|
||
class AdminPage extends \BlueChip\Security\Core\AdminSettingsPage | ||
{ | ||
/** @var string Page slug */ | ||
const SLUG = 'bc-security-notifications'; | ||
|
||
|
||
/** | ||
* @param \BlueChip\Security\Modules\Login\Settings $settings Notifications settings | ||
*/ | ||
function __construct($settings) | ||
{ | ||
parent::__construct($settings); | ||
|
||
$this->page_title = _x('Notifications Settings', 'Dashboard page title', 'bc-security'); | ||
$this->menu_title = _x('Notifications', 'Dashboard menu item name', 'bc-security'); | ||
$this->slug = self::SLUG; | ||
} | ||
|
||
|
||
/** | ||
* Render admin page. | ||
*/ | ||
public function render() | ||
{ | ||
echo '<div class="wrap">'; | ||
echo '<h1>' . esc_html($this->page_title) . '</h1>'; | ||
echo $this->settings_api_helper->renderForm(); | ||
echo '</div>'; | ||
} | ||
|
||
|
||
/** | ||
* Run on `admin_init` hook. | ||
*/ | ||
public function admin_init() | ||
{ | ||
// Form helper is going to be useful here. | ||
$form_helper = new \BlueChip\Security\Helpers\FormHelper(); | ||
|
||
// Shortcut | ||
$settings_api_helper = $this->settings_api_helper; | ||
|
||
// Register setting first. | ||
$settings_api_helper->register(); | ||
|
||
// Set page as current. | ||
$settings_api_helper->setSettingsPage($this->slug); | ||
|
||
// Section: When to notify? | ||
$settings_api_helper->addSettingsSection( | ||
'when-to-notify', | ||
_x('When to send notification?', 'Settings section title', 'bc-security'), | ||
function () { | ||
echo '<p>' . esc_html__('Immediately send email notification when:', 'bc-security') . '</p>'; | ||
} | ||
); | ||
$settings_api_helper->addSettingsField( | ||
Settings::ADMIN_USER_LOGIN, | ||
__('User with admin privileges logs in', 'bc-security'), | ||
[$form_helper, 'renderCheckbox'] | ||
); | ||
$settings_api_helper->addSettingsField( | ||
Settings::KNOWN_IP_LOCKOUT, | ||
__('Known IP address is locked out', 'bc-security'), | ||
[$form_helper, 'renderCheckbox'] | ||
); | ||
$settings_api_helper->addSettingsField( | ||
Settings::CORE_UPDATE_AVAILABLE, | ||
__('WordPress update is available', 'bc-security'), | ||
[$form_helper, 'renderCheckbox'] | ||
); | ||
$settings_api_helper->addSettingsField( | ||
Settings::PLUGIN_UPDATE_AVAILABLE, | ||
__('Plugin update is available', 'bc-security'), | ||
[$form_helper, 'renderCheckbox'] | ||
); | ||
$settings_api_helper->addSettingsField( | ||
Settings::THEME_UPDATE_AVAILABLE, | ||
__('Theme update is available', 'bc-security'), | ||
[$form_helper, 'renderCheckbox'] | ||
); | ||
$settings_api_helper->addSettingsField( | ||
Settings::PLUGIN_DEACTIVATED, | ||
__('BC Security is deactivated', 'bc-security'), | ||
[$form_helper, 'renderCheckbox'] | ||
); | ||
|
||
// Section: Who to notify? | ||
$settings_api_helper->addSettingsSection( | ||
'who-to-notify', | ||
_x('Whom to send notification?', 'Settings section title', 'bc-security') | ||
); | ||
$settings_api_helper->addSettingsField( | ||
Settings::NOTIFY_SITE_ADMIN, | ||
__('Notify site admin', 'bc-security'), | ||
[$form_helper, 'renderCheckbox'], | ||
[ 'description' => sprintf(__('Currently: %s', 'bc-security'), get_option('admin_email')), ] | ||
); | ||
$settings_api_helper->addSettingsField( | ||
Settings::NOTIFICATION_RECIPIENTS, | ||
__('Send notifications to:', 'bc-security'), | ||
[$form_helper, 'renderTextArea'], | ||
[ 'description' => __('Enter one email per line.', 'bc-security'), ] | ||
); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
classes/BlueChip/Security/Modules/Notifications/Mailman.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
/** | ||
* @package BC_Security | ||
*/ | ||
|
||
namespace BlueChip\Security\Modules\Notifications; | ||
|
||
use BlueChip\Security\Modules\Notifications\AdminPage; | ||
|
||
|
||
abstract class Mailman | ||
{ | ||
/** @var string End-of-line character for email body. */ | ||
const EOL = "\r\n"; | ||
|
||
/** | ||
* Add some boilerplate to $subject and $message and send notification via wp_mail(). | ||
* | ||
* @see wp_mail() | ||
* | ||
* @param array|string $to Email address(es) of notification recipient(s). | ||
* @param string $subject Subject of notification. | ||
* @param array|string $message Body of notification. | ||
* @return bool True, if notifications was sent successfully, false otherwise. | ||
*/ | ||
public static function send($to, $subject, $message) | ||
{ | ||
return wp_mail( | ||
$to, | ||
self::formatSubject($subject), | ||
self::formatMessage(is_array($message) ? $message : [$message]) | ||
); | ||
} | ||
|
||
|
||
/** | ||
* Add plugin boilerplate to $message. | ||
* | ||
* @param array $message Message body as list of lines. | ||
* @return string | ||
*/ | ||
private static function formatMessage(array $message) | ||
{ | ||
$boilerplate_intro = [ | ||
sprintf( | ||
__('This email was sent from your website "%1$s" by BC Security plugin on %2$s at %3$s.'), | ||
get_option('blogname'), | ||
date_i18n('d.m.Y'), | ||
date_i18n('H:i:s') | ||
), | ||
'', | ||
]; | ||
|
||
$boilerplate_outro = [ | ||
'', | ||
sprintf( | ||
__('To change your notification settings, visit: %s', 'bc-security'), | ||
AdminPage::getPageUrl(AdminPage::SLUG) | ||
), | ||
]; | ||
|
||
return implode(self::EOL, array_merge($boilerplate_intro, $message, $boilerplate_outro)); | ||
} | ||
|
||
|
||
/** | ||
* Prepare subject for email (prepend site name and "BC Security Alert"). | ||
* | ||
* @param string $subject | ||
* @return string | ||
*/ | ||
private static function formatSubject($subject) | ||
{ | ||
return sprintf('[%s | %s] %s', get_option('blogname'), __('BC Security Alert', 'bc-security'), $subject); | ||
} | ||
} |
Oops, something went wrong.