PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.21
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.21
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / app / Modules / StorageDrivers / BaseStorageDriver.php

BaseStorageDriver.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.21, at app/Modules/StorageDrivers/BaseStorageDriver.php

237 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Modules\StorageDrivers;
4
5 use FluentCart\Api\Helper;
6 use FluentCart\Api\StorageDrivers;
7 use FluentCart\Framework\Support\Arr;
8 use FluentCart\App\Modules\StorageDrivers\Contracts\BaseStorageInterface;
9 use FluentCart\Framework\Support\Collection;
10 use FluentCart\Framework\Validator\Validator;
11 use WP_Error;
12
13 abstract class BaseStorageDriver implements BaseStorageInterface
14 {
15 public $slug;
16
17 public $title;
18
19 public $brandColor = '#ccc';
20
21 protected $driverHandler;
22
23 public static $drivers = [];
24
25 public static $routes = [];
26
27 abstract public function getLogo();
28
29 public function getDarkLogo()
30 {
31 return null;
32 }
33
34 abstract public function getDescription();
35
36 abstract public function getSettings();
37
38 abstract public function fields();
39
40 public function __construct($title, $slug, $brandColor)
41 {
42 $this->title = $title;
43 $this->slug = $slug;
44 $this->brandColor = $brandColor;
45 $this->driverHandler = 'fluent_cart_storage_settings_' . $slug;
46 }
47
48 /**
49 * @param array $data
50 * @return array|true|WP_Error
51 */
52 public function verifyConnectInfo(array $data, $args = [])
53 {
54 return true;
55 }
56
57 public abstract function hiddenSettingKeys(): array;
58
59 public function init()
60 {
61 add_filter('fluent_cart/storage/get_global_storage_settings_' . $this->slug, [$this, 'globalFields'], 10, 2);
62 add_filter('fluent_cart/storage/get_global_storage_drivers', [$this, 'register'], 10, 2);
63 add_filter('fluent_cart/storage/storage_driver_settings_routes', [$this, 'setRoutes'], 10, 2);
64 add_filter('fluent_cart/storage/get_global_storage_driver_status_' . $this->slug, [$this, 'getActiveStatus'], 10, 2);
65
66 $this->registerHooks();
67 }
68
69 public function registerHooks()
70 {
71 //This hook will allow others to register their storage driver with individual storage providers
72 }
73
74 public function handleRedirectData()
75 {
76 return '';
77 }
78
79 public function setRoutes($data, $args)
80 {
81 static::$routes[] = [
82 'path' => $this->slug,
83 'name' => $this->slug,
84 'meta' => [
85 'title' => $this->title
86 ]
87 ];
88 return static::$routes;
89 }
90
91 public function register($data, $args)
92 {
93
94 $data = [
95 "title" => $this->title,
96 "route" => $this->slug,
97 "description" => $this->getDescription(),
98 "logo" => $this->getLogo(),
99 "dark_logo" => $this->getDarkLogo(),
100 "status" => $this->isEnabled(),
101 "brand_color" => $this->brandColor,
102 "has_bucket" => $this->hasBucket(),
103 "instance" => $this
104 ];
105
106 if ($this->hasBucket()) {
107 $data['need_reconfigure'] = $this->needsReconfigure();
108 $data['buckets'] = [];
109 }
110 static::$drivers[$this->slug] = $data;
111 return static::$drivers;
112 }
113
114 public function hasBucket(): bool
115 {
116 return false;
117 }
118
119 public function needsReconfigure(): bool
120 {
121 return false;
122 }
123
124 public function getActiveStatus($data, $args)
125 {
126 $settings = $this->getSettings();
127 return Arr::get($settings, 'is_active') === 'yes' ? true : false;
128 }
129
130 public function getTitle($scope = 'admin')
131 {
132 return $this->title;
133 }
134
135 public function renderDescription()
136 {
137 echo '';
138 }
139
140 public function saveSettings($data)
141 {
142 return $this->updateSettings($data);
143 }
144
145 public function resetSettings()
146 {
147 fluent_cart_update_option($this->driverHandler, []);
148
149 return $this->getSettings();
150 }
151
152 public function updateSettings($data)
153 {
154 $oldSettings = $this->getSettings();
155
156 $settings = $data;
157
158 $settings = apply_filters('fluent_cart/storage/storage_settings_before_update_' . $this->slug, $settings, $oldSettings);
159
160 $settings = Helper::sanitize($settings, $this->fields());
161
162 fluent_cart_update_option($this->driverHandler, $settings);
163
164 return $this->getSettings();
165 }
166
167 public function globalFields($data, $args)
168 {
169 // ignore hidden fields
170 $filtered = Collection::make($this->fields())->filter(function ($item) {
171 return Arr::get($item, 'visible', 'yes') === 'yes';
172 })->toArray();
173
174 $response = [
175 'fields' => $filtered,
176 'settings' => Arr::except($this->getSettings(), $this->hiddenSettingKeys())
177 ];
178
179 if ($this->hasSettingsTemplate()) {
180 $response['template'] = $this->getSettingsTemplate();
181 $response['template_payload'] = $this->getSettingsTemplatePayload($response);
182 }
183
184 $response = apply_filters('fluent_cart/storage/settings_response', $response, $this);
185
186 return apply_filters('fluent_cart/storage/settings_response_' . $this->slug, $response, $this);
187 }
188
189 public function hasSettingsTemplate(): bool
190 {
191 return (bool) $this->getSettingsTemplate();
192 }
193
194 public function getSettingsTemplate(): ?string
195 {
196 return null;
197 }
198
199 public function getSettingsTemplatePayload(array $response = []): array
200 {
201 return [
202 'driver' => $this->slug,
203 'driver_label' => $this->title,
204 'description' => $this->getDescription(),
205 'logo' => $this->getLogo(),
206 'dark_logo' => $this->getDarkLogo(),
207 'settings' => Arr::get($response, 'settings', []),
208 'fields' => Arr::get($response, 'fields', [])
209 ];
210 }
211
212 public function sanitize($data, $fields)
213 {
214 foreach ($fields as $key => $value) {
215 if (isset($data[$key])) {
216 if ('email' === $value['type']) {
217 $data[$key] = sanitize_email($data[$key]);
218 } else {
219 $data[$key] = sanitize_text_field($data[$key]);
220 }
221 }
222 }
223
224 return $data;
225 }
226
227 protected function validate($data, array $rules = [])
228 {
229 $validator = (new Validator())->make($data, $rules);
230 if ($validator->validate()->fails()) {
231 wp_send_json($validator->errors(), 423);
232 }
233 }
234
235 abstract public function getDriverClass(): string;
236 }
237