HEX
Server: Apache/2.4.65 (Unix) OpenSSL/1.1.1k
System: Linux server-manager.elshandawiily.com 4.18.0-553.87.1.el8_10.x86_64 #1 SMP Mon Dec 1 05:11:16 EST 2025 x86_64
User: elshanda (1002)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: /home/elshanda/public_html/wp-content/plugins/mailpoet/lib/Subscription/Captcha/CaptchaPhrase.php
<?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing

namespace MailPoet\Subscription\Captcha;

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


use MailPoetVendor\Gregwar\Captcha\PhraseBuilder;

class CaptchaPhrase {


  /** @var CaptchaSession  */
  private $session;

  /** @var PhraseBuilder  */
  private $phraseBuilder;

  public function __construct(
    CaptchaSession $session,
    PhraseBuilder $phraseBuilder = null
  ) {
    $this->session = $session;
    $this->phraseBuilder = $phraseBuilder ? $phraseBuilder : new PhraseBuilder();
  }

  public function getPhrase(): ?string {
    $storage = $this->session->getCaptchaHash();
    return (isset($storage['phrase']) && is_string($storage['phrase'])) ? $storage['phrase'] : null;
  }

  public function resetPhrase() {
    $this->session->setCaptchaHash(null);
  }

  public function getPhraseForType(string $type, string $sessionId = null): string {
    $this->session->init($sessionId);
    $storage = $this->session->getCaptchaHash();
    if (!$storage) {
      $storage = [
        'phrase' => $this->phraseBuilder->build(),
        'total_loaded' => 1,
        'loaded_by_types' => [],
      ];
    }
    if (!isset($storage['loaded_by_types'][$type])) {
      $storage['loaded_by_types'][$type] = 0;
    }

    if ($this->needsToRegenerateCaptcha($storage, $type)) {
      $storage['phrase'] = $this->phraseBuilder->build();
      $storage['total_loaded']++;
    }
    $storage['loaded_by_types'][$type]++;

    $this->session->setCaptchaHash($storage);
    return $storage['phrase'];
  }

  private function needsToRegenerateCaptcha(array $storage, string $type): bool {
    return $storage['loaded_by_types'][$type] === $storage['total_loaded'];
  }
}