Options/Ask Me after activation of the plugin. To see the plugin in action you can visit Sara's or Jelena's blog Inspired by Dean Edwards' about me script http://dean.edwards.name/about */ /********************** CONSTANTS **********************/ define("TABLE_AM_QUESTIONS", $table_prefix."askme_questions"); define("TABLE_AM_QUESTIONS_PHRASE", $table_prefix."askme_questions_phrases"); define("TABLE_AM_COWARDS", $table_prefix."askme_cowards"); define("TABLE_AM_SETTINGS", $table_prefix."askme_settings"); /* // WordPress 1.5 includes this file again on the options page. Avoid duplicate declaration: if ( function_exists("is_plugin_page") && is_plugin_page() ) { askme_options_page(); return; } */ add_action('options_page_askme', 'askme_options_page'); /******************** INSTALLATION FUNC ********************/ function askme_install () { global $table_prefix, $wpdb; global $user_ID; require_once(ABSPATH . 'wp-admin/upgrade-functions.php'); $table_name_questions = $table_prefix . "askme_questions"; $table_name_questions_phrase = $table_prefix . "askme_questions_phrases"; $table_name_cowards = $table_prefix . "askme_cowards"; $table_name_settings = $table_prefix . "askme_settings"; if($wpdb->get_var("show tables like '$table_name_questions'") != $table_name_questions) { $sql = "CREATE TABLE ".$table_name_questions." ( question_ID bigint(20) NOT NULL auto_increment, question text NOT NULL, answer text, user varchar(50) default NULL, email varchar(100) default '', url varchar(100) default '', approved tinyint(1) default '0', date_entered datetime default '0000-00-00 00:00:00', wants_return_email tinyint(1) default '0', PRIMARY KEY (`question_ID`) );"; dbDelta($sql); } if($wpdb->get_var("show tables like '$table_name_questions_phrase'") != $table_name_questions_phrase) { $sql = "CREATE TABLE ".$table_name_questions_phrase." ( phrase_ID bigint(20) NOT NULL auto_increment, phrase varchar(255) NOT NULL default '', PRIMARY KEY (`phrase_ID`) )"; dbDelta($sql); $sql = "INSERT INTO ".$table_name_questions_phrase." (phrase_ID, phrase) VALUES (1, 'is left to wonder'), (2, 'is pondering'), (3, 'needs to know'), (4, 'wants to know'), (5, 'is curious about'), (6, 'wonders'), (7, 'is contemplating'), (8, 'is clueless about'), (9, 'has no idea about'), (10, 'is bugging me to know'), (11, 'thinks Google sucks and instead asks me'), (12, 'is too lazy to google and asks me'), (13, 'lies awake at night and wonders');"; $wpdb->query($sql); } if($wpdb->get_var("show tables like '$table_name_cowards'") != $table_name_questions) { $sql = "CREATE TABLE ".$table_name_cowards." ( coward_ID bigint(20) NOT NULL auto_increment, coward varchar(255) NOT NULL default '', PRIMARY KEY (`coward_ID`) );"; dbDelta($sql); $sql = "INSERT INTO ".$table_name_cowards." (coward_ID, coward) VALUES (1, 'Prefers to remain anonymous'), (2, 'Curious Coward'), (3, 'Icognito'), (4, 'Invisible Wo/man'), (5, 'Random Poster'), (6, 'My Stalker');"; $wpdb->query($sql); } //create a page called Ask me //prepare date: $date_now = current_time('mysql'); $date_now_gm = get_gmt_from_date($date_now); $page_url = get_option('siteurl')."/ask-me/"; $content = "Regular about pages are boring, aren't they? In addition to having a typical \"About Me\" page, I decided to have a page where visitors could ask what they want to know instead of me telling them what I think they should know. So go ahead, ask me a question. Of course these questions are moderated, but pretty much anything goes (within reason). "; $content = $wpdb->escape($content); $sql = "INSERT INTO ".$table_prefix."posts SET"; $sql .= " post_author = '".$user_ID."'"; $sql .= ", post_date = '".$date_now."'"; $sql .= ", post_date_gmt = '".$date_now_gm."'"; $sql .= ", post_title = 'Ask me'"; $sql .= ", post_name = 'ask-me'"; $sql .= ", post_status = 'publish'"; $sql .= ", post_category = 0"; $sql .= ", post_parent = 0"; $sql .= ", post_type = 'page'"; $sql .= ", post_content = '".$content."'"; $wpdb->query($sql); if($wpdb->get_var("show tables like '$table_name_settings'") != $table_name_questions) { $sql = "CREATE TABLE ".$table_name_settings." ( SettingsID int(11) NOT NULL auto_increment, Email varchar(255) NOT NULL default '', PerPage smallint(6) NOT NULL default '0', PerPagePublic tinyint(4) NOT NULL default '0', FormAction varchar(100) NOT NULL, PRIMARY KEY (`SettingsID`) );"; dbDelta($sql); $email = get_option('admin_email'); $sql = "INSERT INTO ".$table_name_settings." (SettingsID, Email, PerPage, PerPagePublic, FormAction) VALUES (1, '" . $email . "', 20, 20, '".$page_url."');"; $results = $wpdb->query($sql); } return true; } function askme_get_questions() { global $wpdb; if(!isset($_GET['start'])) { $start = 0; } else { $start = $_GET['start']; } //get No of records from settings tbl $sql = "SELECT PerPagePublic FROM ".TABLE_AM_SETTINGS." LIMIT 1"; $settings = $wpdb->get_row($sql); $limit = $settings->PerPagePublic; // No of records to be shown per page. askme_print_list($start, $limit); $numrows = askme_get_questions_count("SELECT question, answer, user, email, url, date_entered FROM ".TABLE_AM_QUESTIONS." WHERE approved='1' "); echo ""; } function askme_get_questions_count($sql) { global $wpdb; $questions = $wpdb->get_results($sql); return count($questions); } function askme_print_list($start, $limit) { //global $limit; //global $begin; global $wpdb; $questions = $wpdb->get_results("SELECT question, answer, user, email, url, date_entered FROM ".TABLE_AM_QUESTIONS." WHERE approved='1' ORDER BY date_entered DESC limit " . $start . ", " . $limit); $phrases = $wpdb->get_results("SELECT phrase FROM ".TABLE_AM_QUESTIONS_PHRASE." ORDER BY rand()"); if(is_array($questions) && !empty($questions)) { foreach ($questions as $question) { $randNumber = rand(1, count($phrases)-1); $post_question = stripslashes($question->question); $post_answer = stripslashes($question->answer); $url = $question->url; $name = $question->user; if (trim($name) == "") { $name = askme_get_coward(); } //start the question/answer div here echo "
"; //the phrase.. i.e. Sara wants to know: echo "
"; if($url) { echo "" . $name . " "; } else { echo $name . " "; } echo $phrases[$randNumber]->phrase .":"; echo "
"; echo "
" . $post_question . "
"; echo "
" . $post_answer . "
"; echo "
"; } } else { echo "

No questions answered at the moment. Be the first!

"; } } //get a random anonymous user title function askme_get_coward() { global $wpdb; $cowards = $wpdb->get_row("SELECT coward FROM ".TABLE_AM_COWARDS." ORDER BY RAND() LIMIT 1"); return $cowards->coward; }//askme_get_coward() function output_form() { global $start; global $limit; global $wpdb; //get action from the settings $result = $wpdb->get_row("select FormAction from " . TABLE_AM_SETTINGS); $action = $result->FormAction; $form = '

Feeling Nosy?

'; $form .= '
 
'; echo $form; }//output_form() function check_submit() { if (isset($_POST['question'])) { $errors = verify_input(); if (!empty($errors)) { echo "" . $errors . ""; } else { echo "Yay! Your question is on the way to be answered. :)"; insert_question(); send_email(); } } }//check_submit() function send_email() { global $wpdb; $settings = $wpdb->get_row("SELECT Email, PerPage FROM ".TABLE_AM_SETTINGS." LIMIT 1"); $subject = "Someone asked you a question!"; $from = "From: " . $settings->Email; $body = "A question is waiting for your moderation:\n\n"; $body .= "User: " . $_POST['username'] . "\n"; $body .= "Email: " . $_POST['email'] . "\n"; $body .= "URL: " . $_POST['url'] . "\n"; $body .= "Question: " . $_POST['question'] . "\n"; mail($settings->Email, $subject, $body, $from); }//send_email() function insert_question() { global $wpdb; if($_POST['wants_return_email']) { $checked = 1; } else { $checked = 0; } $name = $wpdb->escape($_POST['username']); $email = $wpdb->escape($_POST['email']); $url = $wpdb->escape($_POST['url']); if ($url == "http://") { $url = ""; } $question = $wpdb->escape($_POST['question']); $query = "INSERT INTO ".TABLE_AM_QUESTIONS." SET"; $query .= " question = '".$question."'"; $query .= ", user = '".$name."'"; $query .= ", url = '".$url."'"; $query .= ", email = '".$email."'"; $query .= ", approved = 0"; $query .= ", date_entered = NOW()"; $query .= ", wants_return_email = " . $checked; $wpdb->query($query); }//insert_question() function verify_input() { $errString = ''; if ($_POST['email'] == '') { $errString .= "You must enter your email.
"; } else if (!eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$', $_POST['email'])) { $errString .= "Your email must be properly formatted.
"; } if ($_POST['question'] == '') { $errString .= "You must ask a question."; } return $errString; }//verify_input() function askme_add_options_page() { if (function_exists('add_options_page')) { // WordPress 1.5 sometimes doesn't show the options page if called in the first style if ( $wp_version > "1.5" ) { add_options_page('Ask Me Plugin', 'Ask Me', 8, basename(__FILE__),'askme_options_page'); } else { add_options_page('Ask Me Plugin', 'Ask Me', 8, basename(__FILE__)); } } }//askme_add_options_page() function askme_admin_get_questions() { global $wpdb; if(!isset($_GET['start'])) { $start = 0; } else { $start = $_GET['start']; } //get No of records from settings tbl $sql = "SELECT PerPage FROM ".TABLE_AM_SETTINGS." LIMIT 1"; $settings = $wpdb->get_row($sql); $limit = $settings->PerPage; // No of records to be shown per page. $sql = "SELECT question_id, SUBSTRING(question, 1, 40) as question, SUBSTRING(answer, 1, 40) as answer, user, SUBSTRING(url, 1, 25) as url, DATE_FORMAT(date_entered, '%D %b %Y') as date_entered, approved FROM ".TABLE_AM_QUESTIONS; if ($_GET['filterBy'] != '') { if ($_GET['filterBy'] != 2) { $sql .= " WHERE approved= " . $_GET['filterBy']; } } $numrows = askme_get_questions_count($sql); echo "
Update Settings

You have " . $numrows . " questions.



"; echo "Filter your results. Show:
"; ?>
"; echo ""; if($start > 0) { print "
PREV"; } echo ""; $i=0; $j=1; for($i=0;$i < $numrows;$i=$i+$limit) { if($i <> $start) { echo " " . $j . " "; } else { echo "" . $j . ""; } /// Current page is not displayed as link and given font color blue $j=$j+1; } echo ""; if($start <= $numrows - $limit) { print "NEXT"; } echo "
"; }//askme_admin_get_questions() function askme_admin_print_list($sql, $start, $limit) { global $wpdb; if (!$start) $start = 0; $questions = $wpdb->get_results($sql . " ORDER BY approved, date_entered DESC limit " . $start . ", " . $limit); if(is_array($questions) && !empty($questions)) { ?> '; echo '
'; echo ""; foreach ($questions as $question) { $class = ('alternate' == $class) ? '' : 'alternate'; echo ""; ?>
Question Answer Name Url Date Approved? Action
question, 0, 300));?> answer, 0, 300))?> user)) ? "Anonymous" : $name = $question->user;?> url; ?> date_entered; ?> approved) == 0 ? "No" : "Yes";?> Edit | ?page=askme.php&filterBy=&start=&action=delete&id=question_id;?>' onclick='return confirm("Are you sure you want to delete this question");'>Delete
There are no questions in the database at the moment."; } }//askme_admin_print_list($sql) function askme_admin_display_form($id) { global $wpdb; $question = $wpdb->get_row("SELECT question, answer, user, email, url, date_entered,wants_return_email, approved FROM ".TABLE_AM_QUESTIONS." WHERE question_id = " . $id); ?>
Question

