Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Register Hook
- * @since 1.0
- * @version 1.0
- */
- add_filter( 'mycred_setup_hooks', 'mycred_hook_dw_qanda' );
- function mycred_hook_dw_qanda( $installed ) {
- $installed['dw_questions'] = array(
- 'title' => __( 'DW Question Answer', 'mycred' ),
- 'description' => __( 'Awards %_plural% for best answers. To award %_plural% for new questions and answers, use the "%plural% for publishing content" hook above.', 'mycred' ),
- 'callback' => array( 'myCRED_DW_Question_Answer' )
- );
- return $installed;
- }
- if ( class_exists( 'myCRED_Hook' ) ) :
- /**
- * DW Question Answer Hook
- * @since 1.0
- * @version 1.0
- */
- if ( ! class_exists( 'myCRED_DW_Question_Answer' ) ) {
- class myCRED_DW_Question_Answer extends myCRED_Hook {
- /**
- * Construct
- */
- function __construct( $hook_prefs ) {
- parent::__construct( array(
- 'id' => 'dw_questions',
- 'defaults' => array(
- 'vote_best_answer' => array(
- 'creds' => 0,
- 'log' => '%plural% for giving best answer'
- ),
- 'unvoted_best_answer' => array(
- 'creds' => 0,
- 'log' => '%plural% deduction for unvoted best answer'
- ),
- 'selected_best_answer' => array(
- 'creds' => 0,
- 'log' => '%plural% for selecting a best answer'
- ),
- 'removed_best_answer' => array(
- 'creds' => 0,
- 'log' => '%plural% for removing your best answer selection'
- )
- )
- ), $hook_prefs );
- }
- /**
- * Run
- * @since 0.1
- * @version 1.0
- */
- public function run() {
- add_action( 'wp_ajax_dwqa-vote-best-answer', array( $this, 'vote_best_answer' ), 1 );
- add_action( 'wp_ajax_dwqa-unvote-best-answer', array( $this, 'unvote_best_answer' ), 1 );
- }
- /**
- * Vote Best Answer
- * @since 0.1
- * @version 1.0
- */
- public function vote_best_answer() {
- // Security
- check_ajax_referer( '_dwqa_vote_best_answer', 'nonce' );
- // Must be logged in
- if ( ! is_user_logged_in() ) return;
- // Required
- if ( ! isset( $_POST['answer'] ) )
- return;
- $this->best_answer( (int) $_POST['answer'] );
- }
- /**
- * Un-Vote Best Answer
- * @since 0.1
- * @version 1.0
- */
- public function unvote_best_answer() {
- // Security
- check_ajax_referer( '_dwqa_vote_best_answer', 'nonce' );
- // Must be logged in
- if ( ! is_user_logged_in() ) return;
- // Required
- if ( ! isset( $_POST['answer'] ) )
- return;
- $this->best_answer( (int) $_POST['answer'], true );
- }
- /**
- * Award Best Answer
- * @since 0.1
- * @version 1.0
- */
- public function best_answer( $answer_id, $deduct = false ) {
- // Current user
- $user_id = get_current_user_id();
- // Get Question ID
- $question_id = get_post_meta( $answer_id, '_question', true );
- $question_id = absint( $question_id );
- // Get Question
- $question = get_post( $question_id );
- // Get Answer
- $answer = get_post( $answer_id );
- // If we are the question author or editor
- if ( $question->post_author == $user_id || current_user_can( 'edit_posts' ) ) {
- // Make sure author is not excluded
- if ( ! $this->core->exclude_user( $answer->post_author ) ) {
- // Execute
- if ( ! $deduct && $this->prefs['vote_best_answer']['creds'] != 0 )
- $this->core->add_creds(
- 'voted_best_answer',
- $answer->post_author,
- $this->prefs['vote_best_answer']['creds'],
- $this->prefs['vote_best_answer']['log'],
- $answer_id,
- array( 'ref_type' => 'post' )
- );
- elseif ( $deduct && $this->prefs['unvote_best_answer']['creds'] != 0 )
- $this->core->add_creds(
- 'unvoted_best_answer',
- $answer->post_author,
- $this->prefs['unvote_best_answer']['creds'],
- $this->prefs['unvote_best_answer']['log'],
- $answer_id,
- array( 'ref_type' => 'post' )
- );
- }
- // Check Exclusion
- if ( ! $this->core->exclude_user( $question->post_author ) ) {
- // Execute
- if ( ! $deduct && $this->prefs['selected_best_answer']['creds'] != 0 )
- $this->core->add_creds(
- 'selected_best_answer',
- $question->post_author,
- $this->prefs['selected_best_answer']['creds'],
- $this->prefs['selected_best_answer']['log'],
- $question_id,
- array( 'ref_type' => 'post' )
- );
- elseif ( $deduct && $this->prefs['removed_best_answer']['creds'] != 0 )
- $this->core->add_creds(
- 'removed_best_answer',
- $question->post_author,
- $this->prefs['removed_best_answer']['creds'],
- $this->prefs['removed_best_answer']['log'],
- $question_id,
- array( 'ref_type' => 'post' )
- );
- }
- }
- }
- /**
- * Preferences for Hook
- * @since 1.0
- * @version 1.0
- */
- public function preferences() {
- $prefs = $this->prefs; ?>
- <h3>Answer Author</h3>
- <label for="<?php echo $this->field_id( array( 'vote_best_answer', 'creds' ) ); ?>" class="subheader"><?php _e( 'Voted Best Answer', 'mycred' ); ?></label>
- <ol>
- <li>
- <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>
- </li>
- <li class="empty"> </li>
- <li>
- <label for="<?php echo $this->field_id( array( 'vote_best_answer', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
- <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>
- <span class="description"><?php _e( 'Available template tags: General and Post (Answer) related.', 'mycred' ); ?></span>
- </li>
- </ol>
- <!-- Answer Author Unvoted Best Answer -->
- <label for="<?php echo $this->field_id( array( 'unvoted_best_answer', 'creds' ) ); ?>" class="subheader"><?php _e( 'Unvoted Best Answer', 'mycred' ); ?></label>
- <ol>
- <li>
- <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>
- </li>
- <li class="empty"> </li>
- <li>
- <label for="<?php echo $this->field_id( array( 'unvoted_best_answer', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
- <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>
- <span class="description"><?php _e( 'Available template tags: General and Post (Answer) related.', 'mycred' ); ?></span>
- </li>
- </ol>
- <h3>Question Author</h3>
- <!-- Question Author Set Best Answer -->
- <label for="<?php echo $this->field_id( array( 'selected_best_answer', 'creds' ) ); ?>" class="subheader"><?php _e( 'Choosing a Best Answer', 'mycred' ); ?></label>
- <ol>
- <li><?php echo $this->core->template_tags_general( __( 'Option to award %_plural% to the Question author for selecting a best answer.', 'mycred' ) ); ?></li>
- <li>
- <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>
- </li>
- <li class="empty"> </li>
- <li>
- <label for="<?php echo $this->field_id( array( 'selected_best_answer', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
- <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>
- <span class="description"><?php _e( 'Available template tags: General and Post (Question) related.', 'mycred' ); ?></span>
- </li>
- </ol>
- <!-- Question Author Remove Best Answer -->
- <label for="<?php echo $this->field_id( array( 'removed_best_answer', 'creds' ) ); ?>" class="subheader"><?php _e( 'Removing Best Answer', 'mycred' ); ?></label>
- <ol>
- <li>
- <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>
- </li>
- <li class="empty"> </li>
- <li>
- <label for="<?php echo $this->field_id( array( 'removed_best_answer', 'log' ) ); ?>"><?php _e( 'Log template', 'mycred' ); ?></label>
- <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>
- <span class="description"><?php _e( 'Available template tags: General and Post (Question) related.', 'mycred' ); ?></span>
- </li>
- </ol>
- <?php
- }
- }
- }
- endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement