| 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 |
/** |
| 120 |
* The bucket this driver is configured to use, resolved from server-side |
| 121 |
* settings only. Bucket-backed drivers override this; it is the single |
| 122 |
* source of truth for signing downloads, so a request-supplied bucket must |
| 123 |
* never reach it. |
| 124 |
* |
| 125 |
* @return string '' when the driver has no bucket or none is configured yet |
| 126 |
*/ |
| 127 |
public function getEffectiveBucket(): string |
| 128 |
{ |
| 129 |
return ''; |
| 130 |
} |
| 131 |
|
| 132 |
public function needsReconfigure(): bool |
| 133 |
{ |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
public function getActiveStatus($data, $args) |
| 138 |
{ |
| 139 |
$settings = $this->getSettings(); |
| 140 |
return Arr::get($settings, 'is_active') === 'yes' ? true : false; |
| 141 |
} |
| 142 |
|
| 143 |
public function getTitle($scope = 'admin') |
| 144 |
{ |
| 145 |
return $this->title; |
| 146 |
} |
| 147 |
|
| 148 |
public function renderDescription() |
| 149 |
{ |
| 150 |
echo ''; |
| 151 |
} |
| 152 |
|
| 153 |
public function saveSettings($data) |
| 154 |
{ |
| 155 |
return $this->updateSettings($data); |
| 156 |
} |
| 157 |
|
| 158 |
public function resetSettings() |
| 159 |
{ |
| 160 |
fluent_cart_update_option($this->driverHandler, []); |
| 161 |
|
| 162 |
return $this->getSettings(); |
| 163 |
} |
| 164 |
|
| 165 |
public function updateSettings($data) |
| 166 |
{ |
| 167 |
$oldSettings = $this->getSettings(); |
| 168 |
|
| 169 |
$settings = $data; |
| 170 |
|
| 171 |
$settings = apply_filters('fluent_cart/storage/storage_settings_before_update_' . $this->slug, $settings, $oldSettings); |
| 172 |
|
| 173 |
$settings = Helper::sanitize($settings, $this->fields()); |
| 174 |
|
| 175 |
fluent_cart_update_option($this->driverHandler, $settings); |
| 176 |
|
| 177 |
return $this->getSettings(); |
| 178 |
} |
| 179 |
|
| 180 |
public function globalFields($data, $args) |
| 181 |
{ |
| 182 |
// ignore hidden fields |
| 183 |
$filtered = Collection::make($this->fields())->filter(function ($item) { |
| 184 |
return Arr::get($item, 'visible', 'yes') === 'yes'; |
| 185 |
})->toArray(); |
| 186 |
|
| 187 |
$response = [ |
| 188 |
'fields' => $filtered, |
| 189 |
'settings' => Arr::except($this->getSettings(), $this->hiddenSettingKeys()) |
| 190 |
]; |
| 191 |
|
| 192 |
if ($this->hasSettingsTemplate()) { |
| 193 |
$response['template'] = $this->getSettingsTemplate(); |
| 194 |
$response['template_payload'] = $this->getSettingsTemplatePayload($response); |
| 195 |
} |
| 196 |
|
| 197 |
$response = apply_filters('fluent_cart/storage/settings_response', $response, $this); |
| 198 |
|
| 199 |
return apply_filters('fluent_cart/storage/settings_response_' . $this->slug, $response, $this); |
| 200 |
} |
| 201 |
|
| 202 |
public function hasSettingsTemplate(): bool |
| 203 |
{ |
| 204 |
return (bool) $this->getSettingsTemplate(); |
| 205 |
} |
| 206 |
|
| 207 |
public function getSettingsTemplate(): ?string |
| 208 |
{ |
| 209 |
return null; |
| 210 |
} |
| 211 |
|
| 212 |
public function getSettingsTemplatePayload(array $response = []): array |
| 213 |
{ |
| 214 |
return [ |
| 215 |
'driver' => $this->slug, |
| 216 |
'driver_label' => $this->title, |
| 217 |
'description' => $this->getDescription(), |
| 218 |
'logo' => $this->getLogo(), |
| 219 |
'dark_logo' => $this->getDarkLogo(), |
| 220 |
'settings' => Arr::get($response, 'settings', []), |
| 221 |
'fields' => Arr::get($response, 'fields', []) |
| 222 |
]; |
| 223 |
} |
| 224 |
|
| 225 |
public function sanitize($data, $fields) |
| 226 |
{ |
| 227 |
foreach ($fields as $key => $value) { |
| 228 |
if (isset($data[$key])) { |
| 229 |
if ('email' === $value['type']) { |
| 230 |
$data[$key] = sanitize_email($data[$key]); |
| 231 |
} else { |
| 232 |
$data[$key] = sanitize_text_field($data[$key]); |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
return $data; |
| 238 |
} |
| 239 |
|
| 240 |
protected function validate($data, array $rules = []) |
| 241 |
{ |
| 242 |
$validator = (new Validator())->make($data, $rules); |
| 243 |
if ($validator->validate()->fails()) { |
| 244 |
wp_send_json($validator->errors(), 423); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
abstract public function getDriverClass(): string; |
| 249 |
} |
| 250 |
|