Advertisement
designbymerovingi

myCRED: DW Questions & Answers Custom hook

Jan 24th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.59 KB | None | 0 0
  1. /**
  2.  * Register Hook
  3.  * @since 1.0
  4.  * @version 1.0
  5.  */
  6. add_filter( 'mycred_setup_hooks', 'mycred_hook_dw_qanda' );
  7. function mycred_hook_dw_qanda( $installed ) {
  8.     $installed['dw_questions'] = array(
  9.         'title'       => __( 'DW Question Answer', 'mycred' ),
  10.         'description' => __( 'Awards %_plural% for best answers. To award %_plural% for new questions and answers, use the "%plural% for publishing content" hook above.', 'mycred' ),
  11.         'callback'    => array( 'myCRED_DW_Question_Answer' )
  12.     );
  13.     return $installed;
  14. }
  15.  
  16. if ( class_exists( 'myCRED_Hook' ) ) :
  17.  
  18.     /**
  19.      * DW Question Answer Hook
  20.      * @since 1.0
  21.      * @version 1.0
  22.      */
  23.     if ( ! class_exists( 'myCRED_DW_Question_Answer' ) ) {
  24.         class myCRED_DW_Question_Answer extends myCRED_Hook {
  25.  
  26.             /**
  27.              * Construct
  28.              */
  29.             function __construct( $hook_prefs ) {
  30.                 parent::__construct( array(
  31.                     'id'       => 'dw_questions',
  32.                     'defaults' => array(
  33.                         'vote_best_answer' => array(
  34.                             'creds'       => 0,
  35.                             'log'         => '%plural% for giving best answer'
  36.                         ),
  37.                         'unvoted_best_answer' => array(
  38.                             'creds'       => 0,
  39.                             'log'         => '%plural% deduction for unvoted best answer'
  40.                         ),
  41.                         'selected_best_answer' => array(
  42.                             'creds'       => 0,
  43.                             'log'         => '%plural% for selecting a best answer'
  44.                         ),
  45.                         'removed_best_answer' => array(
  46.                             'creds'       => 0,
  47.                             'log'         => '%plural% for removing your best answer selection'
  48.                         )
  49.                     )
  50.                 ), $hook_prefs );
  51.             }
  52.  
  53.             /**
  54.              * Run
  55.              * @since 0.1
  56.              * @version 1.0
  57.              */
  58.             public function run() {
  59.                 add_action( 'wp_ajax_dwqa-vote-best-answer',   array( $this, 'vote_best_answer' ), 1 );
  60.                 add_action( 'wp_ajax_dwqa-unvote-best-answer', array( $this, 'unvote_best_answer' ), 1 );
  61.             }
  62.  
  63.             /**
  64.              * Vote Best Answer
  65.              * @since 0.1
  66.              * @version 1.0
  67.              */
  68.             public function vote_best_answer() {
  69.                 // Security
  70.                 check_ajax_referer( '_dwqa_vote_best_answer', 'nonce' );
  71.  
  72.                 // Must be logged in
  73.                 if ( ! is_user_logged_in() ) return;
  74.  
  75.                 // Required
  76.                 if ( ! isset( $_POST['answer'] ) )
  77.                     return;
  78.  
  79.                 $this->best_answer( (int) $_POST['answer'] );
  80.             }
  81.  
  82.             /**
  83.              * Un-Vote Best Answer
  84.              * @since 0.1
  85.              * @version 1.0
  86.              */
  87.             public function unvote_best_answer() {
  88.                 // Security
  89.                 check_ajax_referer( '_dwqa_vote_best_answer', 'nonce' );
  90.  
  91.                 // Must be logged in
  92.                 if ( ! is_user_logged_in() ) return;
  93.  
  94.                 // Required
  95.                 if ( ! isset( $_POST['answer'] ) )
  96.                     return;
  97.  
  98.                 $this->best_answer( (int) $_POST['answer'], true );
  99.             }
  100.  
  101.             /**
  102.              * Award Best Answer
  103.              * @since 0.1
  104.              * @version 1.0
  105.              */
  106.             public function best_answer( $answer_id, $deduct = false ) {
  107.                 // Current user
  108.                 $user_id = get_current_user_id();
  109.  
  110.                 // Get Question ID
  111.                 $question_id = get_post_meta( $answer_id, '_question', true );
  112.                 $question_id = absint( $question_id );
  113.  
  114.                 // Get Question
  115.                 $question = get_post( $question_id );
  116.  
  117.                 // Get Answer
  118.                 $answer = get_post( $answer_id );
  119.  
  120.                 // If we are the question author or editor
  121.                 if ( $question->post_author == $user_id || current_user_can( 'edit_posts' ) ) {
  122.  
  123.                     // Make sure author is not excluded
  124.                     if ( ! $this->core->exclude_user( $answer->post_author ) )  {
  125.  
  126.                         // Execute
  127.                         if ( ! $deduct && $this->prefs['vote_best_answer']['creds'] != 0 )
  128.                             $this->core->add_creds(
  129.                                 'voted_best_answer',
  130.                                 $answer->post_author,
  131.                                 $this->prefs['vote_best_answer']['creds'],
  132.                                 $this->prefs['vote_best_answer']['log'],
  133.                                 $answer_id,
  134.                                 array( 'ref_type' => 'post' )
  135.                             );
  136.                         elseif ( $deduct && $this->prefs['unvote_best_answer']['creds'] != 0 )
  137.                             $this->core->add_creds(
  138.                                 'unvoted_best_answer',
  139.                                 $answer->post_author,
  140.                                 $this->prefs['unvote_best_answer']['creds'],
  141.                                 $this->prefs['unvote_best_answer']['log'],
  142.                                 $answer_id,
  143.                                 array( 'ref_type' => 'post' )
  144.                             );
  145.  
  146.                     }
  147.  
  148.                     // Check Exclusion
  149.                     if ( ! $this->core->exclude_user( $question->post_author ) ) {
  150.  
  151.                         // Execute
  152.                         if ( ! $deduct && $this->prefs['selected_best_answer']['creds'] != 0 )
  153.                             $this->core->add_creds(
  154.                                 'selected_best_answer',
  155.                                 $question->post_author,
  156.                                 $this->prefs['selected_best_answer']['creds'],
  157.                                 $this->prefs['selected_best_answer']['log'],
  158.                                 $question_id,
  159.                                 array( 'ref_type' => 'post' )
  160.                             );
  161.                         elseif ( $deduct && $this->prefs['removed_best_answer']['creds'] != 0 )
  162.                             $this->core->add_creds(
  163.                                 'removed_best_answer',
  164.                                 $question->post_author,
  165.                                 $this->prefs['removed_best_answer']['creds'],
  166.                                 $this->prefs['removed_best_answer']['log'],
  167.                                 $question_id,
  168.                                 array( 'ref_type' => 'post' )
  169.                             );
  170.  
  171.                     }
  172.  
  173.                 }
  174.            
  175.             }
  176.  
  177.             /**
  178.              * Preferences for Hook
  179.              * @since 1.0
  180.              * @version 1.0
  181.              */
  182.             public function preferences() {
  183.                 $prefs = $this->prefs; ?>
  184.  
  185. <h3>Answer Author</h3>
  186. <label for="<?php echo $this->field_id( array( 'vote_best_answer', 'creds' ) ); ?>" class="subheader"><?php _e( 'Voted Best Answer', 'mycred' ); ?></label>
  187. <ol>
  188.     <li>
  189.         <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'vote_best_answer', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'vote_best_answer', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['vote_best_answer']['creds'] ); ?>" size="8" /></div>
  190.     </li>
  191.     <li class="empty">&nbsp;</li>
  192.     <li>
  193.         <label for="<?php echo $this->field_id( array( 'vote_best_answer', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
  194.         <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'vote_best_answer', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'vote_best_answer', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['vote_best_answer']['log'] ); ?>" class="long" /></div>
  195.         <span class="description"><?php _e( 'Available template tags: General and Post (Answer) related.', 'mycred' ); ?></span>
  196.     </li>
  197. </ol>
  198. <!-- Answer Author Unvoted Best Answer -->
  199. <label for="<?php echo $this->field_id( array( 'unvoted_best_answer', 'creds' ) ); ?>" class="subheader"><?php _e( 'Unvoted Best Answer', 'mycred' ); ?></label>
  200. <ol>
  201.     <li>
  202.         <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'unvoted_best_answer', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'unvoted_best_answer', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['unvoted_best_answer']['creds'] ); ?>" size="8" /></div>
  203.     </li>
  204.     <li class="empty">&nbsp;</li>
  205.     <li>
  206.         <label for="<?php echo $this->field_id( array( 'unvoted_best_answer', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
  207.         <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'unvoted_best_answer', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'unvoted_best_answer', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['unvoted_best_answer']['log'] ); ?>" class="long" /></div>
  208.         <span class="description"><?php _e( 'Available template tags: General and Post (Answer) related.', 'mycred' ); ?></span>
  209.     </li>
  210. </ol>
  211. <h3>Question Author</h3>
  212. <!-- Question Author Set Best Answer -->
  213. <label for="<?php echo $this->field_id( array( 'selected_best_answer', 'creds' ) ); ?>" class="subheader"><?php _e( 'Choosing a Best Answer', 'mycred' ); ?></label>
  214. <ol>
  215.     <li><?php echo $this->core->template_tags_general( __( 'Option to award %_plural% to the Question author for selecting a best answer.', 'mycred' ) ); ?></li>
  216.     <li>
  217.         <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'selected_best_answer', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'selected_best_answer', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['selected_best_answer']['creds'] ); ?>" size="8" /></div>
  218.     </li>
  219.     <li class="empty">&nbsp;</li>
  220.     <li>
  221.         <label for="<?php echo $this->field_id( array( 'selected_best_answer', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
  222.         <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'selected_best_answer', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'selected_best_answer', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['selected_best_answer']['log'] ); ?>" class="long" /></div>
  223.         <span class="description"><?php _e( 'Available template tags: General and Post (Question) related.', 'mycred' ); ?></span>
  224.     </li>
  225. </ol>
  226. <!-- Question Author Remove Best Answer -->
  227. <label for="<?php echo $this->field_id( array( 'removed_best_answer', 'creds' ) ); ?>" class="subheader"><?php _e( 'Removing Best Answer', 'mycred' ); ?></label>
  228. <ol>
  229.     <li>
  230.         <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'removed_best_answer', 'creds' ) ); ?>" id="<?php echo $this->field_id( array( 'removed_best_answer', 'creds' ) ); ?>" value="<?php echo $this->core->number( $prefs['removed_best_answer']['creds'] ); ?>" size="8" /></div>
  231.     </li>
  232.     <li class="empty">&nbsp;</li>
  233.     <li>
  234.         <label for="<?php echo $this->field_id( array( 'removed_best_answer', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
  235.         <div class="h2"><input type="text" name="<?php echo $this->field_name( array( 'removed_best_answer', 'log' ) ); ?>" id="<?php echo $this->field_id( array( 'removed_best_answer', 'log' ) ); ?>" value="<?php echo esc_attr( $prefs['removed_best_answer']['log'] ); ?>" class="long" /></div>
  236.         <span class="description"><?php _e( 'Available template tags: General and Post (Question) related.', 'mycred' ); ?></span>
  237.     </li>
  238. </ol>
  239. <?php
  240.             }
  241.         }
  242.     }
  243. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement