'randomtext',
'description' => ' Display randomized text from the selected category.');
$this->WP_Widget('randomtext', 'Random Text', $widget_ops);
}
function get_randomtext($category='', $random=false) {
global $wpdb;
$table_name = $wpdb->prefix . 'randomtext';
$sql = 'SELECT randomtext_id, text FROM '. $table_name." WHERE visible='yes' ";
$sql .= ($category!='') ? " AND category = '$category'" : '' ;
if($random)
$sql .= ' ORDER BY RAND() LIMIT 1 ';
else
$sql .= ' ORDER BY timestamp, randomtext_id LIMIT 1 ';
$row = $wpdb->get_row($sql);
// update the timestamp of the row we just seleted (used by rotator, not by random)
if(!$random AND intval($row->randomtext_id)) {
$sql = 'UPDATE '.$table_name.' SET timestamp = Now() WHERE randomtext_id = '.intval($row->randomtext_id);
$wpdb->query($sql);
}
// now we can safely render shortcodes without self recursion (unless there is only one item containing [randomtext] shortcode - don't do that, it's just silly!)
$snippet = do_shortcode($row->text);
return $snippet;
}
function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
$category = empty($instance['category']) ? '' : $instance['category'];
$random = intval($instance['random']);
$snippet = $this->get_randomtext($category, $random);
if($snippet!='') {
echo $before_widget;
if($title)
echo $before_title.$title.$after_title;
echo $instance['pretext'].$snippet.$instance['posttext'];
echo $after_widget;
}
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags(stripslashes($new_instance['title']));
$instance['category'] = strip_tags(strip_tags(stripslashes($new_instance['category'])));
$instance['pretext'] = $new_instance['pretext'];
$instance['posttext'] = $new_instance['posttext'];
$instance['random'] = intval($new_instance['random']);
return $instance;
}
function form($instance) {
$instance = wp_parse_args((array)$instance, array('title' => 'Random Text', 'category' => '', 'pretext' => '', 'posttext' => ''));
$title = htmlspecialchars($instance['title']);
$category = htmlspecialchars($instance['category']);
$pretext = htmlspecialchars($instance['pretext']);
$posttext = htmlspecialchars($instance['posttext']);
if(!isset($instance['random'])) { $instance['random'] = 0; }
echo '
Note: Random can be more intensive with large record sets, and some items may never appear.
';
}
}
function randomtext($category, $random=FALSE){
$randomtext = new randomtext;
echo $randomtext->get_randomtext($category,$random);
}
function randomtext_init() {
register_widget('randomtext');
}
function randomtext_get_category_options($category='') {
global $wpdb;
$table_name = $wpdb->prefix . 'randomtext';
$sql = 'SELECT category FROM '.$table_name.' GROUP BY category ORDER BY category';
$rows = $wpdb->get_results($sql);
$option_nocategory = false;
$nocategory_name = 'No Category';
foreach($rows as $row){
$selected = ($category==$row->category) ? 'SELECTED' : '';
$categoryname = $row->category;
if(trim($categoryname)==''){
$categoryname = $nocategory_name;
$option_nocategory = true;
}
$result .= '';
}
if(!$option_nocategory)
$result = ''.$result;
return $result;
}
function randomtext_install() {
global $wpdb, $user_ID;
$table_name = $wpdb->prefix . 'randomtext';
// create the table if it doesn't exist
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$sql = "CREATE TABLE `$table_name` (
`randomtext_id` int(10) unsigned NOT NULL auto_increment,
`category` varchar(32) character set utf8 NOT NULL,
`text` text character set utf8 NOT NULL,
`visible` enum('yes','no') NOT NULL default 'yes',
`user_id` int(10) unsigned NOT NULL,
`timestamp` timestamp NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`randomtext_id`),
KEY `visible` (`visible`),
KEY `category` (`category`),
KEY `timestamp` (`timestamp`)
)";
$results = $wpdb->query( $sql );
// add some test data
$data = array ('category' => 'Installer', 'user_id'=> $user_ID, 'text' => 'Creativity is the ability to introduce order into the randomness of nature. - Eric Hoffer' );
$wpdb->insert($table_name, $data);
$data['text'] = 'So much of life, it seems to me, is determined by pure randomness. - Sidney Poitier';
$wpdb->insert($table_name, $data);
}
}
add_action('widgets_init', 'randomtext_init');
register_activation_hook(__FILE__,'randomtext_install');
if(is_admin()) {
$plugin_basename = plugin_basename(__FILE__);
include 'randomtext_admin.php';
}
// Shortcode implementation
function randomtext_shortcode($attribs) {
extract(shortcode_atts(array('category' => '', 'random' => FALSE, ), $attribs));
$randomtext = new randomtext;
return $randomtext->get_randomtext($category,$random);
}
add_shortcode('randomtext', 'randomtext_shortcode');
?>