Advertisement
Viruthagiri

qa

Jul 25th, 2011
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /**
  4.  * The following functions are meant to be used directly in template files.
  5.  * They always echo.
  6.  */
  7.  
  8. /* = General Template Tags
  9. -------------------------------------------------------------- */
  10.  
  11. function the_qa_menu() {
  12.     $menu = array(
  13.         array(
  14.             'title' => __( 'Questions', QA_TEXTDOMAIN ),
  15.             'type' => 'archive',
  16.             'current' => !is_qa_page( 'unanswered' ) && !is_qa_page( 'ask' ) && !is_qa_page( 'edit' )
  17.         ),
  18.         array(
  19.             'title' => __( 'Unanswered', QA_TEXTDOMAIN ),
  20.             'type' => 'unanswered',
  21.             'current' => is_qa_page( 'unanswered' )
  22.         ),
  23.         $menu[] = array(
  24.             'title' => __( 'Ask a Question', QA_TEXTDOMAIN ),
  25.             'type' => 'ask',
  26.             'current' => is_qa_page( 'ask' )
  27.         )
  28.     );
  29.  
  30.     echo "<div id='qa-menu'>";
  31.  
  32.     echo "<ul>";
  33.     foreach ( $menu as $item ) {
  34.         extract( $item );
  35.  
  36.         $url = qa_get_url( $type );
  37.  
  38.         $id = $current ? 'qa-current-url' : '';
  39.  
  40.         echo _qa_html( 'li', array( 'id' => $id ),
  41.             _qa_html( 'a', array( 'href' => $url ),
  42.                 $title
  43.             )
  44.         );
  45.     }
  46.     echo "</ul>";
  47.  
  48.     the_qa_search_form();
  49.     echo "</div>";
  50. }
  51.  
  52. function the_qa_error_notice() {
  53.     if ( !isset( $_GET['qa_error'] ) )
  54.         return;
  55. ?>
  56.     <div id="qa-error-notice">
  57.         <?php _e( 'An error has occured while processing your submission.', QA_TEXTDOMAIN ); ?>
  58.     </div>
  59. <?php
  60. }
  61.  
  62. function the_qa_search_form() {
  63. ?>
  64. <form method="get" action="<?php echo qa_get_url('archive'); ?>">
  65.     <input type="text" name="s" value="<?php the_search_query(); ?>" />
  66.     <button><?php _e( 'Search', QA_TEXTDOMAIN ); ?></button>
  67. </form>
  68. <?php
  69. }
  70.  
  71. function the_qa_pagination( $query = null ) {
  72.     if ( is_null( $query ) )
  73.         $query = $GLOBALS['wp_query'];
  74.  
  75.     if ( $query->max_num_pages <= 1 )
  76.         return;
  77.  
  78.     $current_page = max( 1, $query->get( 'paged' ) );
  79.     $total_pages = $query->max_num_pages;
  80.  
  81.     $padding = 2;
  82.     $range_start = max( 1, $current_page - $padding );
  83.     $range_finish = min( $total_pages, $current_page + $padding );
  84.  
  85.     echo '<div class="qa-pagination">';
  86.  
  87.     if ( $current_page > 1 )
  88.         _qa_single_page_link( $query, $current_page - 1, __( 'prev', QA_TEXTDOMAIN ), 'prev' );
  89.  
  90.     if ( $range_start > 1 )
  91.         _qa_single_page_link( $query, 1 );
  92.  
  93.     if ( $range_start > $padding )
  94.         echo '<span class="dots">...</span>';
  95.  
  96.     foreach ( range( $range_start, $range_finish ) as $num ) {
  97.         if ( $num == $current_page )
  98.             echo _qa_html( 'span', array( 'class' => 'current' ), number_format_i18n( $num ) );
  99.         else
  100.             _qa_single_page_link( $query, $num );
  101.     }
  102.  
  103.     if ( $range_finish + $padding <= $total_pages )
  104.         echo '<span class="dots">...</span>';
  105.  
  106.     if ( $range_finish < $total_pages )
  107.         _qa_single_page_link( $query, $total_pages );
  108.  
  109.     if ( $current_page < $total_pages )
  110.         _qa_single_page_link( $query, $current_page + 1, __( 'next', QA_TEXTDOMAIN ), 'next' );
  111.  
  112.     echo '</div>';
  113. }
  114.  
  115. function _qa_single_page_link( $query, $num, $title = '', $class = '' ) {
  116.     if ( !$title )
  117.         $title = number_format_i18n( $num );
  118.  
  119.     $args = array( 'href' => get_pagenum_link( $num ) );
  120.  
  121.     if ( $class )
  122.         $args['class'] = $class;
  123.  
  124.     echo _qa_html( 'a', $args, $title );
  125. }
  126.  
  127. function the_qa_time( $id ) {
  128.     $post = get_post( $id );
  129.  
  130.     $t_time = get_the_time( __( 'Y/m/d g:i:s A' ) );
  131.     $m_time = $post->post_date;
  132.     $time = get_post_time( 'G', true, $post );
  133.  
  134.     $time_diff = time() - $time;
  135.  
  136.     if ( $time_diff > 0 && $time_diff < 24*60*60 )
  137.         $h_time = sprintf( __( '%s ago', QA_TEXTDOMAIN ), human_time_diff( $time ) );
  138.     else
  139.         $h_time = mysql2date( get_option( 'date_format' ), $m_time );
  140.  
  141.     echo '<span class="qa-timediff">' . $h_time . '</span>';
  142. }
  143.  
  144. function the_qa_author_box( $id ) {
  145.     $user_id = get_post_field( 'post_author', $id );
  146. ?>
  147. <div class="qa-user-box">
  148.     <?php echo get_avatar( $user_id, 32 ); ?>
  149.     <div class="qa-user-details">
  150.         <?php the_qa_user_link( $user_id ); ?>
  151.         <?php the_qa_user_rep( $user_id ); ?>
  152.     </div>
  153. </div>
  154. <?php
  155. }
  156.  
  157. function the_qa_action_links( $id ) {
  158.     $links = array();
  159.  
  160.     $links['single'] = __( 'link', QA_TEXTDOMAIN );
  161.  
  162.     if ( current_user_can( 'edit_post', $id ) )
  163.         $links['edit'] = __( 'edit', QA_TEXTDOMAIN );
  164.  
  165.     if ( current_user_can( 'delete_post', $id ) )
  166.         $links['delete'] = __( 'delete', QA_TEXTDOMAIN );
  167.  
  168.     foreach ( $links as $type => $title )
  169.         $links[ $type ] = _qa_html( 'a', array( 'href' => qa_get_url( $type, $id ) ), $title );
  170.  
  171.     echo '<div id="qa-action-links">';
  172.     echo implode( ' | ', $links );
  173.     echo '</div>';
  174. }
  175.  
  176. function the_qa_user_link( $user_id ) {
  177.     $author_name = get_the_author_meta( 'display_name', $user_id );
  178.     $author_url = qa_get_url( 'user', $user_id );
  179.  
  180.     echo "<a class='qa-user-link' href='$author_url'>$author_name</a>";
  181. }
  182.  
  183. function the_qa_user_rep( $user_id ) {
  184. ?>
  185.     <div class="qa-user-rep"><?php echo number_format_i18n( qa_get_user_rep( $user_id ) ); ?></div>
  186. <?php
  187. }
  188.  
  189. /* = Question Template Tags
  190. -------------------------------------------------------------- */
  191.  
  192. function the_question_link( $question_id = 0 ) {
  193.     if ( !$question_id )
  194.         $question_id = get_the_ID();
  195.  
  196.     echo _qa_html( 'a', array( 'class' => 'question-link', 'href' => qa_get_url( 'single', $question_id ) ), get_the_title( $question_id ) );
  197. }
  198.  
  199. function the_question_score( $question_id = 0 ) {
  200.     if ( !$question_id )
  201.         $question_id = get_the_ID();
  202.  
  203.     list( $up, $down ) = qa_get_votes( $question_id );
  204.  
  205.     $score = $up - $down;
  206.  
  207.     echo "<div class='question-score'>";
  208.     echo    "<div class='mini-count'>" . number_format_i18n( $score ) . "</div>";
  209.     echo    "<div>" . _n( 'vote', 'votes', $score, QA_TEXTDOMAIN ) . "</div>";
  210.     echo "</div>";
  211. }
  212.  
  213. function the_question_voting( $question_id = 0 ) {
  214.     global $_qa_core;
  215.  
  216.     if ( !$question_id )
  217.         $question_id = get_the_ID();
  218.  
  219.     list( $up, $down, $current ) = qa_get_votes( $question_id );
  220.  
  221.     $buttons = array(
  222.         'up' => __( 'This question is useful and clear (click again to undo)', QA_TEXTDOMAIN ),
  223.         'down' => __( 'This question is unclear or not useful (click again to undo)', QA_TEXTDOMAIN )
  224.     );
  225.  
  226.     foreach ( $buttons as $type => $text ) {
  227.         $buttons[ $type ] = $GLOBALS['_qa_votes']->get_link( $question_id, $type, $current, $text );
  228.     }
  229.  
  230. ?>
  231. <div class="qa-voting-box">
  232.     <?php echo $buttons['up']; ?>
  233.     <span title="<?php _e( 'Score', QA_TEXTDOMAIN ); ?>"><?php echo number_format_i18n( $up - $down ); ?></span>
  234.     <?php echo $buttons['down']; ?>
  235. </div>
  236. <?php
  237. }
  238.  
  239. function the_question_subscription() {
  240.     echo $GLOBALS['_qa_subscriptions']->get_link(
  241.         get_queried_object_id(),
  242.         __( 'Click here to be notified of followup answers via e-mail', QA_TEXTDOMAIN ),
  243.         __( 'Stop notifying me of followup answers via e-mail', QA_TEXTDOMAIN )
  244.     );
  245. }
  246.  
  247. function the_answer_voting( $answer_id ) {
  248.     list( $up, $down, $current ) = qa_get_votes( $answer_id );
  249.  
  250.     $buttons = array(
  251.         'up' => __( 'This answer is useful (click again to undo)', QA_TEXTDOMAIN ),
  252.         'down' => __( 'This answer is not useful (click again to undo)', QA_TEXTDOMAIN )
  253.     );
  254.  
  255.     foreach ( $buttons as $type => $text ) {
  256.         $buttons[ $type ] = $GLOBALS['_qa_votes']->get_link( $answer_id, $type, $current, $text );
  257.     }
  258.  
  259. ?>
  260. <div class="qa-voting-box">
  261.     <?php echo $buttons['up']; ?>
  262.     <span title="<?php _e( 'Score', QA_TEXTDOMAIN ); ?>"><?php echo number_format_i18n( $up - $down ); ?></span>
  263.     <?php echo $buttons['down']; ?>
  264.  
  265.     <?php the_answer_accepted( $answer_id ); ?>
  266. </div>
  267. <?php
  268. }
  269.  
  270. function the_answer_accepted( $answer_id ) {
  271.     $question_id = get_post_field( 'post_parent', $answer_id );
  272.  
  273.     $user_can_accept = get_post_field( 'post_author', $question_id ) == get_current_user_id();
  274.  
  275.     $is_accepted = get_post_meta( $question_id, '_accepted_answer', true ) == $answer_id;
  276.  
  277.     if ( $user_can_accept ) {
  278.         $data = array(
  279.             'action' => 'qa_accept',
  280.             'answer_id' => $answer_id,
  281.             'accept' => ( $is_accepted ? 'off' : 'on' )
  282.         );
  283. ?>
  284. <form method="post" action="">
  285.     <?php wp_nonce_field( 'qa_accept' ); ?>
  286.  
  287.     <?php foreach ( $data as $key => $value ) {
  288.         echo _qa_html( 'input', array( 'type' => 'hidden', 'name' => $key, 'value' => $value ) );
  289.     } ?>
  290.  
  291.     <?php echo _qa_html( 'input', array(
  292.         'type' => 'submit',
  293.         'title' => __( 'Accept answer (click again to undo)', QA_TEXTDOMAIN ),
  294.         'class' => 'vote-accepted-' . ( $is_accepted ? 'on' : 'off' )
  295.     ) ); ?>
  296. </form>
  297. <?php
  298.     }
  299.     elseif ( $is_accepted ) {
  300.         echo _qa_html( 'span', array(
  301.             'title' => __( 'Accepted answer', QA_TEXTDOMAIN ),
  302.             'class' => 'vote-accepted-on'
  303.         ), __( 'accepted', QA_TEXTDOMAIN ) );
  304.     }
  305. }
  306.  
  307. function the_question_status( $question_id = 0 ) {
  308.     if ( !$question_id )
  309.         $question_id = get_the_ID();
  310.  
  311.     $count = get_answer_count( $question_id );
  312.  
  313.     if ( get_post_meta( $question_id, '_accepted_answer', true ) )
  314.         $status = 'answered-accepted';
  315.     elseif ( $count > 0 )
  316.         $status = 'answered';
  317.     else
  318.         $status = 'unanswered';
  319.  
  320.     echo "<div class='question-status $status'>";
  321.     echo    "<div class='mini-count'>" . number_format_i18n( $count ) . "</div>";
  322.     echo    "<div>" . _n( 'answer', 'answers', $count, QA_TEXTDOMAIN ) . "</div>";
  323.     echo "</div>";
  324. }
  325.  
  326. function the_question_tags( $before = '', $sep = ', ', $after = '' ) {
  327.     the_terms( 0, 'question_tag', $before, $sep, $after );
  328. }
  329.  
  330. function the_question_category( $before = '', $sep = ', ', $after = '' ) {
  331.     the_terms( 0, 'question_category', $before, $sep, $after );
  332. }
  333.  
  334. function the_question_form() {
  335.     global $wp_query;
  336.  
  337.     if ( is_qa_page( 'edit' ) ) {
  338.         $question = $wp_query->posts[0];
  339.  
  340.         if ( !current_user_can( 'edit_question', $question->ID ) )
  341.             return;
  342.  
  343.         $question->tags = wp_get_object_terms( $question->ID, 'question_tag', array( 'fields' => 'names' ) );
  344.        
  345.         $cats = wp_get_object_terms( $question->ID, 'question_category', array( 'fields' => 'ids' ) );
  346.         $question->cat = empty( $cats ) ? false : reset( $cats );
  347.     } else {
  348.         $question = (object) array(
  349.             'ID' => '',
  350.             'post_content' => '',
  351.             'post_title' => '',
  352.             'tags' => array(),
  353.             'cat' => false
  354.         );
  355.     }
  356.  
  357. ?>
  358. <form id="question-form" method="post" action="<?php echo qa_get_url( 'archive' ); ?>">
  359.     <?php wp_nonce_field( 'qa_edit' ); ?>
  360.  
  361.     <input type="hidden" name="qa_action" value="edit_question" />
  362.     <input type="hidden" name="question_id" value="<?php echo esc_attr( $question->ID ); ?>" />
  363.  
  364.     <table>
  365.         <tr>
  366.             <td id="question-title-label">
  367.                 <label for="question-title"><?php _e('Title:', QA_TEXTDOMAIN); ?></label>
  368.             </td>
  369.             <td id="question-title-td">
  370.                 <input type="text" id="question-title" name="question_title" value="<?php echo esc_attr( $question->post_title ); ?>" />
  371.             </td>
  372.         </tr>
  373.     </table>
  374.  
  375.     <textarea name="question_content"><?php echo esc_textarea( $question->post_content ); ?></textarea>
  376. <!-- I need to add custom fields here -->
  377.     <table id="question-taxonomies">
  378.         <tr>
  379.             <td id="question-category-td">
  380.             <?php wp_dropdown_categories( array(
  381.                 'taxonomy' => 'question_category',
  382.                 'selected' => $question->cat,
  383.                 'hide_empty' => false,
  384.                 'hierarchical' => true,
  385.                 'name' => 'question_cat',
  386.                 'class' => '',
  387.                 'show_option_none' => __( 'Select category...', QA_TEXTDOMAIN )
  388.             ) ); ?>
  389.             </td>
  390.             <td id="question-tags-label">
  391.                 <label for="question-tags"><?php _e('Tags:', QA_TEXTDOMAIN); ?></label>
  392.             </td>
  393.             <td id="question-tags-td">
  394.                 <input type="text" id="question-tags" name="question_tags" value="<?php echo implode( ', ', $question->tags ); ?>" />
  395.             </td>
  396.         </tr>
  397.     </table>
  398.  
  399.     <?php the_qa_submit_button(); ?>
  400. </form>
  401. <?php
  402. }
  403.  
  404. /* = Answer Template Tags
  405. -------------------------------------------------------------- */
  406.  
  407. function the_answer_link( $answer_id ) {
  408.     $question_id = get_post_field( 'post_parent', $answer_id );
  409.  
  410.     echo _qa_html( 'a', array( 'class' => 'answer-link', 'href' => qa_get_url( 'single', $answer_id ) ), get_the_title( $question_id ) );
  411. }
  412.  
  413. function the_answer_count( $question_id = 0 ) {
  414.     $count = get_answer_count( $question_id ? $question_id : get_the_ID() );
  415.  
  416.     printf( _n( '1 Answer', '%d Answers', $count, QA_TEXTDOMAIN ), number_format_i18n( $count ) );
  417. }
  418.  
  419. function the_answer_list() {
  420.     $question_id = get_the_ID();
  421.  
  422. #   if ( !current_user_can( 'read_answers', $question_id ) )
  423. #       return;
  424.  
  425.     $accepted_answer = get_post_meta( $question_id, '_accepted_answer', true );
  426.  
  427.     $answers = new WP_Query( array(
  428.         'post_type' => 'answer',
  429.         'post_parent' => $question_id,
  430.         'post__not_in' => array( $accepted_answer ),
  431.         'orderby' => 'qa_score',
  432.         'posts_per_page' => QA_ANSWERS_PER_PAGE,
  433.         'paged' => get_query_var( 'paged' )
  434.     ) );
  435.  
  436.     if ( $accepted_answer && !get_query_var( 'paged' ) )
  437.         array_unshift( $answers->posts, get_post( $accepted_answer ) );
  438.  
  439.     the_qa_pagination( $answers );
  440.  
  441.     foreach ( $answers->posts as $answer ) {
  442.         setup_postdata( $answer );
  443. ?>
  444.     <div id="answer-<?php echo $answer->ID; ?>" class="answer">
  445.         <?php the_answer_voting( $answer->ID ); ?>
  446.         <div class="answer-body">
  447.             <?php echo get_the_content(); ?>
  448.             <?php the_qa_author_box( $answer->ID ); ?>
  449.             <?php the_qa_action_links( $answer->ID ); ?>
  450.         </div>
  451.     </div>
  452. <?php
  453.     }
  454.  
  455.     the_qa_pagination( $answers );
  456.  
  457.     wp_reset_postdata();
  458. }
  459.  
  460. function the_answer_form() {
  461.     global $wp_query;
  462.  
  463.     if ( is_qa_page( 'edit' ) ) {
  464.         $answer = $wp_query->posts[0];
  465.  
  466.         if ( !current_user_can( 'edit_post', $answer->ID ) )
  467.             return;
  468.     } else {
  469.         $answer = (object) array(
  470.             'ID' => '',
  471.             'post_parent' => get_the_ID(),
  472.             'post_content' => ''
  473.         );
  474.     }
  475.  
  476. ?>
  477. <form id="answer-form" method="post" action="<?php echo qa_get_url( 'archive' ); ?>">
  478.     <?php wp_nonce_field( 'qa_answer' ); ?>
  479.  
  480.     <input type="hidden" name="qa_action" value="edit_answer" />
  481.     <input type="hidden" name="question_id" value="<?php echo esc_attr( $answer->post_parent ); ?>" />
  482.     <input type="hidden" name="answer_id" value="<?php echo esc_attr( $answer->ID ); ?>" />
  483.  
  484.     <p><textarea name="answer"><?php echo esc_textarea( $answer->post_content ); ?></textarea></p>
  485.  
  486.     <?php the_qa_submit_button(); ?>
  487. </form>
  488. <?php
  489. }
  490.  
  491. function the_qa_submit_button() {
  492.     if ( is_user_logged_in() ) {
  493.         $button = __( 'Submit', QA_TEXTDOMAIN );
  494.     } elseif ( get_option( 'users_can_register' ) ) {
  495.         $button = __( 'Register/Login and Submit', QA_TEXTDOMAIN );
  496.     } else {
  497.         $button = __( 'Login and Submit', QA_TEXTDOMAIN );
  498.     }
  499. ?>
  500.     <input class="qa-edit-submit" type="submit" value="<?php echo $button; ?>" />
  501. <?php
  502. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement