'.$exclude;
$sql = "SELECT quote_id, quote, author, source
FROM " . $wpdb->prefix . "quotescollection
WHERE visible = 'yes'
".$exclude_condition."
ORDER BY RAND()
LIMIT 1";
$random_quote = $wpdb->get_row($sql, ARRAY_A);
if ( !empty($random_quote) ) {
return $random_quote;
}
else
return 0;
}
function quotescollection_count($condition = "")
{
global $wpdb;
$sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . "quotescollection ".$condition;
$count = $wpdb->get_var($sql);
return $count;
}
function quotescollection_js_head() // this is a PHP function
{
// use JavaScript SACK library for AJAX
wp_print_scripts( array( 'sack' ));
// Define custom JavaScript function
?>
". wptexturize(str_replace(array("\r\n", "\r", "\n"), '', nl2br($random_quote['quote']))) ." ";
if( ($show_author && $random_quote['author']) || ($show_source && $random_quote['source']) )
$display .= " — ";
if($show_author && $random_quote['author'])
$display .= "".$random_quote['author']." ";
if($show_source && $random_quote['source'])
$display .= "from ".$random_quote['source']." ";
$display .= "
";
// We don't want to display the 'next quote' link if there is no more than 1 quote
$quotes_count = quotescollection_count("WHERE visible='yes'");
if($ajax_refresh == 1 && $quotes_count > 1) {
$display .= "\n";
}
if ($ajax_refresh == 2 && $quotes_count > 1) {
$display .= "Next quote »
";
return $display;
}
$display = "{$display}
";
echo $display;
$quotescollection_instances++;
return;
}
function quotescollection_init()
{
if ( !function_exists('register_sidebar_widget') || !function_exists('register_widget_control') )
return;
function quotescollection_widget($args) {
if($random_quote = quotescollection_get_randomquote()) {
$options = get_option('quotescollection');
$title = isset($options['title'])?$options['title']:__('Random Quote');
$show_author = isset($options['show_author'])?$options['show_author']:1;
$show_source = isset($options['show_source'])?$options['show_source']:1;
$ajax_refresh = isset($options['ajax_refresh'])?$options['ajax_refresh']:1;
extract($args);
echo $before_widget;
if($title) echo $before_title . $title . $after_title . "\n";
quotescollection_display_randomquote($show_author, $show_source, $ajax_refresh, $random_quote);
echo $after_widget;
}
}
function quotescollection_widget_control()
{
// default values for options
$options = array(
'title' => 'Random Quote',
'show_author' => 1,
'show_source' => 0,
'ajax_refresh' => 1,
);
if($options_saved = get_option('quotescollection'))
$options = array_merge($options, $options_saved);
// Update options in db when user updates options in the widget page
if($_REQUEST['quotescollection-submit']) {
$options['title']
= strip_tags(stripslashes($_REQUEST['quotescollection-title']));
$options['show_author'] = $_REQUEST['quotescollection-show_author']?1:0;
$options['show_source'] = $_REQUEST['quotescollection-show_source']?1:0;
$options['ajax_refresh'] = $_REQUEST['quotescollection-ajax_refresh']?1:0;
update_option('quotescollection', $options);
}
// Now we define the display of widget options menu
$title = htmlspecialchars($options['title'], ENT_QUOTES);
if($options['show_author'])
$show_author_checked = ' checked="checked"';
if($options['show_source'])
$show_source_checked = ' checked="checked"';
if($options['ajax_refresh'])
$ajax_refresh_checked = ' checked="checked"';
echo "Title:
";
echo " Show author?
";
echo " Show source?
";
echo " Ajax refresh feature
";
echo " ";
echo "Manage your collection of quotes atManage->Quotes Collection
";
}
register_sidebar_widget(array('Random Quote', 'widgets'), 'quotescollection_widget');
register_widget_control('Random Quote', 'quotescollection_widget_control', 250, 230);
}
function quotescollection_admin_menu()
{
add_management_page('Quotes Collection', 'Quotes Collection', 8, __FILE__, 'quotescollection_quotes_management');
}
function quotescollection_addquote($quote, $author = "", $source = "", $visible = 'yes')
{
if(!$quote) return "Nothing added to the database.";
global $wpdb;
$table_name = $wpdb->prefix . "quotescollection";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name)
return "Database not found";
else //Add the quote data to the database
{
if ( ini_get('magic_quotes_gpc') ) {
$quote = stripslashes($quote);
$author = stripslashes($author);
$source = stripslashes($source);
}
$quote = "'".$wpdb->escape($quote)."'";
$author = $author?"'".$wpdb->escape($author)."'":"NULL";
$source = $source?"'".$wpdb->escape($source)."'":"NULL";
if(!$visible) $visible = "'no'";
else $visible = "'yes'";
$insert = "INSERT INTO " . $table_name .
"(quote, author, source, visible, time_added)" .
"VALUES ({$quote}, {$author}, {$source}, {$visible}, NOW())";
$results = $wpdb->query( $insert );
if(FALSE === $results)
return "There was an error in the MySQL query";
else
return "Quote added";
}
}
function quotescollection_editquote($quote_id, $quote, $author = "", $source = "", $visible = 'yes')
{
if(!$quote) return "Quote not updated.";
if(!$quote_id) return srgq_addquote($quote, $author, $source, $visible);
global $wpdb;
$table_name = $wpdb->prefix . "quotescollection";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name)
return "Database not found";
else //Update database
{
if ( ini_get('magic_quotes_gpc') ) {
$quote = stripslashes($quote);
$author = stripslashes($author);
$source = stripslashes($source);
}
$quote = "'".$wpdb->escape($quote)."'";
$author = $author?"'".$wpdb->escape($author)."'":"NULL";
$source = $source?"'".$wpdb->escape($source)."'":"NULL";
if(!$visible) $visible = "'no'";
else $visible = "'yes'";
$update = "UPDATE " . $table_name . "
SET quote = {$quote},
author = {$author},
source = {$source},
visible = {$visible},
time_updated = NOW()
WHERE quote_id = $quote_id";
$results = $wpdb->query( $update );
if(FALSE === $results)
return "There was an error in the MySQL query";
else
return "Changes saved";
}
}
function quotescollection_deletequote($quote_id)
{
if($quote_id) {
global $wpdb;
$sql = "DELETE from " . $wpdb->prefix ."quotescollection" .
" WHERE quote_id = " . $quote_id;
if(FALSE === $wpdb->query($sql))
return "There was an error in the MySQL query";
else
return "Quote deleted";
}
else return "The quote cannot be deleted";
}
function quotescollection_getquotedata($quote_id)
{
global $wpdb;
$sql = "SELECT quote_id, quote, author, source, visible
FROM " . $wpdb->prefix . "quotescollection
WHERE quote_id = {$quote_id}";
$quote_data = $wpdb->get_row($sql, ARRAY_A);
return $quote_data;
}
function quotescollection_editform($quote_id = 0)
{
$visible_selected = " checked=\"checked\"";
$submit_value = "Add Quote";
$form_name = "addquote";
if($quote_id) {
$form_name = "editquote";
$quote_data = quotescollection_getquotedata($quote_id);
foreach($quote_data as $key => $value)
$quote_data[$key] = $quote_data[$key];
extract($quote_data);
$quote = htmlentities($quote);
$author = htmlentities($author);
$source = htmlentities($source);
$hidden_input = " ";
if($visible == 'no') $visible_selected = "";
$submit_value = "Save changes";
$back = " ";
}
$display .=<<< EDITFORM
EDITFORM;
return $display;
}
function quotescollection_changevisibility($quote_ids, $visibility = 'yes')
{
if(!$quote_ids)
return "Nothing done!";
global $wpdb;
$sql = "UPDATE ".$wpdb->prefix."quotescollection
SET visible = '".$visibility."',
time_updated = NOW()
WHERE quote_id IN (".implode(', ', $quote_ids).")";
$wpdb->query($sql);
return "Visibility status of selected quotes set to '{$visibility}'.";
}
function quotescollection_bulkdelete($quote_ids)
{
if(!$quote_ids)
return "Nothing done!";
global $wpdb;
$sql = "DELETE FROM ".$wpdb->prefix."quotescollection
WHERE quote_id IN (".implode(', ', $quote_ids).")";
$wpdb->query($sql);
return "Quote(s) deleted";
}
function quotescollection_quotes_management()
{
if($_REQUEST['submit'] == 'Add Quote') {
extract($_REQUEST);
$msg = quotescollection_addquote($quote, $author, $source, $visible);
}
else if($_REQUEST['submit'] == 'Save changes') {
extract($_REQUEST);
$msg = quotescollection_editquote($quote_id, $quote, $author, $source, $visible);
}
else if($_REQUEST['action'] == 'editquote') {
$display .= "\n
Edit quote ";
$display .= quotescollection_editform($_REQUEST['id']);
$display .= "";
echo $display;
return;
}
else if($_REQUEST['action'] == 'delquote') {
$msg = quotescollection_deletequote($_REQUEST['id']);
}
else if(isset($_REQUEST['bulkaction'])) {
if($_REQUEST['bulkaction'] == "Delete")
$msg = quotescollection_bulkdelete($_REQUEST['bulkcheck']);
if($_REQUEST['bulkaction'] == "Make visible") {
$msg = quotescollection_changevisibility($_REQUEST['bulkcheck'], 'yes');
}
if($_REQUEST['bulkaction'] == "Make invisible") {
$msg = quotescollection_changevisibility($_REQUEST['bulkcheck'], 'no');
}
}
$display .= "";
$display .= "\n
Add new quote ";
$display .= quotescollection_editform();
$display .= "";
echo $display;
}
function quotescollection_admin_head()
{
?>
prefix . "quotescollection";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name)
{
//Creating the table
$sql = "CREATE TABLE " . $table_name . " (
quote_id mediumint(9) NOT NULL AUTO_INCREMENT,
quote text NOT NULL,
author varchar(255),
source varchar(255),
visible enum('yes', 'no') DEFAULT 'yes' NOT NULL,
time_added datetime NOT NULL,
time_updated datetime,
PRIMARY KEY (quote_id)
);";
$results = $wpdb->query( $sql );
}
$query = "ALTER TABLE `{$table_name}` charset=utf8";
$wpdb->query($query);
$query = "ALTER TABLE `{$table_name}` MODIFY `quote` TEXT CHARACTER SET utf8, MODIFY `author` TEXT CHARACTER SET utf8, MODIFY `source` TEXT CHARACTER SET utf8";
$wpdb->query($query);
}
function quotescollection_displayquote($quote_id = 0)
{
global $wpdb;
$sql = "SELECT quote_id, quote, author, source
FROM " . $wpdb->prefix . "quotescollection
WHERE visible = 'yes' ";
if(!$quote_id) {
$sql .= "ORDER BY RAND()
LIMIT 1";
}
else {
$sql .= "AND quote_id = {$quote_id}";
}
$quote_data = $wpdb->get_row($sql, ARRAY_A);
if ( !empty($quote_data) ) {
$display = "".wptexturize(nl2br($quote_data['quote']))." ";
if($quote_data['author'] || $quote_data['source'])
$display .= " — ";
if($quote_data['author'])
$display .= "{$quote_data['author']} ";
if($quote_data['source']) {
$display .="from {$quote_data['source']} ";
}
$display .= " ";
return $display;
}
else
return "";
}
function quotescollection_displayquotes($source = "")
{
global $wpdb;
$source = html_entity_decode($source);
$sql = "SELECT quote_id, quote, author, source
FROM " . $wpdb->prefix . "quotescollection
WHERE visible = 'yes' ";
if(!$source) {
$sql .= "ORDER BY quote";
}
else if($source == "Anonymous") {
$sql .= "AND (author = '' OR author ='Anonymous')";
}
else {
$sql .= "AND (source = '{$source}' OR author = '{$source}')";
}
$quotes = $wpdb->get_results($sql, ARRAY_A);
if ( !empty($quotes) ) {
foreach($quotes as $quote_data) {
$display .= "".wptexturize(nl2br($quote_data['quote']))." ";
if($quote_data['author'] || $quote_data['source'])
$display .= " — ";
if($quote_data['author'])
$display .= "{$quote_data['author']} ";
if($quote_data['source']) {
$display .="from {$quote_data['source']} ";
}
$display .= " ";
}
return $display;
}
else
return "";
}
function quotescollection_inpost( $text ) {
$start = strpos($text,"[quote|id=");
if ($start !== FALSE) {
$text = preg_replace( "/\[quote\|id=(\d+)\]/ie", "quotescollection_displayquote('\\1')", $text );
}
$start = strpos($text,"[quote|random]");
if ($start !== FALSE) {
$text = preg_replace( "/\[quote\|random\]/ie", "quotescollection_displayquote()", $text );
}
$start = strpos($text,"[quote|all]");
if ($start !== FALSE) {
$text = preg_replace( "/\[quote\|all\]/ie", "quotescollection_displayquotes()", $text );
}
$start = strpos($text,"[quote|author=");
if($start !== FALSE) {
$text = preg_replace("/\[quote\|author=(.{1,})?\]/ie", "quotescollection_displayquotes('\\1')", $text);
}
$start = strpos($text,"[quote|source=");
if($start !== FALSE) {
$text = preg_replace("/\[quote\|source=(.{1,})?\]/ie", "quotescollection_displayquotes('\\1')", $text);
}
return $text;
}
function quotescollection_css_head()
{
?>