Skip to content

Commit

Permalink
Implemented gh-17 LogRotation.
Browse files Browse the repository at this point in the history
  • Loading branch information
No3x committed Nov 23, 2014
1 parent 30971dd commit 4ce038b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
82 changes: 82 additions & 0 deletions WPML_LogRotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/**
* Log rotation for database.
* @author No3x
* @since 1.4
*/
class WPML_LogRotation {

const WPML_LOGROTATION_SCHEDULE_HOOK = 'LogRotationScheduleHook';
const WPML_LOGROTATION_SCHEDULE = 'LogRotationSchedule';

public static function init() {
add_action( self::WPML_LOGROTATION_SCHEDULE_HOOK , array('WPML_LogRotation', self::WPML_LOGROTATION_SCHEDULE) );
new WPML_LogRotation();
}

public function __construct() {
global $wpml_settings;
$this->unschedule();
if ( $wpml_settings['log-rotation-limit-amout'] == '1' || $wpml_settings['log-rotation-delete-time'] == '1' ) {
$this->schedule();
} else {
$this->unschedule();
}
}

/**
* Schedules an event.
* @since 1.4
*/
function schedule() {
if ( !wp_next_scheduled( self::WPML_LOGROTATION_SCHEDULE_HOOK ) ) {
wp_schedule_event( time(), 'hourly', self::WPML_LOGROTATION_SCHEDULE_HOOK );
}
}

/**
* Unschedules an event.
* @since 1.4
*/
function unschedule() {
$timestamp = wp_next_scheduled( self::WPML_LOGROTATION_SCHEDULE_HOOK );
wp_unschedule_event( $timestamp, self::WPML_LOGROTATION_SCHEDULE_HOOK );
}

/**
* Executes log rotation periodically.
* @since 1.4
*/
static function LogRotationSchedule() {
global $wpml_settings, $wpdb;
$tableName = WPML_Plugin::getTablename( 'mails' );

if ( $wpml_settings['log-rotation-limit-amout'] == '1') {
$keep = $wpml_settings['log-rotation-limit-amout-keep'];
if ( $keep > 0 ) {
$wpdb->query(
"DELETE p
FROM
$tableName AS p
JOIN
( SELECT mail_id
FROM $tableName
ORDER BY mail_id
LIMIT 1 OFFSET $keep
) AS lim
ON p.mail_id < lim.mail_id;"
);
}
}

if ( $wpml_settings['log-rotation-delete-time'] == '1') {
$days = $wpml_settings['log-rotation-delete-time-days'];
if ( $days > 0 ) {
$wpdb->query( "DELETE FROM $tableName WHERE DATEDIFF(now(), timestamp) >= $days" );
}
}
}
}

add_action('plugins_loaded', array( 'WPML_LogRotation', 'init' ), 11 );
2 changes: 1 addition & 1 deletion inc/redux/options-init.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public function setSections() {
'id' => 'can-see-submission-data',
'type' => 'select',
'data' => 'capabilities',
'default' => 'manage_options',
'default' => 'manage_options',
'title' => __('Can See Submission data', 'wpml'),
'subtitle' => __('Select the minimum role.', 'wpml'),

Expand Down
1 change: 1 addition & 0 deletions wp-mail-logging_init.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
function WPML_init($file) {

require_once('WPML_Plugin.php');
require_once('WPML_LogRotation.php');
require_once(plugin_dir_path( __FILE__ ) . 'inc/redux/admin-init.php');

$aPlugin = new WPML_Plugin();
Expand Down

0 comments on commit 4ce038b

Please sign in to comment.