PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.20
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.20
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.20, at app/Modules/StorageDrivers/BaseStorageDriver.php

200 lines 5.2 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 abstract public function getDescription();
30
31 abstract public function getSettings();
32
33 abstract public function fields();
34
35 public function __construct($title, $slug, $brandColor)
36 {
37 $this->title = $title;
38 $this->slug = $slug;
39 $this->brandColor = $brandColor;
40 $this->driverHandler = 'fluent_cart_storage_settings_' . $slug;
41 }
42
43 /**
44 * @param array $data
45 * @return array|true|WP_Error
46 */
47 public function verifyConnectInfo(array $data, $args = [])
48 {
49 return true;
50 }
51
52 public abstract function hiddenSettingKeys(): array;
53
54 public function init()
55 {
56 add_filter('fluent_cart/storage/get_global_storage_settings_' . $this->slug, [$this, 'globalFields'], 10, 2);
57 add_filter('fluent_cart/storage/get_global_storage_drivers', [$this, 'register'], 10, 2);
58 add_filter('fluent_cart/storage/storage_driver_settings_routes', [$this, 'setRoutes'], 10, 2);
59 add_filter('fluent_cart/storage/get_global_storage_driver_status_' . $this->slug, [$this, 'getActiveStatus'], 10, 2);
60
61 $this->registerHooks();
62 }
63
64 public function registerHooks()
65 {
66 //This hook will allow others to register their storage driver with individual storage providers
67 }
68
69 public function handleRedirectData()
70 {
71 return '';
72 }
73
74 public function setRoutes($data, $args)
75 {
76 static::$routes[] = [
77 'path' => $this->slug,
78 'name' => $this->slug,
79 'meta' => [
80 'title' => $this->title
81 ]
82 ];
83 return static::$routes;
84 }
85
86 public function register($data, $args)
87 {
88
89 $data = [
90 "title" => $this->title,
91 "route" => $this->slug,
92 "description" => $this->getDescription(),
93 "logo" => $this->getLogo(),
94 "status" => $this->isEnabled(),
95 "brand_color" => $this->brandColor,
96 "has_bucket" => $this->hasBucket(),
97 "instance" => $this
98 ];
99
100 if ($this->hasBucket()) {
101 $bucketList = Arr::get($this->getSettings(), 'buckets', []);
102
103 $buckets = [];
104
105 if (is_array($bucketList)) {
106 foreach ($bucketList as $bucket) {
107 $buckets[] = array(
108 "label" => $bucket,
109 "value" => $bucket,
110 );
111 }
112 }
113
114 $data['buckets'] = $buckets;
115 }
116 static::$drivers[$this->slug] = $data;
117 return static::$drivers;
118 }
119
120 public function hasBucket(): bool
121 {
122 return false;
123 }
124
125 public function getActiveStatus($data, $args)
126 {
127 $settings = $this->getSettings();
128 return Arr::get($settings, 'is_active') === 'yes' ? true : false;
129 }
130
131 public function getTitle($scope = 'admin')
132 {
133 return $this->title;
134 }
135
136 public function renderDescription()
137 {
138 echo '';
139 }
140
141 public function saveSettings($data)
142 {
143 return $this->updateSettings($data);
144 }
145
146 public function updateSettings($data)
147 {
148 $oldSettings = $this->getSettings();
149
150 $settings = $data;
151
152 $settings = apply_filters('fluent_cart/storage/storage_settings_before_update_' . $this->slug, $settings, $oldSettings);
153
154 $settings = Helper::sanitize($settings, $this->fields());
155
156 fluent_cart_update_option($this->driverHandler, $settings);
157
158 return $this->getSettings();
159 }
160
161 public function globalFields($data, $args)
162 {
163 // ignore hidden fields
164 $filtered = Collection::make($this->fields())->filter(function ($item) {
165 return Arr::get($item, 'visible', 'yes') === 'yes';
166 })->toArray();
167
168 return [
169 'fields' => $filtered,
170 'settings' => Arr::except($this->getSettings(), $this->hiddenSettingKeys())
171 ];
172 }
173
174 public function sanitize($data, $fields)
175 {
176 foreach ($fields as $key => $value) {
177 if (isset($data[$key])) {
178 if ('email' === $value['type']) {
179 $data[$key] = sanitize_email($data[$key]);
180 } else {
181 $data[$key] = sanitize_text_field($data[$key]);
182 }
183 }
184 }
185
186 return $data;
187 }
188
189 protected function validate($data, array $rules = [])
190 {
191 $validator = (new Validator())->make($data, $rules);
192 if ($validator->validate()->fails()) {
193 wp_send_json($validator->errors(), 423);
194 }
195 }
196
197 abstract public function getDriverClass(): string;
198 }
199
200