prefix."amsgboard"; // If needed, update who read the last message to include the current user function amsgboardreadby() { global $table_name; global $current_user; global $wpdb; if ( "Delete!" != $_POST['amsgboardform'] and "Delete all messages" != $_POST['amsgboardform'] ) { $amsgboardlast = $wpdb->get_row("SELECT id,readby,sentto FROM $table_name order by id DESC limit 1", ARRAY_A); // Explode the string that saves who read already the story into an array and check for the current user if ( !in_array($current_user->ID, explode("-", $amsgboardlast["readby"])) and "subscriber" != $current_user->roles[0] and ( in_array($current_user->ID, explode("-", $amsgboardlast["sentto"])) or "all users" == $amsgboardlast["sentto"] ) ) { // Add the current user to the string and update the field $amsgboardlast["readby"] .= "-" . $current_user->ID; $wpdb->query("UPDATE $table_name SET readby = '$amsgboardlast[readby]' WHERE id = $amsgboardlast[id]"); } } } // Function to display how long ago the messages were posted function amsgboardtimesago( $amsgboarddatestored ) { //Get the current time $amsgboardtimenowtime = current_time(timestamp); // Do the math to check the difference between when it was stored and right now $amsgboardtimesum = $amsgboardtimenowtime - $amsgboarddatestored; if ( $amsgboardtimesum > 31104000 ) $amsgboardreturned = round($amsgboardtimesum / 60 / 60 / 24 / 30 / 12) . __(' years', 'wp-admin-msg-board'); elseif ( $amsgboardtimesum > 2592000 ) $amsgboardreturned = round($amsgboardtimesum / 60 / 60 / 24 / 30) . __(' months', 'wp-admin-msg-board'); elseif ( $amsgboardtimesum > 604800 ) $amsgboardreturned = round($amsgboardtimesum / 60 / 60 / 24 / 7) . __(' weeks', 'wp-admin-msg-board'); elseif ( $amsgboardtimesum > 86400 ) $amsgboardreturned = round($amsgboardtimesum / 60 / 60 / 24) . __(' days', 'wp-admin-msg-board'); elseif ( $amsgboardtimesum > 3600 ) $amsgboardreturned = round($amsgboardtimesum / 60 / 60) . __(' hours', 'wp-admin-msg-board'); elseif ( $amsgboardtimesum > 60 ) $amsgboardreturned = round($amsgboardtimesum / 60) . __(' minutes', 'wp-admin-msg-board'); else $amsgboardreturned = $amsgboardtimesum . __(' seconds', 'wp-admin-msg-board'); // Remove the extra "s" if the number is 1, only in English and Spanish, the languages I created and know $current_locale = get_locale(); if ( $current_locale == "" or $current_locale == "es_ES" ) { if ( in_array("1", explode(" ",$amsgboardreturned)) ) $amsgboardreturned = substr($amsgboardreturned, 0, -1); // Fix error with "meses" (Spanish for months) that in singular should be "mes" and not "mese" if ( $current_locale == "es_ES" and $amsgboardtimesum < 5184000 and $amsgboardtimesum > 2592000 ) $amsgboardreturned = substr($amsgboardreturned, 0, -1); } $amsgboardreturned .= __(' ago', 'wp-admin-msg-board'); return $amsgboardreturned; } // If something was done (added a message, deleted all messages or deleted the oldest "x" messages) if ( !empty($_POST['amsgboardform']) ) { switch ( $_POST['amsgboardform'] ) { // The current user sent a new message case __('Send', 'wp-amsgboard'): // Get all the info to populate the db and insert it $dateamsgboard = current_time(timestamp); $msgamsgboard = $wpdb->escape($_POST["message"]); $readbyamsgboard = $current_user->ID; $amsgboardsentto = ""; if ( "all users" == $wpdb->escape($_POST["amsgboardall"]) ) $amsgboardsentto = "all users"; elseif ( "no" == $wpdb->escape($_POST["amsgboardall"]) ) { $amsgboardi=1; while ($_POST["amsgboardlastvalue"] >= $amsgboardi) { if ( is_numeric($wpdb->escape($_POST['amsgboardusers'.$amsgboardi])) ) $amsgboardsentto .= $wpdb->escape($_POST['amsgboardusers'.$amsgboardi]) . "-"; $amsgboardi++; } } // Get the last message id and add one $idamsgboard = $wpdb->get_var("SELECT id FROM $table_name order by id DESC limit 1") + 1; $insert = "INSERT INTO " . $table_name . " (id, dateposted, msgposted, sentto, readby) " . "VALUES ('$idamsgboard', '$dateamsgboard', '$msgamsgboard', '$amsgboardsentto', '$readbyamsgboard')"; $results = $wpdb->query($insert); break; // The current user deleted all messages case __('Delete all messages', 'wp-amsgboard'): // Check if the current user is administrator if ( "administrator" == $current_user->roles[0] ) $truncateamsgboard = $wpdb->query("TRUNCATE TABLE $table_name"); break; // The current user dropped entire database table case __('Drop entire table', 'wp-amsgboard'): // Check if the current user is administrator if ( "administrator" == $current_user->roles[0] ) { // Drop table $dropamsgboard = $wpdb->query("DROP TABLE $table_name"); // Delete option and deactivate plugin delete_option("amsgboard_db_version"); $amsgboardcurrent = get_option('active_plugins'); array_splice($amsgboardcurrent, array_search( "admin-msg-board/admin-msg-board.php", $amsgboardcurrent), 1 ); // Array-fu! update_option('active_plugins', $amsgboardcurrent); do_action('deactivate_' . trim( "admin-msg-board/admin-msg-board.php" )); echo ""; } break; // The current user deleted the oldest "x" messages case __('Delete!', 'wp-amsgboard'): // Check if the current user is the administrator if ( "administrator" == $current_user->roles[0] ) { $amsgboardmany = $_POST["amsgboardformdel"] + 1; $amsgboarddelid = $wpdb->get_results("SELECT id FROM $table_name order by id ASC limit $amsgboardmany", ARRAY_A); $amsgboardmax = $amsgboarddelid[count($amsgboarddelid)-1][id]; if ( count( 0 == $amsgboarddelid)-1 ) $deleteamsgboard = $wpdb->query("DELETE FROM $table_name WHERE id = $amsgboardmax"); elseif ( count($amsgboarddelid)-1 > 0 ) $deleteamsgboard = $wpdb->query("DELETE FROM $table_name WHERE id < $amsgboardmax"); } break; } } // Calls the function to update who read the last message to include the current user if needed amsgboardreadby(); ?>
| '; $amsgboarduser = get_userdata($amsgboardauthor); echo $amsgboarduser->display_name; echo ' ' . amsgboardtimesago($amsgboardshow->dateposted) . 'msg id ' . $amsgboardshow->id . ' |
| ' . $amsgboardshow->msgposted . '
'; _e('Sent to:', 'wp-admin-msg-board'); echo " "; if ( "all users" == $amsgboardshow->sentto ) _e('All users.', 'wp-admin-msg-board'); else { $amsgboardlist = explode("-",$amsgboardshow->sentto); for ( $i=0; $i < count($amsgboardlist); $i++ ) { $amsgboarduser = get_userdata($amsgboardlist[$i]); echo $amsgboarduser->display_name; if ( 2 < count($amsgboardlist) - $i ) echo ", "; } echo ". "; } echo ' |
Admin Msg Board " . __('version ', 'wp-admin-msg-board') . get_option( "amsgboard_db_version" ) . __(' by', 'wp-admin-msg-board') . " Julian Yanover"; ?>