<?php
/**
 * Created by PhpStorm.
 * User: adiardana
 * Date: 13/03/19
 * Time: 18.31
 */
function z_trim($content, $length = 100){
    if(!$content) return;

    $content = str_replace(array("\r","\n"), '' , strip_tags(strip_shortcodes($content)));

    $final_data = substr($content, 0, $length);
    if(strlen($content) > $length){
        $final_data .= '...';
    }

    return $final_data;
}
function z_console_dump($q){
    if( empty($q) ) return false;

    ob_start();
    ?>
    <script type="text/javascript">
        console.log(<?php echo json_encode( $q );?>);
    </script>
    <?php
    echo ob_get_clean();
}
function z_custom_style($var){
    if(!$var) return;
    $html = '';
    if($var['override_margin']){
        $html .= 'margin-top: '.$var['margin'][0]['top'].'px;';
        $html .= 'margin-right: '.$var['margin'][0]['right'].'px;';
        $html .= 'margin-bottom: '.$var['margin'][0]['bottom'].'px;';
        $html .= 'margin-left: '.$var['margin'][0]['left'].'px;';
    }
    if($var['override_padding']){
        $html .= 'padding-top: '.$var['padding'][0]['top'].'px;';
        $html .= 'padding-right: '.$var['padding'][0]['right'].'px;';
        $html .= 'padding-bottom: '.$var['padding'][0]['bottom'].'px;';
        $html .= 'padding-left: '.$var['padding'][0]['left'].'px;';
    }

    return $html;
}
function z_render_service($data){
    if(!$data) return;
    extract($data);
    $html = '<div class="z-service clearfix '.$color.'">';
        $html .= '<div class="content">';
            $html .= '<div class="icon">';
                if(isset($link['url'])){
                    $html .= '<a href="'.$link['url'].'" target="'.$link['target'].'"><img src="'.$icon.'" alt="'.esc_html($heading).'"/></a>';
                } else {
                    $html .= '<img src="'.$icon.'" alt="'.esc_html($heading).'"/>';
                }
            $html .= '</div>';
                if(isset($link['url'])){
                    $html .= '<a href="'.$link['url'].'" target="'.$link['target'].'"><h2>'.$heading.'</h2></a>';
                } else {
                    $html .= '<h2>'.$heading.'</h2>';
                }
            $html .= '<div class="text"><p>'.$blurb.'</p></div>';
        $html .= '</div>';
    $html .= '</div>';

    return $html;
}
function z_render_lawyer_grid($number_of_post, $order){
    if(!$number_of_post) return;
    $_order = 'DESC';
    $_orderby = 'date';
    if($order == 'oldest'){
        $_order = 'ASC';
    } elseif ($order == 'menu-order'){
        $_order = 'ASC';
        $_orderby = 'menu_order';
    }
    $args = array(
        'post_type' => 'staff',
        'post_status' => 'publish',
        'posts_per_page' => $number_of_post,
        'order' => $_order,
        'orderby' => $_orderby,
    );

    $the_query = new WP_Query( $args );
    $html = '<div class="z-lawyer-grid-wrapper">';
    if ( $the_query->have_posts() ) :
        foreach ($the_query->posts as $post) {
            $html .= z_render_lawyer($post);
        }
    endif;
    $html .= '</div>';
    // Reset Post Data
    wp_reset_postdata();
    return $html;
}
function z_render_lawyer($post){
    if(!$post) return;
    $link = get_permalink($post);
    $wpex_staff_position = get_post_meta($post->ID, 'wpex_staff_position', true);
    $feat_image = THEMEURL.'/assets/images/placeholder.jpg';
    if(has_post_thumbnail($post->ID)){
        $feat_image = wp_get_attachment_image_url(get_post_thumbnail_id($post->ID), 'large');
    }
    $html = '<div class="lawyer-grid equal-height lawyer-'.$post->ID.'">';
        $html .= '<div class="image"><a href="'.$link.'"><img src="'.$feat_image.'"/></a></div>';
        $html .= '<h4><a href="'.$link.'">'.$post->post_title.'</a></h4>';
        if($wpex_staff_position){
            $html .= '<p>'.$wpex_staff_position.'</p>';
        }
    $html .= '</div>';

    return $html;
}
function z_render_slider($slides){
//    var_dump($slides);
    if(!$slides) return;

    $html = "<div class='testimonial-slider' data-slick='{\"adaptiveHeight\": true}'>";

        switch ($slides['source']){
            case 'testimony-category':
                $html .= z_render_testimonial_category_slide($slides);
                break;
            case 'testimony-post':
                $html .= z_render_testimonial_post_slide($slides);
                break;
            case 'manual':
                $html .= z_render_manual_slide($slides);
                break;
        }

    $html .= '</div>';
    return $html;
}
function z_render_testimonial_category_slide($slides){
    if(!$slides) return;
    $testimonials = new WP_Query( array(
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'testimonials_category',
                'field' => 'id',
                'terms' => $slides['testimony_category'],
                'include_children' => false,
                'operator' => 'IN'
            ),
        ),
        'post_type' => 'testimonials',
        'post_status' => 'publish',
        'posts_per_page' => ($slides['number_of_post'] ? $slides['number_of_post'] : 3)
    ) );
    $content = $testimonials->posts;
    wp_reset_postdata();

    $html = '';
    if($content){
        foreach ($content as $item) {
            $html .= z_render_testimonial_slide($item);
        }
    }
    return $html;
}
function z_render_testimonial_post_slide($slides){
    if(!$slides) return;

    $html = '';
    if($slides['testimony_post']){
        foreach ($slides['testimony_post'] as $item) {
            $html .= z_render_testimonial_slide($item);
        }
    }
    return $html;
}
function z_render_manual_slide($slides){
    if(!$slides) return;

    $html = '';
    if($slides['manual_content']){
        foreach ($slides['manual_content'] as $slide) {
            $html .= '<div class="testimonial-item">';
            $html .= '<p>'.$slide['content'].'</p>';
            $html .= '</div>';
        }
    }
    return $html;
}
function z_render_testimonial_slide($item){
    if(!$item) return;

    $rating = round(get_post_meta($item->ID, 'wpex_post_rating', true));
    $author = get_post_meta($item->ID, 'wpex_testimonial_author', true);

    $html = '<div class="testimonial-item">';
        $html .= '<p>'.$item->post_content.'</p>';
        if($rating){
            $html .= '<div class="rating">';
            for ($i = 0; $i<$rating; $i++){
                $html .= '<i class="fa fa-star" aria-hidden="true"></i>';
            }
            $html .= '</div>';
        }
        if($author){
            $html .= '<p class="author">'.$author.'</p>';
        }
    $html .= '</div>';
    return $html;
}
function z_render_posts_grid($posts, $display_type = 'article'){
    if(!$posts) return;
    $html = '<div class="z-posts-grid z-posts-grid-'.$display_type.' clearfix">';
        foreach ($posts as $post) {
            $html .= z_render_post($post, $display_type);
        }
    $html .= '</div>';

    return $html;
}
function z_render_post($post, $display_type = 'article'){
    if(!$post) return;
    $link = get_permalink($post);
    $img = wp_get_attachment_image_url(get_post_thumbnail_id($post), 'medium');
    $video = get_field('video_url', $post->ID);
    $video_id = $video_platform = '';
    if(strpos($video, 'you') !== false){
        $video_platform = 'youtube';
        preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $video, $match);
        $video_id = $match[1];
    }elseif (strpos($video, 'vim') !== false){
        $video_platform = 'vimeo';
        preg_match('%^https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)(?:[?]?.*)$%im', $video, $match);
        $video_id = $match[3];
    }

    $html = '<div class="post-item post-item-'.$display_type.' equal-height">';
        $html .= '<div class="image" style="background-image: url('.$img.');">';
        if($display_type == 'video'){
            $html .= '<a href="javascript:void(0);" data-platform="'.$video_platform.'" data-id="'.$video_id.'" title="'.$post->post_title.'"><i class="fa fa-play" aria-hidden="true"></i></a>';
        } else {
            $html .= '<a href="'.$link.'" title="'.$post->post_title.'"></a>';
        }
        $html .= '</div>';
        $html .= '<h2><a href="'.$link.'" title="'.$post->post_title.'">'.$post->post_title.'</a></h2>';
        $html .= '<p>'.z_trim(strip_tags($post->post_content), 150).'</p>';
    $html .= '</div>';

    return $html;
}









