v2.0.0
This commit is contained in:
parent
c98dcb7b50
commit
be6f679724
5 changed files with 13721 additions and 5952 deletions
File diff suppressed because it is too large
Load diff
222
includes/class-robotstxt-smtp-encryption.php
Normal file
222
includes/class-robotstxt-smtp-encryption.php
Normal file
|
|
@ -0,0 +1,222 @@
|
|||
<?php
|
||||
/**
|
||||
* Encryption utility for ROBOTSTXT SMTP plugin.
|
||||
*
|
||||
* Provides AES-256-GCM encryption for sensitive data using WordPress authentication salts.
|
||||
* Encryption keys are derived using PBKDF2 (10,000 iterations) for enhanced security.
|
||||
*
|
||||
* @package Robotstxt_SMTP
|
||||
* @since 1.2.1
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class Robotstxt_SMTP_Encryption
|
||||
*
|
||||
* Handles encryption and decryption of sensitive credentials.
|
||||
*
|
||||
* @since 1.2.1
|
||||
*/
|
||||
class Robotstxt_SMTP_Encryption {
|
||||
|
||||
/**
|
||||
* Encryption cipher method.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private const CIPHER = 'aes-256-gcm';
|
||||
|
||||
/**
|
||||
* Prefix for encrypted values to identify them.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private const PREFIX = 'enc:v1:';
|
||||
|
||||
/**
|
||||
* Encrypts a plaintext string using AES-256-GCM.
|
||||
*
|
||||
* Requires AUTH_KEY and AUTH_SALT to be defined in wp-config.php.
|
||||
* Returns the original plaintext if encryption is not available.
|
||||
*
|
||||
* @since 1.2.1
|
||||
*
|
||||
* @param string $plaintext The plaintext to encrypt.
|
||||
* @return string Encrypted string with prefix, or original plaintext if encryption unavailable.
|
||||
*/
|
||||
public static function encrypt( string $plaintext ): string {
|
||||
// Return empty strings as-is.
|
||||
if ( '' === $plaintext ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Check if already encrypted.
|
||||
if ( self::is_encrypted( $plaintext ) ) {
|
||||
return $plaintext;
|
||||
}
|
||||
|
||||
// Check if encryption is available.
|
||||
if ( ! self::is_encryption_available() ) {
|
||||
return $plaintext;
|
||||
}
|
||||
|
||||
// Generate random IV (Initialization Vector).
|
||||
$iv_length = openssl_cipher_iv_length( self::CIPHER );
|
||||
$iv = openssl_random_pseudo_bytes( $iv_length );
|
||||
|
||||
// Derive encryption key from WordPress constants.
|
||||
$key = self::get_encryption_key();
|
||||
|
||||
// Encrypt the data.
|
||||
$tag = '';
|
||||
$ciphertext = openssl_encrypt(
|
||||
$plaintext,
|
||||
self::CIPHER,
|
||||
$key,
|
||||
OPENSSL_RAW_DATA,
|
||||
$iv,
|
||||
$tag,
|
||||
'',
|
||||
16
|
||||
);
|
||||
|
||||
if ( false === $ciphertext ) {
|
||||
// Encryption failed, return plaintext.
|
||||
return $plaintext;
|
||||
}
|
||||
|
||||
// Combine IV + ciphertext + tag and encode.
|
||||
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Used for encryption, not obfuscation.
|
||||
$encrypted = base64_encode( $iv . $ciphertext . $tag );
|
||||
|
||||
return self::PREFIX . $encrypted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypts an encrypted string.
|
||||
*
|
||||
* Returns the original string if it's not encrypted or decryption fails.
|
||||
*
|
||||
* @since 1.2.1
|
||||
*
|
||||
* @param string $encrypted The encrypted string (with prefix).
|
||||
* @return string Decrypted plaintext or original string if not encrypted.
|
||||
*/
|
||||
public static function decrypt( string $encrypted ): string {
|
||||
// Return empty strings as-is.
|
||||
if ( '' === $encrypted ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Check if it's actually encrypted.
|
||||
if ( ! self::is_encrypted( $encrypted ) ) {
|
||||
return $encrypted;
|
||||
}
|
||||
|
||||
// Check if decryption is available.
|
||||
if ( ! self::is_encryption_available() ) {
|
||||
// Can't decrypt without proper setup, return as-is.
|
||||
return $encrypted;
|
||||
}
|
||||
|
||||
// Remove prefix.
|
||||
$encrypted_data = substr( $encrypted, strlen( self::PREFIX ) );
|
||||
|
||||
// Decode from base64.
|
||||
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- Used for decryption, not obfuscation.
|
||||
$decoded = base64_decode( $encrypted_data, true );
|
||||
|
||||
if ( false === $decoded ) {
|
||||
// Invalid base64, return original.
|
||||
return $encrypted;
|
||||
}
|
||||
|
||||
// Extract IV, ciphertext, and tag.
|
||||
$iv_length = openssl_cipher_iv_length( self::CIPHER );
|
||||
$tag_length = 16;
|
||||
|
||||
if ( strlen( $decoded ) < ( $iv_length + $tag_length + 1 ) ) {
|
||||
// Data too short, corrupted.
|
||||
return $encrypted;
|
||||
}
|
||||
|
||||
$iv = substr( $decoded, 0, $iv_length );
|
||||
$tag = substr( $decoded, -$tag_length );
|
||||
$ciphertext = substr( $decoded, $iv_length, -$tag_length );
|
||||
|
||||
// Derive encryption key.
|
||||
$key = self::get_encryption_key();
|
||||
|
||||
// Decrypt.
|
||||
$plaintext = openssl_decrypt(
|
||||
$ciphertext,
|
||||
self::CIPHER,
|
||||
$key,
|
||||
OPENSSL_RAW_DATA,
|
||||
$iv,
|
||||
$tag
|
||||
);
|
||||
|
||||
if ( false === $plaintext ) {
|
||||
// Decryption failed (wrong key, corrupted data, etc).
|
||||
return $encrypted;
|
||||
}
|
||||
|
||||
return $plaintext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a string is encrypted (has the encryption prefix).
|
||||
*
|
||||
* @since 1.2.1
|
||||
*
|
||||
* @param string $value The value to check.
|
||||
* @return bool True if encrypted, false otherwise.
|
||||
*/
|
||||
public static function is_encrypted( string $value ): bool {
|
||||
return 0 === strpos( $value, self::PREFIX );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if encryption is available and properly configured.
|
||||
*
|
||||
* @since 1.2.1
|
||||
*
|
||||
* @return bool True if encryption is available, false otherwise.
|
||||
*/
|
||||
public static function is_encryption_available(): bool {
|
||||
// Check if OpenSSL extension is loaded.
|
||||
if ( ! extension_loaded( 'openssl' ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if AES-256-GCM is supported.
|
||||
if ( ! in_array( self::CIPHER, openssl_get_cipher_methods(), true ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if WordPress AUTH_KEY and AUTH_SALT are defined.
|
||||
if ( ! defined( 'AUTH_KEY' ) || '' === AUTH_KEY || ! defined( 'AUTH_SALT' ) || '' === AUTH_SALT ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Derives a 256-bit encryption key from WordPress AUTH_KEY and AUTH_SALT.
|
||||
*
|
||||
* @since 1.2.1
|
||||
*
|
||||
* @return string Binary encryption key (32 bytes for AES-256).
|
||||
*/
|
||||
private static function get_encryption_key(): string {
|
||||
$key_material = AUTH_KEY . AUTH_SALT;
|
||||
$salt = defined( 'SECURE_AUTH_KEY' ) ? SECURE_AUTH_KEY : 'robotstxt_smtp_salt';
|
||||
|
||||
return hash_pbkdf2( 'sha256', $key_material, $salt, 10000, 32, true );
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,107 +1,109 @@
|
|||
<?php
|
||||
/**
|
||||
* SMTP diagnostics client.
|
||||
*
|
||||
* @package Robotstxt_SMTP
|
||||
*/
|
||||
|
||||
namespace Robotstxt_SMTP\Admin;
|
||||
|
||||
use PHPMailer\PHPMailer\SMTP;
|
||||
|
||||
/**
|
||||
* SMTP client wrapper used for diagnostics.
|
||||
*/
|
||||
class SMTP_Diagnostics_Client extends SMTP {
|
||||
/**
|
||||
* Tracks whether the most recent command timed out.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private bool $last_command_timed_out = false;
|
||||
|
||||
/**
|
||||
* Executes an SMTP command and returns the raw reply.
|
||||
*
|
||||
* @param string $name Command name.
|
||||
* @param string $command_string Full command string.
|
||||
* @param array<int,int> $expected_codes Expected success codes.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function execute_command( string $name, string $command_string, array $expected_codes = array( 250 ) ): array {
|
||||
$this->last_command_timed_out = false;
|
||||
$this->reset_error_state();
|
||||
|
||||
$expected = ! empty( $expected_codes ) ? array_map( 'intval', $expected_codes ) : array( 250 );
|
||||
|
||||
$success = parent::sendCommand( $name, $command_string, $expected );
|
||||
|
||||
if ( ! $success ) {
|
||||
$this->last_command_timed_out = $this->error_indicates_timeout( parent::getError() );
|
||||
}
|
||||
|
||||
return array(
|
||||
'success' => $success,
|
||||
'reply' => $this->getLastReply(),
|
||||
'timed_out' => $this->last_command_timed_out,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether the last command timed out.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function did_last_command_timeout(): bool {
|
||||
return $this->last_command_timed_out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates STARTTLS while tracking timeout state.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function startTLS(): bool {
|
||||
$this->last_command_timed_out = false;
|
||||
$this->reset_error_state();
|
||||
|
||||
$result = parent::startTLS();
|
||||
|
||||
if ( ! $result ) {
|
||||
$this->last_command_timed_out = $this->error_indicates_timeout( parent::getError() );
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the stored error state.
|
||||
*/
|
||||
private function reset_error_state(): void {
|
||||
parent::setError( '' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether an error array represents a timeout.
|
||||
*
|
||||
* @param array<string, mixed> $error Error details.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function error_indicates_timeout( array $error ): bool {
|
||||
foreach ( array( 'error', 'detail', 'smtp_code_ex' ) as $key ) {
|
||||
if ( empty( $error[ $key ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = (string) $error[ $key ];
|
||||
|
||||
if ( false !== stripos( $value, 'timed out' ) || false !== stripos( $value, 'timed-out' ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* SMTP diagnostics client.
|
||||
*
|
||||
* @package Robotstxt_SMTP
|
||||
*/
|
||||
|
||||
namespace Robotstxt_SMTP\Admin;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
use PHPMailer\PHPMailer\SMTP;
|
||||
|
||||
/**
|
||||
* SMTP client wrapper used for diagnostics.
|
||||
*/
|
||||
class SMTP_Diagnostics_Client extends SMTP {
|
||||
/**
|
||||
* Tracks whether the most recent command timed out.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private bool $last_command_timed_out = false;
|
||||
|
||||
/**
|
||||
* Executes an SMTP command and returns the raw reply.
|
||||
*
|
||||
* @param string $name Command name.
|
||||
* @param string $command_string Full command string.
|
||||
* @param array<int,int> $expected_codes Expected success codes.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function execute_command( string $name, string $command_string, array $expected_codes = array( 250 ) ): array {
|
||||
$this->last_command_timed_out = false;
|
||||
$this->reset_error_state();
|
||||
|
||||
$expected = ! empty( $expected_codes ) ? array_map( 'intval', $expected_codes ) : array( 250 );
|
||||
|
||||
$success = parent::sendCommand( $name, $command_string, $expected );
|
||||
|
||||
if ( ! $success ) {
|
||||
$this->last_command_timed_out = $this->error_indicates_timeout( parent::getError() );
|
||||
}
|
||||
|
||||
return array(
|
||||
'success' => $success,
|
||||
'reply' => $this->getLastReply(),
|
||||
'timed_out' => $this->last_command_timed_out,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether the last command timed out.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function did_last_command_timeout(): bool {
|
||||
return $this->last_command_timed_out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates STARTTLS while tracking timeout state.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function startTLS(): bool {
|
||||
$this->last_command_timed_out = false;
|
||||
$this->reset_error_state();
|
||||
|
||||
$result = parent::startTLS();
|
||||
|
||||
if ( ! $result ) {
|
||||
$this->last_command_timed_out = $this->error_indicates_timeout( parent::getError() );
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the stored error state.
|
||||
*/
|
||||
private function reset_error_state(): void {
|
||||
parent::setError( '' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether an error array represents a timeout.
|
||||
*
|
||||
* @param array<string, mixed> $error Error details.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function error_indicates_timeout( array $error ): bool {
|
||||
foreach ( array( 'error', 'detail', 'smtp_code_ex' ) as $key ) {
|
||||
if ( empty( $error[ $key ] ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = (string) $error[ $key ];
|
||||
|
||||
if ( false !== stripos( $value, 'timed out' ) || false !== stripos( $value, 'timed-out' ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
287
includes/class-statistics.php
Normal file
287
includes/class-statistics.php
Normal file
|
|
@ -0,0 +1,287 @@
|
|||
<?php
|
||||
/**
|
||||
* Email statistics tracking for ROBOTSTXT SMTP plugin.
|
||||
*
|
||||
* Provides daily email statistics with support for WordPress Multisite.
|
||||
* Tracks emails sent per day per site and handles automatic cleanup.
|
||||
*
|
||||
* @package Robotstxt_SMTP
|
||||
* @since 1.3.0
|
||||
*/
|
||||
|
||||
namespace Robotstxt_SMTP;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Class Statistics
|
||||
*
|
||||
* Handles email statistics tracking and retrieval.
|
||||
*
|
||||
* @since 1.3.0
|
||||
*/
|
||||
class Statistics {
|
||||
|
||||
/**
|
||||
* Database table name (without prefix).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private const TABLE_NAME = 'robotstxt_smtp_stats';
|
||||
|
||||
/**
|
||||
* Database version for migration tracking.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private const DB_VERSION = '1.0';
|
||||
|
||||
/**
|
||||
* Option name for storing database version.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private const DB_VERSION_OPTION = 'robotstxt_smtp_stats_db_version';
|
||||
|
||||
/**
|
||||
* Singleton instance.
|
||||
*
|
||||
* @var Statistics|null
|
||||
*/
|
||||
private static ?Statistics $instance = null;
|
||||
|
||||
/**
|
||||
* Gets the singleton instance.
|
||||
*
|
||||
* @return Statistics
|
||||
*/
|
||||
public static function get_instance(): Statistics {
|
||||
if ( null === self::$instance ) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Private constructor to enforce singleton.
|
||||
*/
|
||||
private function __construct() {
|
||||
$this->maybe_create_table();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the full table name with WordPress prefix.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_table_name(): string {
|
||||
global $wpdb;
|
||||
return $wpdb->base_prefix . self::TABLE_NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the statistics table if it doesn't exist.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function maybe_create_table(): void {
|
||||
$installed_version = get_site_option( self::DB_VERSION_OPTION, '0' );
|
||||
|
||||
if ( version_compare( $installed_version, self::DB_VERSION, '>=' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
global $wpdb;
|
||||
$table_name = self::get_table_name();
|
||||
$charset_collate = $wpdb->get_charset_collate();
|
||||
|
||||
$sql = "CREATE TABLE {$table_name} (
|
||||
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
||||
blog_id bigint(20) unsigned NOT NULL DEFAULT 1,
|
||||
date date NOT NULL,
|
||||
emails_sent bigint(20) unsigned NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY blog_date (blog_id, date),
|
||||
KEY date_idx (date),
|
||||
KEY blog_id_idx (blog_id)
|
||||
) {$charset_collate};";
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
||||
dbDelta( $sql );
|
||||
|
||||
update_site_option( self::DB_VERSION_OPTION, self::DB_VERSION );
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the email counter for today.
|
||||
*
|
||||
* @param int|null $blog_id Blog ID (defaults to current blog).
|
||||
*
|
||||
* @return bool True on success, false on failure.
|
||||
*/
|
||||
public function increment_today( ?int $blog_id = null ): bool {
|
||||
global $wpdb;
|
||||
|
||||
if ( null === $blog_id ) {
|
||||
$blog_id = get_current_blog_id();
|
||||
}
|
||||
|
||||
$table_name = self::get_table_name();
|
||||
$today = current_time( 'Y-m-d' );
|
||||
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||
$result = $wpdb->query(
|
||||
$wpdb->prepare(
|
||||
"INSERT INTO {$table_name} (blog_id, date, emails_sent)
|
||||
VALUES (%d, %s, 1)
|
||||
ON DUPLICATE KEY UPDATE emails_sent = emails_sent + 1",
|
||||
$blog_id,
|
||||
$today
|
||||
)
|
||||
);
|
||||
|
||||
return false !== $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets statistics for a specific date range.
|
||||
*
|
||||
* @param string $start_date Start date (Y-m-d format).
|
||||
* @param string $end_date End date (Y-m-d format).
|
||||
* @param int|null $blog_id Blog ID (null for all sites).
|
||||
*
|
||||
* @return array<int, object> Array of statistics objects.
|
||||
*/
|
||||
public function get_stats( string $start_date, string $end_date, ?int $blog_id = null ): array {
|
||||
global $wpdb;
|
||||
|
||||
$table_name = self::get_table_name();
|
||||
|
||||
if ( null !== $blog_id ) {
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||
$results = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
"SELECT blog_id, date, emails_sent
|
||||
FROM {$table_name}
|
||||
WHERE blog_id = %d
|
||||
AND date BETWEEN %s AND %s
|
||||
ORDER BY date ASC",
|
||||
$blog_id,
|
||||
$start_date,
|
||||
$end_date
|
||||
)
|
||||
);
|
||||
} else {
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||
$results = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
"SELECT blog_id, date, emails_sent
|
||||
FROM {$table_name}
|
||||
WHERE date BETWEEN %s AND %s
|
||||
ORDER BY date ASC, blog_id ASC",
|
||||
$start_date,
|
||||
$end_date
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return is_array( $results ) ? $results : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets total emails sent for a date range.
|
||||
*
|
||||
* @param string $start_date Start date (Y-m-d format).
|
||||
* @param string $end_date End date (Y-m-d format).
|
||||
* @param int|null $blog_id Blog ID (null for all sites).
|
||||
*
|
||||
* @return int Total emails sent.
|
||||
*/
|
||||
public function get_total( string $start_date, string $end_date, ?int $blog_id = null ): int {
|
||||
global $wpdb;
|
||||
|
||||
$table_name = self::get_table_name();
|
||||
|
||||
if ( null !== $blog_id ) {
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||
$total = $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
"SELECT SUM(emails_sent)
|
||||
FROM {$table_name}
|
||||
WHERE blog_id = %d
|
||||
AND date BETWEEN %s AND %s",
|
||||
$blog_id,
|
||||
$start_date,
|
||||
$end_date
|
||||
)
|
||||
);
|
||||
} else {
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||
$total = $wpdb->get_var(
|
||||
$wpdb->prepare(
|
||||
"SELECT SUM(emails_sent)
|
||||
FROM {$table_name}
|
||||
WHERE date BETWEEN %s AND %s",
|
||||
$start_date,
|
||||
$end_date
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return absint( $total );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets statistics aggregated by blog for multisite.
|
||||
*
|
||||
* @param string $start_date Start date (Y-m-d format).
|
||||
* @param string $end_date End date (Y-m-d format).
|
||||
*
|
||||
* @return array<int, object> Array with blog_id and total_emails.
|
||||
*/
|
||||
public function get_stats_by_blog( string $start_date, string $end_date ): array {
|
||||
global $wpdb;
|
||||
|
||||
$table_name = self::get_table_name();
|
||||
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||
$results = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
"SELECT blog_id, SUM(emails_sent) as total_emails
|
||||
FROM {$table_name}
|
||||
WHERE date BETWEEN %s AND %s
|
||||
GROUP BY blog_id
|
||||
ORDER BY total_emails DESC",
|
||||
$start_date,
|
||||
$end_date
|
||||
)
|
||||
);
|
||||
|
||||
return is_array( $results ) ? $results : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes statistics older than specified days.
|
||||
*
|
||||
* @param int $days Number of days to retain.
|
||||
*
|
||||
* @return int Number of rows deleted.
|
||||
*/
|
||||
public function cleanup_old_stats( int $days ): int {
|
||||
global $wpdb;
|
||||
|
||||
$table_name = self::get_table_name();
|
||||
$cutoff_date = gmdate( 'Y-m-d', strtotime( "-{$days} days" ) );
|
||||
|
||||
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
|
||||
$deleted = $wpdb->query(
|
||||
$wpdb->prepare(
|
||||
"DELETE FROM {$table_name} WHERE date < %s",
|
||||
$cutoff_date
|
||||
)
|
||||
);
|
||||
|
||||
return absint( $deleted );
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue