Skip to content

Commit e0b599a

Browse files
PLUG-5137 - Load logo's via static and cache
1 parent 15f8257 commit e0b599a

2 files changed

Lines changed: 56 additions & 14 deletions

File tree

includes/classes/PPMFWC/Gateway/Abstract.php

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,58 @@ public function getIcon()
6363
{
6464
if (!empty($this->get_option('external_logo')) && wc_is_valid_url($this->get_option('external_logo'))) {
6565
return $this->get_option('external_logo');
66+
}
67+
68+
if ($this->saveLogo($this->get_option('payment_image_cached'))) {
69+
return PPMFWC_PLUGIN_URL . 'assets/cache' . $this->get_option('payment_image_cached');
70+
} else {
71+
return 'https://static.pay.nl' . $this->get_option('payment_image_cached');
6672
}
67-
if (!empty($this->get_option('payment_image'))) {
68-
return $this->get_option('payment_image');
69-
}
73+
}
74+
75+
/**
76+
* Save logo
77+
*
78+
* @param $imagePath
79+
* @return bool
80+
*/
81+
public function saveLogo($imagePath): bool
82+
{
83+
$path = PPMFWC_PLUGIN_PATH . 'assets/cache';
84+
if (file_exists($path . $imagePath) && (time() - filemtime($path . $imagePath) < 86400)) {
85+
return true;
86+
}
87+
$imageUrl = 'https://static.pay.nl/' . $imagePath;
88+
return $this->downloadImage($imageUrl, $path, $imagePath);
89+
}
7090

71-
if (!empty($this->getImagePathName())) {
72-
return PPMFWC_PLUGIN_URL . 'assets/logos/payment_method_groups/' . $this->getImagePathName();
91+
/**
92+
* Download image from url
93+
*
94+
* @param string $url
95+
* @param string $basePath
96+
* @param string $image
97+
* @return bool
98+
*/
99+
public function downloadImage(string $url, string $basePath, string $image): bool
100+
{
101+
$data = file_get_contents($url);
102+
if ($data === false) {
103+
return false;
73104
}
74105

75-
// if any of the above settings are not set use static.pay for checkout images
76-
return 'https://static.pay.nl/payment_profiles/100x100/' . $this->getOptionId() . '.png';
106+
$fullPath = rtrim($basePath, '/') . '/' . ltrim($image, '/');
107+
108+
$dir = dirname($fullPath);
109+
if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
110+
return false;
111+
}
112+
113+
try {
114+
return file_put_contents($fullPath, $data) !== false;
115+
} catch (\Throwable $th) {
116+
return false;
117+
}
77118
}
78119

79120
/**
@@ -319,7 +360,7 @@ public function init_form_fields()
319360
}
320361

321362
if (
322-
(!$this->get_option('payment_image')) || (strlen($this->get_option('payment_image')) == 0) ||
363+
(!$this->get_option('payment_image_cached')) || (strlen($this->get_option('payment_image_cached')) == 0) ||
323364
(!$this->get_option('min_amount')) || (strlen($this->get_option('min_amount')) == 0) ||
324365
(!$this->get_option('max_amount')) || (strlen($this->get_option('max_amount')) == 0) ||
325366
$this->get_option('description') == 'pay_init'
@@ -342,12 +383,10 @@ public function init_form_fields()
342383
if ((isset($payDefaults))) {
343384
$minAmount = $payDefaults->getMinAmount();
344385
$maxAmount = $payDefaults->getMaxAmount();
345-
346-
$im = str_replace(['/payment_methods/', '.svg'], ['', '.png'], $payDefaults->getImage());
347-
$image = PPMFWC_PLUGIN_URL . 'assets/logos/' . $im;
386+
$icon = $payDefaults->getImage();
348387
}
349388

350-
$this->set_option_default('payment_image', (isset($image)) ? $image : '', true);
389+
$this->set_option_default('payment_image_cached', (isset($icon)) ? $icon : '', true);
351390
$this->set_option_default('min_amount', (isset($minAmount)) ? floatval($minAmount / 100) : '', false);
352391
$this->set_option_default('max_amount', (isset($maxAmount)) ? floatval($maxAmount / 100) : '', false);
353392

includes/classes/PPMFWC/Helper/Data.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,11 @@ public static function loadPaymentMethods()
156156

157157
foreach ($paymentOptions as $method)
158158
{
159-
$im = str_replace(['/payment_methods/', '.svg'], ['', '.png'], $method->getImage());
160-
$image = PPMFWC_PLUGIN_URL . 'assets/logos/' . $im;
159+
if (file_exists(PPMFWC_PLUGIN_PATH . 'assets/cache' . $method->getImage())) {
160+
$image = PPMFWC_PLUGIN_URL . 'assets/cache' . $method->getImage();
161+
} else {
162+
$image = 'https://static.pay.nl/' . $method->getImage();
163+
}
161164

162165
$sql = 'INSERT INTO `' . $table_name_options . '` (id,name,image,update_date) VALUES (%d,%s,%s,%s) ON DUPLICATE KEY UPDATE name = %s, image = %s, update_date = %s';
163166
$sql = $wpdb->prepare($sql, $method->getId(), $method->getName(), $image, current_time('mysql'), $method->getName(), $image, current_time('mysql'));

0 commit comments

Comments
 (0)