plugins - Use a hook or filter, or overwrite this Gamipress function?

admin2025-01-07  4

Currently, Gamipress allows you to import a CSV file that lists a username (email or ID), and a points value to add to the points type for that user. It uses this function to achieve this, which writes to the gamipress log.

/**
 * AJAX handler for the import points tool
 *
 * @since 1.6.4
 */
function gamipress_import_export_points_tool_ajax_import() {
    // Security check, forces to die if not security passed
    check_ajax_referer( 'gamipress_admin', 'nonce' );

    // Check user capabilities
    if( ! current_user_can( gamipress_get_manager_capability() ) ) {
        wp_send_json_error( __( 'You are not allowed to perform this action.', 'gamipress' ) );
    }

    // Check parameters received
    if( ! isset( $_FILES['file'] ) ) {
        wp_send_json_error( __( 'No file to import.', 'gamipress' ) );
    }

    $import_file = $_FILES['file']['tmp_name'];

    if( empty( $import_file ) ) {
        wp_send_json_error( __( 'Can\'t retrieve the file to import, check server file permissions.', 'gamipress' ) );
    }

    ignore_user_abort( true );

    if ( ! gamipress_is_function_disabled( 'set_time_limit' ) ) {
        set_time_limit( 0 );
    }

    // Retrieve the content from the file
    $file_contents = file_get_contents( $import_file );

    if( empty( $file_contents ) ) {
        wp_send_json_error( __( 'Empty file, so nothing to import.', 'gamipress' ) );
    }

    // Setup vars
    $points_types = gamipress_get_points_types();

    // Explode by line breaks
    $lines = explode( "\n", $file_contents );

    foreach( $lines as $number => $line ) {

        $columns = str_getcsv( $line );

        if( count( $columns ) >= 3 ) {

            $user = false;
            $points = 0;
            $points_type = '';
            $log_description = '';
            $deduct = false;

            // User
            if( isset( $columns[0] ) && ! empty( $columns[0] ) ) {

                $user_field = 'login';

                if( filter_var( $columns[0], FILTER_VALIDATE_EMAIL ) ) {
                    $user_field = 'email';
                } else if( is_numeric( $columns[0] ) ) {
                    $user_field = 'id';
                }

                $user = get_user_by( $user_field, $columns[0] );

            }

            // Points
            if( isset( $columns[1] ) && is_numeric( $columns[1] ) ) {

                // If points amount has a negative sign, then user is looking for deduct
                if ( substr( $columns[1], 0, 1 ) === '-') {
                    $deduct = true;
                    $columns[1] = substr( $columns[1], 1); // Remove the negative sign
                }

                $points = absint( $columns[1] );

            }

            // Points Type
            if( isset( $columns[2] ) && ! empty( $columns[2] ) && isset( $points_types[$columns[2]] ) ) {

                $points_type = $columns[2];

            }

            // Log Description
            if( isset( $columns[3] ) && ! empty( $columns[3] ) ) {

                $log_description = $columns[3];

            }

            // Check if everything is done
            if( $user && $points > 0 && ! empty( $points_type ) ) {

                // When award points passing an admin ID, we need to pass the full new amount
                $current_points = gamipress_get_user_points( $user->ID, $points_type );

                $args = array(
                    'admin_id'  => get_current_user_id(),
                    'reason'    => $log_description,
                    'log_type'  => 'points_award'
                );

                // If log description is empty, let GamiPress to setup it from log settings
                if( empty( $log_description ) ) {
                    $args = array( 'admin_id'  => get_current_user_id() );
                }

                if( $deduct ) {
                    // Deduct points to the user
                    gamipress_deduct_points_to_user( $user->ID, $current_points - $points, $points_type, $args );
                } else {
                    // Award points to the user
                    gamipress_award_points_to_user( $user->ID, $points + $current_points, $points_type, $args );
                }

            }

        }

    }

    // Return a success message
    wp_send_json_success( __( 'User\'s points balances has been updated successfully.', 'gamipress' ) );
}
add_action( 'wp_ajax_gamipress_import_export_points_tool_import', 'gamipress_import_export_points_tool_ajax_import' );

This function does not add the points to the users points total. It just writes it to the log. I then have this function I have written to register the points for the user. However, it registers the total amount of points, rather than adds the points value - as the import function passes points +/- current points. I would like to register the points value - not the points +/- current points.

