PHP 8.2.31
Preview: NewsletterValidator.php Size: 3.58 KB
/home/nshryvcy/himaltourism.com/wp-content/plugins/mailpoet/lib/Newsletter/NewsletterValidator.php

<?php declare(strict_types = 1);

namespace MailPoet\Newsletter;

if (!defined('ABSPATH')) exit;


use MailPoet\Entities\NewsletterEntity;
use MailPoet\Services\Bridge;
use MailPoet\Settings\TrackingConfig;
use MailPoet\Validator\ValidationException;

class NewsletterValidator {
  
  /** @var Bridge */
  private $bridge;

  /** @var TrackingConfig */
  private $trackingConfig;

  public function __construct(
    Bridge $bridge,
    TrackingConfig $trackingConfig
  ) {
    $this->bridge = $bridge;
    $this->trackingConfig = $trackingConfig;
  }
  
  public function validate(NewsletterEntity $newsletterEntity): ?string {
    try {
      $this->validateBody($newsletterEntity);
      $this->validateUnsubscribeRequirements($newsletterEntity);
      $this->validateReEngagementRequirements($newsletterEntity);
      $this->validateAutomaticLatestContentRequirements($newsletterEntity);
    } catch (ValidationException $exception) {
      return $exception->getMessage();
    }
    return null;
  }

  private function validateUnsubscribeRequirements(NewsletterEntity $newsletterEntity): void {
    if (!$this->bridge->isMailpoetSendingServiceEnabled()) {
      return;
    }
    $content = $newsletterEntity->getContent();
    $hasUnsubscribeUrl = strpos($content, '[link:subscription_unsubscribe_url]') !== false;
    $hasUnsubscribeLink = strpos($content, '[link:subscription_unsubscribe]') !== false;

    if (!$hasUnsubscribeLink && !$hasUnsubscribeUrl) {
      throw new ValidationException(__('All emails must include an "Unsubscribe" link. Add a footer widget to your email to continue.', 'mailpoet'));
    }
  }

  private function validateBody(NewsletterEntity $newsletterEntity): void {
    $emptyBodyErrorMessage = __('Poet, please add prose to your masterpiece before you send it to your followers.', 'mailpoet');
    $content = $newsletterEntity->getContent();

    if ($content === '') {
      throw new ValidationException($emptyBodyErrorMessage);
    }

    $contentBlocks = $newsletterEntity->getBody()['content']['blocks'] ?? [];
    if (count($contentBlocks) < 1) {
      throw new ValidationException($emptyBodyErrorMessage);
    }
  }

  private function validateReEngagementRequirements(NewsletterEntity $newsletterEntity): void {
    if ($newsletterEntity->getType() !== NewsletterEntity::TYPE_RE_ENGAGEMENT) {
      return;
    }

    if (strpos($newsletterEntity->getContent(), '[link:subscription_re_engage_url]') === false) {
      throw new ValidationException(__('A re-engagement email must include a link with [link:subscription_re_engage_url] shortcode.', 'mailpoet'));
    }

    if (!$this->trackingConfig->isEmailTrackingEnabled()) {
      throw new ValidationException(__('Re-engagement emails are disabled because open and click tracking is disabled in MailPoet → Settings → Advanced.', 'mailpoet'));
    }
  }

  private function validateAutomaticLatestContentRequirements(NewsletterEntity $newsletterEntity) {
    if ($newsletterEntity->getType() !== NewsletterEntity::TYPE_NOTIFICATION) {
      return;
    }
    $content = $newsletterEntity->getContent();
    if (
      strpos($content, '"type":"automatedLatestContent"') === false && 
      strpos($content, '"type":"automatedLatestContentLayout"') === false
    ) {
      throw new ValidationException(_x('Please add an “Automatic Latest Content” widget to the email from the right sidebar.', '(Please reuse the current translation used for the string “Automatic Latest Content”) This Error message is displayed when a user tries to send a “Post Notification” email without any “Automatic Latest Content” widget inside', 'mailpoet'));
    }
  }
}

Directory Contents

Dirs: 12 × Files: 12

Name Size Perms Modified Actions
Editor DIR
- drwxr-xr-x 2023-04-04 05:35:40
Edit Download
Links DIR
- drwxr-xr-x 2023-04-04 05:35:40
Edit Download
Listing DIR
- drwxr-xr-x 2023-04-04 05:35:40
Edit Download
Options DIR
- drwxr-xr-x 2023-04-04 05:35:40
Edit Download
Preview DIR
- drwxr-xr-x 2023-04-04 05:35:40
Edit Download
Renderer DIR
- drwxr-xr-x 2023-04-04 05:35:40
Edit Download
Scheduler DIR
- drwxr-xr-x 2023-04-04 05:35:40
Edit Download
Segment DIR
- drwxr-xr-x 2023-04-04 05:35:40
Edit Download
Sending DIR
- drwxr-xr-x 2023-04-04 05:35:40
Edit Download
- drwxr-xr-x 2023-04-04 05:35:40
Edit Download
- drwxr-xr-x 2023-04-04 05:35:40
Edit Download
- drwxr-xr-x 2023-04-04 05:35:40
Edit Download
1.69 KB lrw-r--r-- 2023-04-04 05:35:40
Edit Download
3.95 KB lrw-r--r-- 2023-04-04 05:35:40
Edit Download
2.10 KB lrw-r--r-- 2023-04-04 05:35:40
Edit Download
5.76 KB lrw-r--r-- 2023-04-04 05:35:40
Edit Download
0 B lrw-r--r-- 2023-04-04 05:35:40
Edit Download
1.08 KB lrw-r--r-- 2023-04-04 05:35:40
Edit Download
2.85 KB lrw-r--r-- 2023-04-04 05:35:40
Edit Download
441 B lrw-r--r-- 2023-04-04 05:35:40
Edit Download
15.33 KB lrw-r--r-- 2023-04-04 05:35:40
Edit Download
22.10 KB lrw-r--r-- 2023-04-04 05:35:40
Edit Download
3.58 KB lrw-r--r-- 2023-04-04 05:35:40
Edit Download
2.25 KB lrw-r--r-- 2023-04-04 05:35:40
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).