<?php
/*
    VC basic replacement
    integrated in core theme
*/

if( ! defined( 'ABSPATH' ) ) exit;

define( 'VC_SHORTCODE_CUSTOM_CSS_FILTER_TAG', 'vc_shortcodes_css_class' );
if ( !function_exists('vc_build_link') ) { function vc_build_link($value) {
    $result = array( 'url' => '', 'title' => '', 'target' => '', 'rel' => '' );
    $params_pairs = explode( '|', $value );
    if ( ! empty( $params_pairs ) ) {
        foreach ( $params_pairs as $pair ) {
            $param = preg_split( '/\:/', $pair );
            if ( ! empty( $param[0] ) && isset( $param[1] ) ) {
                $result[ $param[0] ] = trim( rawurldecode( $param[1] ) );
            }
        }
    }
    return $result;
} }
if ( !function_exists('vc_shortcode_custom_css_class') ) { function vc_shortcode_custom_css_class($param_value, $prefix='') {
    $css_class = preg_match( '/\s*\.([^\{]+)\s*\{\s*([^\}]+)\s*\}\s*/', $param_value ) ? $prefix . preg_replace( '/\s*\.([^\{]+)\s*\{\s*([^\}]+)\s*\}\s*/', '$1', $param_value ) : '';
    if ($css_class != "") \enqueue::add_custom_style($param_value);
    return $css_class;
} }
if ( !function_exists('vc_shortcode_custom_css_has_property') ) { function vc_shortcode_custom_css_has_property($subject, $property, $strict=false) {
    $styles = array();
    $pattern = '/\{([^\}]*?)\}/i';
    preg_match( $pattern, $subject, $styles );
    if ( array_key_exists( 1, $styles ) ) {
        $styles = explode( ';', $styles[1] );
    }
    $new_styles = array();
    foreach ( $styles as $val ) {
        $val = explode( ':', $val );
        if ( is_array( $property ) ) {
            foreach ( $property as $prop ) {
                $pos = strpos( $val[0], $prop );
                $full = ( $strict ) ? ( 0 === $pos && strlen( $val[0] ) === strlen( $prop ) ) : true;
                if ( false !== $pos && $full ) {
                    $new_styles[] = $val;
                }
            }
        } else {
            $pos = strpos( $val[0], $property );
            $full = ( $strict ) ? ( 0 === $pos && strlen( $val[0] ) === strlen( $property ) ) : true;
            if ( false !== $pos && $full ) {
                $new_styles[] = $val;
            }
        }
    }
    return !empty($new_styles);
} }
if ( !function_exists('vc_param_group_parse_atts') ) { function vc_param_group_parse_atts($atts_string) {
    $array = json_decode( urldecode( $atts_string ), true );
    return $array;
} }

if ( !function_exists('vc_map_get_attributes') ) { function vc_map_get_attributes($sc, $atts) {
    if ($atts == "") $atts = array();
    // debug($sc);
    if ($sc == 'vc_column' && !isset($atts['width'])) $atts['width'] = "1/1";
    // else debug($atts);
    return $atts;
} }
if ( !function_exists('wpb_js_remove_wpautop') ) { function wpb_js_remove_wpautop($content, $autop=false) {
    if ($autop) $content = wpautop( preg_replace( '/<\/?p\>/', "\n", $content ) . "\n" );
    return do_shortcode( shortcode_unautop( $content ) );
} }

if ( !function_exists('wpb_translateColumnWidthToSpan') ) { function wpb_translateColumnWidthToSpan( $width ) {
    $output = $width;
    preg_match( '/(\d+)\/(\d+)/', $width, $matches );
    if ( ! empty( $matches ) ) {
        $part_x = (int) $matches[1];
        $part_y = (int) $matches[2];
        if ( $part_x > 0 && $part_y > 0 ) {
            $value = ceil( $part_x / $part_y * 12 );
            if ( $value > 0 && $value <= 12 ) {
                $output = 'vc_col-sm-' . $value;
            }
        }
    }
    if ( preg_match( '/\d+\/5$/', $width ) ) {
        $output = 'vc_col-sm-' . $width;
    }
    return $output;
} }
if ( !function_exists('vc_column_offset_class_merge') ) { function vc_column_offset_class_merge($column_offset, $width) {
    // // Remove offset settings if
    // if ( '1' === vc_settings()->get( 'not_responsive_css' ) ) {
    // 	$column_offset = preg_replace( '/vc_col\-(lg|md|xs)[^\s]*/', '', $column_offset );
    // }
    if ( preg_match( '/vc_col\-sm\-\d+/', $column_offset ) ) {
        return $column_offset;
    }
    return $width . ( empty( $column_offset ) ? '' : ' ' . $column_offset );
} }
if ( !function_exists('wpb_widget_title') ) { function wpb_widget_title($params) {
    return '';
} }


add_filter( 'the_content', function($content = null) {
    if ($content) {
        $s = array(
            '/' . preg_quote( '</div>', '/' ) . '[\s\n\f]*' . preg_quote( '</p>', '/' ) . '/i',
            '/' . preg_quote( '<p>', '/' ) . '[\s\n\f]*' . preg_quote( '<div ', '/' ) . '/i',
            '/' . preg_quote( '<p>', '/' ) . '[\s\n\f]*' . preg_quote( '<section ', '/' ) . '/i',
            '/' . preg_quote( '</section>', '/' ) . '[\s\n\f]*' . preg_quote( '</p>', '/' ) . '/i',
        );
        $r = array( '</div>', '<div ', '<section ', '</section>', );
        $content = preg_replace( $s, $r, $content );
        return $content;
    }
    return null;
}, 11 );

add_action( 'wp_enqueue_scripts', function() {
    wp_process_style('default_style', get_template_directory().'/assets/css/deprecated/vc_basics.css');
    /**
     * deprecated
     * overwritten by plugin styles - can be deleted
     */
    wp_process_style('default_style', get_template_directory().'/assets/css/deprecated/vc_anims.css');
}, 11 );

new vc_shortcode('vc_section');
new vc_shortcode('vc_row');
new vc_shortcode('vc_row_inner');
new vc_shortcode('vc_column');
new vc_shortcode('vc_column_inner');
new vc_shortcode('vc_column_text');
new vc_shortcode('vc_video');
new vc_shortcode('vc_raw_html');
new vc_shortcode('vc_raw_js');
// vc_tta_accordion vc_tta_section

class vc_shortcode {
    public $settings = array( 'base' => '' );
    public function __construct($base) {
        $this->settings['base'] = $base;
        add_shortcode( $base, array($this, 'renderShortcode') );
    }
    public function renderShortcode($atts, $content=null) {
        ob_start();
        $template = $this->settings['base'];
        if ($this->settings['base'] == 'vc_raw_js') $template = 'vc_raw_html';
        include(get_template_directory() . '/vc_templates/'.$template.'.php');
        if ($template == 'vc_raw_html' || $template == 'vc_section') echo $output;
        $return = ob_get_contents();
        ob_end_clean();
        return $return;
    }
    public function getShortcode() { return $this->settings['base']; }
    public function getExtraClass($el_class) { return $el_class; }
    public function getCSSAnimation($css_animation) {
		$output = '';
		if ( '' !== $css_animation && 'none' !== $css_animation ) {
			wp_enqueue_script( 'vc_waypoints' );
			wp_enqueue_style( 'vc_animate-css' );
			$output = ' wpb_animate_when_almost_visible wpb_' . $css_animation . ' ' . $css_animation;
		}

		return $output;
    }
}
