| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | if ( ! defined( 'ABSPATH' ) ) { |
| 15: | exit; |
| 16: | } |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | function epl_sanitize_amount( $amount ) { |
| 28: | $thousands_sep = epl_get_thousands_separator(); |
| 29: | $decimal_sep = epl_get_decimal_separator(); |
| 30: | |
| 31: | |
| 32: | if ( ',' === $decimal_sep && false !== ( strpos( $amount, $decimal_sep ) ) ) { |
| 33: | if ( '.' === $thousands_sep && false !== ( strpos( $amount, $thousands_sep ) ) ) { |
| 34: | $amount = str_replace( $thousands_sep, '', $amount ); |
| 35: | } elseif ( empty( $thousands_sep ) && false !== ( strpos( $amount, '.' ) ) ) { |
| 36: | $amount = str_replace( '.', '', $amount ); |
| 37: | } |
| 38: | |
| 39: | $amount = str_replace( $decimal_sep, '.', $amount ); |
| 40: | } elseif ( ',' === $thousands_sep && false !== ( strpos( $amount, $thousands_sep ) ) ) { |
| 41: | $amount = str_replace( $thousands_sep, '', $amount ); |
| 42: | } |
| 43: | |
| 44: | $decimals = apply_filters( 'epl_sanitize_amount_decimals', 2, $amount ); |
| 45: | $amount = number_format( $amount, $decimals, '.', '' ); |
| 46: | |
| 47: | return apply_filters( 'epl_sanitize_amount', $amount ); |
| 48: | } |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | function epl_format_amount( $amount, $decimals = false, $preserve_format = false ) { |
| 64: | $thousands_sep = epl_get_thousands_separator(); |
| 65: | $decimal_sep = epl_get_decimal_separator(); |
| 66: | $formatted = ''; |
| 67: | |
| 68: | if ( empty( $amount ) ) { |
| 69: | return $amount; |
| 70: | } |
| 71: | |
| 72: | |
| 73: | $sep_found = strpos( $amount, $decimal_sep ); |
| 74: | if ( ',' === $decimal_sep && false !== $sep_found ) { |
| 75: | $whole = substr( $amount, 0, $sep_found ); |
| 76: | $part = substr( $amount, $sep_found + 1, ( strlen( $amount ) - 1 ) ); |
| 77: | $amount = $whole . '.' . $part; |
| 78: | } |
| 79: | |
| 80: | |
| 81: | if ( ',' === $thousands_sep && false !== ( strpos( $amount, $thousands_sep ) ) ) { |
| 82: | $amount = str_replace( ',', '', $amount ); |
| 83: | } |
| 84: | |
| 85: | if ( empty( $amount ) ) { |
| 86: | $amount = 0; |
| 87: | } |
| 88: | if ( $preserve_format && false === ( strpos( $amount, $decimal_sep ) ) ) { |
| 89: | $decimals = false; |
| 90: | } |
| 91: | |
| 92: | $decimals = apply_filters( 'epl_format_amount_decimals', $decimals ? 2 : 0, $amount ); |
| 93: | if ( is_numeric( $amount ) ) { |
| 94: | $formatted = number_format( $amount, $decimals, $decimal_sep, $thousands_sep ); |
| 95: | } |
| 96: | |
| 97: | return apply_filters( 'epl_format_amount', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep ); |
| 98: | } |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | function epl_currency_filter( $price ) { |
| 110: | $currency = epl_get_currency(); |
| 111: | $currency_position = epl_get_currency_position(); |
| 112: | |
| 113: | $position = ( isset( $currency_position ) && ! empty( $currency_position ) ) ? $currency_position : 'before'; |
| 114: | |
| 115: | $negative = floatval( $price ) < 0; |
| 116: | |
| 117: | if ( $negative ) { |
| 118: | $price = substr( $price, 1 ); |
| 119: | } |
| 120: | |
| 121: | if ( 'before' === $position ) : |
| 122: | switch ( $currency ) : |
| 123: | case 'GBP': |
| 124: | $formatted = '£' . $price; |
| 125: | break; |
| 126: | case 'BRL': |
| 127: | $formatted = 'R$' . $price; |
| 128: | break; |
| 129: | case 'EUR': |
| 130: | $formatted = '€' . $price; |
| 131: | break; |
| 132: | case 'USD': |
| 133: | case 'AUD': |
| 134: | case 'NZD': |
| 135: | case 'CAD': |
| 136: | case 'HKD': |
| 137: | case 'MXN': |
| 138: | case 'SGD': |
| 139: | $formatted = '$' . $price; |
| 140: | break; |
| 141: | case 'JPY': |
| 142: | $formatted = '¥' . $price; |
| 143: | break; |
| 144: | case 'ILS': |
| 145: | $formatted = '₪' . $price; |
| 146: | break; |
| 147: | case 'THB': |
| 148: | $formatted = '฿' . $price; |
| 149: | break; |
| 150: | case 'INR': |
| 151: | $formatted = '₹' . $price; |
| 152: | break; |
| 153: | case 'TRY': |
| 154: | $formatted = '₺' . $price; |
| 155: | break; |
| 156: | case 'RIAL': |
| 157: | $formatted = '﷼' . $price; |
| 158: | break; |
| 159: | case 'UAH': |
| 160: | $formatted = '₴' . $price; |
| 161: | break; |
| 162: | case 'VND': |
| 163: | $formatted = '₫' . $price; |
| 164: | break; |
| 165: | case 'ZAR': |
| 166: | $formatted = 'R' . $price; |
| 167: | break; |
| 168: | case 'CZK': |
| 169: | $formatted = 'Kč' . $price; |
| 170: | break; |
| 171: | default: |
| 172: | $formatted = $currency . ' ' . $price; |
| 173: | break; |
| 174: | endswitch; |
| 175: | $formatted = apply_filters( 'epl_' . strtolower( $currency ) . '_currency_filter_before', $formatted, $currency, $price ); |
| 176: | else : |
| 177: | switch ( $currency ) : |
| 178: | case 'GBP': |
| 179: | $formatted = $price . '£'; |
| 180: | break; |
| 181: | case 'BRL': |
| 182: | $formatted = $price . 'R$'; |
| 183: | break; |
| 184: | case 'EUR': |
| 185: | $formatted = $price . '€'; |
| 186: | break; |
| 187: | case 'USD': |
| 188: | case 'AUD': |
| 189: | case 'NZD': |
| 190: | case 'CAD': |
| 191: | case 'HKD': |
| 192: | case 'MXN': |
| 193: | case 'SGD': |
| 194: | $formatted = $price . '$'; |
| 195: | break; |
| 196: | case 'JPY': |
| 197: | $formatted = $price . '¥'; |
| 198: | break; |
| 199: | case 'ILS': |
| 200: | $formatted = $price . '₪'; |
| 201: | break; |
| 202: | case 'THB': |
| 203: | $formatted = $price . '฿'; |
| 204: | break; |
| 205: | case 'INR': |
| 206: | $formatted = $price . '₹'; |
| 207: | break; |
| 208: | case 'TRY': |
| 209: | $formatted = $price . '₺'; |
| 210: | break; |
| 211: | case 'RIAL': |
| 212: | $formatted = $price . '﷼'; |
| 213: | break; |
| 214: | case 'UAH': |
| 215: | $formatted = $price . '₴'; |
| 216: | break; |
| 217: | case 'VND': |
| 218: | $formatted = $price . '₫'; |
| 219: | break; |
| 220: | case 'ZAR': |
| 221: | $formatted = $price . 'R'; |
| 222: | break; |
| 223: | case 'CZK': |
| 224: | $formatted = $price . ' Kč'; |
| 225: | break; |
| 226: | default: |
| 227: | $formatted = $price . ' ' . $currency; |
| 228: | break; |
| 229: | endswitch; |
| 230: | $formatted = apply_filters( 'epl_' . strtolower( $currency ) . '_currency_filter_after', $formatted, $currency, $price ); |
| 231: | endif; |
| 232: | |
| 233: | if ( $negative ) { |
| 234: | |
| 235: | $formatted = '-' . $formatted; |
| 236: | } |
| 237: | |
| 238: | return $formatted; |
| 239: | |
| 240: | } |
| 241: | |
| 242: | |
| 243: | |
| 244: | |
| 245: | |
| 246: | |
| 247: | |
| 248: | |
| 249: | function epl_currency_decimal_filter( $decimals = 2 ) { |
| 250: | $currency = epl_get_currency(); |
| 251: | |
| 252: | switch ( $currency ) { |
| 253: | case 'RIAL': |
| 254: | case 'JPY': |
| 255: | case 'TWD': |
| 256: | $decimals = 0; |
| 257: | break; |
| 258: | } |
| 259: | |
| 260: | return $decimals; |
| 261: | } |
| 262: | add_filter( 'epl_sanitize_amount_decimals', 'epl_currency_decimal_filter' ); |
| 263: | add_filter( 'epl_format_amount_decimals', 'epl_currency_decimal_filter' ); |
| 264: | |
| 265: | |
| 266: | |
| 267: | |
| 268: | |
| 269: | |
| 270: | |
| 271: | |
| 272: | function epl_format_the_excerpt( $content ) { |
| 273: | $content = '<div class="epl-excerpt-content">' . wpautop( $content ) . '</div>'; |
| 274: | return $content; |
| 275: | } |
| 276: | add_filter( 'epl_the_excerpt', 'epl_format_the_excerpt' ); |
| 277: | |
| 278: | |
| 279: | |
| 280: | |
| 281: | |
| 282: | |
| 283: | |
| 284: | |
| 285: | |
| 286: | |
| 287: | function epl_sanitize_key( $key ) { |
| 288: | |
| 289: | $raw_key = $key; |
| 290: | $key = preg_replace( '/[^a-zA-Z0-9_\-\.\:\/]/', '', $key ); |
| 291: | return apply_filters( 'epl_sanitize_key', $key, $raw_key ); |
| 292: | } |
| 293: | |