init(); $this->get_settings(); $this->update_settings(); $this->register_custom_post_type(); $this->setup_settings_page(); $this->setup_dashboard_widgets(); $this->setup_notifications(); $this->setup_additional_pages(); $this->register_scripts(); $this->register_styles(); } private function reset_settings() { update_option( 'askit_settings', $this->defaults ); } /** * Initialize default settings, and the carriers vars. * * @since 1.0 */ public function init() { $this->defaults = array( 'capability_to_see_questions' => '', 'capability_to_ask_questions' => '', 'capability_to_edit_settings' => 'manage_options', 'capability_to_answer_questions' => 'manage_options', 'custom_post_singular' => 'Question', 'custom_post_plural' => 'Questions', 'answered_questions_widget_title' => 'Answered Questions', 'my_questions_dashboard_widget_title' => 'My Questions', 'answered_questions_limit' => -1, 'question_form_dashboard_widget_title' => 'Question? Ask it here!', 'my_questions_limit' => -1, 'offset' => get_option('gmt_offset'), 'notification_question_asked_email' => true, 'notification_question_asked_text' => false, 'notification_question_answered_email' => true, 'question_asked_email_to' => get_option( 'admin_email' ), 'question_asked_email_subject' => '[AskIt] Question Asked!', 'question_asked_text_number' => '', 'question_asked_text_carrier' => '', 'question_asked_text_subject' => '[AskIt Question]', 'question_answered_email_subject' => '[AskIt] Question Answered!', 'question_answered_email_from' => 'AskItQuestions', 'dashboard_cleanup' => array( 'dashboard_right_now' => array( 'remove' => false, 'name' => 'Right Now', ), 'dashboard_recent_comments' => array( 'remove' => false, 'name' => 'Recent Comments', ), 'dashboard_incoming_links' => array( 'remove' => false, 'name' => 'Incoming Links', ), 'dashboard_plugins' => array( 'remove' => false, 'name' => 'Plugins', ), 'dashboard_quick_press' => array( 'remove' => false, 'name' => 'Quick Press', ), 'dashboard_recent_drafts' => array( 'remove' => false, 'name' => 'Recent Drafts', ), 'dashboard_primary' => array( 'remove' => false, 'name' => 'WordPress Blog', ), 'dashboard_secondary' => array( 'remove' => false, 'name' => 'Other WordPress News', ) ) ); // Used in sending text notifications $this->carriers = array( 'verizon' => 'Verizon', 'uscellular' => 'US Cellular', 'sprint' => 'Sprint', 'att' => 'AT&T', 'cingular' => 'Cingular', 'qwest' => 'Qwest', 'nextel' => 'Nextel', 'tmobile' => 'T-Mobile', 'vmobile' => 'Virgin Mobile' ); $this->carrier_domains = array( 'verizon' => 'vtext.com', 'cingular' => 'cingularme.com', 'sprint' => 'messaging.sprintpcs.com', 'uscellular' => 'email.uscc.net', 'att' => 'txt.att.net', 'qwest' => 'qwestmp.com', 'nextel' => 'messaging.nextel.com', 'tmobile' => 'tmomail.net', 'vmobile' => 'vmobl.com' ); } /** * Retrieves settings from WordPress option, * uses defaults if this option has not been set yet. * * @since 1.0 */ public function get_settings() { $this->settings = get_option( 'askit_settings', $this->defaults ); } /** * Runs if the settings form has been submitted. * * @since 1.0 */ public function update_settings() { // Flag used to display the 'updated' notification $this->settings_updated = false; if ( isset( $_POST['askit_settings'] ) ) : // Cycle through the POST vars and use esc_attr() to validate them foreach ( $_POST as $key => $val ) { $_POST[$key] = esc_attr( $val ); } // We can't let the current user set the capability required to edit settings // to a capability that he or she doesn't possess.. that would just be silly $capability_to_edit_settings = ( current_user_can( $_POST['capability-to-edit-settings'] ) ) ? $_POST['capability-to-edit-settings'] : $this->settings['capability_to_edit_settings']; // Same deal with the capability required to ask, answer & see questions $capability_to_see_questions = ( '' === trim( $_POST['capability-to-see-questions'] ) || current_user_can( $_POST['capability-to-see-questions'] ) ) ? $_POST['capability-to-see-questions'] : $this->settings['capability_to_see_questions']; $capability_to_ask_questions = ( '' === trim( $_POST['capability-to-ask-questions'] ) || current_user_can( $_POST['capability-to-ask-questions'] ) ) ? $_POST['capability-to-ask-questions'] : $this->settings['capability_to_ask_questions']; $capability_to_answer_questions = ( current_user_can( $_POST['capability-to-answer-questions'] ) ) ? $_POST['capability-to-answer-questions'] : $this->settings['capability_to_answer_questions']; // These following settings cannot be blank, if they are, we stay with the current setting $custom_post_singular = ( "" != $_POST['custom-post-singular'] ) ? $_POST['custom-post-singular'] : $this->settings['custom_post_singular']; $custom_post_plural = ( "" != $_POST['custom-post-plural'] ) ? $_POST['custom-post-plural'] : $this->settings['custom_post_plural']; $answered_questions_dashboard_widget_title = ( "" != $_POST['answered-questions-dashboard-widget-title'] ) ? $_POST['answered-questions-dashboard-widget-title'] : $this->settings['answered_questions_dashboard_widget_title']; $my_questions_dashboard_widget_title = ( "" != $_POST['my-questions-dashboard-widget-title'] ) ? $_POST['my-questions-dashboard-widget-title'] : $this->settings['my_questions_dashboard_widget_title']; $question_form_dashboard_widget_title = ( "" != $_POST['question-form-dashboard-widget-title'] ) ? $_POST['question-form-dashboard-widget-title'] : $this->settings['question_form_dashboard_widget_title']; // My Questions limit must be an integer $answered_questions_limit = round( (int) $_POST['answered-questions-limit'] ); $my_questions_limit = round( (int) $_POST['my-questions-limit'] ); $settings = array( 'capability_to_see_questions' => $capability_to_see_questions, 'capability_to_ask_questions' => $capability_to_ask_questions, 'capability_to_edit_settings' => $capability_to_edit_settings, 'capability_to_answer_questions' => $capability_to_answer_questions, 'custom_post_singular' => $custom_post_singular, 'custom_post_plural' => $custom_post_plural, 'answered_questions_dashboard_widget_title' => $answered_questions_dashboard_widget_title, 'answered_questions_limit' => $answered_questions_limit, 'my_questions_dashboard_widget_title' => $my_questions_dashboard_widget_title, 'question_form_dashboard_widget_title' => $question_form_dashboard_widget_title, 'my_questions_limit' => $my_questions_limit, 'offset' => get_option('gmt_offset'), 'notification_question_asked_email' => isset( $_POST['notification-question-asked-email'] ), 'notification_question_asked_text' => isset( $_POST['notification-question-asked-text'] ), 'notification_question_answered_email' => isset( $_POST['notification-question-answered-email'] ), 'question_asked_email_to' => $_POST['question-asked-email-to'], 'question_asked_email_subject' => $_POST['question-asked-email-subject'], 'question_asked_text_number' => $_POST['question-asked-text-number'], 'question_asked_text_carrier' => $_POST['question-asked-text-carrier'], 'question_asked_text_subject' => $_POST['question-asked-text-subject'], 'question_answered_email_subject' => $_POST['question-answered-email-subject'], 'question_answered_email_from' => $_POST['question-answered-email-from'], 'remove_dashboard_answered_questions' => isset( $_POST['remove_dashboard_answered_questions'] ), 'remove_dashboard_my_questions' => isset( $_POST['remove_dashboard_my_questions'] ), 'remove_dashboard_question_form' => isset( $_POST['remove_dashboard_question_form'] ), 'dashboard_cleanup' => array( 'dashboard_right_now' => array( 'remove' => isset( $_POST['dashboard_right_now'] ), 'name' => 'Right Now', ), 'dashboard_recent_comments' => array( 'remove' => isset( $_POST['dashboard_recent_comments'] ), 'name' => 'Recent Comments', ), 'dashboard_incoming_links' => array( 'remove' => isset( $_POST['dashboard_incoming_links'] ), 'name' => 'Incoming Links', ), 'dashboard_plugins' => array( 'remove' => isset( $_POST['dashboard_plugins'] ), 'name' => 'Plugins', ), 'dashboard_quick_press' => array( 'remove' => isset( $_POST['dashboard_quick_press'] ), 'name' => 'Quick Press', ), 'dashboard_recent_drafts' => array( 'remove' => isset( $_POST['dashboard_recent_drafts'] ), 'name' => 'Recent Drafts', ), 'dashboard_primary' => array( 'remove' => isset( $_POST['dashboard_primary'] ), 'name' => 'WordPress Blog', ), 'dashboard_secondary' => array( 'remove' => isset( $_POST['dashboard_secondary'] ), 'name' => 'Other WordPress News', ) ) ); $this->settings_updated = add_option( 'askit_settings', $settings ) or update_option( 'askit_settings', $settings ); $this->get_settings(); endif; } /** * Hooks into `admin_menu` to add Ask It settings page. * * @since 1.0 */ public function setup_settings_page() { add_action( 'admin_menu', array( &$this, 'add_settings_page' ) ); } /** * Adds Ask It settings page to menu. * * @since 1.0 */ public function add_settings_page() { add_submenu_page( 'options-general.php', 'Ask It Settings', 'Ask It', $this->settings['capability_to_edit_settings'], 'ask-it-settings', array( &$this, 'settings_page' ) ); } /** * Displays Ask It settings page. * * @since 1.0 */ public function settings_page() { ?>
Ask It settings have been updated.
You must answer this question before making it public.
'askit_question', 'p' => $post_id, 'posts_per_page' => 1 ) ); if ( $questions->have_posts() ) : while ( $questions->have_posts() ) : $questions->the_post(); $status = ( get_the_content() != "" ) ? 'Answered' : 'Asked'; endwhile; endif; wp_reset_query(); add_post_meta( $post_id, 'askit_question_status', $status, true ) or update_post_meta( $post_id, 'askit_question_status', $status ); return; } /** * Adds hooks to remove and add dashboard widgets. * * @since 1.0 */ public function setup_dashboard_widgets() { add_action( 'admin_menu', array( &$this, 'remove_dashboard_widgets' ) ); add_action( 'wp_dashboard_setup', array( &$this, 'add_dashboard_widgets' ) ); } /** * Removes selected default dashboard widgets. * * @since 1.0 */ public function remove_dashboard_widgets() { foreach ( $this->settings['dashboard_cleanup'] as $key => $c ) { if ( $c['remove'] ) { remove_meta_box( $key, 'dashboard', 'core' ); } } } /** * Adds Ask It dashboard widgets (unless specified otherwise in settings). * * @since 1.0 */ public function add_dashboard_widgets() { if ( ! $this->settings['remove_dashboard_answered_questions'] ) wp_add_dashboard_widget( 'askit_answered_questions', $this->settings['answered_questions_dashboard_widget_title'], array( &$this, 'dashboard_answered_questions' ) ); if ( $this->can_ask_question() ) { if ( ! $this->settings['remove_dashboard_my_questions'] ) wp_add_dashboard_widget( 'askit_my_questions', $this->settings['my_questions_dashboard_widget_title'], array( &$this, 'dashboard_my_questions' ) ); if ( ! $this->settings['remove_dashboard_question_form'] ) wp_add_dashboard_widget( 'askit_question_form', $this->settings['question_form_dashboard_widget_title'], array( &$this, 'dashboard_question_form' ) ); } } /** * Adds hooks to setup email & text notifications * * @since 1.0 */ public function setup_notifications() { add_action( 'admin_menu', array( &$this, 'add_alert_page' ) ); add_action( 'save_post', array( &$this, 'notify_asker' ) ); } /** * Adds Ask It alert page for when questions are asked. * * @since 1.0 */ public function add_alert_page() { // We add this page and then instantly remove it so that it never actually shows up in the menu. // We just want to use it as a function page that will be called via ajax when the Question Form is submited via the dashboard. add_menu_page( 'Askit Alert', 'Askit Alert', 'manage_options', 'askit-alert', array( &$this, 'question_asked_notifications' ) ); remove_menu_page( 'askit-alert' ); } /** * Registers & enqueues Ask It scripts * * @since 1.0 */ public function register_scripts() { wp_register_script( 'askit-js', ASKIT_PLUGIN_URL . '/ask-it.js', array( 'jquery' ) ); wp_enqueue_script( 'askit-js' ); } /** * Registers & enqueues Ask It styles * * @since 1.0 */ public function register_styles() { wp_register_style( 'askit-css', ASKIT_PLUGIN_URL . '/ask-it.css' ); wp_enqueue_style( 'askit-css' ); } /** * Dashboard Widget: displays answered questions that have been 'made public'. * * @since 1.0 */ public function dashboard_answered_questions() { $args = array( 'post_type' => 'askit_question', 'posts_per_page' => $this->settings['answered_questions_limit'], 'meta_key' => 'askit_public', 'meta_value' => true ); $questions = new WP_Query( $args ); if ( $questions->have_posts() ) : while ( $questions->have_posts() ) : $questions->the_post(); ?> 'askit_question', 'author' => $current_user->ID, 'posts_per_page' => $this->settings['my_questions_limit'], 'orderby' => 'modified' ); $questions = new WP_Query( $args ); if ( $questions->have_posts() ) : while ( $questions->have_posts() ) : $questions->the_post(); $removed = get_post_meta( get_the_ID(), 'askit_removed', true ); if ( $removed ) continue; ?> settings['notification_question_asked_email'] && ( "" != $this->settings['question_asked_email_to'] ) ) { mail( $this->settings['question_asked_email_to'], $this->settings['question_asked_email_subject'], $asker . ' has asked you a question on ' . get_bloginfo( 'home' ), $headers ); } // Text notification if ( $this->settings['notification_question_asked_text'] && ( "" != $this->settings['question_asked_text_number'] ) && ( "" != $this->settings['question_asked_text_carrier'] ) ) { // We send a text notification by sending an email to the number @ carrier_domain // More info here: @link http://www.makeuseof.com/tag/email-to-sms/ // And here: @link http://www.venture-ware.com/kevin/web-development/email-to-sms/ $to = $this->settings['question_asked_text_number'] . '@' . $this->carrier_domains[$this->settings['question_asked_text_carrier']]; $message = $this->settings['question_asked_text_subject'] . ' Question asked on ' . get_bloginfo( 'home' ) . " by " . $asker; mail( $to, '', $message ); } } } /** * Called on `save_post`. Notifies the asker if their question was answered. * * @since 1.0 * * @param int $id post id */ public function notify_asker( $id ) { if ( ! $this->settings['notification_question_answered_email'] ) return; if ( get_post_type( $id ) !== 'askit_question' ) return; // This will only be set if the asker has already been notified. // This prevents multiple notifications in case an administrator makes minor changes later on. if ( get_post_meta( $id, 'askit_asker_notified', true ) ) return; $args = array( 'post_type' => 'askit_question', 'p' => $id, 'posts_per_page' => 1 ); $question = new WP_Query( $args ); // Notify asker by email if( $question->have_posts() ) : while( $question->have_posts() ) : $question->the_post(); if( get_the_content() != "" ){ global $current_user; get_currentuserinfo(); $to = $current_user->display_name . " <" . $current_user->user_email . ">"; $subject = $this->settings['question_answered_email_subject']; $message = ""; $message .= "