Skip to content

Commit

Permalink
Add the 1.2.9
Browse files Browse the repository at this point in the history
Add logs on the crons
  • Loading branch information
Rahe committed Feb 12, 2015
1 parent c5da17f commit 06959d0
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 4 deletions.
9 changes: 7 additions & 2 deletions bea_sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Author: BeApi
Domain Path: /languages/
Text Domain: bea_sender
Version: 1.2.8
Version: 1.2.9
*/

// Database declarations
Expand All @@ -24,7 +24,7 @@

define('BEA_SENDER_URL', plugin_dir_url ( __FILE__ ));
define('BEA_SENDER_DIR', plugin_dir_path( __FILE__ ));
define( 'BEA_SENDER_VER', '1.2.8' );
define( 'BEA_SENDER_VER', '1.2.9' );
define( 'BEA_SENDER_PPP', '10' );
define( 'BEA_SENDER_DEFAULT_COUNTER', 100 );
define( 'BEA_SENDER_OPTION_NAME', 'bea_s-main' );
Expand Down Expand Up @@ -82,6 +82,11 @@ function _bea_sender_load_files($dir, $files, $prefix = '') {
'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' ) );
Expand Down
80 changes: 80 additions & 0 deletions inc/libs/class-log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
if( !class_exists( 'Bea_Log' ) ) {
class Bea_Log {

private $file_path;
private $file_extention;
private $retention_size = '41943040';

private $is_configured = false;

// Levels of gravity for the logging
const gravity_0 = 'Emerg';
const gravity_1 = 'Alert';
const gravity_2 = 'Crit';
const gravity_3 = 'Err';
const gravity_4 = 'Warning';
const gravity_5 = 'Notice';
const gravity_6 = 'Info';
const gravity_7 = 'Debug';

function __construct( $file_path, $file_extention = '.log' , $retention_size = '' ) {
if( !isset( $file_path ) || empty( $file_path ) ) {
return false;
}

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

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

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

$this->is_configured = true;
}

/**
* Log data in multiple files when full
*
* @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 ) {
return false;
}

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

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

// If the file exists
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 );
}
}

// Log the error
error_log( date('[d-m-Y H:i:s]').'['.$type.'] '.$message."\n", 3, $file_path );
return true;
}
}
}
29 changes: 29 additions & 0 deletions inc/utils/class.bounce.email.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ class Bea_Sender_BounceEmail {
private static $locked = false;
private static $lock_file = '/lock-bounce.lock';

/**
* Log for the bounces
*
* @var Bea_Log
*/
private $log;

public function __construct() {
$this->bmh = new BounceMailHandler();
$this->log = new Bea_Log( WP_CONTENT_DIR.'/bea-sender-bounce-cron' );
}

public function bounce_init() {
Expand All @@ -17,6 +25,9 @@ public function bounce_init() {
return false;
}

// Log start
$this->log->log_this( 'Start Bounce Cron' );

// Ge teh user options
$options = get_option( 'bea_s-main' );

Expand All @@ -40,6 +51,9 @@ public function bounce_init() {

// Check the basic options
if ( empty( $host['mailhost'] ) || empty( $host['mailbox_username'] ) || empty( $host['mailbox_password'] ) ) {
// Log
$this->log->log_this( 'Bounce stopped : mailhost,mailbox_username or mailbox_password empty' );

// Unlock the file
self::unlock();

Expand Down Expand Up @@ -73,16 +87,25 @@ public function bounce_init() {
//$this->bmh->deleteMsgDate = '2009-01-05'; // format must be as
// 'yyyy-mm-dd'

// Log
$this->log->log_this( 'Open the mailbox' );

$this->bmh->openMailbox();
$this->bmh->action_function = array(
$this,
'callback_action'
);

$this->log->log_this( 'Process mailbox' );
$this->bmh->processMailbox();


$this->log->log_this( 'Delete mailbox' );
// Delete flag and do global deletes if true
$this->bmh->globalDelete();


$this->log->log_this( 'Process end' );
// Unlock the file
self::unlock();
}
Expand Down Expand Up @@ -113,11 +136,16 @@ public function callback_action( $msgnum, $bounce_type, $email, $subject, $xhead
/* @var $wpdb wpdb */
global $wpdb;

// Callback action
$this->log->log_this( sprintf( 'Callback action on message for %s', $email ) );
$this->log->log_this( sprintf( 'Email : %s | bounce_cat : %s | bounce_type : %s | bounce_no : %s', $email, $rule_cat, $bounce_type, $rule_no ) );

// The query for update bea_s_receivers table
$receiver_result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->bea_s_receivers WHERE email = %s", $email ) );

// Update the receiver if possible
if ( $receiver_result ) {
$this->log->log_this( sprintf( '%s found on database', $email ) );
$wpdb->update(
$wpdb->bea_s_receivers, array(
'current_status' => 'invalid',
Expand All @@ -138,6 +166,7 @@ public function callback_action( $msgnum, $bounce_type, $email, $subject, $xhead
$re_ca__result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->bea_s_re_ca WHERE id_campaign = %s AND id_receiver = %s", $xheader, $receiver_id ) );

if ( $re_ca__result ) {
$this->log->log_this( sprintf( '%s found on the _re_ca table', $email ) );
$wpdb->update(
$wpdb->bea_s_re_ca, array( 'current_status' => 'bounced' ), array(
'id_campaign' => $xheader,
Expand Down
19 changes: 17 additions & 2 deletions inc/utils/class.sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ class Bea_Sender_Sender {
private static $locked = false;
private static $lock_file = '/lock-send.lock';

function __construct( ) {}
/**
* Log for the bounces
*
* @var Bea_Log
*/
private $log;

function __construct( ) {
$this->log = new Bea_Log( WP_CONTENT_DIR.'/bea-sender-email-cron' );
}

/**
* @return array|bool
Expand All @@ -16,7 +25,10 @@ public function init( ) {
return false;
}

$this->log->log_this( 'Start sending' );

if( !$this->getCampaigns( ) ) {
$this->log->log_this( 'No campaign to send, exit.' );
// Unlock the file
self::unlock();
}
Expand All @@ -34,6 +46,7 @@ private function getCampaigns( ) {

$cols = $wpdb->get_col( "SELECT id FROM $wpdb->bea_s_campaigns WHERE current_status IN( '".implode( "','", Bea_Sender_Campaign::getAuthStatuses( ) )."' ) AND scheduled_from <= '".current_time( 'mysql' )."' ORDER BY add_date ASC" );

$this->log->log_this( sprintf( '%d campaigns to send', count( $cols ) ) );
if( !isset( $cols ) || empty( $cols ) ) {
$this->campaigns = array( );
return false;
Expand All @@ -55,7 +68,7 @@ private function sendCampaigns( ) {
if( $campaign->isData( ) !== true ) {
continue;
}

$this->log->log_this( sprintf( 'Send %s campaign', $campaign_id ) );
do_action( 'bea_sender_before_send_campaign', $campaign_id, $campaign );
// Make the sending
$results[] = $campaign->makeSend( );
Expand All @@ -66,6 +79,8 @@ private function sendCampaigns( ) {

// Unlock the file
self::unlock();

$this->log->log_this( 'End campaigns '.var_export( $results, true ) );

return $results;
}
Expand Down

0 comments on commit 06959d0

Please sign in to comment.