//Gamipress - Register user points when importing points from CSV
function ofc_insert_user_earning_on_import_points_tool( $user_id, $points, $points_type, $args ) {

    if( ! defined( 'DOING_AJAX' ) ) return;
    if( ! DOING_AJAX ) return;
    if( ! isset( $_REQUEST['action'] ) ) return;
    if( $_REQUEST['action'] !== 'gamipress_import_export_points_tool_import' ) return;

    $points_type_obj = gamipress_get_points_type( $points_type );
    $award = ( current_filter() === 'gamipress_award_points_to_user' );

    if( ! $award ) {
        $points *= -1;
    }


    gamipress_insert_user_earning( $user_id, array(
        'title'         => ( isset( $args['reason'] ) ? $args['reason'] : '' ),
        'post_id'       => $points_type_obj['ID'],
        'post_type'     => 'points-type',
        'points'        => $points,
        'points_type'   => $points_type,
        'date'          => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
    ) );

}
add_action( 'gamipress_award_points_to_user', 'ofc_insert_user_earning_on_import_points_tool', 10, 4 );
add_action( 'gamipress_deduct_points_to_user', 'ofc_insert_user_earning_on_import_points_tool', 10, 4 );

How do I tap into the gamipress_import_export_points_tool_ajax_import function to get the $points instead of the points total? (Hopefully that makes sense)

Currently, Gamipress allows you to import a CSV file that lists a username (email or ID), and a points value to add to the points type for that user. It uses this function to achieve this, which writes to the gamipress log.

/**
 * AJAX handler for the import points tool
 *
 * @since 1.6.4
 */
function gamipress_import_export_points_tool_ajax_import() {
    // Security check, forces to die if not security passed
    check_ajax_referer( 'gamipress_admin', 'nonce' );

    // Check user capabilities
    if( ! current_user_can( gamipress_get_manager_capability() ) ) {
        wp_send_json_error( __( 'You are not allowed to perform this action.', 'gamipress' ) );
    }

    // Check parameters received
    if( ! isset( $_FILES['file'] ) ) {
        wp_send_json_error( __( 'No file to import.', 'gamipress' ) );
    }

    $import_file = $_FILES['file']['tmp_name'];

    if( empty( $import_file ) ) {
        wp_send_json_error( __( 'Can\'t retrieve the file to import, check server file permissions.', 'gamipress' ) );
    }

    ignore_user_abort( true );

    if ( ! gamipress_is_function_disabled( 'set_time_limit' ) ) {
        set_time_limit( 0 );
    }

    // Retrieve the content from the file
    $file_contents = file_get_contents( $import_file );

    if( empty( $file_contents ) ) {
        wp_send_json_error( __( 'Empty file, so nothing to import.', 'gamipress' ) );
    }

    // Setup vars
    $points_types = gamipress_get_points_types();

    // Explode by line breaks
    $lines = explode( "\n", $file_contents );

    foreach( $lines as $number => $line ) {

        $columns = str_getcsv( $line );

        if( count( $columns ) >= 3 ) {

            $user = false;
            $points = 0;
            $points_type = '';
            $log_description = '';
            $deduct = false;

            // User
            if( isset( $columns[0] ) && ! empty( $columns[0] ) ) {

                $user_field = 'login';

                if( filter_var( $columns[0], FILTER_VALIDATE_EMAIL ) ) {
                    $user_field = 'email';
                } else if( is_numeric( $columns[0] ) ) {
                    $user_field = 'id';
                }

                $user = get_user_by( $user_field, $columns[0] );

            }

            // Points
            if( isset( $columns[1] ) && is_numeric( $columns[1] ) ) {

                // If points amount has a negative sign, then user is looking for deduct
                if ( substr( $columns[1], 0, 1 ) === '-') {
                    $deduct = true;
                    $columns[1] = substr( $columns[1], 1); // Remove the negative sign
                }

                $points = absint( $columns[1] );

            }

            // Points Type
            if( isset( $columns[2] ) && ! empty( $columns[2] ) && isset( $points_types[$columns[2]] ) ) {

                $points_type = $columns[2];

            }

            // Log Description
            if( isset( $columns[3] ) && ! empty( $columns[3] ) ) {

                $log_description = $columns[3];

            }

            // Check if everything is done
            if( $user && $points > 0 && ! empty( $points_type ) ) {

                // When award points passing an admin ID, we need to pass the full new amount
                $current_points = gamipress_get_user_points( $user->ID, $points_type );

                $args = array(
                    'admin_id'  => get_current_user_id(),
                    'reason'    => $log_description,
                    'log_type'  => 'points_award'
                );

                // If log description is empty, let GamiPress to setup it from log settings
                if( empty( $log_description ) ) {
                    $args = array( 'admin_id'  => get_current_user_id() );
                }

                if( $deduct ) {
                    // Deduct points to the user
                    gamipress_deduct_points_to_user( $user->ID, $current_points - $points, $points_type, $args );
                } else {
                    // Award points to the user
                    gamipress_award_points_to_user( $user->ID, $points + $current_points, $points_type, $args );
                }

            }

        }

    }

    // Return a success message
    wp_send_json_success( __( 'User\'s points balances has been updated successfully.', 'gamipress' ) );
}
add_action( 'wp_ajax_gamipress_import_export_points_tool_import', 'gamipress_import_export_points_tool_ajax_import' );

