JReviews menu
function limit_free_listing_per_category($permission, $params)
{
$auth = S2Object::make('auth');
$listingPermissions = (S2Object::make('perm'))->__('listing');
// Only for logged-in users
if ($auth->id <= 0) {
return $permission;
}
$Model = new S2Model();
// Detect plan ID
$planId = isset($params['paid_plan_id']) ? (int)$params['paid_plan_id'] : 0;
if ($planId <= 0) {
return $permission;
}
// Check if the plan is FREE
$query = "SELECT price FROM #__jreviews_paid_plans WHERE plan_id = " . $planId;
$price = $Model->query($query, 'loadResult');
if ((float)$price > 0) {
// Paid plan → allow
return $permission;
}
// Detect category
$catId = 0;
if (!empty($params['cat_id'])) {
$catId = (int)$params['cat_id'];
}
elseif (!empty($params['category_id'])) {
$catId = (int)$params['category_id'];
}
if ($catId <= 0) {
return $permission;
}
// Count existing listings by user in that category
$query = sprintf(
"SELECT COUNT(*)
FROM %s
WHERE %s = %d
AND catid = %d
AND state = 1",
EverywhereComContentModel::_LISTING_TABLE,
EverywhereComContentModel::_LISTING_USER_ID,
$auth->id,
$catId
);
$count = $Model->query($query,'loadResult');
if ($count >= 1)
{
$listingPermissions->setMessage(
'You can only create one free listing in this category. Please upgrade to a paid plan to add more.'
);
return false;
}
return $permission;
}
Clickfwd\Hook\Filter::add('can_create_listing', 'limit_free_listing_per_category', 10);
