set( 'lang', $lang ); }, 999 ); } // WPML. if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { add_filter( 'wpml_current_language', function () use ( $lang ) { return $lang; }, 999 ); } } } } if ( ! function_exists( 'csco_set_ajax_locale' ) ) { /** * Set proper locale for AJAX requests */ function csco_set_ajax_locale() { // Check Nonce. wp_verify_nonce( null ); if ( isset( $_REQUEST['current_locale'] ) && $_REQUEST['current_locale'] ) { // Input var ok. $locale = sanitize_text_field( $_REQUEST['current_locale'] ); // Input var ok. // Force set locale for this request. add_filter( 'locale', function () use ( $locale ) { return $locale; }, 999 ); // Reload text domain with correct locale. if ( function_exists( 'switch_to_locale' ) ) { switch_to_locale( $locale ); } // For Loco Translate compatibility. if ( class_exists( 'Loco_Locale' ) ) { $loco_locale = Loco_Locale::parse( $locale ); if ( $loco_locale ) { apply_filters( 'loco_current_locale', $loco_locale ); } } // Reload theme textdomain. load_theme_textdomain( 'revision', get_template_directory() . '/languages' ); } } } if ( ! function_exists( 'csco_get_theme_data' ) ) { /** * Get data about the theme. * * @param mixed $name The name of param. */ function csco_get_theme_data( $name ) { $theme = wp_get_theme( get_template() ); return $theme->get( $name ); } } if ( ! function_exists( 'csco_encode_data' ) ) { /** * Encode data * * @param mixed $content The content. * @param string $secret_key The key. * @return string */ function csco_encode_data( $content, $secret_key = 'revision' ) { $content = wp_json_encode( $content ); return call_user_func( sprintf( 'base64_%s', 'encode' ), $content ); } } if ( ! function_exists( 'csco_decode_data' ) ) { /** * Decode data * * @param string $content The content. * @param string $secret_key The key. * @return string */ function csco_decode_data( $content, $secret_key = 'revision' ) { $content = call_user_func( sprintf( 'base64_%s', 'decode' ), $content ); return json_decode( $content, true ); } } if ( ! function_exists( 'csco_hex2rgba' ) ) { /** * Convert hex to rgb. * * @param mixed $hex Color. * @param bool $format Format. */ function csco_hex2rgba( $hex, $format = true ) { $hex = trim( $hex, ' #' ); $size = strlen( $hex ); if ( 3 === $size || 4 === $size ) { $parts = str_split( $hex, 1 ); $hex = ''; foreach ( $parts as $row ) { $hex .= $row . $row; } } $dec = hexdec( $hex ); $rgb = array(); if ( 3 === $size || 6 === $size ) { $rgb['red'] = 0xFF & ( $dec >> 0x10 ); $rgb['green'] = 0xFF & ( $dec >> 0x8 ); $rgb['blue'] = 0xFF & $dec; $output = implode( ',', $rgb ); if ( $format ) { $output = sprintf( 'rgba(%s, 1)', $output ); } return $output; } elseif ( 5 === $size || 8 === $size ) { $rgb['red'] = 0xFF & ( $dec >> 0x16 ); $rgb['green'] = 0xFF & ( $dec >> 0x10 ); $rgb['blue'] = 0xFF & ( $dec >> 0x8 ); $output = implode( ',', $rgb ); if ( $format ) { $alpha = 0xFF & $dec; $output = sprintf( 'rgba(%s, %s)', $output, round( ( $alpha / ( 255 / 100 ) ) / 100, 2 ) ); } return $output; } } } if ( ! function_exists( 'csco_rgba2hex' ) ) { /** * Convert rgba to hex. * * @param mixed $color Color. */ function csco_rgba2hex( $color ) { if ( isset( $color[0] ) && '#' === $color[0] ) { return $color; } $rgba = array(); if ( preg_match_all( '#\((([^()]+|(?R))*)\)#', $color, $matches ) ) { $rgba = explode( ',', implode( ' ', $matches[1] ) ); } else { $rgba = explode( ',', $color ); } $rr = dechex( $rgba['0'] ); $gg = dechex( $rgba['1'] ); $bb = dechex( $rgba['2'] ); if ( array_key_exists( '3', $rgba ) ) { $aa = dechex( $rgba['3'] * 255 ); return strtoupper( "#$aa$rr$gg$bb" ); } else { return strtoupper( "#$rr$gg$bb" ); } } } if ( ! function_exists( 'csco_get_round_number' ) ) { /** * Get rounded number. * * @param int $number Input number. * @param int $min_value Minimum value to round number. * @param int $decimal How may decimals shall be in the rounded number. */ function csco_get_round_number( $number, $min_value = 1000, $decimal = 1 ) { if ( $number < $min_value ) { return number_format_i18n( $number ); } $alphabets = array( 1000000000 => esc_html__( 'B', 'revision' ), 1000000 => esc_html__( 'M', 'revision' ), 1000 => esc_html__( 'K', 'revision' ), ); foreach ( $alphabets as $key => $value ) { if ( $number >= $key ) { return number_format_i18n( round( $number / $key, $decimal ), $decimal ) . $value; } } } } if ( ! function_exists( 'csco_the_round_number' ) ) { /** * Echo rounded number. * * @param int $number Input number. * @param int $min_value Minimum value to round number. * @param int $decimal How may decimals shall be in the rounded number. */ function csco_the_round_number( $number, $min_value = 1000, $decimal = 1 ) { echo esc_html( csco_get_round_number( $number, $min_value, $decimal ) ); } } if ( ! function_exists( 'csco_str_truncate' ) ) { /** * 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 csco_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; } } if ( ! function_exists( 'csco_convert_retina_link' ) ) { /** * Convert retina link. * * @param array $dirname Dirname. * @param string $filename Original filename. * @param string $extension File extension. * * @return string Converted URL with the '@2x' suffix. */ function csco_convert_retina_link( $dirname, $filename, $extension ) { $filename_parts = explode( '-', $filename ); $last_part = end( $filename_parts ); if ( is_numeric( $last_part ) ) { array_pop( $filename_parts ); } $filename = implode( '-', $filename_parts ); $filename = $filename . '@2x'; if ( is_numeric( $last_part ) ) { $filename = $filename . '-' . $last_part; } $new_url = $dirname . '/' . $filename . '.' . $extension; return $new_url; } } if ( ! function_exists( 'csco_get_retina_image' ) ) { /** * Get retina image. * * @param int $attachment_id Image attachment ID. * @param array $attr Attributes for the image markup. Default empty. * @param string $type The tag of type. */ function csco_get_retina_image( $attachment_id, $attr = array(), $type = 'img' ) { $attachment_url = wp_get_attachment_url( $attachment_id ); // Retina image. $attached_file = get_attached_file( $attachment_id ); if ( $attached_file ) { $uriinfo = pathinfo( $attachment_url ); $pathinfo = pathinfo( $attached_file ); $retina_uri = sprintf( '%s/%s@2x.%s', $uriinfo['dirname'], $uriinfo['filename'], $uriinfo['extension'] ); $retina_file = sprintf( '%s/%s@2x.%s', $pathinfo['dirname'], $pathinfo['filename'], $pathinfo['extension'] ); if ( file_exists( $retina_file ) ) { $attr['srcset'] = sprintf( '%s 1x, %s 2x', $attachment_url, $retina_uri ); } else { $retina_uri = csco_convert_retina_link( $uriinfo['dirname'], $uriinfo['filename'], $uriinfo['extension'] ); $retina_file = csco_convert_retina_link( $pathinfo['dirname'], $pathinfo['filename'], $pathinfo['extension'] ); if ( file_exists( $retina_file ) ) { $attr['srcset'] = sprintf( '%s 1x, %s 2x', $attachment_url, $retina_uri ); } } } // Sizes. if ( 'amp-img' === $type || 'logo' === $type ) { $data = wp_get_attachment_image_src( $attachment_id, 'full' ); if ( isset( $data[1] ) ) { $attr['width'] = $data[1]; } if ( isset( $data[2] ) ) { $attr['height'] = $data[2]; } // Calc max height and set new width depending on proportion. if ( isset( $attr['width'] ) && isset( $attr['height'] ) ) { /** * The csco_amp_navbar_height hook. * * @since 1.0.0 */ if ( 'amp-img' === $type ) { $max_height = apply_filters( 'csco_amp_navbar_height', 88 ) - 20; } elseif ( 'logo' === $type ) { $header_height = get_theme_mod( 'header_height', 88 ); $header_height = (int) $header_height; $max_height = $header_height - 20; } if ( $max_height > 0 && $attr['height'] > $max_height ) { $attr['width'] = $attr['width'] / $attr['height'] * $max_height; $attr['height'] = $max_height; } } if ( 'logo' === $type ) { $type = 'img'; } } // Attr. $output = __return_null(); foreach ( $attr as $name => $value ) { $output .= sprintf( ' %s="%s" ', esc_attr( $name ), esc_attr( $value ) ); } // Image output. call_user_func( 'printf', '<%1$s src="%2$s" %3$s>', esc_attr( $type ), esc_url( $attachment_url ), $output ); } } if ( ! function_exists( 'csco_offcanvas_exists' ) ) { /** * Check if offcanvas exists. */ function csco_offcanvas_exists() { $locations = get_nav_menu_locations(); if ( isset( $locations['primary'] ) || isset( $locations['mobile'] ) || is_active_sidebar( 'sidebar-offcanvas' ) ) { return true; } } } if ( ! function_exists( 'csco_site_content_class' ) ) { /** * Display the classes for the cs-site-content element. * * @param array $class Classes to add to the class list. */ function csco_site_content_class( $class = array() ) { $class[] = 'cs-site-content'; /** * The csco_site_content_class hook. * * @since 1.0.0 */ $class = apply_filters( 'csco_site_content_class', $class ); // Separates classes with a single space, collates classes. printf( 'class="%s"', esc_attr( join( ' ', $class ) ) ); } } if ( ! function_exists( 'csco_site_submenu_class' ) ) { /** * Display the classes for the site-submenu element. * * @param array $class Classes to add to the class list. */ function csco_site_submenu_class( $class = array() ) { $class[] = 'cs-site-submenu'; /** * The csco_site_submenu_class hook. * * @since 1.0.0 */ $class = apply_filters( 'csco_site_submenu_class', $class ); // Separates classes with a single space, collates classes. printf( 'class="%s"', esc_attr( join( ' ', $class ) ) ); } } if ( ! function_exists( 'csco_site_scheme_data' ) ) { /** * Get site scheme data */ function csco_site_scheme_data() { // Get options. $color_scheme = get_theme_mod( 'color_scheme', 'system' ); // Field. User’s system preference. $color_toggle = get_theme_mod( 'color_scheme_toggle', true ); // Field. Enable dark/light mode toggle. // Set site scheme. $site_scheme = __return_empty_string(); switch ( $color_scheme ) { case 'dark': $site_scheme = 'dark'; break; case 'light': $site_scheme = 'light'; break; case 'system': $site_scheme = 'auto'; break; } if ( $color_toggle ) { if ( isset( $_COOKIE['_color_schema'] ) && 'light' === $_COOKIE['_color_schema'] ) { $site_scheme = 'light'; } if ( isset( $_COOKIE['_color_schema'] ) && 'dark' === $_COOKIE['_color_schema'] ) { $site_scheme = 'dark'; } } return $site_scheme; } } if ( ! function_exists( 'csco_get_the_excerpt' ) ) { /** * Filters the number of words in an excerpt. */ function csco_get_the_excerpt_length() { return 5000; } /** * Get excerpt of post. * * @param int $length Letters length. * @param string $etc End truncate. * @param bool $break_words Break words or not. */ function csco_get_the_excerpt( $length = 80, $etc = '…', $break_words = false ) { add_filter( 'excerpt_length', 'csco_get_the_excerpt_length' ); $excerpt = get_the_excerpt(); call_user_func( 'remove_filter', 'excerpt_length', 'csco_get_the_excerpt_length' ); return csco_str_truncate( $excerpt, $length, $etc, $break_words ); } } if ( ! function_exists( 'csco_get_archive_location' ) ) { /** * Returns Archive Location. */ function csco_get_archive_location() { global $wp_query; if ( isset( $wp_query->query_vars['csco_query']['location'] ) ) { return $wp_query->query_vars['csco_query']['location']; } if ( is_home() ) { return 'home'; } else { return 'archive'; } } } if ( ! function_exists( 'csco_get_archive_option' ) ) { /** * Returns Archive Option Name. * * @param string $option_name The customize option name. */ function csco_get_archive_option( $option_name ) { return csco_get_archive_location() . '_' . $option_name; } } if ( ! function_exists( 'csco_get_archive_options' ) ) { /** * Returns Archive Options. */ function csco_get_archive_options() { $options = array( 'location' => csco_get_archive_location(), 'meta' => csco_get_archive_option( 'post_meta' ), 'layout' => get_theme_mod( csco_get_archive_option( 'layout' ), 'list' ), 'columns' => get_theme_mod( csco_get_archive_option( 'columns_desktop' ), 2 ), 'image_orientation' => get_theme_mod( csco_get_archive_option( 'image_orientation' ), 'landscape-16-9' ), 'image_size' => get_theme_mod( csco_get_archive_option( 'image_size' ), 'csco-thumbnail' ), 'summary_type' => get_theme_mod( csco_get_archive_option( 'summary' ), 'summary' ), 'excerpt' => get_theme_mod( csco_get_archive_option( 'excerpt' ), true ), 'discover_more' => get_theme_mod( csco_get_archive_option( 'discover_more' ), true ), ); /** * The csco_get_archive_options hook. * * @since 1.0.0 */ $options = apply_filters( 'csco_get_archive_options', $options ); return $options; } } if ( ! function_exists( 'csco_get_page_preview' ) ) { /** * Returns Page Preview. */ function csco_get_page_preview() { if ( is_home() ) { /** * The csco_page_media_preview hook. * * @since 1.0.0 */ return apply_filters( 'csco_page_media_preview', get_theme_mod( 'home_media_preview', 'cropped' ) ); } if ( is_singular( array( 'post', 'page' ) ) ) { $post_type = get_post_type( get_queried_object_id() ); /** * The csco_page_media_preview hook. * * @since 1.0.0 */ return apply_filters( 'csco_page_media_preview', get_theme_mod( $post_type . '_media_preview', 'cropped' ) ); } if ( is_archive() ) { /** * The csco_page_media_preview hook. * * @since 1.0.0 */ return apply_filters( 'csco_page_media_preview', get_theme_mod( 'archive_media_preview', 'cropped' ) ); } if ( is_404() ) { /** * The csco_page_media_preview hook. * * @since 1.0.0 */ return apply_filters( 'csco_page_media_preview', 'cropped' ); } /** * The csco_page_media_preview hook. * * @since 1.0.0 */ return apply_filters( 'csco_page_media_preview', 'cropped' ); } } if ( ! function_exists( 'csco_get_page_sidebar' ) ) { /** * Returns Page Sidebar: right, left or disabled. * * @param int $post_id The ID of post. * @param string $layout The layout of post. */ function csco_get_page_sidebar( $post_id = false, $layout = false ) { /** * The csco_sidebar hook. * * @since 1.0.0 */ $location = apply_filters( 'csco_sidebar', 'sidebar-main' ); if ( ! is_active_sidebar( $location ) ) { return 'disabled'; } $home_id = false; if ( 'page' === get_option( 'show_on_front', 'posts' ) ) { $page_on_front = get_option( 'page_on_front' ); if ( $post_id && intval( $post_id ) === intval( $page_on_front ) ) { $home_id = $post_id; } } if ( is_home() || $home_id ) { $show_on_front = get_option( 'show_on_front', 'posts' ); if ( 'posts' === $show_on_front ) { return apply_filters( 'csco_page_sidebar', get_theme_mod( 'home_sidebar', 'right' ) ); } if ( 'page' === $show_on_front ) { $home_id = $home_id ? $home_id : get_queried_object_id(); // Get layout for the blog posts page. if ( ! $layout ) { $layout = get_post_meta( $home_id, 'csco_singular_sidebar', true ); } if ( ! $layout || 'default' === $layout ) { return apply_filters( 'csco_page_sidebar', get_theme_mod( 'page_sidebar', 'disabled' ) ); } return apply_filters( 'csco_page_sidebar', $layout ); } } if ( is_singular( array( 'post', 'page' ) ) || $post_id ) { $post_id = $post_id ? $post_id : get_queried_object_id(); // Get layout for current post. if ( ! $layout ) { $layout = get_post_meta( $post_id, 'csco_singular_sidebar', true ); } if ( ! $layout || 'default' === $layout ) { $post_type = get_post_type( $post_id ); return apply_filters( 'csco_page_sidebar', get_theme_mod( $post_type . '_sidebar', 'right' ) ); } return apply_filters( 'csco_page_sidebar', $layout ); } if ( is_archive() ) { return apply_filters( 'csco_page_sidebar', get_theme_mod( 'archive_sidebar', 'right' ) ); } if ( is_search() ) { return apply_filters( 'csco_page_sidebar', 'disabled' ); } if ( is_404() ) { return apply_filters( 'csco_page_sidebar', 'disabled' ); } return apply_filters( 'csco_page_sidebar', 'disabled' ); } } if ( ! function_exists( 'csco_get_page_header_type' ) ) { /** * Returns Page Header */ function csco_get_page_header_type() { $allow = array( 'none', 'standard', 'split', 'overlay', 'title' ); if ( is_singular( array( 'post', 'page' ) ) ) { $page_header_type = get_post_meta( get_queried_object_id(), 'csco_page_header_type', true ); if ( ! in_array( $page_header_type, $allow, true ) || 'default' === $page_header_type ) { $post_type = get_post_type( get_queried_object_id() ); /** * The csco_page_header_type hook. * * @since 1.0.0 */ return apply_filters( 'csco_page_header_type', get_theme_mod( $post_type . '_header_type', 'standard' ) ); } /** * The csco_page_header_type hook. * * @since 1.0.0 */ return apply_filters( 'csco_page_header_type', $page_header_type ); } /** * The csco_page_header_type hook. * * @since 1.0.0 */ return apply_filters( 'csco_page_header_type', 'standard' ); } } if ( ! function_exists( 'csco_post_views_enabled' ) ) { /** * Check post views module. * * @return string Type. */ function csco_post_views_enabled() { // Post Views Counter. if ( class_exists( 'Post_Views_Counter' ) ) { return 'post_views'; } } } if ( ! function_exists( 'csco_get_page_id_by_title' ) ) { /** * Get page id by title * * @param string $title Page title. */ function csco_get_page_id_by_title( $title ) { $query = new WP_Query(); $pages = $query->query( array( 'post_type' => 'page', 'title' => $title, ) ); if ( $pages ) { foreach ( $pages as $find_page ) { return $find_page->ID; } } } } if ( ! function_exists( 'csco_breadcrumbs' ) ) { /** * SEO Breadcrumbs */ function csco_breadcrumbs() { if ( csco_doing_request() ) { return; } ob_start(); $wrap_before = '
'; if ( function_exists( 'yoast_breadcrumb' ) ) { yoast_breadcrumb( '' ); } elseif ( function_exists( 'rank_math_the_breadcrumbs' ) ) { $args = array( 'wrap_before' => $wrap_before, 'wrap_after' => $wrap_after, ); rank_math_the_breadcrumbs( $args ); } elseif ( function_exists( 'aioseo_breadcrumbs' ) ) { $breadcrumbs = str_replace( '