rubengc / GamiPress

The most flexible and powerful gamification system for WordPress
https://gamipress.com/
60 stars 23 forks source link

Rank earners not displayed correctly in multisite with NO network-wide activation #24

Closed tambaqui closed 1 year ago

tambaqui commented 1 year ago

Hi @rubengc,

I've detected a couple of inconsistencies regarding the ranks shortcodes [gamipress_ranks] and[gamipress_rank]:

1. Current rank:

The "current-user-rank" class is not being applied correctly in these shortcodes, at least in a multisite installation with NO network-wide activation, although it doesn't seem critical. I think the problem is located in the templates/rank.php template, on line 23, where the user's current rank is obtained:

// Check if this rank is the current one of the user
$current = gamipress_get_user_rank_id( $user_id ) === get_the_ID();

Here the gamipress_get_user_rank_id function is being called without passing the second argument $rank_type, so it always returns 0.

A possible fix would be to get the type from the id that comes in the $gamipress_template_args template arguments, something like this:

// Check if this rank is the current one of the user
$rank_type = isset( $a['id'] ) ? gamipress_get_post_type( absint( $a['id'] ) ) : '';
$current = gamipress_get_user_rank_id( $user_id, $rank_type ) === get_the_ID();

This would be enough for the shortcode [gamipress_rank].

For [gamipress_ranks] there is a small additional bug that should be fixed as well. This shortcode renders the ranks in a loop, using the gamipress_render_rank method, which parses the $template_args arguments with the default parameters from gamipress_rank_shortcode_defaults() via wp_parse_args. As the rank id is not included in the $template_args, the get_the_ID() that comes from the gamipress_rank_shortcode_defaults() is taken by default, but depending on the context, this id is not the rank id but the page id in which shortcode is used. Therefore, the solution here would be to include the rank id in the $template_args before performing the wp_parse_args.

2. Display earners:

Earners are not displayed correctly for each rank. This question is related to issue #23. The problem is basically that in the gamipress_get_rank_earners method, the query clauses are built without taking into account the site prefix. It should be taken into account in the case of multisite with NO network-wide activation.

Regards!

rubengc commented 1 year ago

Hi @tambaqui thank you so much for reporting this issue, our team will review and fix it as soon as possible