, Ruben Garcia * @since 1.0.0 */ // Exit if accessed directly if( !defined( 'ABSPATH' ) ) exit; /** * Display achievements for a user on their profile screen * * @since 1.0.0 * @param object $user The current user's $user object * @return void */ function gamipress_user_profile_data( $user = null ) { ?>

__( 'Rank updated successfully.', 'gamipress' ), 'rank' => array( 'ID' => $rank->ID, 'post_title' => $rank->post_title, 'thumbnail' => gamipress_get_rank_post_thumbnail( $rank->ID, array( 32, 32 ) ), ) ) ); } add_action( 'wp_ajax_gamipress_profile_update_user_rank', 'gamipress_ajax_profile_update_user_rank' ); /** * Update user points ajax handler * * @since 1.5.9 * @updated 1.6.0 Now also return the current user ranks in order to see any rank change through the points earned */ function gamipress_ajax_profile_update_user_points() { // Security check, forces to die if not security passed check_ajax_referer( 'gamipress_admin', 'nonce' ); $points = absint( $_POST['points'] ); $register_movement = ( bool ) $_POST['register_movement']; $earnings_text = stripslashes( sanitize_text_field( $_POST['earnings_text'] ) ); $points_type = sanitize_text_field( $_POST['points_type'] ); $user_id = absint( $_POST['user_id'] ); // Check if user can edit other users if ( ! current_user_can( 'edit_user', $user_id ) ) { wp_send_json_error( __( 'You can perform this action.', 'gamipress' ) ); } // Check if user can manage GamiPress if( ! current_user_can( gamipress_get_manager_capability() ) ) { wp_send_json_error( __( 'You can perform this action.', 'gamipress' ) ); } // Check if valid user ID if( $user_id === 0 ) { wp_send_json_error( __( 'Invalid user ID.', 'gamipress' ) ); } // Check if valid amount if( ! is_numeric( $points ) ) { wp_send_json_error( __( 'Invalid points amount.', 'gamipress' ) ); } // Check if is valid points type if( $points_type !== '' && ! in_array( $points_type, gamipress_get_points_types_slugs() ) ) { wp_send_json_error( __( 'Invalid points type.', 'gamipress' ) ); } // Grab the user's current points $current_points = gamipress_get_user_points( $user_id, $points_type ); // Update the user points gamipress_update_user_points( $user_id, $points, get_current_user_id(), null, $points_type ); if( $register_movement ) { // Insert the custom user earning for the manual balance adjustment gamipress_insert_user_earning( $user_id, array( 'title' => $earnings_text, 'user_id' => $user_id, 'post_id' => gamipress_get_points_type_id( $points_type ), 'post_type' => 'points-type', 'points' => $points - $current_points, 'points_type' => $points_type, 'date' => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ), ) ); } // After update the user points balance, is possible that user unlocks a rank // For that, we need to return the current user ranks again and check for differences $ranks = array(); foreach( gamipress_get_rank_types_slugs() as $rank_type ) { // Get the rank object to build the same response as gamipress_ajax_profile_update_user_rank() function $rank = gamipress_get_user_rank( $user_id, $rank_type ); $ranks[] = array( 'ID' => $rank->ID, 'post_title' => $rank->post_title, 'post_type' => $rank->post_type, // Included to meet the rank type 'thumbnail' => gamipress_get_rank_post_thumbnail( $rank->ID, array( 32, 32 ) ), ); } wp_send_json_success( array( 'message' => __( 'Points updated successfully.', 'gamipress' ), 'points' => gamipress_format_amount( $points, $points_type ), 'ranks' => $ranks ) ); } add_action( 'wp_ajax_gamipress_profile_update_user_points', 'gamipress_ajax_profile_update_user_points' ); /** * Generate markup to show user rank * * @since 1.0.0 * * @param object $user The current user's $user object * * @return string concatenated markup */ function gamipress_profile_user_rank( $user = null ) { $rank_types = gamipress_get_rank_types(); $can_manage = current_user_can( gamipress_get_manager_capability() ); // Return if not rank types and user is not a manager if( empty( $rank_types ) && ! $can_manage ) { return; } ?>
' . __( 'this page', 'gamipress' ) . '' ); ?>
$data ) : ?>
ID, $rank_type ); if( $user_rank ) : ?>
ID, array( 32, 32 ) ); ?>
post_title; ?> $rank_type, 'posts_per_page' => -1 ) ); ?> ' . __( 'this page', 'gamipress' ) . '' ); ?>
' . __( 'this page', 'gamipress' ) . '' ); ?>
ID ); ?>
$data ) : $user_points = gamipress_get_user_points( $user->ID, $points_type ); ?>

