'B', 1000000 => 'M', 1000 => 'K', ); foreach ( $alphabets as $key => $value ) { if ( $number >= $key ) { return round( $number / $key, $decimal ) . $value; } } } } /** * Truncates string with specified length * * @param string $string Text string. * @param int $length Letters length. * @param string $etc End truncate. * @param bool $break_words Break words or not. * @return string */ function cnvs_str_truncate( $string, $length = 80, $etc = '…', $break_words = false ) { if ( 0 === $length ) { return ''; } if ( function_exists( 'mb_strlen' ) ) { // MultiBite string functions. if ( mb_strlen( $string ) > $length ) { $length -= min( $length, mb_strlen( $etc ) ); if ( ! $break_words ) { $string = preg_replace( '/\s+?(\S+)?$/', '', mb_substr( $string, 0, $length + 1 ) ); } return mb_substr( $string, 0, $length ) . $etc; } } else { // Default string functions. if ( strlen( $string ) > $length ) { $length -= min( $length, strlen( $etc ) ); if ( ! $break_words ) { $string = preg_replace( '/\s+?(\S+)?$/', '', substr( $string, 0, $length + 1 ) ); } return substr( $string, 0, $length ) . $etc; } } return $string; } /** * Set number to Short Form * * @param int $n The number. * @param int $decimal The decimal. */ function cnvs_abridged_number( $n, $decimal = 1 ) { // First strip any formatting. $n = (float) str_replace( ',', '', $n ); // Is this a number? if ( ! is_numeric( $n ) ) { return false; } // Return current count. if ( $n < 1000 ) { return number_format_i18n( $n ); } // Add suffix. $suffix = array( 1000000000 => esc_html__( 'B', 'canvas' ), // Billion. 1000000 => esc_html__( 'M', 'canvas' ), // Million. 1000 => esc_html__( 'K', 'canvas' ), // Thousand. ); foreach ( $suffix as $k => $v ) { if ( $n >= $k ) { return number_format_i18n( $n / $k, $decimal ) . $v; } } } /** * Time ago * * @param string $time The time. * @return string */ function cnvs_timing_ago( $time ) { $periods = array( esc_html__( 'second', 'canvas' ), esc_html__( 'minute', 'canvas' ), esc_html__( 'hour', 'canvas' ), esc_html__( 'day', 'canvas' ), esc_html__( 'week', 'canvas' ), esc_html__( 'month', 'canvas' ), esc_html__( 'year', 'canvas' ), esc_html__( 'decade', 'canvas' ) ); $lengths = array( '60', '60', '24', '7', '4.35', '12', '10' ); $now = time(); $difference = $now - $time; $tense = esc_html__( 'ago', 'canvas' ); $lengths_count = count( $lengths ); for ( $j = 0; $difference >= $lengths[ $j ] && $j < $lengths_count - 1; $j++ ) { $difference /= $lengths[ $j ]; } $difference = round( $difference ); if ( 1 !== $difference ) { $periods[ $j ] .= 's'; } return "$difference $periods[$j] {$tense} "; } /** * Encode data * * @param mixed $content The content. * @param string $secret_key The key. * @return string */ function cnvs_encode_data( $content, $secret_key = 'canvas' ) { $content = wp_json_encode( $content ); return base64_encode( $content ); } /** * Decode data * * @param string $content The content. * @param string $secret_key The key. * @return string */ function cnvs_decode_data( $content, $secret_key = 'canvas' ) { $content = base64_decode( $content ); return json_decode( $content ); } /** * Encrypt data * * @param mixed $content The content. * @param string $secret_key The key. * @return string */ function cnvs_encrypt_data( $content, $secret_key = 'canvas' ) { $content = maybe_serialize( $content ); if ( function_exists( 'openssl_encrypt' ) && function_exists( 'hash' ) ) { $encrypt_method = 'AES-256-CBC'; $key = hash( 'sha256', $secret_key ); $iv = substr( hash( 'sha256', 'secret key' ), 0, 16 ); return base64_encode( openssl_encrypt( $content, $encrypt_method, $key, 0, $iv ) ); } else { return base64_encode( $content ); } } /** * Decrypt data * * @param string $content The content. * @param string $secret_key The key. * @return string */ function cnvs_decrypt_data( $content, $secret_key = 'canvas' ) { if ( function_exists( 'openssl_encrypt' ) && function_exists( 'hash' ) ) { $encrypt_method = 'AES-256-CBC'; $key = hash( 'sha256', $secret_key ); $iv = substr( hash( 'sha256', 'secret key' ), 0, 16 ); $content = openssl_decrypt( base64_decode( $content ), $encrypt_method, $key, 0, $iv ); } else { $content = base64_decode( $content ); } $content = maybe_unserialize( $content ); return $content; } /** * Get the user uuid * * @return string */ function cnvs_get_user_uuid() { if ( getenv( 'HTTP_CLIENT_IP' ) ) { return getenv( 'HTTP_CLIENT_IP' ); } elseif ( getenv( 'HTTP_X_FORWARDED_FOR' ) ) { return getenv( 'HTTP_X_FORWARDED_FOR' ); } elseif ( getenv( 'HTTP_X_FORWARDED' ) ) { return getenv( 'HTTP_X_FORWARDED' ); } elseif ( getenv( 'HTTP_FORWARDED_FOR' ) ) { return getenv( 'HTTP_FORWARDED_FOR' ); } elseif ( getenv( 'HTTP_FORWARDED' ) ) { return getenv( 'HTTP_FORWARDED' ); } elseif ( getenv( 'REMOTE_ADDR' ) ) { return getenv( 'REMOTE_ADDR' ); } return uniqid( 'x', true ); } /** * Retrieve paginated link for archive post pages. * * @param string|array $args Array or string of arguments for generating paginated links for archives. */ function cnvs_paginate_links( $args = '' ) { global $wp_query, $wp_rewrite; // Setting up default values based on the current URL. $pagenum_link = html_entity_decode( get_pagenum_link() ); $url_parts = explode( '?', $pagenum_link ); // Get max pages and current page out of the current query, if available. $total = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1; $current = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1; // Append the format placeholder to the base URL. $pagenum_link = trailingslashit( $url_parts[0] ) . '%_%'; // URL base depends on permalink settings. $format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : ''; $format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%'; $defaults = array( 'base' => $pagenum_link, 'format' => $format, 'total' => $total, 'current' => $current, 'aria_current' => 'page', 'show_all' => false, 'prev_next' => true, 'prev_text' => esc_html__( '« Previous', 'canvas' ), 'next_text' => esc_html__( 'Next »', 'canvas' ), 'end_size' => 1, 'mid_size' => 2, 'type' => 'plain', 'add_args' => array(), 'add_fragment' => '', 'before_page_number' => '', 'after_page_number' => '', 'merge_query_vars' => true, ); $args = wp_parse_args( $args, $defaults ); if ( ! is_array( $args['add_args'] ) ) { $args['add_args'] = array(); } // Merge additional query vars found in the original URL into 'add_args' array. if ( isset( $url_parts[1] ) && $args['merge_query_vars'] ) { // Find the format argument. $format = explode( '?', str_replace( '%_%', $args['format'], $args['base'] ) ); $format_query = isset( $format[1] ) ? $format[1] : ''; wp_parse_str( $format_query, $format_args ); // Find the query args of the requested URL. wp_parse_str( $url_parts[1], $url_query_args ); // Remove the format argument from the array of query arguments, to avoid overwriting custom format. foreach ( $format_args as $format_arg => $format_arg_value ) { unset( $url_query_args[ $format_arg ] ); } $args['add_args'] = array_merge( $args['add_args'], urlencode_deep( $url_query_args ) ); } // Who knows what else people pass in $args. $total = (int) $args['total']; if ( $total < 2 ) { return; } $current = (int) $args['current']; $end_size = (int) $args['end_size']; if ( $end_size < 1 ) { $end_size = 1; } $mid_size = (int) $args['mid_size']; if ( $mid_size < 0 ) { $mid_size = 2; } $add_args = $args['add_args']; $r = ''; $page_links = array(); $dots = false; if ( $args['prev_next'] && $current && 1 < $current ) : $link = str_replace( '%_%', 2 === $current ? '' : $args['format'], $args['base'] ); $link = str_replace( '%#%', $current - 1, $link ); if ( $add_args ) { $link = add_query_arg( $add_args, $link ); } $link .= $args['add_fragment']; /** * Filters the paginated links for the given archive pages. * * @param string $link The paginated link URL. */ $page_links[] = '' . $args['prev_text'] . ''; endif; for ( $n = 1; $n <= $total; $n++ ) : if ( $n === $current ) : $page_links[] = "" . $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number'] . ''; $dots = true; else : if ( $args['show_all'] || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) : $link = str_replace( '%_%', 1 === $n ? '' : $args['format'], $args['base'] ); $link = str_replace( '%#%', $n, $link ); if ( $add_args ) { $link = add_query_arg( $add_args, $link ); } $link .= $args['add_fragment']; /** This filter is documented in wp-includes/general-template.php */ $page_links[] = "" . $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number'] . ''; $dots = true; elseif ( $dots && ! $args['show_all'] ) : $page_links[] = '' . esc_html__( '…', 'canvas' ) . ''; $dots = false; endif; endif; endfor; if ( $args['prev_next'] && $current && $current < $total ) : $link = str_replace( '%_%', $args['format'], $args['base'] ); $link = str_replace( '%#%', $current + 1, $link ); if ( $add_args ) { $link = add_query_arg( $add_args, $link ); } $link .= $args['add_fragment']; /** This filter is documented in wp-includes/general-template.php */ $page_links[] = '' . $args['next_text'] . ''; endif; switch ( $args['type'] ) { case 'array': return $page_links; case 'list': $r .= "