<?php
/*
Simple:Press Forum
Cron - global code
$LastChangedDate: 2010-03-12 12:19:16 -0700 (Fri, 12 Mar 2010) $
$Rev: 3703 $
*/

if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) {
	echo (__('Access Denied', "sforum"));
	die();
}

function spf_remove_pms()
{
	global $wpdb;

	# make sure auto removal is enabled
	$sfpm = sf_get_option('sfpm');
	if ($sfpm['sfpmremove'])
	{
		$wpdb->query("DELETE FROM ".SFMESSAGES." WHERE sent_date < DATE_SUB(NOW(), INTERVAL ".$sfpm['sfpmkeep']." DAY)");
	} else {
		wp_clear_scheduled_hook('spf_cron_pm');
	}
}

function spf_remove_users()
{
	require_once(ABSPATH.'wp-admin/includes/user.php');

	global $wpdb;

	# make sure auto removal is enabled
	$sfuser = sf_get_option('sfuserremoval');
	if ($sfuser['sfuserremove'])
	{
		# see if removing users with no posts
		if ($sfuser['sfusernoposts'])
		{
			$users = $wpdb->get_results("SELECT ID FROM ".SFUSERS."
										JOIN ".SFMEMBERS." on ".SFUSERS.".ID = ".SFMEMBERS.".user_id
										WHERE user_registered < DATE_SUB(NOW(), INTERVAL ".$sfuser['sfuserperiod']." DAY) AND posts=0");
			if ($users)
			{
				foreach ($users as $user)
				{
					wp_delete_user($user->ID);
				}
			}
		}

		# see if removing inactive users
		if ($sfuser['sfuserinactive'])
		{
			$users = $wpdb->get_results("SELECT user_id FROM ".SFMEMBERS."
										WHERE lastvisit < DATE_SUB(NOW(), INTERVAL ".$sfuser['sfuserperiod']." DAY)");
			if ($users)
			{
				foreach ($users as $user)
				{
					wp_delete_user($user->user_id);
				}
			}
		}
		#$wpdb->query("DELETE FROM ".SFMESSAGES." WHERE sent_date < DATE_SUB(NOW(), INTERVAL ".$sfpm['sfpmkeep']." DAY)");
	} else {
		wp_clear_scheduled_hook('spf_cron_user');
	}
}

function spf_generate_sitemap()
{
    if (sf_get_option('sfbuildsitemap') == 3)
    {
        do_action("sm_rebuild");
    } else {
    	wp_clear_scheduled_hook('spf_cron_sitemap');
    }
}

?>