<?php
/*
The file contain theme functions.
*/
$sell_opt_name = 'secretlab';

// Fix float in admin area
add_action('admin_head', 'sell_admin_styles');
function sell_admin_styles() {
    echo '<style>
    #setting-error-the-guard {display:block} 
  </style>';
}
// Page preloader
add_action('wp_head', 'sell_preloader_styles');
function sell_preloader_styles() {
    global $secretlab;
if (isset ($secretlab['pageloader'])) {
    if ($secretlab['pageloader'] == 1) {
        echo '<style>
    /* Pageloader */
        #page-preloader {position: absolute;  left: 0; top:0; right: 0; bottom: 0; height: 100%; width: 100%; cursor: default;  pointer-events: none; text-align: center; vertical-align: middle; background-color: #FFF; z-index: 9999999}
        #page-preloader img {position: absolute; top: calc(50% - 48px); left: calc(50% - 42px); animation-name: rotationY; animation-duration: 1350ms; animation-iteration-count: infinite; animation-timing-function: linear;}
        
        @-ms-keyframes rotationY {
            0% {transform:rotate(0deg) translateZ(0);}
            25% {transform:rotate(15deg) translateZ(0);}
            50% {transform:rotate(0deg) translateZ(0);}
            75% {transform:rotate(-15deg) translateZ(0);}
            100% {transform:rotate(0deg) translateZ(0);}
        }
        @-moz-keyframes rotationY {
            0% {transform:rotate(0deg) translateZ(0);}
            25% {transform:rotate(15deg) translateZ(0);}
            50% {transform:rotate(0deg) translateZ(0);}
            75% {transform:rotate(-15deg) translateZ(0);}
            100% {transform:rotate(0deg) translateZ(0);}
        }
        @-webkit-keyframes rotationY {
            0% {transform:rotate(0deg) translateZ(0);}
            25% {transform:rotate(15deg) translateZ(0);}
            50% {transform:rotate(0deg) translateZ(0);}
            75% {transform:rotate(-15deg) translateZ(0);}
            100% {transform:rotate(0deg) translateZ(0);}
        }
        @keyframes rotationY {
            0% {transform:rotate(0deg) translateZ(0);}
            25% {transform:rotate(15deg) translateZ(0);}
            50% {transform:rotate(0deg) translateZ(0);}
            75% {transform:rotate(-15deg) translateZ(0);}
            100% {transform:rotate(0deg) translateZ(0);}
        }
        
      </style>';
    }
}
}

// Set  type elements animation
add_action('wp_head', 'theguard_style_type');
function theguard_style_type() {
    global $secretlab;
    if (isset ($secretlab['style_type'])) {
        $sl_style_type = $secretlab['style_type'];
        if ($sl_style_type != 0) {
            echo '<style>
				.hiddens {opacity: 0;}
				.visible {opacity: 1;}
				.animated {-webkit-animation-duration: 1s;animation-duration: 1s;-webkit-animation-fill-mode: both;animation-fill-mode: both;}
				.animated.infinite {-webkit-animation-iteration-count: infinite;animation-iteration-count: infinite;}
				.animated.hinge {-webkit-animation-duration: 2s;animation-duration: 2s;}
				.animated.flipOutX,.animated.flipOutY,.animated.bounceIn,.animated.bounceOut {-webkit-animation-duration: .75s;animation-duration: .75s;}
				 </style>';
        }

        if ($sl_style_type == 1) {
            echo '<style>
				@-webkit-keyframes fadeIn {from {opacity: 0;} to {opacity: 1;}}
				@keyframes fadeIn {from {opacity: 0;} to {opacity: 1;}}
				.fadeIn {-webkit-animation-name: fadeIn;animation-name: fadeIn;}
				 </style>';
            echo "<script>
                    jQuery(document).ready(function() {
                     jQuery('.vc_row').addClass('hiddens').viewportChecker({
                            classToAdd: 'visible animated fadeInLeft',
                            offset: 300
                        });
                        jQuery('.wpb_column').addClass('hiddens').viewportChecker({
                            classToAdd: 'visible animated fadeInLeft',
                            offset: 300
                        });
                    });
                </script>";
        }

        if ($sl_style_type == 2) {
            echo '<style>
		@-webkit-keyframes zoomIn {from {opacity: 0;-webkit-transform: scale3d(.3, .3, .3);transform: scale3d(.3, .3, .3);}50% {opacity: 1;}}
		@keyframes zoomIn {from {opacity: 0;-webkit-transform: scale3d(.3, .3, .3);transform: scale3d(.3, .3, .3);}50% {opacity: 1;}}
		.zoomIn {-webkit-animation-name: zoomIn;animation-name: zoomIn;}
		 </style>';
            echo "<script>
                jQuery(document).ready(function() {
                 jQuery('.vc_row').addClass('hiddens').viewportChecker({
                            classToAdd: 'visible animated zoomIn',
                            offset: 300
                        });
                    jQuery('.wpb_column').addClass('hiddens').viewportChecker({
                        classToAdd: 'visible animated zoomIn',
                        offset: 300
                    });
                });
            </script>";
        }

        if ($sl_style_type == 3) {
            echo '<style>
			@-webkit-keyframes slideInUp {from {-webkit-transform: translate3d(0, 100%, 0);transform: translate3d(0, 100%, 0);visibility: visible;}
			to {-webkit-transform: translate3d(0, 0, 0);transform: translate3d(0, 0, 0);}}
			@keyframes slideInUp {from {-webkit-transform: translate3d(0, 100%, 0);transform: translate3d(0, 100%, 0);visibility: visible;}
			to {-webkit-transform: translate3d(0, 0, 0);transform: translate3d(0, 0, 0);}}
			.slideInUp {-webkit-animation-name: slideInUp;animation-name: slideInUp;}
			</style>';
            echo "<script>
                    jQuery(document).ready(function() {
                        jQuery('.vc_row').addClass('hiddens').viewportChecker({
                            classToAdd: 'visible animated slideInUp',
                            offset: 300
                        });
                        jQuery('.wpb_column').addClass('hiddens').viewportChecker({
                            classToAdd: 'visible animated slideInUp',
                            offset: 300
                        });
                    });
                </script>";
        }
    }
}

