Limit the Number of Reports

I would like to limit the amount of reports overall (not for each listing) to 5 per week. I wrote this code, but it doesn’t work. How can I fix this?

add_filter(
    'hivepress/v1/forms/listing_report/errors',
    function( $errors, $form ) {
        $user_id = get_current_user_id();

        if ( $user_id ) {
            $report_count = \HivePress\Models\Report::query()->filter(
                [
                    'user'       => $user_id,
                    'date_query' => [
                        [
                            'after'     => '1 week ago',
                            'inclusive' => true,
                        ],
                    ],
                ]
            )->get_count();

            if ( $report_count >= 5 ) {
                $errors[] = 'Only 5 reports per week are allowed.';
            }
        }

        return $errors;
    },
    1000,
    2
);

Hi,

Unfortunately, we can’t provide general recommendations here, as this requires a detailed check and custom implementation. Please note that reports are just emails to the administration (so there is no Report model in the code), so you need to count each email sent, store this number somewhere, and check it. As a workaround, you can just add these kinds of emails to spam if you need to.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.