# acymailing/11.0.4/back/Controllers/Segments/Campaign.php

AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress, version 11.0.4. 118 lines.

- Page: https://pluginprobe.com/plugins/acymailing/11.0.4/code/back/Controllers/Segments/Campaign.php
- Raw: https://pluginprobe.com/plugins/acymailing/11.0.4/raw/back/Controllers/Segments/Campaign.php
- Modified: 2026-08-03T10:18:54+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/acymailing/11.0.4/code/back/Controllers/Segments/Campaign.php#L10-L20`.

```php
<?php

namespace AcyMailing\Controllers\Segments;

use AcyMailing\Classes\SegmentClass;
use AcyMailing\Helpers\AutomationHelper;

trait Campaign
{
    public function countSegmentById(int $segmentId, array $listsIds, bool $ajax = true, bool $exclude = false): int
    {
        acym_arrayToInteger($listsIds);

        if (empty($listsIds)) {
            return 0;
        }

        if (empty($segmentId)) {
            if ($ajax) {
                acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_COUNT_USER'), [], false);
            } else {
                return 0;
            }
        }

        $segmentClass = new SegmentClass();
        $segment = $segmentClass->getOneById($segmentId);
        if (empty($segment)) {
            if ($ajax) {
                acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_COUNT_USER'), [], false);
            } else {
                return 0;
            }
        }

        $automationHelperBase = new AutomationHelper();
        $automationHelperBase->removeFlag(self::FLAG_COUNT);

        $automationHelpers = [];
        foreach ($segment->filters as $or => $orValues) {
            if (empty($orValues)) continue;
            $automationHelpers[$or] = new AutomationHelper();
            foreach ($orValues as $and => $andValues) {
                $and = intval($and);
                foreach ($andValues as $filterName => $options) {
                    acym_trigger('onAcymProcessFilter_'.$filterName, [&$automationHelpers[$or], &$options, $and.'_'.$or]);
                }
            }

            $automationHelpers[$or]->excludeSelected = $exclude;
            $automationHelpers[$or]->addFlag(self::FLAG_COUNT);
        }

        $join = ' #__acym_user_has_list AS user_list ON user_list.user_id = user.id ';
        $join .= 'AND user_list.list_id IN ('.implode(',', $listsIds).') ';
        $join .= 'AND user_list.status = 1 ';
        if ($this->config->get('require_confirmation', 1) == 1) {
            $join .= 'AND user.confirmed = 1 ';
        }

        if (empty($automationHelpers)) {
            $automationHelperBase->join['user_list'] = $join;

            return acym_loadResult($automationHelperBase->getQuery(['COUNT(DISTINCT user.id)']));
        } else {
            $automationHelperBase = array_pop($automationHelpers);
            $automationHelperBase->join['user_list'] = $join;
            $numberOfRecipients = acym_loadResult($automationHelperBase->getQuery(['COUNT(DISTINCT user.id)']));
            $automationHelperBase->removeFlag(self::FLAG_COUNT);

            return $numberOfRecipients;
        }
    }

    public function countGlobalBySegmentId(): void
    {
        wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');

        $segmentId = acym_getVar('int', 'segmentId', 0);
        $isExclude = acym_getVar('boolean', 'exclude', false);
        $listsIds = json_decode(acym_getVar('string', 'lists', '[]'), true);
        if (empty($listsIds)) {
            $listsIds = [];
        }

        if (empty($segmentId)) {
            acym_sendAjaxResponse('', ['count' => $this->countSegmentByParams([], $listsIds, $isExclude)]);
        }
        acym_sendAjaxResponse('', ['count' => $this->countSegmentById($segmentId, $listsIds, true, $isExclude)]);
    }

    public function saveFromCampaign(): void
    {
        wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token');

        $filters = acym_getVar('array', 'acym_action', []);
        acym_blockRestrictedFilterAjax($filters);

        $name = acym_getVar('string', 'segment_name', '');

        $segmentClass = new SegmentClass();

        $segment = new \stdClass();

        $segment->name = $name;
        $segment->creation_date = acym_date('now', 'Y-m-d H:i:s');
        $segment->active = 1;
        $segment->filters = json_encode($filters['filters']);

        $segmentId = $segmentClass->save($segment);
        if (!empty($segmentId)) {
            acym_sendAjaxResponse(acym_translation('ACYM_SEGMENT_WELL_SAVE'), ['segment_id' => $segmentId]);
        } else {
            acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_SAVE_SEGMENT'), [], false);
        }
    }
}

```
