cloudflare/Cloudflare-WordPress

Public

mirrored fromhttps://github.com/cloudflare/Cloudflare-WordPress

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v4.8.3

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/WordPress/DataStore.php

192lines · modecode

1<?php
2
3namespace CF\WordPress;
4
5use CF\Integration\DefaultLogger;
6use CF\Integration\DataStoreInterface;
7use CF\API\Plugin;
8use Symfony\Polyfill\Tests\Intl\Idn;
9
10class DataStore implements DataStoreInterface
11{
12 const API_KEY = 'cloudflare_api_key';
13 const EMAIL = 'cloudflare_api_email';
14 const CACHED_DOMAIN_NAME = 'cloudflare_cached_domain_name';
15
16 protected $wordPressWrapper;
17
18 /**
19 * @param DefaultLogger $logger
20 */
21 public function __construct(DefaultLogger $logger)
22 {
23 $this->logger = $logger;
24
25 $this->wordPressWrapper = new WordPressWrapper();
26 }
27
28 public function setWordPressWrapper(WordPressWrapper $wordPressWrapper)
29 {
30 $this->wordPressWrapper = $wordPressWrapper;
31 }
32
33 /**
34 * @param $client_api_key
35 * @param $email
36 * @param $unique_id
37 * @param $user_key
38 *
39 * @return bool
40 */
41 public function createUserDataStore($client_api_key, $email, $unique_id, $user_key)
42 {
43 if (defined('CLOUDFLARE_API_KEY') && defined('CLOUDFLARE_EMAIL')) {
44 return true;
45 }
46
47 // Clear options
48 $this->set(self::API_KEY, '');
49 $this->set(self::EMAIL, '');
50
51 // Fill options
52 $isUpdated1 = $this->set(self::API_KEY, $client_api_key);
53 $isUpdated2 = $this->set(self::EMAIL, $email);
54
55 return $isUpdated1 && $isUpdated2;
56 }
57
58 /**
59 * @return unique id for the current user for use in the host api
60 */
61 public function getHostAPIUserUniqueId()
62 {
63 return;
64 }
65
66 /**
67 * @return client v4 api key for current user
68 */
69 public function getClientV4APIKey()
70 {
71 if (defined('CLOUDFLARE_API_KEY') && CLOUDFLARE_API_KEY !== '') {
72 return CLOUDFLARE_API_KEY;
73 }
74
75 return $this->get(self::API_KEY);
76 }
77
78 /**
79 * @return mixed
80 */
81 public function getHostAPIUserKey()
82 {
83 return;
84 }
85
86 /**
87 * @return mixed
88 */
89 public function getDomainNameCache()
90 {
91 if (defined('CLOUDFLARE_DOMAIN_NAME') && CLOUDFLARE_DOMAIN_NAME !== '') {
92 return idn_to_utf8(CLOUDFLARE_DOMAIN_NAME, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
93 }
94
95 $cachedDomainName = $this->get(self::CACHED_DOMAIN_NAME);
96 if (empty($cachedDomainName)) {
97 return;
98 }
99
100 return $cachedDomainName;
101 }
102
103 /**
104 * @return mixed
105 */
106 public function setDomainNameCache($domainName)
107 {
108 if (defined('CLOUDFLARE_DOMAIN_NAME') && CLOUDFLARE_DOMAIN_NAME !== '') {
109 return;
110 }
111
112 return $this->set(self::CACHED_DOMAIN_NAME, $domainName);
113 }
114
115 /**
116 * @return cloudflare email
117 */
118 public function getCloudFlareEmail()
119 {
120 if (defined('CLOUDFLARE_EMAIL') && CLOUDFLARE_EMAIL !== '') {
121 return CLOUDFLARE_EMAIL;
122 }
123
124 return $this->get(self::EMAIL);
125 }
126
127 /**
128 * @param $settingId Plugin::[PluginSettingName]
129 *
130 * @return mixed
131 */
132 public function getPluginSetting($settingId)
133 {
134 $settingName = $this->getPluginSettingName($settingId);
135 if (!$settingName) {
136 return false;
137 }
138
139 return $this->get($settingName);
140 }
141
142 private function getPluginSettingName($settingId)
143 {
144 return in_array($settingId, Plugin::getPluginSettingsKeys()) ? $settingId : false;
145 }
146
147 /**
148 * @param $key
149 *
150 * @return mixed
151 */
152 public function get($key)
153 {
154 $result = $this->wordPressWrapper->getOption($key, null);
155
156 return $result;
157 }
158
159 /**
160 * @param $key
161 * @param $value
162 *
163 * @return mixed
164 */
165 public function set($key, $value)
166 {
167 return $this->wordPressWrapper->updateOption($key, $value);
168 }
169
170 /**
171 * @param $key
172 */
173 public function clear($key)
174 {
175 $this->wordPressWrapper->deleteOption($key);
176 }
177
178 public function clearDataStore()
179 {
180 $pluginKeys = \CF\API\Plugin::getPluginSettingsKeys();
181
182 // Delete Plugin Setting Options
183 foreach ($pluginKeys as $optionName) {
184 $this->clear($optionName);
185 }
186
187 // Delete DataStore Options
188 $this->clear(self::API_KEY);
189 $this->clear(self::EMAIL);
190 $this->clear(self::CACHED_DOMAIN_NAME);
191 }
192}