// Check pagetype for Boxed background setting
function theguard_check_pagetype($theguard_pagetype_prefix) {
    global $theguard_layout, $secretlab;

    
        $props = array ('shop-' => array('boxed-background', 'boxed-background-color'),
            'blog-' => array() );
    
if (isset($secretlab['boxed-background']) or isset($secretlab['boxed-background-color'])) {
    foreach ($props[$theguard_pagetype_prefix] as $prop) {
        if (!isset($theguard_layout[$theguard_pagetype_prefix . $prop])) {
            $theguard_layout[$theguard_pagetype_prefix . $prop] = $secretlab[$prop];
        }
    }
}

    if(isset($secretlab['blog-columns'])) {
        $theguard_layout['blog-columns'] = $secretlab['blog-columns'];
    }

}

// Remove Injected classes, ID's and Page ID's from Navigation <li> items
function theguard_my_css_attributes_filter($var)
{
    return is_array($var) ? array() : '';
}

//The function set global variables for correct work of Metabox Plugin with page setting: sidebar and slider
function sell_set_globals() {

    global $secretlab, $theguard_layout, $post;
	
    $plugins = get_option('active_plugins');
    if (!in_array('revslider/revslider.php', $plugins) && !in_array('LayerSlider/layerslider.php', $plugins)) $secretlab['is_active_slider_plugins'] = false; else $secretlab['is_active_slider_plugins'] = true;		

    if (is_singular()) {
        $theguard_layout = json_decode(get_post_meta($post->ID, 'layout_settings', true), true);
        if (!$theguard_layout) $theguard_layout = $secretlab;
        if ( in_category( 'blog' ) || theguard_post_is_in_descendant_category(get_term_by( 'name', 'blog', 'category' ) )) {
            $secretlab['theguard_page_type'] = 'blog';
            $secretlab['theguard_design_layout'] = 'blog-layout';
            $secretlab['theguard_pagetype_prefix'] = 'blog-';
            theguard_check_pagetype($secretlab['theguard_pagetype_prefix']);
        }
        else if (function_exists('is_woocommerce') && is_woocommerce()) {
            $secretlab['theguard_page_type'] = 'shop';
            $secretlab['theguard_design_layout'] = 'design-layout';
            $secretlab['theguard_pagetype_prefix'] = 'shop-';
            theguard_check_pagetype($secretlab['theguard_pagetype_prefix']);
        }
        else {
            $secretlab['theguard_page_type'] = '';
            $secretlab['theguard_design_layout'] = 'design-layout';
            $secretlab['theguard_pagetype_prefix'] = '';
        }
    }
    else if (function_exists('is_woocommerce') && is_woocommerce()) {
        $theguard_layout = $secretlab;
        $secretlab['theguard_page_type'] = 'shop';
        $secretlab['theguard_design_layout'] = 'design-layout';
        $secretlab['theguard_pagetype_prefix'] = 'shop-';
        theguard_check_pagetype($secretlab['theguard_pagetype_prefix']);
    }
    else if (is_category()) {
        $theguard_layout = $secretlab;
        $category = get_category( get_query_var( 'cat' ) );
        $cat_tree = get_category_parents($category->term_id, FALSE, ':', TRUE);
        $top_cat = explode(':',$cat_tree);
        $parent = $top_cat[0];
        if ($top_cat[0] == 'blog') {
            $secretlab['theguard_page_type'] = 'blog';
            $secretlab['theguard_design_layout'] = 'blog-layout';
            $secretlab['theguard_pagetype_prefix'] = 'blog-';
        }
        else {
            $theguard_layout = $secretlab;
            $secretlab['theguard_page_type'] = '';
            $secretlab['theguard_design_layout'] = 'design-layout';
            $secretlab['theguard_pagetype_prefix'] = '';
        }
    }
    else {
        $theguard_layout = $secretlab;
        $secretlab['theguard_page_type'] = '';
        $secretlab['theguard_design_layout'] = 'design-layout';
        $secretlab['theguard_pagetype_prefix'] = '';
    }

}

