Skip to content

Commit c3aada0

Browse files
authored
Merge pull request #200 from paynl/feature/PLUG-5137
PLUG-5137 - Load logo's via static and cache
2 parents 7d82c19 + f7c0cc3 commit c3aada0

3 files changed

Lines changed: 91 additions & 14 deletions

File tree

includes/classes/PPMFWC/Gateway/Abstract.php

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,92 @@ public function __construct()
5959
/**
6060
* @return string
6161
*/
62-
public function getIcon()
62+
public function getIcon(): string
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');
6666
}
67-
if (!empty($this->get_option('payment_image'))) {
68-
return $this->get_option('payment_image');
67+
68+
$paymentImage = $this->get_option('payment_image_cached');
69+
70+
if (empty($paymentImage)) {
71+
PPMFWC_Helper_Data::ppmfwc_payLogger('paymentImage empty: ' . print_r($paymentImage, true));
72+
return '';
73+
}
74+
75+
if ($this->saveLogo($paymentImage)) {
76+
return PPMFWC_PLUGIN_URL . 'assets/cache' . $paymentImage;
77+
}
78+
79+
return 'https://static.pay.nl' . $paymentImage;
80+
}
81+
82+
/**
83+
* Save logo
84+
*
85+
* @param string $imagePath
86+
* @return bool
87+
*/
88+
public function saveLogo(string $imagePath): bool
89+
{
90+
$imagePath = ltrim($imagePath, '/');
91+
$path = rtrim(PPMFWC_PLUGIN_PATH . 'assets/cache', '/');
92+
93+
if (file_exists($path . '/' . $imagePath) && (time() - filemtime($path . '/' . $imagePath) < 86400)) {
94+
return true;
95+
}
96+
97+
$imageUrl = 'https://static.pay.nl/' . $imagePath;
98+
return $this->downloadImage($imageUrl, $path, $imagePath);
99+
}
100+
101+
/**
102+
* Download image from url
103+
*
104+
* @param string $url
105+
* @param string $basePath
106+
* @param string $image
107+
* @return bool
108+
*/
109+
public function downloadImage(string $url, string $basePath, string $image): bool
110+
{
111+
$image = ltrim($image, '/');
112+
113+
if (str_contains($image, '..')) {
114+
return false;
115+
}
116+
if (!preg_match('~^[a-zA-Z0-9/_\.\-]+$~', $image)) {
117+
return false;
69118
}
70119

71-
if (!empty($this->getImagePathName())) {
72-
return PPMFWC_PLUGIN_URL . 'assets/logos/payment_method_groups/' . $this->getImagePathName();
120+
// Alleen bekende image-extensies toestaan
121+
if (!preg_match('~\.(svg|png|jpe?g|webp)$~i', $image)) {
122+
return false;
123+
}
124+
125+
// Download (404 e.d. geeft geen warning)
126+
$data = @file_get_contents($url);
127+
if ($data === false) {
128+
return false;
73129
}
74130

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';
131+
132+
$fullPath = rtrim($basePath, '/') . '/' . $image;
133+
134+
$dir = dirname($fullPath);
135+
if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
136+
return false;
137+
}
138+
139+
$writeResult = false;
140+
if (is_writable(dirname($fullPath))) {
141+
$writeResult = file_put_contents($fullPath, $data) !== false;
142+
}
143+
144+
return $writeResult;
77145
}
78146

147+
79148
/**
80149
* @param string $key
81150
* @param mixed $value
@@ -319,7 +388,7 @@ public function init_form_fields()
319388
}
320389

321390
if (
322-
(!$this->get_option('payment_image')) || (strlen($this->get_option('payment_image')) == 0) ||
391+
(!$this->get_option('payment_image_cached')) || (strlen($this->get_option('payment_image_cached')) == 0) ||
323392
(!$this->get_option('min_amount')) || (strlen($this->get_option('min_amount')) == 0) ||
324393
(!$this->get_option('max_amount')) || (strlen($this->get_option('max_amount')) == 0) ||
325394
$this->get_option('description') == 'pay_init'
@@ -342,12 +411,10 @@ public function init_form_fields()
342411
if ((isset($payDefaults))) {
343412
$minAmount = $payDefaults->getMinAmount();
344413
$maxAmount = $payDefaults->getMaxAmount();
345-
346-
$im = str_replace(['/payment_methods/', '.svg'], ['', '.png'], $payDefaults->getImage());
347-
$image = PPMFWC_PLUGIN_URL . 'assets/logos/' . $im;
414+
$icon = $payDefaults->getImage();
348415
}
349416

350-
$this->set_option_default('payment_image', (isset($image)) ? $image : '', true);
417+
$this->set_option_default('payment_image_cached', (isset($icon)) ? $icon : '', true);
351418
$this->set_option_default('min_amount', (isset($minAmount)) ? floatval($minAmount / 100) : '', false);
352419
$this->set_option_default('max_amount', (isset($maxAmount)) ? floatval($maxAmount / 100) : '', false);
353420

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'));

woocommerce-payment-paynl.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
# Load plugin functionality.
2222
require_once(ABSPATH . '/wp-admin/includes/plugin.php');
2323

24+
$apilogFile = '/src/public/apilog_functions.php';
25+
if (file_exists($apilogFile)) {
26+
include_once($apilogFile);
27+
}
28+
29+
30+
2431
define('PPMFWC_WOOCOMMERCE_TEXTDOMAIN', 'woocommerce-paynl-payment-methods');
2532
define('PPMFWC_PLUGIN_URL', plugins_url('/', __FILE__));
2633
define('PPMFWC_PLUGIN_PATH', plugin_dir_path(__FILE__));

0 commit comments

Comments
 (0)