コード例 #1
0
 /**
  * Delete sessions from the database.
  *
  * ## OPTIONS
  *
  * [--all]
  * : Flag whether or not to purge all sessions from the database.
  *
  * [--batch=<batch>]
  * : Set the batch size for deleting old sessions
  *
  * [--limit=<limit>]
  * : Delete just this number of old sessions
  *
  * ## EXAMPLES
  *
  *      wp session delete
  *      wp session delete [--batch=<batch>]
  *      wp session delete [--limit=<limit>]
  *      wp session delete [--all]
  *
  * @synopsis [--all] [--batch=<batch>] [--limit=<limit>]
  *
  * @param array $args
  * @param array $assoc_args
  */
 public function delete($args, $assoc_args)
 {
     if (isset($assoc_args['limit'])) {
         $limit = absint($assoc_args['limit']);
         $count = WP_Session_Utils::delete_old_sessions($limit);
         if ($count > 0) {
             \WP_CLI::line(sprintf('Deleted %d sessions.', $count));
         }
         // Clear memory
         self::free_up_memory();
         return;
     }
     // Determine if we're deleting all sessions or just a subset.
     $all = isset($assoc_args['all']);
     /**
      * Determine the size of each batch for deletion.
      *
      * @param int
      */
     $batch = isset($assoc_args['batch']) ? absint($assoc_args['batch']) : apply_filters('wp_session_delete_batch_size', 1000);
     switch ($all) {
         case true:
             $count = WP_Session_Utils::delete_all_sessions();
             \WP_CLI::line(sprintf('Deleted all %d sessions.', $count));
             break;
         case false:
             do {
                 $count = WP_Session_Utils::delete_old_sessions($batch);
                 if ($count > 0) {
                     \WP_CLI::line(sprintf('Deleted %d sessions.', $count));
                 }
                 // Clear memory
                 self::free_up_memory();
             } while ($count > 0);
             break;
     }
 }
コード例 #2
0
/**
 * Clean up expired sessions by removing data and their expiration entries from
 * the WordPress options table.
 *
 * This method should never be called directly and should instead be triggered as part
 * of a scheduled task or cron job.
 */
function wp_session_cleanup()
{
    if (defined('WP_SETUP_CONFIG')) {
        return;
    }
    if (!defined('WP_INSTALLING')) {
        /**
         * Determine the size of each batch for deletion.
         *
         * @param int
         */
        $batch_size = apply_filters('wp_session_delete_batch_size', 1000);
        // Delete a batch of old sessions
        WP_Session_Utils::delete_old_sessions($batch_size);
    }
    // Allow other plugins to hook in to the garbage collection process.
    do_action('wp_session_cleanup');
}