DotcomHelperFunctions.php
5 months ago
DotcomLicenseProvisioner.php
2 years ago
index.php
3 years ago
DotcomHelperFunctions.php
141 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\WPCOM; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\WP\Functions; |
| 9 | |
| 10 | /** |
| 11 | * Plan detection documentation: |
| 12 | * https://github.com/Automattic/wc-calypso-bridge#active-plan-detection |
| 13 | */ |
| 14 | class DotcomHelperFunctions { |
| 15 | |
| 16 | private Functions $wp; |
| 17 | |
| 18 | public function __construct( |
| 19 | Functions $wp |
| 20 | ) { |
| 21 | $this->wp = $wp; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Returns true if in the context of WordPress.com Atomic platform. |
| 26 | * |
| 27 | * @return bool |
| 28 | */ |
| 29 | public function isAtomicPlatform(): bool { |
| 30 | // ATOMIC_CLIENT_ID === '2' corresponds to WordPress.com client on the Atomic platform |
| 31 | $is_atomic_platform = defined('IS_ATOMIC') && IS_ATOMIC && defined('ATOMIC_CLIENT_ID') && (ATOMIC_CLIENT_ID === '2'); |
| 32 | return (bool)$this->wp->applyFilters('mailpoet_is_atomic_platform', $is_atomic_platform); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Returns true if the site is on WordPress.com. |
| 37 | */ |
| 38 | public function isDotcom(): bool { |
| 39 | return $this->isAtomicPlatform(); |
| 40 | } |
| 41 | |
| 42 | public function isWooExpressPerformance(): bool { |
| 43 | return function_exists('wc_calypso_bridge_is_woo_express_performance_plan') && wc_calypso_bridge_is_woo_express_performance_plan(); |
| 44 | } |
| 45 | |
| 46 | public function isWooExpressEssential(): bool { |
| 47 | return function_exists('wc_calypso_bridge_is_woo_express_essential_plan') && wc_calypso_bridge_is_woo_express_essential_plan(); |
| 48 | } |
| 49 | |
| 50 | public function isBusiness(): bool { |
| 51 | return function_exists('wc_calypso_bridge_is_business_plan') && wc_calypso_bridge_is_business_plan(); |
| 52 | } |
| 53 | |
| 54 | public function isEcommerceTrial(): bool { |
| 55 | return function_exists('wc_calypso_bridge_is_ecommerce_trial_plan') && wc_calypso_bridge_is_ecommerce_trial_plan(); |
| 56 | } |
| 57 | |
| 58 | public function isEcommerceWPCom(): bool { |
| 59 | return function_exists('wc_calypso_bridge_is_wpcom_ecommerce_plan') && wc_calypso_bridge_is_wpcom_ecommerce_plan(); |
| 60 | } |
| 61 | |
| 62 | public function isEcommerce(): bool { |
| 63 | return function_exists('wc_calypso_bridge_is_ecommerce_plan') && wc_calypso_bridge_is_ecommerce_plan(); |
| 64 | } |
| 65 | |
| 66 | public function isGarden(): bool { |
| 67 | return defined('IS_COMMERCE_GARDEN') && IS_COMMERCE_GARDEN; |
| 68 | } |
| 69 | |
| 70 | protected function getWpcloudConfig(string $key): ?string { |
| 71 | if (!function_exists('garden_get_wpcloud_config')) { |
| 72 | return null; |
| 73 | } |
| 74 | $value = \garden_get_wpcloud_config($key); |
| 75 | return is_string($value) && $value !== '' ? $value : null; |
| 76 | } |
| 77 | |
| 78 | protected function getSiteMetaValue(string $meta_key): ?string { |
| 79 | if (function_exists('get_site_meta')) { |
| 80 | $blog_id = \get_current_blog_id(); |
| 81 | $value = \get_site_meta($blog_id, $meta_key, true); |
| 82 | if (is_string($value) && $value !== '') { |
| 83 | return $value; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if ($this->isGarden()) { |
| 88 | return $this->getWpcloudConfig($meta_key); |
| 89 | } |
| 90 | |
| 91 | return null; |
| 92 | } |
| 93 | |
| 94 | public function gardenName(): ?string { |
| 95 | if (!$this->isGarden()) { |
| 96 | return null; |
| 97 | } |
| 98 | return $this->getSiteMetaValue('garden_name'); |
| 99 | } |
| 100 | |
| 101 | public function gardenPartner(): ?string { |
| 102 | if (!$this->isGarden()) { |
| 103 | return null; |
| 104 | } |
| 105 | return $this->getSiteMetaValue('garden_partner'); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns the plan name for the current site if hosted on WordPress.com. |
| 110 | * Empty otherwise. |
| 111 | */ |
| 112 | public function getDotcomPlan(): string { |
| 113 | if ($this->isWooExpressPerformance()) { |
| 114 | return 'performance'; |
| 115 | } elseif ($this->isWooExpressEssential()) { |
| 116 | return 'essential'; |
| 117 | } elseif ($this->isBusiness()) { |
| 118 | return 'business'; |
| 119 | } elseif ($this->isEcommerceTrial()) { |
| 120 | return 'ecommerce_trial'; |
| 121 | } elseif ($this->isEcommerceWPCom()) { |
| 122 | return 'ecommerce_wpcom'; |
| 123 | } elseif ($this->isEcommerce()) { |
| 124 | return 'ecommerce'; |
| 125 | } |
| 126 | |
| 127 | // Garden plan detection via WP Cloud persistent data |
| 128 | if ($this->isGarden()) { |
| 129 | $planInfo = $this->getWpcloudConfig('plan_info'); |
| 130 | if ($planInfo !== null) { |
| 131 | $decoded = json_decode($planInfo, true); |
| 132 | if (is_array($decoded) && isset($decoded['plan_type']) && is_string($decoded['plan_type']) && $decoded['plan_type'] !== '') { |
| 133 | return $decoded['plan_type']; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return ''; |
| 139 | } |
| 140 | } |
| 141 |