@@ -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
0 commit comments