No Randomize-Entries - why don't you fill in some data?"; else echo $i['random_string']; } # Returns a random number - if category is specified, only numbers from that category function Randomize_GetRandNumber($cat) { $sql = 'SELECT id FROM %sRandomize'; $sql = sprintf($sql, $GLOBALS['table_prefix']); if ($cat != "*") $sql .= " WHERE category_id='$cat'"; $wpdb =& $GLOBALS['wpdb']; $entries = $wpdb->get_col($sql); return $entries[array_rand($entries)]; } function Randomize_GetEntries($sqlsort) { $sql = 'SELECT id FROM %sRandomize'; $sql = sprintf($sql, $GLOBALS['table_prefix']); $sql = $sql . $sqlsort; $wpdb =& $GLOBALS['wpdb']; return $wpdb->get_col($sql); } function Randomize_GetRand($id) { if ($id == "") return false; else { $sql = "SELECT * FROM %sRandomize"; $sql = sprintf($sql, $GLOBALS['table_prefix']); $wpdb =& $GLOBALS['wpdb']; $sql .= " WHERE id=$id"; return $wpdb->get_row($sql,ARRAY_A); } } function Randomize_AddRand($cat, $obj) { $obj = nl2br($obj); $sql = "INSERT INTO %sRandomize (category_id, random_string, posted) values ('$cat', '$obj', now())"; $sql = sprintf($sql, $GLOBALS['table_prefix']); $wpdb =& $GLOBALS['wpdb']; $res = $wpdb->query($sql); if ($res) echo "

Yep - new entry inserted in category $cat...

"; else echo "

Doh! Wasn't inserted...

"; } function Randomize_DelRand($id) { $sql = 'DELETE FROM %sRandomize WHERE id=%u'; $sql = sprintf($sql, $GLOBALS['table_prefix'], $id); $wpdb =& $GLOBALS['wpdb']; $res = $wpdb->query($sql); if ($res) { echo "

Yep - entry $id is history...

"; } else { echo "

Doh! Entry $id wasn't deleted...

";} } function Randomize_GetTotalAmount() { $sql = 'SELECT COUNT(*) FROM %sRandomize'; $sql = sprintf($sql, $GLOBALS['table_prefix']); $wpdb =& $GLOBALS['wpdb']; return $wpdb->get_var($sql); } function Randomize_GetCategories() { $sql = "SELECT DISTINCT category_id FROM %sRandomize"; $sql = sprintf($sql, $GLOBALS['table_prefix']); $wpdb =& $GLOBALS['wpdb']; return $wpdb->get_results($sql,ARRAY_A); } function Randomize_GetAmountCategories() { return count(Randomize_GetCategories()); } function Randomize_DateSort($what) { $sql = 'SELECT posted FROM %sRandomize ORDER BY posted'; $sql = sprintf($sql, $GLOBALS['table_prefix']); if ($what == "oldest") $sql .= " ASC"; else $sql .= " DESC"; $wpdb =& $GLOBALS['wpdb']; return date('j M Y, G:i', strtotime($wpdb->get_var($sql))); } function AddRandomizeManagePage() { add_management_page(__("Randomize"), __('Randomize'), 1, 'rand.php'); } function Randomize_Admin_Footer() { $admin = dirname($_SERVER['SCRIPT_FILENAME']); $admin = substr($admin, strrpos($admin, '/')+1); $oldest = Randomize_DateSort("oldest"); $newest = Randomize_DateSort("newest"); if ($admin == 'wp-admin' && basename($_SERVER['SCRIPT_FILENAME']) == 'index.php') { $content = "

" . _('Randomize Status') . " »

"; $content .= _('There\'s ') . ' ' . Randomize_GetTotalAmount() . ' ' . _('entries in ') . Randomize_GetAmountCategories() . " categories."; $content .= "

" . _('Newest from ') . $newest . "

" . _('Oldest from ') . $oldest . ".

"; print ' '; } } # Used for first-time initialization... function Randomize_Install() { $pre = $GLOBALS['table_prefix']; $wpdb =& $GLOBALS['wpdb']; $sql = 'SHOW TABLES LIKE \'%sRandomize\''; $sql = sprintf($sql, $pre); $results = $wpdb->query($sql); # Hep, it's not there - let's create it, shall we? if ($results == 0) { $sql = " create table %sRandomize ( id integer not null auto_increment, category_id varchar(50) not null, posted datetime not null, random_string text not null, primary key(id) ) "; $sql = sprintf($sql, $pre); $results = $wpdb->query($sql); # INSERT some stuff, so it's not completely empty.... $sql = "INSERT INTO %sRandomize (category_id, posted, random_string) values ('quotes', now(), 'If you never fail, you\'re not trying hard enough...')"; $sql = sprintf($sql, $pre); $results = $wpdb->query($sql); $sql = "INSERT INTO %sRandomize (category_id, posted, random_string) values ('quotes', now(), 'Failure is not an option.. - it comes bundled with your Microsoft Products...')"; $sql = sprintf($sql, $pre); $results = $wpdb->query($sql); $sql = "INSERT INTO %sRandomize (category_id, posted, random_string) values ('links', now(), 'http://www.wordpress.org')"; $sql = sprintf($sql, $pre); $results = $wpdb->query($sql); $sql = "INSERT INTO %sRandomize (category_id, posted, random_string) values ('images', now(), 'http://static.wordpress.org/2004/logo.png')"; $sql = sprintf($sql, $pre); $results = $wpdb->query($sql); } } # Do the installation stuff, if the plugin is marked to be activated... $install = (basename($_SERVER['SCRIPT_NAME']) == 'plugins.php' && isset($_GET['activate']));; if ($install) { Randomize_Install(); } # Set it up... - add to Dashboard, manage and options-page. add_action('admin_footer', 'Randomize_Admin_Footer'); add_action('admin_menu', 'AddRandomizeManagePage'); ?>