absint( $user->ID ), 'items_per_page' => $items_per_page, ), array( 'views' => false, 'search_box' => false ) ); ?>

$data ) : ?>
absint( $user_id ), 'achievement_type' => $post_type ) ); $achievement_ids = array_map( function( $achievement ) { return $achievement->ID; }, $achievements ); // On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails $blog_id = gamipress_switch_to_main_site_if_network_wide_active(); ob_start(); ?> $data ) : // Skip if not is the points type to render if( $post_type !== $slug ) { continue; } ?>
$slug, 'posts_per_page' => -1, 'post_status' => 'publish', 'suppress_filters' => false ) ); if ( $the_query->have_posts() ) : ?> have_posts() ) : $the_query->the_post(); // Setup our award URL $award_url = add_query_arg( array( 'action' => 'award', 'achievement_id' => absint( get_the_ID() ), 'user_id' => absint( $user_id ) ) ); ?>
' . gamipress_get_post_field( 'post_title', get_the_ID() ) . ''; ?> 'revoke', 'user_id' => absint( $user_id ), 'achievement_id' => absint( get_the_ID() ), ) ); ?> |

$data ) : ?>
absint( $user_id ), 'achievement_type' => $post_type ) ); $achievement_ids = array_map( function( $achievement ) { return $achievement->ID; }, $achievements ); // On network wide active installs, we need to switch to main blog mostly for posts permalinks and thumbnails $blog_id = gamipress_switch_to_main_site_if_network_wide_active(); ob_start(); ?> $data ) : // Skip if not is the points type to render if( $post_type !== $slug ) { continue; } ?>
$slug, 'posts_per_page' => -1, 'post_status' => 'publish', 'suppress_filters' => false ) ); if ( $the_query->have_posts() ) : ?> have_posts() ) : $the_query->the_post(); // If not parent object, skip if( $slug === 'step' && ! $achievement = gamipress_get_step_achievement( get_the_ID() ) ) { continue; } else if( $slug === 'points-award' && ! $points_type = gamipress_get_points_award_points_type( get_the_ID() ) ) { continue; } else if( $slug === 'points-deduct' && ! $points_type = gamipress_get_points_deduct_points_type( get_the_ID() ) ) { continue; } else if( $slug === 'rank-requirement' && ! $rank = gamipress_get_rank_requirement_rank( get_the_ID() ) ) { continue; } // Setup our award URL $award_url = add_query_arg( array( 'action' => 'award', 'achievement_id' => absint( get_the_ID() ), 'user_id' => absint( $user_id ) ) ); ?>
ID, array( 32, 32 ) ); ?> post_type] ) ? '
' . $achievement_types[$achievement->post_type]['singular_name'] . ': ' : '' ); ?> ID ) . '">' . gamipress_get_post_field( 'post_title', $achievement->ID ) . ''; ?> ID, array( 32, 32 ) ); ?>
ID ) . '">' . gamipress_get_post_field( 'post_title', $points_type->ID ) . ''; ?> ID, array( 32, 32 ) ); ?> post_type] ) ? '
' . $rank_types[$rank->post_type]['singular_name'] . ': ' : '' ); ?> ID ) . '">' . gamipress_get_post_field( 'post_title', $rank->ID ) . ''; ?>
'revoke', 'user_id' => absint( $user_id ), 'achievement_id' => absint( get_the_ID() ), ) ); ?> |