// Admin Panel Features Image 55x55 in posts list and pages list
function theguard_get_featured_image($post_ID) {
    $post_thumbnail_id = get_post_thumbnail_id($post_ID);
    if ($post_thumbnail_id) {
        $post_thumbnail_img = wp_get_attachment_image_src($post_thumbnail_id, 'theguard_featured_preview');
        return $post_thumbnail_img[0];
    }
}
// ADD NEW COLUMN
function theguard_columns_head($defaults) {
    $defaults['featured_image'] = esc_html__('Featured Image', 'the-guard');
    return $defaults;
}

// SHOW THE FEATURED IMAGE
function theguard_columns_content($column_name, $post_ID) {
    if ($column_name == 'featured_image') {
        $post_featured_image = theguard_get_featured_image($post_ID);
        if ($post_featured_image) {
            echo '<img src="' . $post_featured_image . '" />';
        } else {
            echo '<img src="'.esc_url(get_template_directory_uri()).'/images/not55.jpg" alt="" />';
        }
    }
}
add_filter('manage_posts_columns', 'theguard_columns_head');
add_action('manage_posts_custom_column', 'theguard_columns_content', 10, 2);

/* Color Schemes - Generate CSS */
function theguard_to_row($arr) {

    $keys = array();
    $values = array();
    foreach ($arr as $key=>$val) {
        if (is_array($val)) {
            foreach ($val as $k=>$v) {
                if (!is_array($v)) {
                    $keys[] = '/\$'.$key.'_'.$k.'\$/';
                    $values[] = $v;
                }
                else {
                    foreach ($v as $k1=>$v1) {
                        $keys[] = '/\$'.$key.'_'.$k.'_'.$k1.'\$/';
                        $values[] = $v1;
                    }
                }
            }
        }
        else {
            $keys[] = '/\$'.$key.'\$/';
            $values[] = $val;
        }
    }
    $result = array();
    $result['keys'] = $keys; ksort($result['keys']);
    $result['values'] = $values; ksort($result['values']);
    
    return $result;
}
add_action ('redux/options/' . $sell_opt_name . '/settings/change', 'theguard_change_action', 10, 3);

/* Generate CSS by Template */
function theguard_change_action($opts) {

    global $wp_filesystem, $secretlab;

    $sl_design_css = isset($secretlab['design-css']) ? $secretlab['design-css'] : 1;
    if ( $sl_design_css == 1 ) {
        $template_css = get_template_directory() . '/css/theme.css';
    }
    if ( $sl_design_css == 2 ) {
        $template_css = get_template_directory() . '/css/themesystems.css';
    }


    if( empty( $wp_filesystem ) ) {
        WP_Filesystem();
    }

    if( $wp_filesystem ) {
        $css = get_template_directory() . '/css/dynamic.css';
        $content = $wp_filesystem->get_contents($template_css);
        $opts = theguard_to_row($opts);
        $content = preg_replace($opts['keys'], $opts['values'], $content);
        $wp_filesystem->put_contents($css, $content, FS_CHMOD_FILE);
    }
}

add_action( 'wp_print_styles', 'theguard_get_dynamic_css' );

function theguard_get_dynamic_css() {
    wp_register_style('theguard_css', esc_url(get_template_directory_uri()) . '/css/dynamic.css', array(), false, 'all');
    wp_enqueue_style('theguard_css');
}

