From 4ce038bea3f17a797ac0043f0213ff914cd46443 Mon Sep 17 00:00:00 2001 From: No3x Date: Sun, 23 Nov 2014 01:16:44 +0100 Subject: [PATCH] Implemented gh-17 LogRotation. --- WPML_LogRotation.php | 82 ++++++++++++++++++++++++++++++++++++++ inc/redux/options-init.php | 2 +- wp-mail-logging_init.php | 1 + 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 WPML_LogRotation.php diff --git a/WPML_LogRotation.php b/WPML_LogRotation.php new file mode 100644 index 00000000..c21e22ec --- /dev/null +++ b/WPML_LogRotation.php @@ -0,0 +1,82 @@ +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 ); \ No newline at end of file diff --git a/inc/redux/options-init.php b/inc/redux/options-init.php index c42c82f0..74c7785f 100644 --- a/inc/redux/options-init.php +++ b/inc/redux/options-init.php @@ -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'), diff --git a/wp-mail-logging_init.php b/wp-mail-logging_init.php index 69844009..bd4dd7bc 100644 --- a/wp-mail-logging_init.php +++ b/wp-mail-logging_init.php @@ -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();