Array_Utility.php
3 years ago
Encryption_Utility.php
3 years ago
Helper.php
3 years ago
Money_Utility.php
3 years ago
String_Utility.php
3 years ago
Helper.php
83 lines
| 1 | <?php |
| 2 | namespace WooCommerce\Square\Utilities; |
| 3 | |
| 4 | defined( 'ABSPATH' ) || exit; |
| 5 | |
| 6 | /** |
| 7 | * WooCommerce Square Helper Class |
| 8 | * |
| 9 | * The purpose of this class is to centralize common utility functions. |
| 10 | * |
| 11 | * @since 2.2.0 |
| 12 | */ |
| 13 | class Helper { |
| 14 | /** |
| 15 | * Format a number with 2 decimal points, using a period for the decimal |
| 16 | * separator and no thousands separator. |
| 17 | * |
| 18 | * Commonly used for payment gateways which require amounts in this format. |
| 19 | * |
| 20 | * @since 3.0.0 |
| 21 | * @param float $number |
| 22 | * @return string |
| 23 | */ |
| 24 | public static function number_format( $number ) { |
| 25 | |
| 26 | return number_format( (float) $number, 2, '.', '' ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Safely get and trim data from $_POST |
| 31 | * |
| 32 | * @since 3.0.0 |
| 33 | * @param string $key array key to get from $_POST array |
| 34 | * @return string value from $_POST or blank string if $_POST[ $key ] is not set |
| 35 | */ |
| 36 | public static function get_post( $key ) { |
| 37 | |
| 38 | if ( isset( $_POST[ $key ] ) ) { |
| 39 | return trim( $_POST[ $key ] ); |
| 40 | } |
| 41 | |
| 42 | return ''; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Add and store a notice. |
| 47 | * |
| 48 | * WC notice functions are not available in the admin |
| 49 | * |
| 50 | * @since 3.0.2 |
| 51 | * @param string $message The text to display in the notice. |
| 52 | * @param string $notice_type The singular name of the notice type - either error, success or notice. [optional] |
| 53 | */ |
| 54 | public static function wc_add_notice( $message, $notice_type = 'success' ) { |
| 55 | |
| 56 | if ( function_exists( 'wc_add_notice' ) ) { |
| 57 | wc_add_notice( $message, $notice_type ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Gets the full URL to the log file for a given $handle |
| 63 | * |
| 64 | * @since 3.0.0 |
| 65 | * @param string $handle log handle |
| 66 | * @return string URL to the WC log file identified by $handle |
| 67 | */ |
| 68 | public static function get_wc_log_file_url( $handle ) { |
| 69 | return admin_url( sprintf( 'admin.php?page=wc-status&tab=logs&log_file=%s-%s-log', $handle, sanitize_file_name( wp_hash( $handle ) ) ) ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Helper method to get the version of the currently installed WooCommerce |
| 74 | * |
| 75 | * @since 3.0.0 |
| 76 | * @return string woocommerce version number or null |
| 77 | */ |
| 78 | public static function get_wc_version() { |
| 79 | |
| 80 | return defined( 'WC_VERSION' ) && WC_VERSION ? WC_VERSION : null; |
| 81 | } |
| 82 | } |
| 83 |