// Add page slug to body class, love this - Credit: Starkers Wordpress Theme
function theguard_add_slug_to_body_class($classes)
{
    global $post;
    if (is_home()) {
        $key = array_search('blog', $classes);
        if ($key > -1) {
            unset($classes[$key]);
        }
    } elseif (is_page()) {
        $classes[] = sanitize_html_class($post->post_name);
    } elseif (is_singular()) {
        $classes[] = sanitize_html_class($post->post_name);
    }

    return $classes;
}

// Remove thumbnail width and height dimensions that prevent fluid images in the_thumbnail
function theguard_remove_thumbnail_dimensions( $html ) {
    $html = preg_replace('/(width|height)=\"\d*\"\s/', "", $html);
    return $html;
}

// Add menu to locations after import
function sell_set_menu_to_locations() {
    $native_menu = wp_get_nav_menu_object('Header Menu');
    $locations = array('theguard_top_menu');
    $menu_set = array();
    foreach ($locations as $location) {
        $menu_set[$location] = $native_menu->term_id;
    }
    if (count($menu_set) > 0) {
        set_theme_mod('nav_menu_locations', $menu_set);
    }

    global $secretlab;
    $sl_design_css = isset($secretlab['design-css']) ? $secretlab['design-css'] : 1;
    if ( $sl_design_css == 1 ) {
        $homepage = get_page_by_title( 'Home Page Security Agency' );
    }
    if ( $sl_design_css == 2 ) {
        $homepage = get_page_by_title( 'Home Page Security Systems' );
    }


	if (($homepage && $homepage->ID)) {
	    update_option('show_on_front', 'page');
        if ($homepage && $homepage->ID) {
            update_option('page_on_front', $homepage->ID); // Front Page
        }		
	}
	
}

add_action('import_end', 'sell_set_menu_to_locations');
// ttt
function secretlab_option( $id, $fallback = false, $param = false ) {
    global $secretlab;
    if (!isset($secretlab)) {
        $s = get_option('secretlab');
    }
    else $s = $secretlab;
    if ( $fallback == false ) $fallback = '';
    $output = ( isset($s[$id]) && $s[$id] !== '' ) ? $s[$id] : $fallback;
    if ( !empty($s[$id]) && $param ) {
        $output = ( isset($s[$id][$param]) && $s[$id][$param] !== '' ) ? $s[$id][$param] : $fallback;
    }
    return $output;
}

//Disable Redux welcome page
add_filter('wp_redirect', 'theguard_disable_redux_welcome');

function theguard_disable_redux_welcome($path) {
    if (preg_match('/redux.about|vc.welcome/', $path)) {
        $path = preg_replace('/redux.about|vc.welcome/', 'secretlab-welcome', $path);
    }

    return $path;
}
/**
 * Return the post URL.
 *
 * @uses get_url_in_content() to get the URL in the post meta (if it exists) or
 * the first link found in the post content.
 *
 * Falls back to the post permalink if no URL is found in the post.
 *

 *
 * @return string The Link format URL.
 */
function theguard_get_link_url() {
    $content = get_the_content();
    $has_url = get_url_in_content( $content );

    return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
}
/**
 * Replaces "[...]" (appended to automatically generated excerpts) with ...
 * and a Continue reading link.
 *sprintf( ' <a href="%1$s" class="more-link">Continue reading  <span class="meta-nav">&rarr;</span></a>',
esc_url( get_permalink( get_the_ID() ) )
)
 * @param string $more Default Read More excerpt link.
 * @return string Filtered Read More excerpt link.
 */
if ( ! function_exists( 'theguard_excerpt_more' ) && ! is_admin() ) :
    function theguard_excerpt_more( $more ) {
        $link = '';
        return $link;
    }
    add_filter( 'excerpt_more', 'theguard_excerpt_more' );
endif;

// Excerpt length
function sell_excerpt_length($use_excerpt = false) {
    global $secretlab;
    global $post;
    if (isset($secretlab['blog-columns'])) {
        $sl_blog_columns = $secretlab['blog-columns'];

		if ($use_excerpt) $excerpt = $post->post_excerpt; else $excerpt = null;
        if ( ! is_single()) {
            if ( $sl_blog_columns == 4) {
                echo theguard_get_excerpt($post, $excerpt, 10);
            } else {
                echo theguard_get_excerpt($post, $excerpt, 30);
            }
        }
    } else {
        if ($use_excerpt) $excerpt = $post->post_excerpt; else $excerpt = null;
        if ( ! is_single()) {
            echo theguard_get_excerpt($post, $excerpt, 30);
        }
    }

}



?>
