ReferralDetector.php
44 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Referrals; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Settings\SettingsController; |
| 9 | use MailPoet\WP\Functions as WPFunctions; |
| 10 | |
| 11 | class ReferralDetector { |
| 12 | const REFERRAL_CONSTANT_NAME = 'MAILPOET_REFERRAL_ID'; |
| 13 | const REFERRAL_SETTING_NAME = 'referral_id'; |
| 14 | |
| 15 | /** @var WPFunctions */ |
| 16 | private $wp; |
| 17 | |
| 18 | /** @var SettingsController */ |
| 19 | private $settings; |
| 20 | |
| 21 | public function __construct( |
| 22 | WPFunctions $wp, |
| 23 | SettingsController $settings |
| 24 | ) { |
| 25 | $this->wp = $wp; |
| 26 | $this->settings = $settings; |
| 27 | } |
| 28 | |
| 29 | public function detect() { |
| 30 | $referralId = $this->settings->get(self::REFERRAL_SETTING_NAME, null); |
| 31 | if ($referralId) { |
| 32 | return $referralId; |
| 33 | } |
| 34 | $referralId = $this->wp->getOption(self::REFERRAL_CONSTANT_NAME, null); |
| 35 | if ($referralId === null && defined(self::REFERRAL_CONSTANT_NAME)) { |
| 36 | $referralId = constant(self::REFERRAL_CONSTANT_NAME); |
| 37 | } |
| 38 | if ($referralId !== null) { |
| 39 | $this->settings->set(self::REFERRAL_SETTING_NAME, $referralId); |
| 40 | } |
| 41 | return $referralId; |
| 42 | } |
| 43 | } |
| 44 |