%quote%

by %author%

'); if ($wpdb->get_var("show tables like '" . YARQ_QUOTES_TABLE . "'") != YARQ_QUOTES_TABLE) { require_once(ABSPATH . 'wp-admin/upgrade-functions.php'); dbDelta("CREATE TABLE " . YARQ_QUOTES_TABLE . " ( id mediumint(9) NOT NULL AUTO_INCREMENT, author VARCHAR(255) NOT NULL, source VARCHAR(255) NOT NULL, quote TEXT NOT NULL, UNIQUE KEY id (id) );"); $wpdb->query("INSERT INTO " . YARQ_QUOTES_TABLE . " (author, source, quote) VALUES ('WordPress', 'http://wordpress.org', 'Code is poetry.');"); } } // // Generate links in the admin menu to the YARQ admin pages // function yarq_generate_admin_menu() { if (function_exists('add_options_page')) { add_options_page('Yet Another Random Quote', 'YARQ', 10, basename(__FILE__), 'yarq_admin_options'); add_management_page('Manage Random Quotes', 'Quotes', 10, basename(__FILE__), 'yarq_admin_manage'); } } // // YARQ Admin options panel // function yarq_admin_options() { if (isset($_POST['update_options'])) { update_option('yarq_format', $_POST['yarq_format']); echo '

' . __('Options updated.') . '

'; } echo '
' . "\n"; echo '

' . __('Yet Another Random Quote Options') . '

' . "\n"; echo '
' . "\n"; echo '
' . "\n"; echo '' . __('Format') . '' . "\n"; echo '

' . __('The format and style used to display the quote. %quote% is replaced with the random quote, %author% is replaced by the name of the author of the quote, and %source% is replaced by the source of the quote.') . '

' . "\n"; echo '

' . "\n"; echo '
' . "\n"; echo '
' . "\n"; } // // YARQ Admin manage quotes panel // function yarq_admin_manage() { global $wpdb; // // Delete a quote // if (isset($_GET['delete'])) { $wpdb->query("DELETE FROM " . YARQ_QUOTES_TABLE . " WHERE id = '" . intval($_GET['delete']) . "'"); echo '

' . sprintf(__('Quote %d has been deleted.'), intval($_GET['delete'])) . '

'; } //Default values $yarq_author = ''; $yarq_source = ''; $yarq_quote = ''; $yarq_id = ''; $is_edit = false; //Check if we're editing and haven't run into errors yet. Then we should //have the id in the querystring. if (isset($_GET['id'])) { $yarq_id = intval($_GET['id']); $quotes = $wpdb->get_results("SELECT * FROM " . YARQ_QUOTES_TABLE . " WHERE id = $yarq_id;"); $quote = $quotes[0]; $yarq_source = $quote->source; $yarq_quote = $quote->quote; $yarq_author = $quote->author; $is_edit = true; } // // Add a quote // if (isset($_POST['save'])) { $errors = array(); if (empty($_POST['yarq_quote'])) { $errors[] = __('You did not enter a quote.'); } if (empty($_POST['yarq_author'])) { $errors[] = __('You did not enter an author.'); } $yarq_author = yarq_sql_post_value('yarq_author'); $yarq_source = yarq_sql_post_value('yarq_source'); $yarq_quote = yarq_sql_post_value('yarq_quote'); if (!empty($_POST['yarq_id'])) { $yarq_id = intval($_POST['yarq_id']); $is_edit = true; } if (count($errors) > 0) { echo '
' . "\n"; } else { if (empty($_POST['yarq_id'])) { $wpdb->query("INSERT INTO " . YARQ_QUOTES_TABLE . " (author, source, quote) VALUES ('$yarq_author','$yarq_source', '$yarq_quote');"); echo '

' . __('Quote added.') . '

'; } else { $yarq_id = intval($_POST['yarq_id']); $wpdb->query("UPDATE " . YARQ_QUOTES_TABLE . " SET author = '$yarq_author', source = '$yarq_source', quote = '$yarq_quote' WHERE id = $yarq_id;"); echo '

' . __("Quote $yarq_id updated.") . '

'; } //Clear these since we've already saved successfully $yarq_author = ''; $yarq_source = ''; $yarq_quote = ''; $yarq_id = ''; $is_edit = false; } } // // Add form // echo '
' . "\n"; echo '

' . __($is_edit ? 'Edit Quote ' . $yarq_id : 'Add Quote') . '

' . "\n"; echo '
' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '
' . __('Quote') . '
' . __('Author') . '
' . __('Source URL') . '
' . "\n"; echo '
' . "\n"; echo '
' . "\n"; // // List quotes // echo '
' . "\n"; echo '

' . __('Manage Quotes') . '

' . "\n"; $quotes = $wpdb->get_results("SELECT * FROM " . YARQ_QUOTES_TABLE . " ORDER BY id"); if ($quotes) { echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; $alternate = true; foreach($quotes as $quote) { echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; if (!empty($quote->source)) { echo '' . "\n"; } else { echo '' . "\n"; } echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; } echo '
' . __('ID') . '' . __('Quote') . '' . __('Author') . '' . __('Source') . '
' . $quote->id . '' . $quote->quote . '' . $quote->author . '' . $quote->source . '' . __('Edit') . '' . __('Delete') . '
' . "\n"; } else { echo '

' . __('No quotes found.') . '

'; } echo '
' . "\n"; echo '' . "\n"; } function yarq_sql_post_value($name) { return htmlspecialchars($_POST[$name], ENT_QUOTES); } // // Display a random quote // function yarq_display($format = '') { global $wpdb; if (empty($format)) { $format = stripslashes(get_option('yarq_format')); } $quote = $wpdb->get_row("SELECT * FROM " . YARQ_QUOTES_TABLE . " ORDER BY RAND() LIMIT 1"); $output = str_replace('%quote%', $quote->quote, $format); $output = str_replace('%author%', $quote->author, $output); echo str_replace('%source%', $quote->source, $output); } // // Add hooks // add_action('activate_yarq.php', 'yarq_install'); add_action('admin_menu', 'yarq_generate_admin_menu'); ?>