le = sprintf( esc_html__( 'Your Security Digest for %s', 'better-wp-security' ), '' . $period . '' );
break;
}
$data_proxy = new ITSEC_Notify_Data_Proxy( $data );
$mail = $nc->mail( 'digest' );
$mail->add_header(
$title,
$banner_title,
false,
sprintf( esc_html__( 'The following is a summary of security related activity on your site: %s', 'better-wp-security' ), '' . $mail->get_display_url() . '' )
);
$content = $mail->get_content();
/**
* Fires before the main content of the Security Digest is added.
*
* @param ITSEC_Mail $mail
* @param ITSEC_Notify_Data_Proxy $data_proxy
* @param int $last_sent
*/
do_action( 'itsec_security_digest_before', $mail, $data_proxy, $last_sent );
if ( $content !== $mail->get_content() ) {
$send_email = true;
}
$mail->add_section_heading( esc_html__( 'Lockouts', 'better-wp-security' ), 'lock' );
$user_count = $itsec_lockout->get_lockouts( 'user', array( 'after' => $last_sent, 'current' => false, 'return' => 'count' ) );
$host_count = $itsec_lockout->get_lockouts( 'host', array( 'after' => $last_sent, 'current' => false, 'return' => 'count' ) );
if ( $host_count > 0 || $user_count > 0 ) {
$mail->add_lockouts_summary( $user_count, $host_count );
$send_email = true;
} else {
$mail->add_text( esc_html__( 'No lockouts since the last email check.', 'better-wp-security' ) );
}
if ( $data_proxy->has_message( 'file-change' ) ) {
$mail->add_section_heading( esc_html__( 'File Changes', 'better-wp-security' ), 'folder' );
$mail->add_text( esc_html__( 'File changes detected on the site.', 'better-wp-security' ) );
$send_email = true;
}
if ( ! $send_email ) {
$content = $mail->get_content();
}
/**
* Fires when additional info should be attached to the Security Digest.
*
* @since 3.9.0
*
* @param ITSEC_Mail $mail
* @param ITSEC_Notify_Data_Proxy $data_proxy
* @param int $last_sent
*/
do_action( 'itsec_security_digest_attach_additional_info', $mail, $data_proxy, $last_sent );
if ( ! $send_email && $content !== $mail->get_content() ) {
$send_email = true;
}
$messages = $this->get_general_messages( $data );
if ( $messages ) {
$mail->add_section_heading( esc_html__( 'Messages', 'better-wp-security' ), 'message' );
foreach ( $messages as $message ) {
$mail->add_text( $message );
}
$send_email = true;
}
if ( ! $send_email ) {
return true;
}
$mail->add_details_box( sprintf(
esc_html__( 'For more details, %1$svisit your security logs%2$s', 'better-wp-security' ),
'',
''
) );
$mail->add_footer();
return $nc->send( 'digest', $mail );
}
/**
* Get general digest messages.
*
* @param array $data
*
* @return string[]
*/
private function get_general_messages( $data ) {
$messages = array();
foreach ( $data as $datum ) {
if ( ! is_array( $datum ) || ! isset( $datum['message'] ) ) {
continue;
}
if ( isset( $datum['type'] ) && 'general' !== $datum['type'] ) {
continue;
}
$messages[] = $datum['message'];
}
return $messages;
}
/**
* Used by the File Change Detection module to tell the notification system about found file changes.
*
* @since 2.6.0
*
* @return null
*/
public function register_file_change() {
_deprecated_function( __METHOD__, '3.9.0', 'ITSEC_Notification_Center::enqueue_data' );
ITSEC_Core::get_notification_center()->enqueue_data( 'digest', array( 'type' => 'file-change' ) );
}
/**
* Set HTML content type for email
*
* @since 4.5
*
* @return string html content type
*/
public function wp_mail_content_type() {
return 'text/html';
}
}
class ITSEC_Notify_Data_Proxy {
/** @var array */
private $data;
/**
* ITSEC_Notify_Data_Proxy constructor.
*
* @param array $data
*/
public function __construct( $data ) { $this->data = $data; }
/**
* Check for a queued message.
*
* @param string $type
*
* @return array|null
*/
public function has_message( $type ) {
foreach ( $this->data as $datum ) {
if ( ! is_array( $datum ) ) {
continue;
}
if ( isset( $datum['type'] ) && $type === $datum['type'] ) {
return $datum;
}
}
return null;
}
/**
* Get all messages of a given type.
*
* @param string $type
*
*' @return array
*/
public function get_messages_of_type( $type ) {
$of_type = array();
foreach ( $this->data as $datum ) {
if ( ! is_array( $datum ) ) {
continue;
}
if ( isset( $datum['type'] ) && $type === $datum['type'] ) {
$of_type[] = $datum;
}
}
return $of_type;
}
}