get_var("SELECT count(ID) FROM ".$wpdb->prefix."activity");
$act_delete = $act_count - $options_act['act_prune'];
if ($act_delete > 0) {
$wpdb->query("DELETE FROM ".$wpdb->prefix."activity ORDER BY id ASC LIMIT ".$delete);
}
}
add_action('act_cron_install','act_cron');
function act_desactive() {
wp_clear_scheduled_hook('act_cron_install');
}
function act_install()
{
global $wpdb;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$table = $wpdb->prefix."activity";
$act_structure = "CREATE TABLE `".$table."` (
`id` int(9) NOT NULL auto_increment,
`user_id` bigint(20) NOT NULL,
`act_type` varchar(20) NOT NULL,
`act_date` datetime default NULL,
`act_params` text,
UNIQUE KEY `id` (`id`),
KEY `user_id` (`user_id`),
KEY `act_date` (`act_date`)
);";
dbDelta($act_structure);
$options_act['act_prune'] = '1000';
$options_act['act_feed_display'] = false;
$options_act['act_date_format'] = 'yyyy/mm/dd';
$options_act['act_date_relative']= true;
$options_act['act_connect']= true;
$options_act['act_profiles']= true;
$options_act['act_posts']= true;
$options_act['act_comments']= true;
$options_act['act_links']= true;
$options_act['act_feed_connect']= false;
$options_act['act_feed_profiles']= true;
$options_act['act_feed_posts']= true;
$options_act['act_feed_comments']= true;
$options_act['act_feed_links']= true;
$options_act['act_icons']= 'g';
$options_act['act_old']= true;
add_option('act_settings', $options_act);
wp_schedule_event(time(), 'daily', 'act_cron_install');
}
register_activation_hook( __FILE__, 'act_install' );
register_deactivation_hook(__FILE__, 'act_desactive');
//we add actions to hooks to log their events
add_action('send_headers', 'act_session');
add_action('profile_update', 'act_profile_edit');
add_action('publish_post', 'act_post_add');
add_action('comment_post', 'act_comment_add');
add_action('add_link', 'act_link_add');
function act_header(){
$altcss = TEMPLATEPATH.'/wp-activity.css';
echo ' ' . "\n";
}
add_action('wp_head', 'act_header');
function act_profile_option(){
global $wpdb, $user_ID;
$act_private = get_usermeta($user_ID, 'act_private');
?>
query("INSERT INTO ".$wpdb->prefix."activity (user_id, act_type, act_date, act_params) VALUES($user_ID,'CONNECT', '".$act_time."', '')");
$act_url = parse_url(get_option('home'));
}
}
}
function act_reinit(){
if ($_COOKIE['act_logged']){ setcookie ("act_logged", "", time() - 3600);}
}
add_action('wp_login', 'act_reinit');
add_action('wp_logout', 'act_reinit');
function act_profile_edit($act_user){
global $wpdb, $user_ID, $options_act;
if ($options_act['act_profiles'] and !get_usermeta($user_ID, 'act_private')){
$act_time=mysql2date("Y-m-d H:i:s", time());
$wpdb->query("INSERT INTO ".$wpdb->prefix."activity (user_id, act_type, act_date, act_params) VALUES($user_ID, 'PROFILE_EDIT', '".$act_time."', $act_user)");
}
}
function act_post_add($act_post){
global $wpdb, $user_ID, $options_act;
if ($options_act['act_posts'] and !get_usermeta($user_ID, 'act_private')){
$act_time=mysql2date("Y-m-d H:i:s", time());
if ($wpdb->get_var("SELECT COUNT(*) FROM ".$wpdb->prefix."activity WHERE act_params=$act_post AND act_type='POST_ADD'") > 0){
$act_type='POST_EDIT';
}else{
$act_type='POST_ADD';
}
$wpdb->query("INSERT INTO ".$wpdb->prefix."activity (user_id, act_type, act_date, act_params) VALUES($user_ID, '".$act_type."', '".$act_time."', $act_post)");
}
}
function act_comment_add($act_comment){
global $wpdb, $user_ID, $options_act;
if ($options_act['act_comments'] and !get_usermeta($user_ID, 'act_private')){
$act_time=mysql2date("Y-m-d H:i:s", time());
$wpdb->query("INSERT INTO ".$wpdb->prefix."activity (user_id, act_type, act_date, act_params) VALUES($user_ID,'COMMENT_ADD', '".$act_time."', $act_comment)");
}
}
function act_link_add($act_link){
global $wpdb, $user_ID, $options_act;
if ($options_act['act_links'] and !get_usermeta($user_ID, 'act_private')){
$act_time=mysql2date("Y-m-d H:i:s", time());
$wpdb->query("INSERT INTO ".$wpdb->prefix."activity (user_id, act_type, act_date, act_params) VALUES($user_ID, 'LINK_ADD', '".$act_time."', $act_link)");
}
}
function act_last_connect($act_user=''){
global $wpdb, $options_act, $user_ID;
if (!$act_user){ $act_user = $user_ID; }
if ($options_act['act_connect'] and !get_usermeta($act_user, 'act_private')){
$act_last_connect = $wpdb->get_var("SELECT MAX(act_date) FROM ".$wpdb->prefix."activity WHERE user_id = '".$act_user."'");
echo __("Last logon :", 'wp-activity')." ".nicetime($act_last_connect);
}
}
function act_stream_user($act_user=''){
global $options_act, $user_ID;
if (!$act_user){ $act_user = $user_ID; }
if (!get_usermeta($act_user, 'act_private')){
act_stream_common('-1', '', true, $act_user);
}
}
function act_stream_shortcode ($attr) {
$attr = shortcode_atts(array('number' => '-1',
'title' => '',), $attr);
return act_stream_common($attr['number'], $attr['title'], true, '');
}
add_shortcode('ACT_STREAM', 'act_stream_shortcode');
function act_stream($act_number='30', $act_title=''){
act_stream_common($act_number, $act_title, false,'');
}
//$act_number = -1 : no limit
function act_stream_common($act_number='30', $act_title='', $archive = false, $act_user = ''){
global $wpdb, $options_act, $user_ID;
if ($act_title == ''){
$act_title= __("Recent Activity", 'wp-activity');
}
if ($options_act['act_feed_display']){
$act_title .= ' ';
}
if ($options_act['act_page_link'] and !$archive){
$act_title .= ' '.__('Archives', 'wp-activity').' ';
}
$wp_url = get_bloginfo('wpurl');
$act_old_class = '';
$act_old_flag = -1;
$act_list_users = $wpdb->get_results("SELECT ID, display_name, user_nicename FROM $wpdb->users");
foreach ($act_list_users as $act_list_user) {
$act_users_nicename[$act_list_user->ID]=$act_list_user->user_nicename;
$act_users_display[$act_list_user->ID]=$act_list_user->display_name;
}
echo ''.$act_title.' ';
if ($archive == false) {echo '';}else{echo '';}
$sql = "SELECT * FROM ".$wpdb->prefix."activity";
if ($act_user!= ''){
$sql .= " WHERE user_id = '".$act_user."'";
}
$sql .= " ORDER BY id DESC";
if ($act_number > 0){
$sql .= " LIMIT $act_number";
}
if ( $act_logins = $wpdb->get_results( $sql)){
foreach ( (array) $act_logins as $act ){
$act_user_nicename = $act_users_nicename[$act->user_id];
if ($options_act['act_old'] and $act_old_flag > 0 and !$archive){
$act_old_class = 'act-old';
}else{
$act_old_class = '';
}
echo '';
if ($options_act['act_icons']== 'g'){
echo ' ';
}elseif ($options_act['act_icons']== 'a'){
if ($act->act_type == 'CONNECT' or $act->act_type == 'PROFILE_EDIT'){
echo get_avatar( $act->user_id, '16'); ;
}else{
echo ' ';
}
}
switch ($act->act_type){
case 'CONNECT':
echo ''.$act_users_display[$act->user_id].' '.__('has logged.', 'wp-activity');
if ($act->user_id == $user_ID and $options_act['act_old']){
$act_old_flag++;
}
break;
case 'COMMENT_ADD':
$act_comment=get_comment($act->act_params);
$act_post=get_post($act_comment->comment_post_ID);
echo ''.$act_comment->comment_author.' '.__('commented', 'wp-activity').' '.$act_post->post_title.' ';
break;
case 'POST_ADD':
$act_post=get_post($act->act_params);
if ($act->user_id != $act_post->post_author and !$strict_logs){ //this is a check if post author has been changed in admin post edition.
$sql = "UPDATE ".$wpdb->prefix."activity SET user_id = '".$act_post->post_author."' WHERE id = '".$act->id."'";
$wpdb->query( $sql);
}else{
echo ''.$act_users_display[$act_post->post_author].' '.__('published', 'wp-activity').' '.$act_post->post_title.' ';
}
break;
case 'POST_EDIT':
$act_post=get_post($act->act_params);
echo ''.$act_users_display[$act_post->post_author].' '.__('edited', 'wp-activity').' '.$act_post->post_title.' ';
break;
case 'PROFILE_EDIT':
echo ''.$act_users_display[$act->user_id].' '.__('has updated his profile.', 'wp-activity');
break;
case 'LINK_ADD':
$act_link = get_bookmark($act->act_params);
if ($act_link->link_visible == 'Y'){
echo ''.$act_users_display[$act->user_id].' '.__('has added a link to', 'wp-activity').' '.$act_link->link_name.' .';
}
break;
default:
break;
}
echo ''.nicetime($act->act_date).' ';
echo ' ';
}
}
echo ' ';
}
function nicetime($posted_date, $admin=false) {
$act_opt=get_option('act_settings');
$date_relative = $act_opt['act_date_relative'];
$date_format = $act_opt['act_date_format'];
$in_seconds = strtotime($posted_date);
$relative_date = '';
$diff = time()-$in_seconds;
$months = floor($diff/2592000);
$diff -= $months*2419200;
$weeks = floor($diff/604800);
$diff -= $weeks*604800;
$days = floor($diff/86400);
$diff -= $days*86400;
$hours = floor($diff/3600);
$diff -= $hours*3600;
$minutes = floor($diff/60);
$diff -= $minutes*60;
$seconds = $diff;
if ($months>0 or !$date_relative or $admin) {
// over a month old, just show date
if (!$date_relative or $admin){
$h = substr($posted_date,10);
} else {
$h = '';
}
switch ($date_format){
case 'dd/mm/yyyy':
return substr($posted_date,8,2).'/'.substr($posted_date,5,2).'/'.substr($posted_date,0,4).$h;
break;
case 'mm/dd/yyyy':
return substr($posted_date,5,2).'/'.substr($posted_date,8,2).'/'.substr($posted_date,0,4).$h;
break;
case 'yyyy/mm/dd':
default:
return substr($posted_date,0,4).'/'.substr($posted_date,5,2).'/'.substr($posted_date,8,2).$h;
break;
}
} else {
if ($weeks>0) {
// weeks and days
$relative_date .= ($relative_date?', ':'').$weeks.' '.($weeks>1? __('weeks', 'wp-activity'):__('week', 'wp-activity'));
$relative_date .= $days>0?($relative_date?', ':'').$days.' '.($days>1? __('days', 'wp-activity'):__('day', 'wp-activity')):'';
} elseif ($days>0) {
// days and hours
$relative_date .= ($relative_date?', ':'').$days.' '.($days>1? __('days', 'wp-activity'):__('day', 'wp-activity'));
$relative_date .= $hours>0?($relative_date?', ':'').$hours.' '.($hours>1? __('hours', 'wp-activity'):__('hour', 'wp-activity')):'';
} elseif ($hours>0) {
// hours and minutes
$relative_date .= ($relative_date?', ':'').$hours.' '.($hours>1? __('hours', 'wp-activity'):__('hour', 'wp-activity'));
$relative_date .= $minutes>0?($relative_date?', ':'').$minutes.' '.($minutes>1? __('minutes', 'wp-activity'):__('minute', 'wp-activity')):'';
} elseif ($minutes>0) {
// minutes only
$relative_date .= ($relative_date?', ':'').$minutes.' '.($minutes>1? __('minutes', 'wp-activity'):__('minute', 'wp-activity'));
} else {
// seconds only
$relative_date .= ($relative_date?', ':'').$seconds.' '.($seconds>1? __('seconds', 'wp-activity'):__('second', 'wp-activity'));
}
}
// show relative date and add proper verbiage
return sprintf(__('%s ago', 'wp-activity'), $relative_date);
}
function act_admin_menu(){
$act_plugin_page = add_options_page('WP-Activity', 'WP-Activity', 8, 'wp-activity', 'act_admin');
add_action( 'admin_head-'. $act_plugin_page, 'act_header' );
}
add_action('admin_menu', 'act_admin_menu');
add_action('admin_init','act_admin_scripts');
function act_admin_scripts(){
wp_enqueue_script('jquery-ui-tabs');
wp_enqueue_style('act_tabs', WP_PLUGIN_URL .'/wp-activity/jquery.ui.tabs.css', false, '2.5.0', 'screen');
}
function act_admin(){
global $wpdb, $act_version;
?>
WP-Activity
'.__('Options saved.').'
';
}
}elseif(isset($_POST['act-reset']) and check_admin_referer('wp-activity-reset','act_admin_reset')){
$sql="DELETE FROM ".$wpdb->prefix."activity";
if ( $results = $wpdb->query( $sql ) ){
echo ''.__('Activity logs deleted.', 'wp-activity').'
';
}
}elseif(isset($_POST['act-uninst']) and check_admin_referer('wp-activity-uninst','act_admin_uninst')){
act_desactive(); //Delete activity cron
delete_option('act_settings'); //delete activity settings
$sql="DROP TABLE ".$wpdb->prefix."activity"; //delete activity table
if ( $results = $wpdb->query( $sql ) ){
echo ''.sprintf(__('Activity Plugin has been uninstalled. You can now desactivate this plugin : Plugins Page ', 'wp-activity'),get_bloginfo('wpurl').'/wp-admin/plugins.php').'
';
}
}
$act_opt=get_option('act_settings');
if (!is_array($act_opt)){
echo ''.sprintf(__('Activity Plugin has been uninstalled. You can now desactivate this plugin : Plugins Page ', 'wp-activity'),get_bloginfo('wpurl').'/wp-admin/plugins.php').' ';
}else{
$act_count = $wpdb->get_var("SELECT count(ID) FROM ".$wpdb->prefix."activity");
extract($act_opt);
?>
Dric. Version %s .', 'wp-activity'), $act_version) ?>
'wp-activity', 'description' => __('Display a stream of registered users events', 'wp-activity') );
/* Widget control settings. */
$control_ops = array( 'height' => 350, 'id_base' => 'wp-activity' );
/* Create the widget. */
$this->WP_Widget( 'wp-activity', __('Wp-Activity Widget', 'wp-activity'), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', $instance['title'] );
$number = $instance['number'];
echo $before_widget;
if ( $title )
$title = $before_title . $title . $after_title;
act_stream_common($number, $title, false, '');
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['number'] = $new_instance['number'];
return $instance;
}
function form( $instance ) {
$defaults = array( 'title' => __('Recent Activity', 'wp-activity'), 'number' => '30');
$instance = wp_parse_args( (array) $instance, $defaults ); ?>