Guest User

Untitled

a guest
Jun 26th, 2016
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.00 KB | None | 0 0
  1. <?php
  2. if ( class_exists('DW_Question_Answer') && function_exists('bp_is_active')) {
  3.   /*-----------------------------------------------------------------------------------*/
  4.   /*  Setup Questions in BuddyPress User Profile
  5.   /*-----------------------------------------------------------------------------------*/
  6.   add_action( 'bp_setup_nav', 'dw_buddypress_integrate_bp_nav_adder' );
  7.   function dw_buddypress_integrate_bp_nav_adder() {
  8.      bp_core_new_nav_item(
  9.       array(
  10.         'name' => __('Questions', 'dwqa'),
  11.         'slug' => 'questions',
  12.         'position' => 21,
  13.         'show_for_displayed_user' => true,
  14.         'screen_function' => 'questions_list',
  15.         'item_css_id' => 'questions',
  16.         'default_subnav_slug' => 'public'
  17.       ));
  18.      bp_core_new_nav_item(
  19.       array(
  20.         'name' => __('Answer', 'dwqa'),
  21.         'slug' => 'answers',
  22.         'position' => 22,
  23.         'show_for_displayed_user' => true,
  24.         'screen_function' => 'answer_list',
  25.         'item_css_id' => 'answer',
  26.         'default_subnav_slug' => 'public'
  27.       ));
  28.   }
  29.  
  30.   function questions_list() {
  31.     add_action( 'bp_template_content', 'profile_questions_loop' );
  32.     bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
  33.   }
  34.  
  35.   function answer_list() {
  36.     add_action( 'bp_template_content', 'profile_answers_loop' );
  37.     bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
  38.   }
  39.  
  40.   function profile_questions_loop() {
  41.     global $dwqa_options;
  42.     $submit_question_link = get_permalink( $dwqa_options['pages']['submit-question'] );
  43.     $questions = get_posts(  array(
  44.       'posts_per_page' => -1,
  45.       'author'         => bp_displayed_user_id(),
  46.       'post_type'      => 'dwqa-question'
  47.     ));
  48.     if( ! empty($questions) ) { ?>
  49.     <div class="dwqa-questions-archive">
  50.         <div class="dwqa-questions-list">
  51.             <?php foreach ($questions as $q) { ?>
  52.             <div class="dwqa-question-item">
  53.                 <div class="dwqa-question-title"><a href="<?php echo get_post_permalink($q->ID); ?>"><?php echo $q->post_title; ?></a></div>
  54.                 <div class="dwqa-question-meta">
  55.                     <?php dwqa_question_print_status( $q->ID ) ?>
  56.                     <?php
  57.                         global $post;
  58.                         $user_id = $q->post_author ? $q->post_author : false;
  59.                         $time = human_time_diff(  get_post_time( 'U' , false,  $q->ID ) , current_time('timestamp') );
  60.                         $text = __( 'asked', 'dwqa' );
  61.                         $latest_answer = dwqa_get_latest_answer( $q->ID );
  62.                         if ( $latest_answer ) {
  63.                             $time = human_time_diff( strtotime( $latest_answer->post_date ) );
  64.                             $text = __( 'answered', 'dwqa' );
  65.                         }
  66.                     ?>
  67.                     <?php printf( __( '<span><a href="%s">%s%s</a> %s %s ago</span>', 'dwqa' ), dwqa_get_author_link( $user_id ), get_avatar( $user_id, 48 ), get_the_author_meta( 'display_name', $user_id ), $text, $time ) ?>
  68.                     <?php echo get_the_term_list( $q->ID, 'dwqa-question_category', '<span class="dwqa-question-category">' . __( '&nbsp;&bull;&nbsp;', 'dwqa' ), ', ', '</span>' ); ?>
  69.                 </div>
  70.                 <div class="dwqa-question-stats">
  71.                     <span class="dwqa-views-count">
  72.                         <?php $views_count = dwqa_question_views_count( $q->ID ) ?>
  73.                         <?php printf( __( '<strong>%1$s</strong> views', 'dwqa' ), $views_count ); ?>
  74.                     </span>
  75.                     <span class="dwqa-answers-count">
  76.                         <?php $answers_count = dwqa_question_answers_count( $q->ID ); ?>
  77.                         <?php printf( __( '<strong>%1$s</strong> answers', 'dwqa' ), $answers_count ); ?>
  78.                     </span>
  79.                     <span class="dwqa-votes-count">
  80.                         <?php $vote_count = dwqa_vote_count( $q->ID ) ?>
  81.                         <?php printf( __( '<strong>%1$s</strong> votes', 'dwqa' ), $vote_count ); ?>
  82.                     </span>
  83.                 </div>
  84.             </div>
  85.             <?php } ?>
  86.                     </div>
  87.             </div>
  88.     <?php } else { ?>
  89.     <div class="info" id="message">
  90.       <?php if( get_current_user_id() == bp_displayed_user_id() ) : ?>
  91.         Why don't you have question for us. <a href="<?php echo $submit_question_link ?>">Start asking</a>!
  92.       <?php else : ?>
  93.         <p><strong><?php bp_displayed_user_fullname(); ?></strong> has not asked any question.</p>
  94.       <?php endif; ?>
  95.     </div>
  96.     <?php }
  97.   }
  98.  
  99.   function profile_answers_loop() {
  100.     global $dwqa_options;
  101.     $submit_question_link = get_permalink( $dwqa_options['pages']['submit-question'] );
  102.     $answers = get_posts(  array(
  103.       'posts_per_page' => -1,
  104.       'author'         => bp_displayed_user_id(),
  105.       'post_type'      => 'dwqa-answer'
  106.     ));
  107.     if( ! empty($answers) ) { ?>
  108.       <div class="dwqa-questions-archive">
  109.         <div class="dwqa-questions-list">
  110.             <?php foreach ($answers as $a) { ?>
  111.                 <div class="dwqa-question-item" style="padding-left: 20px!important">
  112.                     <a class="dwqa-title" href="<?php echo get_post_permalink($a->ID); ?>" title="Permalink to <?php echo $a->post_title ?>" rel="bookmark"><?php echo $a->post_title ?></a>
  113.                     <div class="dwqa-meta">
  114.                         <span><strong> At : </strong><?php echo get_the_time( 'M d, Y, g:i a', $a->ID ); ?></span>
  115.                     </div>
  116.                 </div>
  117.             <?php } ?>
  118.         </div>
  119.       </div>
  120.     <?php } else { ?>
  121.     <div class="info" id="message">
  122.       <?php if( get_current_user_id() == bp_displayed_user_id() ) : ?>
  123.         Why don't you have question for us. <a href="<?php echo $submit_question_link ?>">Start asking</a>!
  124.       <?php else : ?>
  125.         <p><strong><?php bp_displayed_user_fullname(); ?></strong> has not asked any question.</p>
  126.       <?php endif; ?>
  127.     </div>
  128.     <?php }
  129.   }
  130.  
  131.   /*-----------------------------------------------------------------------------------*/
  132.   /*  Record Activities
  133.   /*-----------------------------------------------------------------------------------*/
  134.   // Question
  135.   function dw_qa_bb_record_question_activity( $post_id ) {
  136.     $post = get_post($post_id);
  137.     if(($post->post_status != 'publish') && ($post->post_status != 'private'))
  138.       return;
  139.  
  140.     $user_id = get_current_user_id();
  141.     $post_permalink = get_permalink( $post_id );
  142.     $post_title = get_the_title( $post_id );
  143.     $activity_action = sprintf( __( '%s asked a new question: %s', 'dwqa' ), bp_core_get_userlink( $user_id ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
  144.     $content = $post->post_content;
  145.     $hide_sitewide = ($post->post_status == 'private') ? true : false;
  146.  
  147.     bp_blogs_record_activity( array(
  148.       'user_id' => $user_id,
  149.       'action' => $activity_action,
  150.       'content' => $content,
  151.       'primary_link' => $post_permalink,
  152.       'type' => 'new_blog_post',
  153.       'item_id' => 0,
  154.       'secondary_item_id' => $post_id,
  155.       'recorded_time' => $post->post_date_gmt,
  156.       'hide_sitewide' => $hide_sitewide,
  157.     ));
  158.   }
  159.   add_action( 'dwqa_add_question', 'dw_qa_bb_record_question_activity');
  160.  
  161.   //Answer
  162.   function dw_qa_bb_record_answer_activity( $post_id ) {
  163.     $post = get_post($post_id);
  164.  
  165.     if($post->post_status != 'publish')
  166.       return;
  167.  
  168.     $user_id = $post->post_author;
  169.  
  170.     $question_id = get_post_meta( $post_id, '_question', true );
  171.     $question = get_post( $question_id );
  172.  
  173.     $post_permalink = get_permalink($question_id);
  174.     $activity_action = sprintf( __( '%s answered the question: %s', 'dwqa' ), bp_core_get_userlink( $user_id ), '<a href="' . $post_permalink . '">' . $question->post_title . '</a>' );
  175.     $content = $post->post_content;
  176.  
  177.     $hide_sitewide = ($question->post_status == 'private') ? true : false;
  178.  
  179.     bp_blogs_record_activity( array(
  180.       'user_id' => $user_id,
  181.       'action' => $activity_action,
  182.       'content' => $content,
  183.       'primary_link' => $post_permalink,
  184.       'type' => 'new_blog_post',
  185.       'item_id' => 0,
  186.       'secondary_item_id' => $post_id,
  187.       'recorded_time' => $post->post_date_gmt,
  188.       'hide_sitewide' => $hide_sitewide,
  189.     ));
  190.   }
  191.   add_action( 'dwqa_add_answer', 'dw_qa_bb_record_answer_activity');
  192.   add_action( 'dwqa_update_answer', 'dw_qa_bb_record_answer_activity');
  193.  
  194.   //Comment
  195.   function dw_qa_bb_record_comment_activity( $comment_id ) {
  196.     $comment = get_comment($comment_id);
  197.     $user_id = get_current_user_id();
  198.     $post_id = $comment->comment_post_ID;
  199.     $content = $comment->comment_content;
  200.  
  201.     if(get_post_type($post_id) == 'dwqa-question') {
  202.       $post = get_post( $post_id );
  203.       $post_permalink = get_permalink( $post_id );
  204.       $activity_action = sprintf( __( '%s commented on the question: %s', 'dwqa' ), bp_core_get_userlink( $user_id ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
  205.       $hide_sitewide = ($post->post_status == 'private') ? true : false;
  206.     } else {
  207.       $post = get_post( $post_id );
  208.       $question_id = get_post_meta( $post_id, '_question', true );
  209.       $question = get_post( $question_id );
  210.       $post_permalink = get_permalink( $question_id );
  211.       $activity_action = sprintf( __( '%s commented on the answer at: %s', 'dwqa' ), bp_core_get_userlink( $user_id ), '<a href="' . $post_permalink . '">' . $question->post_title . '</a>' );
  212.       $hide_sitewide = ($question->post_status == 'private') ? true : false;
  213.     }
  214.  
  215.     bp_blogs_record_activity( array(
  216.       'user_id' => $user_id,
  217.       'action' => $activity_action,
  218.       'content' => $content,
  219.       'primary_link' => $post_permalink,
  220.       'type' => 'new_blog_comment',
  221.       'item_id' => 0,
  222.       'secondary_item_id' => $comment_id,
  223.       'recorded_time' => $comment->comment_date_gmt,
  224.       'hide_sitewide' => $hide_sitewide,
  225.     ));
  226.   }
  227.   add_action( 'dwqa_add_comment', 'dw_qa_bb_record_comment_activity');
  228. }
Add Comment
Please, Sign In to add comment