This function does not add the points to the users points total. It just writes it to the log. I then have this function I have written to register the points for the user. However, it registers the total amount of points, rather than adds the points value - as the import function passes points +/- current points. I would like to register the points value - not the points +/- current points.

//Gamipress - Register user points when importing points from CSV
function ofc_insert_user_earning_on_import_points_tool( $user_id, $points, $points_type, $args ) {

    if( ! defined( 'DOING_AJAX' ) ) return;
    if( ! DOING_AJAX ) return;
    if( ! isset( $_REQUEST['action'] ) ) return;
    if( $_REQUEST['action'] !== 'gamipress_import_export_points_tool_import' ) return;

    $points_type_obj = gamipress_get_points_type( $points_type );
    $award = ( current_filter() === 'gamipress_award_points_to_user' );

    if( ! $award ) {
        $points *= -1;
    }


    gamipress_insert_user_earning( $user_id, array(
        'title'         => ( isset( $args['reason'] ) ? $args['reason'] : '' ),
        'post_id'       => $points_type_obj['ID'],
        'post_type'     => 'points-type',
        'points'        => $points,
        'points_type'   => $points_type,
        'date'          => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
    ) );

}
add_action( 'gamipress_award_points_to_user', 'ofc_insert_user_earning_on_import_points_tool', 10, 4 );
add_action( 'gamipress_deduct_points_to_user', 'ofc_insert_user_earning_on_import_points_tool', 10, 4 );

How do I tap into the gamipress_import_export_points_tool_ajax_import function to get the $points instead of the points total? (Hopefully that makes sense)

Share Improve this question asked Mar 1, 2022 at 5:38 roly151roly151 354 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Turns out it was as simple as adding this line to my function:

$current_total_points = gamipress_get_user_points( $user_id, $points_type);

and then passing $points - $current_total_points to Gamipress's insert user earnings function.

function ofc_insert_user_earning_on_import_points_tool( $user_id, $points, $points_type, $args ) {

    if( ! defined( 'DOING_AJAX' ) ) return;
    if( ! DOING_AJAX ) return;
    if( ! isset( $_REQUEST['action'] ) ) return;
    if( $_REQUEST['action'] !== 'gamipress_import_export_points_tool_import' ) return;

    $points_type_obj = gamipress_get_points_type( $points_type );
    $award = ( current_filter() === 'gamipress_award_points_to_user' );

    //if we are deducting points, then make the points negative
    if( ! $award ) {
        $points *= -1;
    }

    $current_total_points = gamipress_get_user_points( $user_id, $points_type);

    gamipress_insert_user_earning( $user_id, array(
        'title'         => ( isset( $args['reason'] ) ? $args['reason'] : '' ),
        'post_id'       => $points_type_obj['ID'],
        'post_type'     => 'points-type',
        'points'        => $points - $current_total_points,
        'points_type'   => $points_type,
        'date'          => date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ),
    ) );

}
add_action( 'gamipress_award_points_to_user', 'ofc_insert_user_earning_on_import_points_tool', 10, 4 );
add_action( 'gamipress_deduct_points_to_user', 'ofc_insert_user_earning_on_import_points_tool', 10, 4 );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736258787a540.html

最新回复(0)