function z_render_slider_new($slides){
    if(!$slides) return;

    $html = "<div class='testimonial-slider' data-slick='{\"adaptiveHeight\": true}'>";

        switch ($slides['source']){
            case 'testimony-category':
                $html .= z_render_testimonial_category_slide_new($slides);
                break;
            case 'testimony-post':
                $html .= z_render_testimonial_post_slide_new($slides);
                break;
            case 'manual':
                $html .= z_render_manual_slide_new($slides);
                break;
        }

    $html .= '</div>';
    return $html;
}
function z_render_testimonial_category_slide_new($slides){
    if(!$slides) return;
    $testimonials = new WP_Query( array(
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'testimonials_category',
                'field' => 'id',
                'terms' => $slides['testimony_category'],
                'include_children' => false,
                'operator' => 'IN'
            ),
        ),
        'post_type' => 'testimonials',
        'post_status' => 'publish',
        'posts_per_page' => ($slides['number_of_post'] ? $slides['number_of_post'] : 3)
    ) );
    $content = $testimonials->posts;
    wp_reset_postdata();

    $html = '';
    if($content){
        foreach ($content as $item) {
            $html .= z_render_testimonial_slide_new($item);
        }
    }
    return $html;
}
function z_render_testimonial_post_slide_new($slides){
    if(!$slides) return;

    $html = '';
    if($slides['testimony_post']){
        foreach ($slides['testimony_post'] as $item) {
            $html .= z_render_testimonial_slide_new($item);
        }
    }
    return $html;
}
function z_render_manual_slide_new($slides){
    if(!$slides) return;

    $html = '';
    if($slides['manual_content']){
        foreach ($slides['manual_content'] as $slide) {
            $html .= '<div class="testimonial-item">';
            $html .= '<p>'.$slide['content'].'</p>';
            $html .= '</div>';
        }
    }
    return $html;
}
function z_render_testimonial_slide_new($item){
    if(!$item) return;

    $rating = round(get_post_meta($item->ID, 'wpex_post_rating', true));
    $author = get_post_meta($item->ID, 'wpex_testimonial_author', true);

    $layout = 'left';
    $background_color = '#f00';
    $image = '';
    $heading = 'heading';
    $heading_icon = '';
    $slider_title = 'title';

    $contenthtml = '<div class="testimonial-item">';
        $contenthtml .= '<p>'.$item->post_content.'</p>';
        if($rating){
            $contenthtml .= '<div class="rating">';
            for ($i = 0; $i<$rating; $i++){
                $contenthtml .= '<i class="fa fa-star" aria-hidden="true"></i>';
            }
            $contenthtml .= '</div>';
        }
        if($author){
            $contenthtml .= '<p class="author">'.$author.'</p>';
        }
    $contenthtml .= '</div>';

    $html = '';

    if($layout == 'left'){
        $html .= '<div class="image"><img src="'.$image.'"/></div>';
    }

    $html .= '<div class="content">';
        $html .= '<h2>'.$heading_icon.$heading.'</h2>';
        $html .= '<h3>'.$slider_title.'</h3>';
        $html .= $contenthtml;
    $html .= '</div>';

    if($layout == 'right'){
        $html .= '<div class="image"><img src="'.$image.'"/></div>';
    }    

    return $html;
}