8889841cwp-content/plugins/mailpoet/lib/Automation/Integrations/Core/Filters/DateTimeFilter.php000064400000013600150513027410033145 0ustar00home/clixcotz/tcchp.clix.co.tzlocalTimezone = $localTimezone; } public function getFieldType(): string { return Field::TYPE_DATETIME; } public function getConditions(): array { return [ self::CONDITION_BEFORE => __('before', 'mailpoet'), self::CONDITION_AFTER => __('after', 'mailpoet'), self::CONDITION_ON => __('on', 'mailpoet'), self::CONDITION_NOT_ON => __('not on', 'mailpoet'), self::CONDITION_IN_THE_LAST => __('in the last', 'mailpoet'), self::CONDITION_NOT_IN_THE_LAST => __('not in the last', 'mailpoet'), self::CONDITION_IS_SET => __('is set', 'mailpoet'), self::CONDITION_IS_NOT_SET => __('is not set', 'mailpoet'), self::CONDITION_ON_THE_DAYS_OF_THE_WEEK => __('on the day(s) of the week', 'mailpoet'), ]; } public function getArgsSchema(): ObjectSchema { return Builder::object([ 'value' => Builder::oneOf([ Builder::string()->pattern(self::REGEX_DATETIME), Builder::string()->pattern(self::REGEX_DATE), Builder::array(Builder::integer()->minimum(0)->maximum(6))->minItems(1), Builder::object([ 'number' => Builder::integer()->minimum(1)->required(), 'unit' => Builder::string()->pattern('^days|weeks|months$')->required(), ]), ]), ]); } public function matches(FilterData $data, $value): bool { $filterValue = $data->getArgs()['value'] ?? null; $condition = $data->getCondition(); // is set/is not set if (in_array($condition, [self::CONDITION_IS_SET, self::CONDITION_IS_NOT_SET], true)) { return $this->matchesSet($condition, $value); } // in the last/not in the last if (in_array($condition, [self::CONDITION_IN_THE_LAST, self::CONDITION_NOT_IN_THE_LAST], true)) { return $this->matchesInTheLast($condition, $filterValue, $value); } // on the day(s) of the week if ($condition === self::CONDITION_ON_THE_DAYS_OF_THE_WEEK) { return $this->matchesOnTheDaysOfTheWeek($filterValue, $value); } // other conditions if (!is_string($filterValue) || !$value instanceof DateTimeInterface) { return false; } $datetime = $this->convertToLocalTimezone($value); switch ($condition) { case 'before': $ref = DateTimeImmutable::createFromFormat(self::FORMAT_DATETIME, $filterValue, $this->localTimezone); return $ref && $datetime < $ref; case 'after': $ref = DateTimeImmutable::createFromFormat(self::FORMAT_DATETIME, $filterValue, $this->localTimezone); return $ref && $datetime > $ref; case 'on': return $datetime->format(self::FORMAT_DATE) === $filterValue; case 'not-on': return $datetime->format(self::FORMAT_DATE) !== $filterValue; default: return false; } } /** @param mixed $value */ private function matchesSet(string $condition, $value): bool { switch ($condition) { case self::CONDITION_IS_SET: return $value !== null; case self::CONDITION_IS_NOT_SET: return $value === null; default: return false; } } /** * @param mixed $filterValue * @param mixed $value */ private function matchesInTheLast(string $condition, $filterValue, $value): bool { if (!is_array($filterValue) || !isset($filterValue['number']) || !isset($filterValue['unit']) || !$value instanceof DateTimeInterface) { return false; } $number = $filterValue['number']; $unit = $filterValue['unit']; if (!is_integer($number) || !in_array($unit, ['days', 'weeks', 'months'], true)) { return false; } $now = new DateTimeImmutable('now', $this->localTimezone); $ref = $now->modify("-$number $unit"); $matches = $ref <= $value && $value <= $now; return $condition === self::CONDITION_IN_THE_LAST ? $matches : !$matches; } /** * @param mixed $filterValue * @param mixed $value */ private function matchesOnTheDaysOfTheWeek($filterValue, $value): bool { if (!is_array($filterValue) || !$value instanceof DateTimeInterface) { return false; } foreach ($filterValue as $day) { if (!is_integer($day) || $day < 0 || $day > 6) { return false; } } $date = $this->convertToLocalTimezone($value); $day = (int)$date->format('w'); return in_array($day, $filterValue, true); } private function convertToLocalTimezone(DateTimeInterface $datetime): DateTimeImmutable { $value = DateTimeImmutable::createFromFormat('U', (string)$datetime->getTimestamp(), $this->localTimezone); if (!$value) { throw new InvalidStateException('Failed to convert datetime to WP timezone'); } return $value; } }