0 ? gmdate( 'Y-m-d\TH:i:s\Z', $timestamp ) : null; } /** * An ISO 8601 string as a unix time, or 0. * * @param mixed $iso ISO string. * @return int */ public static function from_iso( $iso ) { if ( ! is_string( $iso ) || '' === $iso ) { return 0; } $time = strtotime( $iso ); return false === $time ? 0 : (int) $time; } /** * Writes a line to WooCommerce's log (WooCommerce > Status > Logs, source "brickmoat"). * Never includes email addresses. * * @param string $message What happened. * @param string $level error, warning or info. */ public static function log( $message, $level = 'error' ) { try { if ( function_exists( 'wc_get_logger' ) ) { wc_get_logger()->log( $level, (string) $message, array( 'source' => 'brickmoat' ) ); } } catch ( \Throwable $e ) { unset( $e ); // Logging must never break a request. } } /** * Logs an error caught by a fail-open guard. * * @param string $where Where it happened. * @param \Throwable $error The error. */ public static function log_error( $where, $error ) { self::log( sprintf( '%s: %s in %s:%d', $where, $error->getMessage(), basename( $error->getFile() ), $error->getLine() ) ); } /** * A price as plain text, e.g. "$1.50". * * @param float $amount Amount in the store currency. * @return string */ public static function money_text( $amount ) { if ( ! function_exists( 'wc_price' ) ) { return (string) $amount; } return trim( html_entity_decode( wp_strip_all_tags( wc_price( (float) $amount ) ), ENT_QUOTES, 'UTF-8' ) ); } /** * A unix time as a date and time in the site's time zone and format. * * @param int $timestamp Unix time. * @param string $format PHP date format; the site's date and time formats when ''. * @return string */ public static function local_time( $timestamp, $format = '' ) { if ( '' === $format ) { $format = get_option( 'date_format', 'F j, Y' ) . ' ' . get_option( 'time_format', 'H:i' ); } return wp_date( $format, (int) $timestamp ); } /** * Takes a database lock that only one request can hold. Returns false when another request has it. * * @param string $name Lock name. * @return bool */ public static function lock( $name ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- A named lock, not a table query. return '1' === (string) $wpdb->get_var( $wpdb->prepare( 'SELECT GET_LOCK(%s, 0)', $wpdb->prefix . 'brickmoat_' . $name ) ); } /** * Releases a lock taken with lock(). * * @param string $name Lock name. */ public static function unlock( $name ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- A named lock, not a table query. $wpdb->get_var( $wpdb->prepare( 'SELECT RELEASE_LOCK(%s)', $wpdb->prefix . 'brickmoat_' . $name ) ); } }