Advertisement
zkoprek

Untitled

Feb 15th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.03 KB | None | 0 0
  1. <?php
  2. /*
  3.   Plugin Name: WP Questions
  4.   Plugin URI: -
  5.   Description: Question and Answer interface for developers
  6.   Version: 1.0
  7.   Author: Rakhitha Nimesh
  8.   Author URI: http://www.innovativephp.com/
  9.   License: GPLv2 or later
  10. */
  11.  
  12. add_action('init', 'register_wp_questions');
  13.  
  14. /*
  15. * Register new custom post type for questions
  16. *
  17. * @return void
  18. */
  19. function register_wp_questions() {
  20.  
  21.     $labels = array(
  22.             'name'                  => __( 'Questions', 'wp_question' ),
  23.             'singular_name'         => __( 'Question', 'wp_question' ),
  24.             'add_new'               => __( 'Add New', 'wp_question' ),
  25.             'add_new_item'          => __( 'Add New Question', 'wp_question' ),
  26.             'edit_item'             => __( 'Edit Questions', 'wp_question' ),
  27.             'new_item'              => __( 'New Question', 'wp_question' ),
  28.             'view_item'             => __( 'View Question', 'wp_question' ),
  29.             'search_items'          => __( 'Search Questions', 'wp_question' ),
  30.             'not_found'             => __( 'No Questions found', 'wp_question' ),
  31.             'not_found_in_trash'    => __( 'No Questions found in Trash', 'wp_question' ),
  32.             'parent_item_colon'     => __( 'Parent Question:', 'wp_question' ),
  33.             'menu_name'             => __( 'Questions', 'wp_question' ),
  34.     );
  35.  
  36.     $args = array(
  37.             'labels'                => $labels,
  38.             'hierarchical'          => true,
  39.             'description'           => __( 'Questions and Answers', 'wp_question' ),
  40.             'supports'              => array( 'title', 'editor', 'comments' ),
  41.             'public'                => true,
  42.             'show_ui'               => true,
  43.             'show_in_menu'          => true,
  44.             'show_in_nav_menus'     => true,
  45.             'publicly_queryable'    => true,
  46.             'exclude_from_search'   => false,
  47.             'has_archive'           => true,
  48.             'query_var'             => true,
  49.             'can_export'            => true,
  50.             'rewrite'               => true,
  51.             'capability_type'       => 'post'
  52.     );
  53.  
  54.     register_post_type( 'wp_question', $args );
  55. }
  56.  
  57. add_action( 'wp_enqueue_scripts', 'wpwa_frontend_scripts' );
  58.  
  59. /*
  60. * Include neccessary scripts and styles for the plugin
  61. *
  62. * @return void
  63. */
  64. function wpwa_frontend_scripts() {
  65.  
  66.     wp_enqueue_script( 'jquery' );
  67.     wp_register_script( 'wp-questions', plugins_url( 'js/questions.js', __FILE__ ), array('jquery'), '1.0', TRUE );
  68.     wp_enqueue_script( 'wp-questions' );
  69.  
  70.     wp_register_style( 'questions', plugins_url( 'css/questions.css', __FILE__ ) );
  71.     wp_enqueue_style( 'questions' );
  72.  
  73.     $config_array = array(
  74.             'ajaxURL' => admin_url( 'admin-ajax.php' ),
  75.             'ajaxNonce' => wp_create_nonce( 'ques-nonce' )
  76.     );
  77.  
  78.     wp_localize_script( 'wp-questions', 'wpwaconf', $config_array );
  79. }
  80.  
  81.  
  82. add_action( 'wp_ajax_mark_answer_status', 'wpwa_mark_answer_status' );
  83.  
  84. /*
  85. * Mark the correct/incorrect status for each answer
  86. *
  87. * @return string
  88. */
  89. function wpwa_mark_answer_status() {
  90.  
  91.     $data = isset( $_POST['data'] ) ? $_POST['data'] : array();
  92.  
  93.     $comment_id     = isset( $data["comment_id"] ) ? absint($data["comment_id"]) : 0;
  94.     $answer_status  = isset( $data["status"] ) ? $data["status"] : 0;
  95.     ;
  96.  
  97.     // Mark answers in correct status to incorrect
  98.     // or incorrect status to correct
  99.     if ("valid" == $answer_status) {
  100.         update_comment_meta( $comment_id, "_wpwa_answer_status", 1 );
  101.     } else {
  102.         update_comment_meta( $comment_id, "_wpwa_answer_status", 0 );
  103.     }
  104.  
  105.     echo json_encode( array("status" => "success") );
  106.     exit;
  107. }
  108.  
  109.  
  110. /*
  111. * Mark the correct/incorrect status for each answer
  112. *
  113. * @param  int    Post Id of the current question
  114. * @return void
  115. */
  116. function wpwa_get_correct_answers( $post_id ) {
  117.  
  118.     $args = array(
  119.             'post_id'   => $post_id,
  120.             'status'    => 'approve',
  121.             'meta_key'  => '_wpwa_answer_status',
  122.             'meta_value'=> 1,
  123.     );
  124.  
  125.     // Get number of correct answers for given question
  126.     $comments = get_comments( $args );
  127.     printf(__('<cite class="fn">%s</cite> correct answers'), count( $comments ) );
  128. }
  129.  
  130.  
  131. /*
  132. * Generate custom comments list with customized fields and values
  133. *
  134. * @param  array Automatically passed comments object
  135. * @param  array Required arguments
  136. * @param  int   Depth of comment
  137. * @return void
  138. */
  139. function wpwa_comment_list( $comment, $args, $depth ) {
  140.     global $post;
  141.  
  142.     $GLOBALS['comment'] = $comment;
  143.  
  144.     // Get current logged in user and author of question
  145.     $current_user           = wp_get_current_user();
  146.     $author_id              = $post->post_author;
  147.     $show_answer_status     = false;
  148.  
  149.     // Set the button status for authors of the question
  150.     if ( is_user_logged_in() && $current_user->ID == $author_id ) {
  151.         $show_answer_status = true;
  152.     }
  153.  
  154.     // Get the correct/incorrect status of the answer
  155.     $comment_id = get_comment_ID();
  156.     $answer_status = get_comment_meta( $comment_id, "_wpwa_answer_status", true );
  157.  
  158.  
  159.  
  160.     ?>
  161. <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
  162. <article id="comment-<?php comment_ID(); ?>">
  163.     <header class="comment-meta comment-author vcard">
  164.             <?php
  165.             // Display image of a tick for correct answers
  166.             if ( $answer_status ) {
  167.                 echo "<div class='tick'><img src='".plugins_url( 'img/tick.png', __FILE__ )."' alt='Answer Status' /></div>";
  168.             }
  169.             ?>
  170.             <?php echo get_avatar( $comment, $size = '48', $default = '<path_to_url>' ); ?>
  171.     <?php printf(__('<cite class="fn">Answered by %s</cite>'), get_comment_author_link() ) ?>
  172.  
  173.     </header>
  174.     <?php if ( '0' == $comment->comment_approved ) : ?>
  175.     <em><?php _e('Your answer is awaiting moderation.') ?></em>
  176.     <br />
  177.     <?php endif; ?>
  178.  
  179.  
  180.     <?php comment_text() ?>
  181.  
  182.     <div class="reply">
  183.     <?php comment_reply_link( array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']) ) ) ?>
  184.     </div>
  185.  
  186.  
  187.     <div>
  188.             <?php
  189.             // Display the button for authors to make the answer as correct or incorrect
  190.             if ( $show_answer_status ) {
  191.  
  192.                 $question_status = '';
  193.                 $question_status_text = '';
  194.                 if ( $answer_status ) {
  195.                     $question_status = 'invalid';
  196.                     $question_status_text = 'Mark as Incorrect';
  197.                 } else {
  198.                     $question_status = 'valid';
  199.                     $question_status_text = 'Mark as Correct';
  200.                 }
  201.  
  202.         ?>
  203.         <input type="button" value="<?php echo $question_status_text; ?>"  class="answer-status answer_status-<?php echo $comment_id; ?>"
  204.                data-ques-status="<?php echo $question_status; ?>" />
  205.         <input type="hidden" value="<?php echo $comment_id; ?>" class="hcomment" />
  206.  
  207.                 <?php
  208.             }
  209.     ?>
  210.     </div>
  211. </article>
  212. </li>
  213.     <?php
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement