".$exclude; else $condition = ""; return quotescollection_get_quote($condition); } function quotescollection_get_quotes($condition = "") { global $wpdb; $sql = "SELECT quote_id, quote, author, source, tags, public FROM " . $wpdb->prefix . "quotescollection" . $condition; if($quotes = $wpdb->get_results($sql, ARRAY_A)) return $quotes; else return array(); } function quotescollection_get_quote($condition = '', $random = 1, $current = 0) { global $wpdb; $sql = "SELECT quote_id, quote, author, source FROM " . $wpdb->prefix . "quotescollection"; if ($condition) $sql .= $condition; if(!$random) { if($current) $sql .= " AND quote_id < {$current}"; $sql .= " ORDER BY quote_id DESC"; } else $sql .= " ORDER BY RAND()"; $sql .= " LIMIT 1"; $random_quote = $wpdb->get_row($sql, ARRAY_A); if ( empty($random_quote) ) { if(!$random && $current) return quotescollection_get_quote($condition, 0, 0); else return 0; } else return $random_quote; } function quotescollection_count($condition = "") { global $wpdb; $sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . "quotescollection ".$condition; $count = $wpdb->get_var($sql); return $count; } function quotescollection_pagenav($total, $current = 1, $format = 0, $paged = 'paged', $url = "") { if($total == 1 && $current == 1) return ""; if(!$url) { $url = 'http'; if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$url .= "s";} $url .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $url .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["PHP_SELF"]; } else { $url .= $_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"]; } if($query_string = $_SERVER['QUERY_STRING']) { $parms = explode('&', $query_string); $y = '?'; foreach($parms as $parm) { $x = explode('=', $parm); if($x[0] == $paged) { $query_string = str_replace($y.$parm, '', $query_string); } else $y = '&'; } if($query_string) { $url .= '?'.$query_string; $a = '&'; } else $a = '?'; } else $a = '?'; } else { $a = '?'; if(strpos($url, '?')) $a = '&'; } if(!$format || $format > 2 || $format < 0 || !is_numeric($format)) { if($total <= 8) $format = 1; else $format = 2; } if($current > $total) $current = $total; $pagenav = ""; if($format == 2) { $first_disabled = $prev_disabled = $next_disabled = $last_disabled = ''; if($current == 1) $first_disabled = $prev_disabled = ' disabled'; if($current == $total) $next_disabled = $last_disabled = ' disabled'; $pagenav .= "«  "; $pagenav .= "  "; $pagenav .= ''.$current.' of '.$total.''; $pagenav .= "  "; $pagenav .= "  »"; } else { $pagenav = __("Goto page:", 'quotes-collection'); for( $i = 1; $i <= $total; $i++ ) { if($i == $current) $pagenav .= " {$i}"; else if($i == 1) $pagenav .= " {$i}"; else $pagenav .= " {$i}"; } } return $pagenav; } function quotescollection_txtfmt($quotedata = array()) { if(!$quotedata) return; foreach($quotedata as $key => $value){ $value = make_clickable($value); $value = wptexturize(str_replace(array("\r\n", "\r", "\n"), '', nl2br(trim($value)))); $quotedata[$key] = $value; } return $quotedata; } function quotescollection_display_randomquote($show_author = 1, $show_source = 1, $ajax_refresh = 1, $random_quote = array()) { $args = "show_author={$show_author}&show_source={$show_source}&ajax_refresh={$ajax_refresh}&char_limit={$char_limit}&echo=1"; return quotescollection_quote($args); } function quotescollection_quote($args = '') { global $quotescollection_instances, $quotescollection_next_quote; if(!$quotescollection_next_quote) $quotescollection_next_quote = __('Next quote', 'quotes-collection')." »"; if(!($instance = $quotescollection_instances)) $instance = $quotescollection_instances = 0; $key_value = explode('&', $args); $options = array(); foreach($key_value as $value) { $x = explode('=', $value); $options[$x[0]] = $x[1]; // $options['key'] = 'value'; } $options_default = array( 'show_author' => 1, 'show_source' => 1, 'ajax_refresh' => 1, 'auto_refresh' => 0, 'tags' => '', 'char_limit' => 500, 'echo' => 1, 'random' => 1, 'exclude' => '', 'current' => 0 ); $options = array_merge($options_default, $options); $condition = " WHERE public = 'yes'"; if($options['random']) $current = 0; else $current = $options['current']; if($options['char_limit'] && is_numeric($options['char_limit'])) $condition .= " AND CHAR_LENGTH(quote) <= ".$options['char_limit']; else $options['char_limit'] = 0; if($options['exclude']) $condition .=" AND quote_id <> ".$options['exclude']; if($options['tags']) { $taglist = explode(',', $options['tags']); $tag_condition = ""; foreach($taglist as $tag) { $tag = mysql_real_escape_string(strip_tags(trim($tag))); if($tag_condition) $tag_condition .= " OR "; $tag_condition .= "tags = '{$tag}' OR tags LIKE '{$tag},%' OR tags LIKE '%,{$tag},%' OR tags LIKE '%,{$tag}'"; } $condition .= " AND ({$tag_condition})"; } $random_quote = quotescollection_get_quote($condition, $options['random'], $current); if(!$random_quote) return; $random_quote = quotescollection_txtfmt($random_quote); $display = "

". $random_quote['quote'] .""; $cite = ""; if($options['show_author'] && $random_quote['author']) $cite = ''. $random_quote['author'] .''; if($options['show_source'] && $random_quote['source']) { if($cite) $cite .= ", "; $cite .= ''. $random_quote['source'] .''; } if($cite) $cite = " — {$cite}"; $display .= $cite."

"; // We don't want to display the 'next quote' link if there is no more than 1 quote $quotes_count = quotescollection_count($condition); if($options['ajax_refresh'] == 1 && $quotes_count > 1) { if($options['auto_refresh']) $display .= ""; else { $display .= "\n"; } } else if ($options['ajax_refresh'] == 2 && $quotes_count) { if($options['auto_refresh']) $display .= ""; else $display .= "

".$quotescollection_next_quote."

"; return $display; } $display = "
{$display}
"; $quotescollection_instances++; if($options['echo']) echo $display; else return $display; } function quotescollection_install() { global $wpdb; $table_name = $wpdb->prefix . "quotescollection"; if(!defined('DB_CHARSET') || !($db_charset = DB_CHARSET)) $db_charset = 'utf8'; $db_charset = "CHARACTER SET ".$db_charset; if(defined('DB_COLLATE') && $db_collate = DB_COLLATE) $db_collate = "COLLATE ".$db_collate; // if table name already exists if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name) { $wpdb->query("ALTER TABLE `{$table_name}` {$db_charset} {$db_collate}"); $wpdb->query("ALTER TABLE `{$table_name}` MODIFY quote TEXT {$db_charset} {$db_collate}"); $wpdb->query("ALTER TABLE `{$table_name}` MODIFY author VARCHAR(255) {$db_charset} {$db_collate}"); $wpdb->query("ALTER TABLE `{$table_name}` MODIFY source VARCHAR(255) {$db_charset} {$db_collate}"); if(!($wpdb->get_results("SHOW COLUMNS FROM {$table_name} LIKE 'tags'"))) { $wpdb->query("ALTER TABLE `{$table_name}` ADD `tags` VARCHAR(255) {$db_charset} {$db_collate} AFTER `source`"); } if(!($wpdb->get_results("SHOW COLUMNS FROM {$table_name} LIKE 'public'"))) { $wpdb->query("ALTER TABLE `{$table_name}` CHANGE `visible` `public` enum('yes', 'no') DEFAULT 'yes' NOT NULL"); } } else { //Creating the table ... fresh! $sql = "CREATE TABLE " . $table_name . " ( quote_id mediumint(9) NOT NULL AUTO_INCREMENT, quote TEXT NOT NULL, author VARCHAR(255), source VARCHAR(255), tags VARCHAR(255), public enum('yes', 'no') DEFAULT 'yes' NOT NULL, time_added datetime NOT NULL, time_updated datetime, PRIMARY KEY (quote_id) ) {$db_charset} {$db_collate};"; $results = $wpdb->query( $sql ); } global $quotescollection_db_version; $options = get_option('quotescollection'); $options['db_version'] = $quotescollection_db_version; update_option('quotescollection', $options); } function quotescollection_css_head() { ?>