-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters