/**
  * Runs when DG is uninstalled.
  */
 public static function uninstall()
 {
     if (!current_user_can('activate_plugins')) {
         return;
     }
     check_admin_referer('bulk-plugins');
     $blogs = array(null);
     if (is_multisite()) {
         $blogs = DG_Util::getBlogIds();
     }
     foreach ($blogs as $blog) {
         self::_uninstall($blog);
     }
     wp_clear_scheduled_hook(DG_Logger::PurgeLogsAction);
 }
Esempio n. 2
0
 /**
  * Truncates all blog logs to the current purge interval.
  *
  * TODO: This is a memory hog. Consider switching to stream filter.
  */
 public static function purgeExpiredEntries()
 {
     self::writeLog(DG_LogLevel::Detail, 'Beginning scheduled log file purge.');
     $blogs = array(null);
     if (is_multisite()) {
         $blogs = DG_Util::getBlogIds();
     }
     // truncate each blog's log file
     $time = time();
     foreach ($blogs as $blog) {
         $blog_num = !is_null($blog) ? $blog : get_current_blog_id();
         $options = self::getOptions($blog);
         $purge_interval = $options['purge_interval'] * DAY_IN_SECONDS;
         // purging is disabled for this blog
         if ($purge_interval <= 0) {
             continue;
         }
         // do perge for this blog
         $file = self::getLogFileName($blog_num);
         if (file_exists($file)) {
             $fp = @fopen($file, 'r');
             if ($fp !== false) {
                 $truncate = false;
                 $offset = 0;
                 // find the first non-expired entry
                 while (($fields = fgetcsv($fp)) !== false) {
                     if (!is_null($fields) && $time > $fields[0] + $purge_interval) {
                         // we've reached the recent entries -- nothing beyond here will be removed
                         break;
                     }
                     $truncate = true;
                     $offset = @ftell($fp);
                     if (false === $offset) {
                         break;
                     }
                 }
                 @fclose($fp);
                 // if any expired entries exist -- remove them from the file
                 if ($truncate) {
                     self::writeLog(DG_LogLevel::Detail, "Purging log entries for blog #{$blog_num}.");
                     $data = file_get_contents($file, false, null, $offset);
                     file_put_contents($file, $data, LOCK_EX);
                 }
             }
         }
     }
 }