| 1 |
<?php |
| 2 |
/** |
| 3 |
* Payment Icons Library. |
| 4 |
* |
| 5 |
* Provides a centralized registry of built-in payment method SVG icons. |
| 6 |
* |
| 7 |
* @package Merchant |
| 8 |
*/ |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly. |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Payment Icons Library Class. |
| 16 |
* |
| 17 |
* @since 2.2.8 |
| 18 |
*/ |
| 19 |
class Merchant_Payment_Icons_Library { |
| 20 |
|
| 21 |
/** |
| 22 |
* Base directory path for icon SVG files. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private static $icons_dir = ''; |
| 27 |
|
| 28 |
/** |
| 29 |
* Base URI for icon SVG files. |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
private static $icons_uri = ''; |
| 34 |
|
| 35 |
/** |
| 36 |
* Registry of available payment icons. |
| 37 |
* |
| 38 |
* Each entry maps a key to its human-readable label and filename. |
| 39 |
* |
| 40 |
* @var array<string, array{label: string, file: string}> |
| 41 |
*/ |
| 42 |
private static $registry = array( |
| 43 |
'visa' => array( |
| 44 |
'label' => 'Visa', |
| 45 |
'file' => 'visa.svg', |
| 46 |
), |
| 47 |
'mastercard' => array( |
| 48 |
'label' => 'Mastercard', |
| 49 |
'file' => 'mastercard.svg', |
| 50 |
), |
| 51 |
'amex' => array( |
| 52 |
'label' => 'American Express', |
| 53 |
'file' => 'americanexpress.svg', |
| 54 |
), |
| 55 |
'discover' => array( |
| 56 |
'label' => 'Discover', |
| 57 |
'file' => 'discover.svg', |
| 58 |
), |
| 59 |
'paypal' => array( |
| 60 |
'label' => 'PayPal', |
| 61 |
'file' => 'paypal.svg', |
| 62 |
), |
| 63 |
'stripe' => array( |
| 64 |
'label' => 'Stripe', |
| 65 |
'file' => 'stripe.svg', |
| 66 |
), |
| 67 |
'apple-pay' => array( |
| 68 |
'label' => 'Apple Pay', |
| 69 |
'file' => 'applepay.svg', |
| 70 |
), |
| 71 |
'google-pay' => array( |
| 72 |
'label' => 'Google Pay', |
| 73 |
'file' => 'googlepay.svg', |
| 74 |
), |
| 75 |
'klarna' => array( |
| 76 |
'label' => 'Klarna', |
| 77 |
'file' => 'klarna.svg', |
| 78 |
), |
| 79 |
'alipay' => array( |
| 80 |
'label' => 'Alipay', |
| 81 |
'file' => 'alipay.svg', |
| 82 |
), |
| 83 |
'bitcoin' => array( |
| 84 |
'label' => 'Bitcoin', |
| 85 |
'file' => 'bitcoin.svg', |
| 86 |
), |
| 87 |
'amazon-pay' => array( |
| 88 |
'label' => 'Amazon Pay', |
| 89 |
'file' => 'amazonpay.svg', |
| 90 |
), |
| 91 |
'bitpay' => array( |
| 92 |
'label' => 'BitPay', |
| 93 |
'file' => 'bitpay.svg', |
| 94 |
), |
| 95 |
'cash-app' => array( |
| 96 |
'label' => 'Cash App', |
| 97 |
'file' => 'cashapp.svg', |
| 98 |
), |
| 99 |
'ethereum' => array( |
| 100 |
'label' => 'Ethereum', |
| 101 |
'file' => 'ethereum.svg', |
| 102 |
), |
| 103 |
'moneygram' => array( |
| 104 |
'label' => 'MoneyGram', |
| 105 |
'file' => 'moneygram.svg', |
| 106 |
), |
| 107 |
'payoneer' => array( |
| 108 |
'label' => 'Payoneer', |
| 109 |
'file' => 'payoneer.svg', |
| 110 |
), |
| 111 |
'skrill' => array( |
| 112 |
'label' => 'Skrill', |
| 113 |
'file' => 'skrill.svg', |
| 114 |
), |
| 115 |
'square' => array( |
| 116 |
'label' => 'Square', |
| 117 |
'file' => 'square.svg', |
| 118 |
), |
| 119 |
'unionpay' => array( |
| 120 |
'label' => 'UnionPay', |
| 121 |
'file' => 'unionpay.svg', |
| 122 |
), |
| 123 |
'western-union' => array( |
| 124 |
'label' => 'Western Union', |
| 125 |
'file' => 'westernunion.svg', |
| 126 |
), |
| 127 |
); |
| 128 |
|
| 129 |
/** |
| 130 |
* Get the base directory path for icon files. |
| 131 |
* |
| 132 |
* @since 2.2.8 |
| 133 |
* |
| 134 |
* @return string |
| 135 |
*/ |
| 136 |
private static function get_icons_dir() { |
| 137 |
if ( empty( self::$icons_dir ) ) { |
| 138 |
self::$icons_dir = MERCHANT_DIR . 'inc/modules/payment-logos/admin/images/'; |
| 139 |
} |
| 140 |
|
| 141 |
return self::$icons_dir; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Get the base URI for icon files. |
| 146 |
* |
| 147 |
* @since 2.2.8 |
| 148 |
* |
| 149 |
* @return string |
| 150 |
*/ |
| 151 |
private static function get_icons_uri() { |
| 152 |
if ( empty( self::$icons_uri ) ) { |
| 153 |
self::$icons_uri = MERCHANT_URI . 'inc/modules/payment-logos/admin/images/'; |
| 154 |
} |
| 155 |
|
| 156 |
return self::$icons_uri; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Get the URL for a specific icon. |
| 161 |
* |
| 162 |
* @since 2.2.8 |
| 163 |
* |
| 164 |
* @param string $key The icon key (e.g. 'visa', 'mastercard'). |
| 165 |
* |
| 166 |
* @return string The icon URL, or empty string if the key is not found. |
| 167 |
*/ |
| 168 |
public static function get_icon_url( $key ) { |
| 169 |
if ( ! isset( self::$registry[ $key ] ) ) { |
| 170 |
return ''; |
| 171 |
} |
| 172 |
|
| 173 |
return self::get_icons_uri() . self::$registry[ $key ]['file']; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Get the filesystem path for a specific icon. |
| 178 |
* |
| 179 |
* @since 2.2.8 |
| 180 |
* |
| 181 |
* @param string $key The icon key (e.g. 'visa', 'mastercard'). |
| 182 |
* |
| 183 |
* @return string The icon file path, or empty string if the key is not found. |
| 184 |
*/ |
| 185 |
public static function get_icon_path( $key ) { |
| 186 |
if ( ! isset( self::$registry[ $key ] ) ) { |
| 187 |
return ''; |
| 188 |
} |
| 189 |
|
| 190 |
return self::get_icons_dir() . self::$registry[ $key ]['file']; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Get all available icons with their metadata. |
| 195 |
* |
| 196 |
* @since 2.2.8 |
| 197 |
* |
| 198 |
* @return array<string, array{label: string, url: string, path: string}> |
| 199 |
*/ |
| 200 |
public static function get_all_icons() { |
| 201 |
$icons = array(); |
| 202 |
foreach ( self::$registry as $key => $data ) { |
| 203 |
$icons[ $key ] = array( |
| 204 |
'label' => $data['label'], |
| 205 |
'url' => self::get_icon_url( $key ), |
| 206 |
'path' => self::get_icon_path( $key ), |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
return $icons; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Get all available icon keys. |
| 215 |
* |
| 216 |
* @since 2.2.8 |
| 217 |
* |
| 218 |
* @return string[] |
| 219 |
*/ |
| 220 |
public static function get_icon_keys() { |
| 221 |
return array_keys( self::$registry ); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Get the human-readable label for a specific icon. |
| 226 |
* |
| 227 |
* @since 2.2.8 |
| 228 |
* |
| 229 |
* @param string $key The icon key. |
| 230 |
* |
| 231 |
* @return string The label, or empty string if the key is not found. |
| 232 |
*/ |
| 233 |
public static function get_icon_label( $key ) { |
| 234 |
if ( ! isset( self::$registry[ $key ] ) ) { |
| 235 |
return ''; |
| 236 |
} |
| 237 |
|
| 238 |
return self::$registry[ $key ]['label']; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Get icon options formatted for admin choices field. |
| 243 |
* |
| 244 |
* Returns an array of key => URL suitable for the 'choices' field type. |
| 245 |
* |
| 246 |
* @since 2.2.8 |
| 247 |
* |
| 248 |
* @return array<string, string> |
| 249 |
*/ |
| 250 |
public static function get_choices_options() { |
| 251 |
$options = array(); |
| 252 |
foreach ( self::$registry as $key => $data ) { |
| 253 |
$options[ $key ] = self::get_icon_url( $key ); |
| 254 |
} |
| 255 |
|
| 256 |
return $options; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Check if an icon key exists in the library. |
| 261 |
* |
| 262 |
* @since 2.2.8 |
| 263 |
* |
| 264 |
* @param string $key The icon key. |
| 265 |
* |
| 266 |
* @return bool |
| 267 |
*/ |
| 268 |
public static function has_icon( $key ) { |
| 269 |
return isset( self::$registry[ $key ] ); |
| 270 |
} |
| 271 |
} |
| 272 |
|