cloudflare/Cloudflare-WordPress
Publicmirrored fromhttps://github.com/cloudflare/Cloudflare-WordPress
src/WordPress/ClientActions.php
110lines · modecode
unknown
| 1 | <?php |
| 2 | |
| 3 | namespace CF\WordPress; |
| 4 | |
| 5 | use CF\API\APIInterface; |
| 6 | use CF\API\Request; |
| 7 | use CF\Integration\DefaultIntegration; |
| 8 | |
| 9 | class ClientActions |
| 10 | { |
| 11 | private $api; |
| 12 | private $config; |
| 13 | private $wordpressAPI; |
| 14 | private $dataStore; |
| 15 | private $logger; |
| 16 | private $request; |
| 17 | |
| 18 | /** |
| 19 | * @param DefaultIntegration $defaultIntegration |
| 20 | * @param APIInterface $api |
| 21 | * @param Request $request |
| 22 | */ |
| 23 | public function __construct(DefaultIntegration $defaultIntegration, APIInterface $api, Request $request) |
| 24 | { |
| 25 | $this->api = $api; |
| 26 | $this->config = $defaultIntegration->getConfig(); |
| 27 | $this->wordpressAPI = $defaultIntegration->getIntegrationAPI(); |
| 28 | $this->dataStore = $defaultIntegration->getDataStore(); |
| 29 | $this->logger = $defaultIntegration->getLogger(); |
| 30 | $this->request = $request; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * GET /zones. |
| 35 | * |
| 36 | * @return mixed |
| 37 | */ |
| 38 | public function returnWordPressDomain() |
| 39 | { |
| 40 | // Call GET /zones |
| 41 | $response = $this->api->callAPI($this->request); |
| 42 | |
| 43 | // Cache the domain for subdomains |
| 44 | $this->cacheDomainName($response); |
| 45 | |
| 46 | // Get zone information |
| 47 | $cf_zones_list = $this->filterZones($response); |
| 48 | |
| 49 | return $cf_zones_list; |
| 50 | } |
| 51 | |
| 52 | private function filterZones($response) |
| 53 | { |
| 54 | $cf_zones_list = $response; |
| 55 | $wpDomainList = $this->wordpressAPI->getDomainList(); |
| 56 | $wpDomain = $wpDomainList[0]; |
| 57 | |
| 58 | $domain_list = array(); |
| 59 | if ($this->api->responseOk($cf_zones_list)) { |
| 60 | $found = false; |
| 61 | foreach ($cf_zones_list['result'] as $cf_zone) { |
| 62 | if ($cf_zone['name'] === $wpDomain) { |
| 63 | $found = true; |
| 64 | array_push($domain_list, $cf_zone); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if ($found === false) { |
| 69 | array_push($domain_list, array( |
| 70 | 'name' => $wpDomain, |
| 71 | 'plan' => array('name' => ''), |
| 72 | 'type' => '', |
| 73 | 'status' => 'inactive', |
| 74 | )); |
| 75 | } |
| 76 | } |
| 77 | $cf_zones_list['result'] = $domain_list; |
| 78 | |
| 79 | return $cf_zones_list; |
| 80 | } |
| 81 | |
| 82 | public function cacheDomainName($response) |
| 83 | { |
| 84 | // Check if domain name needs to cached |
| 85 | $wpDomain = $this->wordpressAPI->getOriginalDomain(); |
| 86 | $cachedDomainList = $this->wordpressAPI->getDomainList(); |
| 87 | $cachedDomain = $cachedDomainList[0]; |
| 88 | |
| 89 | if (Utils::getRegistrableDomain($wpDomain) !== $cachedDomain) { |
| 90 | // If it's not a subdomain cache the current domain |
| 91 | $domainName = $wpDomain; |
| 92 | |
| 93 | // Get cloudflare zones to find if the current domain is a |
| 94 | // subdomain of any cloudflare zones registered |
| 95 | $validDomainName = $this->wordpressAPI->checkIfValidCloudflareSubdomain($response, $wpDomain); |
| 96 | |
| 97 | // Check if it's a subdomain, if it is cache the zone instead of the |
| 98 | // subdomain |
| 99 | if ($this->api->responseOK($response) && $validDomainName) { |
| 100 | $domainName = Utils::getRegistrableDomain($wpDomain); |
| 101 | } |
| 102 | |
| 103 | $this->wordpressAPI->setDomainNameCache($domainName); |
| 104 | |
| 105 | return $domainName; |
| 106 | } |
| 107 | |
| 108 | return $cachedDomain; |
| 109 | } |
| 110 | } |