Answer
Name
Email
URL
Date Entered
Approved:
approved){echo "checked";} ?>" id="askme_approved" />
get_row("SELECT Email, PerPage, PerPagePublic, FormAction FROM ".TABLE_AM_SETTINGS." LIMIT 1"); ?>
Back to Question list

Update Settings

Email Address
Admin Per Page
Public Per Page
Form action
query($sql); echo ("

You have successfully deleted your question.

"); }//askme_admin_delete_question($id) function askme_admin_delete_question_all($id) { global $wpdb; $sql = "DELETE FROM ".TABLE_AM_QUESTIONS; $sql .= " WHERE 1>2"; for($i=0; $i query($sql); echo ("

You have successfully deleted selected questions.

"); }//askme_admin_delete_question_all($id) function askme_admin_edit_question($id) { global $wpdb; $sql = "UPDATE ".TABLE_AM_QUESTIONS." SET " . "question = '" . addslashes($_POST['askme_question']) . "', " . "answer = '" . addslashes($_POST['askme_answer']) . "', " . "user = '" . addslashes($_POST['askme_name']) . "', " . "email = '" . $_POST['askme_email'] . "', " . "url = '" . $_POST['askme_url'] . "', " . "date_entered = '" . $_POST['askme_date'] . "', "; $approved = 0; if ($_POST['askme_approved']) { $approved = 1; } $sql .= "approved = " . $approved . " WHERE question_id = " . $id; // echo $sql; $wpdb->query($sql); echo ("

You have successfully updated your question.

"); //if the person wants to be notified of a response, send email if($_POST['wants_return_email']==1 && $approved) { $sql = "SELECT Email, FormAction FROM ".TABLE_AM_SETTINGS." LIMIT 1"; $settings = $wpdb->get_row($sql); $email = $settings->Email; // Your email address $action = $settings->FormAction; // form action $sql = "select option_value from wp_options where option_name = 'blogname'"; $result = $wpdb->get_row($sql); $blogname = $result->option_value; //this will be the blog name $sql = "select option_value from wp_options where option_name = 'siteurl'"; $result = $wpdb->get_row($sql); $siteurl = $result->option_value; //this will be the site url $headers = "From: " . $email . "\r\n"; $subject = "Your question has been answered at: " . $blogname; $to = $_POST['askme_email']; $message = "Thank you for asking a question, I have answered it now. Please visit my site to see the answer:\n\n "; $message .= $action; mail($to,$subject,$message,$headers); }//end email }//askme_admin_edit_question($id) function askme_admin_edit_settings($data) { global $wpdb;