$length) { $title = mb_substr($string, 0, $length, 'utf-8') . '...'; } else { $title = $string; } return $title; } /** * Add css classes to search form * * @param array $args * * @return string */ public static function search_css_classes($args = array()) { $classes = array(); if (DGWT_WCAS()->settings->get_opt('show_details_box') === 'on') { $classes[] = 'dgwt-wcas-is-detail-box'; } if (DGWT_WCAS()->settings->get_opt('show_submit_button') !== 'on') { $classes[] = 'dgwt-wcas-no-submit'; } if (isset($args['class']) && ! empty($args['class'])) { $classes[] = esc_html($args['class']); } return implode(' ', $classes); } /** * Print magnifier SVG ico */ public static function print_magnifier_ico() { ?> =')) { $short_desc = $product->get_short_description(); } else { $short_desc = $product->post->post_excerpt; } if ( ! empty($short_desc)) { $output = self::strCut(wp_strip_all_tags($short_desc), $length); } else { if (self::compare_wc_version('3.0', '>=')) { $desc = $product->get_description(); } else { $short_desc = $product->post->post_content; } if ( ! empty($desc)) { $output = self::strCut(wp_strip_all_tags($desc), $length); } } } $output = html_entity_decode($output); return $output; } /** * Return HTML for the setting section "How to use?" * * @return string HTML */ public static function how_to_use_html() { $html = ''; ob_start(); include DGWT_WCAS_DIR . 'partials/admin/how-to-use.php'; $html .= ob_get_clean(); return $html; } /** * Minify JS * * @see https://gist.github.com/tovic/d7b310dea3b33e4732c0 * * @param string * * @return string */ public static function minify_js($input) { if (trim($input) === "") { return $input; } return preg_replace( array( // Remove comment(s) '#\s*("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')\s*|\s*\/\*(?!\!|@cc_on)(?>[\s\S]*?\*\/)\s*|\s*(?.*?\*\/)|\/(?!\/)[^\n\r]*?\/(?=[\s.,;]|[gimuy]|$))|\s*([!%&*\(\)\-=+\[\]\{\}|;:,.<>?\/])\s*#s', // Remove the last semicolon '#;+\}#', // Minify object attribute(s) except JSON attribute(s). From `{'foo':'bar'}` to `{foo:'bar'}` '#([\{,])([\'])(\d+|[a-z_]\w*)\2(?=\:)#i', // --ibid. From `foo['bar']` to `foo.bar` '#([\w\)\]])\[([\'"])([a-z_]\w*)\2\]#i', // Replace `true` with `!0` '#(?<=return |[=:,\(\[])true\b#', // Replace `false` with `!1` '#(?<=return |[=:,\(\[])false\b#', // Clean up ... '#\s*(\/\*|\*\/)\s*#' ), array( '$1', '$1$2', '}', '$1$3', '$1.$3', '!0', '!1', '$1' ), $input); } /** * Minify CSS * * @see https://gist.github.com/tovic/d7b310dea3b33e4732c0 * * @param string * * @return string */ public static function minify_css($input) { if (trim($input) === "") { return $input; } // Force white-space(s) in `calc()` if (strpos($input, 'calc(') !== false) { $input = preg_replace_callback('#(?<=[\s:])calc\(\s*(.*?)\s*\)#', function ($matches) { return 'calc(' . preg_replace('#\s+#', "\x1A", $matches[1]) . ')'; }, $input); } return preg_replace( array( // Remove comment(s) '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\/\*(?!\!)(?>.*?\*\/)|^\s*|\s*$#s', // Remove unused white-space(s) '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/))|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~+]|\s*+-(?![0-9\.])|!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si', // Replace `0(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)` with `0` '#(?<=[\s:])(0)(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)#si', // Replace `:0 0 0 0` with `:0` '#:(0\s+0|0\s+0\s+0\s+0)(?=[;\}]|\!important)#i', // Replace `background-position:0` with `background-position:0 0` '#(background-position):0(?=[;\}])#si', // Replace `0.6` with `.6`, but only when preceded by a white-space or `=`, `:`, `,`, `(`, `-` '#(?<=[\s=:,\(\-]|&\#32;)0+\.(\d+)#s', // Minify string value '#(\/\*(?>.*?\*\/))|(?.*?\*\/))|(\burl\()([\'"])([^\s]+?)\3(\))#si', // Minify HEX color code '#(?<=[\s=:,\(]\#)([a-f0-6]+)\1([a-f0-6]+)\2([a-f0-6]+)\3#i', // Replace `(border|outline):none` with `(border|outline):0` '#(?<=[\{;])(border|outline):none(?=[;\}\!])#', // Remove empty selector(s) '#(\/\*(?>.*?\*\/))|(^|[\{\}])(?:[^\s\{\}]+)\{\}#s', '#\x1A#' ), array( '$1', '$1$2$3$4$5$6$7', '$1', ':0', '$1:0 0', '.$1', '$1$3', '$1$2$4$5', '$1$2$3', '$1:0', '$1$2', ' ' ), $input); } /** * Compare WooCommerce function * * @param $version * @param $op * * @return bool */ public static function compare_wc_version($version, $op) { if (function_exists('WC') && (version_compare(WC()->version, $version, $op))) { return true; } return false; } /** * Get rating HTML * * @param $product object WC_Product * * @return string */ public static function get_rating_html($product) { $html = ''; if (self::compare_wc_version('3.0', '>=')) { $html = wc_get_rating_html($product->get_average_rating()); } else { $html = $product->get_rating_html(); } return $html; } /** * Check if is settings page * @return bool */ public static function isSettingsPage() { if (is_admin() && ! empty($_GET['page']) && $_GET['page'] === 'dgwt_wcas_settings') { return true; } return false; } /** * Check if is Freemius checkout page * @return bool */ public static function isCheckoutPage() { if (is_admin() && ! empty($_GET['page']) && $_GET['page'] === 'dgwt_wcas_settings-pricing') { return true; } return false; } /** * Get settings URL * * @return string */ public static function getSettingsUrl() { return admin_url('admin.php?page=dgwt_wcas_settings'); } /** * Get total products * * @return int */ public static function getTotalProducts() { global $wpdb; $sql = "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = 'product' AND post_status = 'publish'"; $total = $wpdb->get_var($sql); return absint($total); } /** * Get all products IDs * @return array */ public static function getProductsForIndex() { global $wpdb; $sql = "SELECT ID FROM $wpdb->posts WHERE post_type = 'product' AND post_status = 'publish' ORDER BY ID ASC"; $ids = $wpdb->get_col($sql); if ( ! is_array($ids) || empty($ids[0]) || ! is_numeric($ids[0])) { $ids = array(); } return $ids; } /** * Logging method. * * @param string $message Log message. * @param string $level Optional. Default 'info'. Possible values: * emergency|alert|critical|error|warning|notice|info|debug. */ public static function log($message, $level = 'info', $source = 'main') { if (defined('DGWT_WCAS_DEBUG') && DGWT_WCAS_DEBUG === true) { if (empty(self::$log)) { self::$log = wc_get_logger(); } self::$log->log($level, $message, array('source' => 'dgwt-wcas-' . $source)); } } /** * Get readable format of memory * * @param int $bytes * * @return string */ public static function getReadableMemorySize($bytes) { $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb'); return @round($bytes / pow(1024, ($i = floor(log($bytes, 1024)))), 2) . ' ' . $unit[$i]; } /** * Get pro icon/label * * @param string $label * @param string $type * @param string $headerSubtitle * * @return string */ public static function getSettingsProLabel($label, $type = 'header', $headerSubtitle = '') { $html = ''; switch ($type) { case 'header': if ( ! empty($headerSubtitle)) { $label = '' . $label . '' . $headerSubtitle . ''; } $html .= '