* @copyright 2014 AnsPress.io & Rahul Aryan * @license GPL-3.0+ https://www.gnu.org/licenses/gpl-3.0.txt * @link https://anspress.io * @package WordPress/AnsPress/Email * * Addon Name: Reputation * Addon URI: https://anspress.io * Description: Award reputation to user based on activities. * Author: Rahul Aryan * Author URI: https://anspress.io */ // If this file is called directly, abort. if ( ! defined( 'WPINC' ) ) { die; } /** * Reputation hooks. */ class AnsPress_Reputation_Hooks { /** * Init class. */ public static function init() { SELF::register_default_events(); anspress()->add_action( 'ap_option_groups', __CLASS__, 'load_options' ); anspress()->add_action( 'wp_ajax_ap_save_events', __CLASS__, 'ap_save_events' ); anspress()->add_action( 'ap_after_new_question', __CLASS__, 'new_question', 10, 2 ); anspress()->add_action( 'ap_after_new_answer', __CLASS__, 'new_answer', 10, 2 ); anspress()->add_action( 'ap_untrash_question', __CLASS__, 'new_question', 10, 2 ); anspress()->add_action( 'ap_trash_question', __CLASS__, 'trash_question', 10, 2 ); anspress()->add_action( 'ap_before_delete_question', __CLASS__, 'trash_question', 10, 2 ); anspress()->add_action( 'ap_untrash_answer', __CLASS__, 'new_answer', 10, 2 ); anspress()->add_action( 'ap_trash_answer', __CLASS__, 'trash_answer', 10, 2 ); anspress()->add_action( 'ap_before_delete_answer', __CLASS__, 'trash_answer', 10, 2 ); anspress()->add_action( 'ap_select_answer', __CLASS__, 'select_answer' ); anspress()->add_action( 'ap_unselect_answer', __CLASS__, 'unselect_answer' ); anspress()->add_action( 'ap_vote_up', __CLASS__, 'vote_up' ); anspress()->add_action( 'ap_vote_down', __CLASS__, 'vote_down' ); anspress()->add_action( 'ap_undo_vote_up', __CLASS__, 'undo_vote_up' ); anspress()->add_action( 'ap_undo_vote_down', __CLASS__, 'undo_vote_down' ); anspress()->add_action( 'ap_publish_comment', __CLASS__, 'new_comment' ); anspress()->add_action( 'ap_unpublish_comment', __CLASS__, 'delete_comment' ); anspress()->add_filter( 'user_register', __CLASS__, 'user_register' ); anspress()->add_action( 'delete_user', __CLASS__, 'delete_user' ); anspress()->add_filter( 'ap_user_display_name', __CLASS__, 'display_name', 10, 2 ); anspress()->add_filter( 'ap_pre_fetch_question_data', __CLASS__, 'pre_fetch_post' ); anspress()->add_filter( 'ap_pre_fetch_answer_data', __CLASS__, 'pre_fetch_post' ); anspress()->add_filter( 'bp_before_member_header_meta', __CLASS__, 'bp_profile_header_meta' ); anspress()->add_filter( 'ap_user_pages', __CLASS__, 'ap_user_pages' ); anspress()->add_filter( 'ap_ajax_load_more_reputation', __CLASS__, 'load_more_reputation' ); anspress()->add_filter( 'ap_bp_nav', __CLASS__, 'ap_bp_nav' ); anspress()->add_filter( 'ap_bp_page', __CLASS__, 'ap_bp_page', 10, 2 ); } /** * Register reputation options */ public static function load_options() { ap_register_option_group( 'reputation', __( 'Reputation', 'anspress-question-answer' ) ); ap_register_option_section( 'reputation', 'events', __( 'Events', 'anspress-question-answer' ), 'ap_option_events_view' ); } /** * Register default reputation events. */ public static function register_default_events() { ap_register_reputation_event( 'register', array( 'points' => 10, 'label' => __( 'Registration', 'anspress-question-answer' ), 'description' => __( 'Points awarded when user account is created', 'anspress-question-answer' ), 'icon' => 'apicon-question', 'activity' => __( 'Registered', 'anspress-question-answer' ), 'parent' => 'question', ) ); ap_register_reputation_event( 'ask', array( 'points' => 2, 'label' => __( 'Asking', 'anspress-question-answer' ), 'description' => __( 'Points awarded when user asks a question', 'anspress-question-answer' ), 'icon' => 'apicon-question', 'activity' => __( 'Asked a question', 'anspress-question-answer' ), 'parent' => 'question', ) ); ap_register_reputation_event( 'answer', array( 'points' => 5, 'label' => __( 'Answering', 'anspress-question-answer' ), 'description' => __( 'Points awarded when user answer a question', 'anspress-question-answer' ), 'icon' => 'apicon-answer', 'activity' => __( 'Posted an answer', 'anspress-question-answer' ), 'parent' => 'answer', ) ); ap_register_reputation_event( 'comment', array( 'points' => 2, 'label' => __( 'Commenting', 'anspress-question-answer' ), 'description' => __( 'Points awarded when user comment on question or answer', 'anspress-question-answer' ), 'icon' => 'apicon-comments', 'activity' => __( 'Commented on a post', 'anspress-question-answer' ), 'parent' => 'comment', ) ); ap_register_reputation_event( 'select_answer', array( 'points' => 2, 'label' => __( 'Selecting an Answer', 'anspress-question-answer' ), 'description' => __( 'Points awarded when user select an answer for thier question', 'anspress-question-answer' ), 'icon' => 'apicon-check', 'activity' => __( 'Selected an answer as best', 'anspress-question-answer' ), 'parent' => 'question', ) ); ap_register_reputation_event( 'best_answer', array( 'points' => 10, 'label' => __( 'Answer selected as best', 'anspress-question-answer' ), 'description' => __( 'Points awarded when user\'s answer selected as best', 'anspress-question-answer' ), 'icon' => 'apicon-check', 'activity' => __( 'Answer was selected as best', 'anspress-question-answer' ), 'parent' => 'answer', ) ); ap_register_reputation_event( 'received_vote_up', array( 'points' => 10, 'label' => __( 'Received up vote', 'anspress-question-answer' ), 'description' => __( 'Points awarded when user receive an upvote', 'anspress-question-answer' ), 'icon' => 'apicon-thumb-up', 'activity' => __( 'Received an upvote', 'anspress-question-answer' ), ) ); ap_register_reputation_event( 'received_vote_down', array( 'points' => -2, 'label' => __( 'Received down vote', 'anspress-question-answer' ), 'description' => __( 'Points awarded when user receive a down vote', 'anspress-question-answer' ), 'icon' => 'apicon-thumb-down', 'activity' => __( 'Received a down vote', 'anspress-question-answer' ), ) ); ap_register_reputation_event( 'given_vote_up', array( 'points' => 0, 'label' => __( 'Gives an up vote', 'anspress-question-answer' ), 'description' => __( 'Points taken from user when they give a vote up', 'anspress-question-answer' ), 'icon' => 'apicon-thumb-up', 'activity' => __( 'Given an up vote', 'anspress-question-answer' ), ) ); ap_register_reputation_event( 'given_vote_down', array( 'points' => 0, 'label' => __( 'Gives down vote', 'anspress-question-answer' ), 'description' => __( 'Points taken from user when user give a down vote', 'anspress-question-answer' ), 'icon' => 'apicon-thumb-down', 'activity' => __( 'Given a down vote', 'anspress-question-answer' ), ) ); } /** * Save reputation events. */ public static function ap_save_events() { check_ajax_referer( 'ap-save-events', '__nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die(); } $events_point = ap_isset_post_value( 'events', 'r' ); $points = []; foreach ( ap_get_reputation_events() as $slug => $event ) { if ( isset( $events_point[ $slug ] ) && (int) $events_point[ $slug ] !== (int) $event['points'] ) { $points[ sanitize_text_field( $slug ) ] = (int) $events_point[ $slug ]; } } update_option( 'anspress_reputation_events', $points ); echo '
' . esc_attr__( 'Successfully updated reputation points!', 'anspress-question-answer' ) . '