'.__("Updates Saved!").'

'; } /** * * Runs the required SQL when the plugin is activated * * @param none * @return none */ function amq_install() { global $wpdb,$amq_db_version,$table_name; $table_name = $wpdb->prefix . "answer_my_question"; $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, date_asked DATETIME NOT NULL, date_response DATETIME NOT NULL, user_name VARCHAR(60) NOT NULL, user_email VARCHAR(60) NOT NULL, url VARCHAR(60) DEFAULT '' NOT NULL, subject VARCHAR(60) NOT NULL, question TEXT NOT NULL, answer TEXT NOT NULL, answered tinyint(1) NOT NULL, notify_user tinyint(1) NOT NULL, show_on_site tinyint(1) NOT NULL DEFAULT '1', UNIQUE KEY id (id));"; dbDelta($sql); add_option("amq_db_version", $amq_db_version); //Update Database Schema $installed_ver = get_option("amq_db_version"); if($installed_ver != $amq_db_version) { $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, date_asked DATETIME NOT NULL, date_response DATETIME NOT NULL, user_name VARCHAR(60) NOT NULL, user_email VARCHAR(60) NOT NULL, url VARCHAR(60) DEFAULT '' NOT NULL, subject VARCHAR(60) NOT NULL, question TEXT NOT NULL, answer TEXT NOT NULL, answered tinyint(1) NOT NULL, notify_user tinyint(1) NOT NULL, show_on_site tinyint(1) NOT NULL DEFAULT '1', UNIQUE KEY id (id));"; dbDelta($sql); update_option("amq_db_version", $amq_db_version); } } /** * * Compares the current database version with the version recorded as installed in the users database. If they don't match, amq_install is run to update the schema * * @param none * @return none */ function amq_update_db_check() { global $amq_db_version; if (get_site_option('amq_db_version') != $amq_db_version) { amq_install(); } } /** * * Adds top level and sub level menu item for the plugin. Editor and above can use the plugin * * @param none * @return none */ function register_amq_menu_page() { add_menu_page("Answer My Question", "Answer My Question", "delete_pages", "answer-my-question", "answerMyQuestionView"); add_submenu_page("answer-my-question", "", "Settings", "delete_pages", "answer-my-question-settings", "answerMyQuestionSettings"); } /** * * Enqueues the plugin specific JavaScript and CSS to to the head for the main admin page * * @param none * @return none */ function loadMainScripts() { global $plugindir; wp_enqueue_script('', $plugindir . '/js/main.js'); echo "\n"; } /** * * Enqueues the plugin specific JavaScript and CSS to the public facing site * * @param none * @return none */ function loadClientAssets(){ echo "\n\n"; wp_enqueue_script('', plugins_url('js/answer_my_question_scripts.js', __FILE__), false, false, true); } /** * * Inserts the modal markup into the public facing site's footer * * @param none * @return none */ function insertModal() { global $wpdb; $table_name = $wpdb->prefix . "options"; //Get the plugin options $result = $wpdb->get_results("SELECT option_value FROM $table_name WHERE option_name = 'amq_option_item' LIMIT 1;"); if(count($result) > 0){ foreach($result as $row){ $modalDetails = unserialize($row->option_value); } } $modalTitle = (!empty($modalDetails['title']) ? sprintf( __('%s'), $modalDetails['title']) : __('Answer My Question')); $modalBody = (!empty($modalDetails['body']) ? str_replace("\n", "
", sprintf( __('%s'), $modalDetails['body'])) : __('Please fill out the form below.')); echo '

'.$modalTitle.'

'.__("CLOSE").'

'.__("Your question has been sent!").'

'.$modalBody.'

'.__("Name ").' *
'.__("Email ").' *
'.__("URL").' ('.__("include").' http://)
'.__("Subject").' *
'.__("Question").' *
* '.__("Required Field").'
'; } /** * * Front end markup for the admin panel screen * * @param none * @return none */ function answerMyQuestionView() { loadMainScripts(); global $plugindir,$wpdb; $table_name = $wpdb->prefix . "answer_my_question"; //Get a list of all questions $result = $wpdb->get_results("SELECT id, date_asked, user_name, user_email, subject, show_on_site, answered FROM $table_name ORDER BY date_asked DESC"); echo '

'.__("Answer My Question").'

Here you can administer all questions submitted by your site users.

 

'; if(count($result) > 0){ echo '
'; foreach($result as $row){ $answerStatus = ($row->answered == 0 ? __('unanswered') : __('answered')); $displayStatus = ($row->show_on_site == 0 ? ''.__("No").'' : ''.__("Yes").''); echo ''; } echo '
'.__("Date Asked").' '.__("Status").' '.__("Name").' '.__("Email").' '.__("Subject").' '.__("Show On Site").' '.__("Actions").'
'.date("m/d/y", strtotime($row->date_asked)).' '.ucfirst($answerStatus).' '.$row->user_name.' '.$row->user_email.' '.$row->subject.' '.$displayStatus.' '.__( '.__(
'; }else{ echo ''.__("Looks like you don't have any questions yet!").''; } echo '
'; } /** * * Updates the database record for the question * * @param array $data: Array containing all posted question data * @return boolean True if row has been updated. False otherwise */ function updateQuestionData($data=array()){ global $wpdb; $table_name = $wpdb->prefix . "answer_my_question"; $questionID = $data['id']; $notifyUser = $data['notify']; $userEmail = $data['user_email']; unset($data['id'], $data['posted'], $data['notify'], $data['user_email']); $data['date_response'] = date("Y-m-d G:i:s"); $data['answered'] = 1; $wpdb->update( $table_name, $data, array('id' => $questionID) ); if($wpdb->rows_affected > 0){ //Should user be notified? if($notifyUser == 1){ $result = $wpdb->get_results("SELECT answer FROM $table_name WHERE id = $questionID LIMIT 1;"); foreach($result as $row){ $response = $row->answer; } sendNotificationMail($userEmail, $data['subject'], $data['date_response'], $response); } } return true; } /** * * Send an email notification to a user that requested to be notified * * @param string $email: Email Address * @param string $subject: The subject of the users question * @param string $responseDate: Date of admin response. Should be in MySQL datetime format * @return boolean True if email was sent. False otherwise */ function sendNotificationMail($email, $subject, $responseDate, $response){ $to = $email; $emailSubject = 'You\'re Question Has Been Answered!'; $message = ' You\'re Question Has Been Answered!

Hello,
This is an automated email from '.get_bloginfo("name").'.

Your quesiton titled: '.$subject.', has been answered on '.date("F j, Y", strtotime($responseDate)).'.


"'.$response.'"

'; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers // NOTE: If multiple admins, you need to explode here and change $headers $headers .= 'To: '.$email.' <'.$email.'>' . "\r\n"; $headers .= 'From: '.get_bloginfo('admin_email').' <'.get_bloginfo('admin_email').'>' . "\r\n"; // Mail it if(mail($to, $emailSubject, $message, $headers)){ return true; }else{ return false; } } /** * * Settings options page * * @param none * @return none */ function answerMyQuestionSettings() { loadMainScripts(); if($_GET['settings-updated'] == true){ echo '

'.__("Settings saved").'.

'; } ?>

/>
.

prefix . "answer_my_question"; $result = $wpdb->get_results("SELECT * FROM $table_name WHERE answered = 1 AND show_on_site = 1 ORDER BY date_asked DESC;"); $amqList = '
'; if(count($result) > 0){ foreach($result as $row){ $userName = (!empty($row->url) ? ''.$row->user_name.'': $row->user_name); $amqList .= date('m/d/Y', strtotime($row->date_asked)).' - '. $userName.' '.__("asks").': '.$row->subject.' "'.$row->question.'" '.__("Answer").':
'.$row->answer.'
'; } } $amqList .= '
'; return $amqList; } function amqModal($atts, $content = null) { return '' . $content . ''; } /** * * Sanitize and validate input * * @param array $input: Array of form values * @return array $input: Array of sanitized values for the database */ function amq_options_validate($input) { //Only valid form values can be passed $possible_values = array( $input['title'], $input['body'], ); if(isset($input['notify'])){ $possible_values[] = $input['notify']; $input['notify'] = ($input['notify'] == 1 ? 1 : 0); } if(isset($input['email'])){ $possible_values[] = $input['email']; } foreach($input as $key=>$value){ if(!in_array($value, $possible_values)){ unset($input[$key]); } } // No HTML tags $input['title'] = wp_filter_nohtml_kses($input['title']); $input['body'] = wp_filter_nohtml_kses($input['body']); return $input; } /** * * Loads the jQuery library on the client facing site * * @param none * @return none */ function loadjQuery() { if (!is_admin()) { wp_deregister_script('jquery'); wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', false, '1.7.1', true); wp_enqueue_script('jquery'); } } load_plugin_textdomain('amq_plugin_I18n', false, basename( dirname( __FILE__ ) ) . '/languages' ); ?>