Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Email sender registrer for WordPress

## Changelog

### 1.4.5
* Added: optional disable for cron file logs via `BEA_SENDER_FILE_LOGGING` or the `bea_sender_file_logging_enabled` filter (`bea_sender_is_file_logging_enabled()` in the main plugin file).
* Changed: `Bea_Log` accepts a `$write_enabled` flag; when rotation runs, rotated files may be compressed to `.zip` when `ZipArchive` is available.

### 1.4.4
* Changed: use native `wp_mail` function to send emails

Expand Down
164 changes: 104 additions & 60 deletions bea_sender.php
Original file line number Diff line number Diff line change
@@ -1,112 +1,156 @@
<?php
/*
Plugin Name: BeApi - Sender
Description: Register email campaigns and send them trough a CRON
Author: BeApi
Domain Path: /languages/
Text Domain: bea_sender
Version: 1.4.4
Plugin Name: BeApi - Sender
Description: Register email campaigns and send them trough a CRON
Author: BeApi
Domain Path: /languages/
Text Domain: bea_sender
Version: 1.4.5
*/

// Database declarations
global $wpdb;
$wpdb->bea_s_campaigns = $wpdb->prefix.'bea_s_campaigns';
$wpdb->bea_s_receivers = $wpdb->prefix.'bea_s_receivers';
$wpdb->bea_s_re_ca = $wpdb->prefix.'bea_s_re_ca';
$wpdb->bea_s_contents = $wpdb->prefix.'bea_s_contents';
$wpdb->bea_s_attachments = $wpdb->prefix.'bea_s_attachments';
$wpdb->bea_s_campaigns = $wpdb->prefix . 'bea_s_campaigns';
$wpdb->bea_s_receivers = $wpdb->prefix . 'bea_s_receivers';
$wpdb->bea_s_re_ca = $wpdb->prefix . 'bea_s_re_ca';
$wpdb->bea_s_contents = $wpdb->prefix . 'bea_s_contents';
$wpdb->bea_s_attachments = $wpdb->prefix . 'bea_s_attachments';

// Add tables to the index of tables for WordPress
$wpdb->tables[] = 'bea_s_campaigns';
$wpdb->tables[] = 'bea_s_receivers';
$wpdb->tables[] = 'bea_s_re_ca';
$wpdb->tables[] = 'bea_s_contents';

define('BEA_SENDER_URL', plugin_dir_url ( __FILE__ ));
define('BEA_SENDER_DIR', plugin_dir_path( __FILE__ ));
define( 'BEA_SENDER_VER', '1.4.4' );
define( 'BEA_SENDER_URL', plugin_dir_url( __FILE__ ) );
define( 'BEA_SENDER_DIR', plugin_dir_path( __FILE__ ) );
define( 'BEA_SENDER_VER', '1.4.5' );
define( 'BEA_SENDER_PPP', '10' );
define( 'BEA_SENDER_DEFAULT_COUNTER', 100 );
define( 'BEA_SENDER_OPTION_NAME', 'bea_s-main' );
define( 'BEA_SENDER_EXPORT_OPTION_NAME', 'bea_s-export' );

/**
* Whether Sender cron jobs write Bea_Log files under WP_CONTENT_DIR.
*
* Disable with `define( 'BEA_SENDER_FILE_LOGGING', false );` in wp-config.php,
* or `add_filter( 'bea_sender_file_logging_enabled', '__return_false' );`.
*
* @return bool
*/
function bea_sender_is_file_logging_enabled() {
if ( defined( 'BEA_SENDER_FILE_LOGGING' ) ) {
return (bool) BEA_SENDER_FILE_LOGGING;
}

return (bool) apply_filters( 'bea_sender_file_logging_enabled', true );
}

