paulgibbs / achievements

Achievements gamifies your WordPress site with challenges, badges, and points.
http://achievementsapp.com
63 stars 9 forks source link

Deleted posts don't decrement progress counts for "on post published" event #67

Open paulgibbs opened 11 years ago

paulgibbs commented 11 years ago

See: http://wordpress.org/support/topic/recalculation-of-the-satisfied-conditions-before-rewarding?replies=3#post-3765745

paulgibbs commented 11 years ago

Support for this would involve special-casing this event. Before doing so, think about if any other achievements have a similar problem, and see if there's a unified way of dealing with them all.

rmccue commented 11 years ago

Here's how I did it, but it breaks in certain circumstances:

    add_filter( 'dpa_maybe_unlock_achievement_progress_increment', array( $this, 'maybe_decrement'));
    add_action( 'sennza_likes', array( $this, 'run_sennza_likes' ), 10 );
    add_action( 'sennza_likes_unlike', array( $this, 'run_sennza_likes' ), 10 );

    public function run_sennza_likes($post_id) {
        $user = get_current_user_id();
        if ( empty( $user ) )
            return;

        if (current_filter() === 'sennza_likes_unlike')
            $this->decrement = true;

        do_action( 'ts_sennza_likes' );
    }

    public function maybe_decrement($step) {
        if ( $this->decrement === true ) {
            $step = -1;
            $this->decrement = false;
        }
        return $step;
    }

Specifically, it breaks when using recurring achievements with no limit/repeat, as the same action is used for both.

In a prospective general solution, it would use the same progress object for both actions and use the same step filter, but I didn't have time for implementing that.