1 ? $amount : rtrim(strval($amount), '0')); } /** * @return string|null */ public static function getIp(): ?string { $ip = null; if (isset($_SERVER['REMOTE_ADDR'])) { $ip = wp_unslash(esc_url_raw(wp_unslash($_SERVER['REMOTE_ADDR']))); $ip = rest_is_ip_address($ip); if (false === $ip) { $ip = null; } } return $ip; } /** * @param callable $function * @param string $file * @param int $time * @return object */ public static function cache(callable $function, string $file, int $time = 600): object { global $wp_filesystem; if (file_exists($file) && time() - $time < filemtime($file)) { $content = $wp_filesystem->get_contents($file); } else { if (file_exists($file)) { wp_delete_file($file); } $content = $function(); $wp_filesystem->put_contents($file, $content, FS_CHMOD_FILE); } return (object) compact('file', 'content'); } /** * @param string $name * @return boolean */ public static function deleteCache(string $name): bool { $path = self::getProp('pluginDir') . 'cache/'; $file = $path . $name . '.html'; if (file_exists($file)) { return wp_delete_file($file); } else { return false; } } /** * @param string|null $address * @return string|null */ public static function parseDomain(?string $address): ?string { if (is_null($address)) { return $address; } $parseUrl = wp_parse_url(trim($address)); if (isset($parseUrl['host'])) { $domain = trim($parseUrl['host']); } else { $domain = explode('/', $parseUrl['path'], 2); $domain = array_shift($domain); } return str_replace(['www.'], '', $domain); } /** * @param string $domain * @return boolean */ public static function isValidDomain(string $domain): bool { if (!preg_match('/^[a-zA-Z0-9\-]+(\.[a-zA-Z]{2,})+$/', $domain)) { return false; } return true; } }