// Function for easy load files
function _bea_sender_load_files($dir, $files, $prefix = '') {
foreach ($files as $file) {
if ( is_file($dir . $prefix . $file . ".php") ) {
require_once($dir . $prefix . $file . ".php");
function _bea_sender_load_files( $dir, $files, $prefix = '' ) {
foreach ( $files as $file ) {
if ( is_file( $dir . $prefix . $file . '.php' ) ) {
require_once $dir . $prefix . $file . '.php';
}
}
}

// Utils
_bea_sender_load_files( BEA_SENDER_DIR . 'inc/utils/', array(
'campaign',
'content',
'attachment',
'receiver',
'sender',
'bounce.email',
'export',
'receivers'
), 'class.' );
_bea_sender_load_files(
BEA_SENDER_DIR . 'inc/utils/',
array(
'campaign',
'content',
'attachment',
'receiver',
'sender',
'bounce.email',
'export',
'receivers',
),
'class.'
);

// Admin
if( is_admin( ) ) {
if ( is_admin() ) {

if( !class_exists( 'WP_List_Table' ) ) {
require (ABSPATH.'/wp-admin/includes/class-wp-list-table.php');
if ( ! class_exists( 'WP_List_Table' ) ) {
require ABSPATH . '/wp-admin/includes/class-wp-list-table.php';
}

_bea_sender_load_files( BEA_SENDER_DIR . 'inc/', array( 'admin' ), 'class.' );
_bea_sender_load_files( BEA_SENDER_DIR . 'inc/utils/', array(
'admin.table',
'admin.table.single',
'admin.bounce.tools'
), 'class.' );
_bea_sender_load_files(
BEA_SENDER_DIR . 'inc/utils/',
array(
'admin.table',
'admin.table.single',
'admin.bounce.tools',
),
'class.'
);
}

// Inc
_bea_sender_load_files( BEA_SENDER_DIR . 'inc/', array(
'client',
'cron',
), 'class.' );
_bea_sender_load_files(
BEA_SENDER_DIR . 'inc/',
array(
'client',
'cron',
),
'class.'
);

// Libs
_bea_sender_load_files( BEA_SENDER_DIR . 'inc/libs/wordpress-settings-api/', array(
'settings-api',
), 'class.' );

_bea_sender_load_files( BEA_SENDER_DIR . 'inc/libs/php-bounce/', array(
'phpmailer-bmh',
), 'class.' );

_bea_sender_load_files( BEA_SENDER_DIR . 'inc/libs/', array(
'log',
), 'class-' );
_bea_sender_load_files(
BEA_SENDER_DIR . 'inc/libs/wordpress-settings-api/',
array(
'settings-api',
),
'class.'
);

_bea_sender_load_files(
BEA_SENDER_DIR . 'inc/libs/php-bounce/',
array(
'phpmailer-bmh',
),
'class.'
);

_bea_sender_load_files(
BEA_SENDER_DIR . 'inc/libs/',
array(
'log',
),
'class-'
);

// Create tables on activation
register_activation_hook( __FILE__, array( 'Bea_Sender_Client', 'activation' ) );
register_uninstall_hook( __FILE__, array( 'Bea_Sender_Client', 'uninstall' ) );

add_action( 'plugins_loaded', 'Bea_sender_init' );

function Bea_sender_init( ) {
function Bea_sender_init() {
global $bea_sender, $bea_send_counter;

$bea_send_counter = apply_filters( 'bea_send_counter', BEA_SENDER_DEFAULT_COUNTER );
$bea_sender['client'] = new Bea_Sender_Client( );
$bea_send_counter = apply_filters( 'bea_send_counter', BEA_SENDER_DEFAULT_COUNTER );
$bea_sender['client'] = new Bea_Sender_Client();
new Bea_Sender_Cron();

if( is_admin( ) ) {
$bea_sender['admin'] = new Bea_Sender_Admin( );
$bea_sender['admin_bounce_tools'] = new BEA_Admin_Settings_Main( );
if ( is_admin() ) {
$bea_sender['admin'] = new Bea_Sender_Admin();
$bea_sender['admin_bounce_tools'] = new BEA_Admin_Settings_Main();
}

if ( defined( 'WP_CLI' ) ) {
_bea_sender_load_files( BEA_SENDER_DIR . 'inc/cli/', array(
'sender-command',
), 'class.' );
_bea_sender_load_files(
BEA_SENDER_DIR . 'inc/cli/',
array(
'sender-command',
),
'class.'
);

\WP_CLI::add_command( 'bea-sender-mail', Bea_Sender_Command::class );
}
Expand Down
74 changes: 58 additions & 16 deletions inc/libs/class-log.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
if( !class_exists( 'Bea_Log' ) ) {
if ( ! class_exists( 'Bea_Log' ) ) {
class Bea_Log {

private $file_path;
Expand All @@ -18,25 +18,31 @@ class Bea_Log {
const gravity_6 = 'Info';
const gravity_7 = 'Debug';

function __construct( $file_path, $file_extention = '.log' , $retention_size = '' ) {
if( !isset( $file_path ) || empty( $file_path ) ) {
/**
* @param string $file_path Base path without extension.
* @param string $file_extention Extension including dot.
* @param string $retention_size Max size in bytes before rotation.
* @param bool $write_enabled When false, log_this is a no-op.
*/
function __construct( $file_path, $file_extention = '.log', $retention_size = '', $write_enabled = true ) {
if ( ! isset( $file_path ) || empty( $file_path ) ) {
return false;
}

// Put file path
$this->file_path = $file_path;

// File extention
if( isset( $file_extention ) ) {
if ( isset( $file_extention ) ) {
$this->file_extention = $file_extention;
}

// Retention size
if( isset( $retention_size ) && !empty( $retention_size ) && (int)$retention_size > 0 ) {
if ( isset( $retention_size ) && ! empty( $retention_size ) && (int) $retention_size > 0 ) {
$this->retention_size = $retention_size;
}

$this->is_configured = true;
$this->is_configured = (bool) $write_enabled;
}

/**
Expand All @@ -45,36 +51,72 @@ function __construct( $file_path, $file_extention = '.log' , $retention_size = '
* @param $message : the message to log
* @return boolean : true on success
* @author Nicolas Juen
*
*/
public function log_this( $message, $type = '' ) {
if( $this->is_configured === false ) {
if ( ! $this->is_configured ) {
return false;
}

// Add the type
if( empty( $type ) ) {
if ( empty( $type ) ) {
$type = self::gravity_7;
}

// Make the file path
$file_path = $this->file_path.$this->file_extention;
$file_path = $this->file_path . $this->file_extention;

// If the file exists
if( is_file( $file_path ) ) {
if ( is_file( $file_path ) ) {
// Get file size
$size = filesize( $file_path );

// Check size
if( $size > $this->retention_size ) {
// Rename the file
rename( $file_path, $this->file_path.'-'.date( 'Y-m-d-H-i-s' ).$this->file_extention );
if ( $size > $this->retention_size ) {
$rotated_path = $this->file_path . '-' . date( 'Y-m-d-H-i-s' ) . $this->file_extention;
if ( rename( $file_path, $rotated_path ) ) {
$this->maybe_zip_rotated_file( $rotated_path );
}
}
}

// Log the error
error_log( date('[d-m-Y H:i:s]').'['.$type.'] '.$message."\n", 3, $file_path );
error_log( date( '[d-m-Y H:i:s]' ) . '[' . $type . '] ' . $message . "\n", 3, $file_path );
return true;
}

/**
* Compress a rotated log into a sibling .zip and remove the plain file on success.
*
* @param string $rotated_path Absolute path to the rotated log file.
*/
private function maybe_zip_rotated_file( $rotated_path ) {
if ( ! class_exists( 'ZipArchive' ) ) {
return;
}

if ( ! is_string( $rotated_path ) || '' === $rotated_path || ! is_file( $rotated_path ) ) {
return;
}

$zip_path = $rotated_path . '.zip';
$zip = new ZipArchive();

if ( true !== $zip->open( $zip_path, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) {
return;
}

$zip->addFile( $rotated_path, basename( $rotated_path ) );

if ( ! $zip->close() ) {
if ( is_file( $zip_path ) ) {
unlink( $zip_path );
}
return;
}

if ( is_file( $zip_path ) && filesize( $zip_path ) > 0 ) {
unlink( $rotated_path );
}
}
}
}
}
Loading