<?php
if ( ! defined('ABSPATH')) exit;  // if direct access


//add_filter('post_grid_layout_elements','post_grid_layout_elements_star_rating');
//
//function post_grid_layout_elements_star_rating($elements_group){
//
//    $elements_group['star_rating'] = array(
//        'group_title'=>'Star Rating',
//    );
//
//    return $elements_group;
//}
//
//
//add_filter('post_grid_layout_group_star_rating','post_grid_layout_group_star_rating');
//
//function post_grid_layout_group_star_rating($group_items){
//
//
//    $group_items['yasr_visitor_votes'] = array('name' =>__('YASR - visitor votes','post-grid'));
//    $group_items['yasr_overall_rating'] = array('name' =>__('YASR- overall rating','post-grid'));
//
//    return $group_items;
//}


add_filter('post_grid_layout_elements','post_grid_pro_yasr_layout_elements');

function post_grid_pro_yasr_layout_elements($elements_group){


    $elements_group['star_rating']['items']['yasr_visitor_votes'] = array('name' =>__('YASR - visitor votes','post-grid'));
    $elements_group['star_rating']['items']['yasr_overall_rating'] = array('name' =>__('YASR- overall rating','post-grid'));


//    $elements_group['star_rating'] = array(
//        'group_title'=>'Star Rating',
//        'items'=>array(
//            'yasr_visitor_votes'=>array('name' =>__('YASR - visitor votes','post-grid')),
//            'yasr_overall_rating'=>array('name' =>__('YASR- overall rating','post-grid')),
//
//        ),
//    );

    return $elements_group;
}



add_action('post_grid_layout_element_option_yasr_visitor_votes','post_grid_layout_element_option_yasr_visitor_votes');
function post_grid_layout_element_option_yasr_visitor_votes($parameters){

    $settings_tabs_field = new settings_tabs_field();

    $input_name = isset($parameters['input_name']) ? $parameters['input_name'] : '{input_name}';
    $element_data = isset($parameters['element_data']) ? $parameters['element_data'] : array();
    $element_index = isset($parameters['index']) ? $parameters['index'] : '';

    $size = isset($element_data['size']) ? $element_data['size'] : 'small';
    $readonly = isset($element_data['readonly']) ? $element_data['readonly'] : 'no';
    $show_average = isset($element_data['show_average']) ? $element_data['show_average'] : 'small';

    $color = isset($element_data['color']) ? $element_data['color'] : '';
    $font_size = isset($element_data['font_size']) ? $element_data['font_size'] : '';
    $margin = isset($element_data['margin']) ? $element_data['margin'] : '';
    $text_align = isset($element_data['text_align']) ? $element_data['text_align'] : '';

    $css = isset($element_data['css']) ? $element_data['css'] : '';
    $css_hover = isset($element_data['css_hover']) ? $element_data['css_hover'] : '';



    ?>
    <div class="item">
        <div class="element-title header ">
            <span class="remove" onclick="jQuery(this).parent().parent().remove()"><i class="fas fa-times"></i></span>
            <span class="sort"><i class="fas fa-sort"></i></span>

            <span class="expand"><?php echo __('YASR - Visitor votes','post-grid'); ?></span>
        </div>
        <div class="element-options options">

            <?php


            $args = array(
                'id'		=> 'size',
                'css_id'		=> $element_index.'_text',
                'parent' => $input_name.'[yasr_visitor_votes]',
                'title'		=> __('Icon size','post-grid'),
                'details'	=> __('Choose icon size.','post-grid'),
                'type'		=> 'select',
                'value'		=> $text_align,
                'default'		=> 'small',
                'args'		=> array('small'=> __('Small', 'post-grid'),'medium'=> __('Medium', 'post-grid'),'large'=> __('Large', 'post-grid') ),
            );

            $settings_tabs_field->generate_field($args);




            $args = array(
                'id'		=> 'color',
                'css_id'		=> $element_index.'_yasr_visitor_votes',
                'parent' => $input_name.'[yasr_visitor_votes]',
                'title'		=> __('Color','post-grid'),
                'details'	=> __('Title text color.','post-grid'),
                'type'		=> 'colorpicker',
                'value'		=> $color,
                'default'		=> '',
            );

            $settings_tabs_field->generate_field($args);

            $args = array(
                'id'		=> 'font_size',
                'css_id'		=> $element_index.'_font_size',
                'parent' => $input_name.'[yasr_visitor_votes]',
                'title'		=> __('Font size','post-grid'),
                'details'	=> __('Set font size.','post-grid'),
                'type'		=> 'text',
                'value'		=> $font_size,
                'default'		=> '',
                'placeholder'		=> '14px',
            );

            $settings_tabs_field->generate_field($args);



            $args = array(
                'id'		=> 'margin',
                'css_id'		=> $element_index.'_margin',
                'parent' => $input_name.'[yasr_visitor_votes]',
                'title'		=> __('Margin','post-grid'),
                'details'	=> __('Set margin.','post-grid'),
                'type'		=> 'text',
                'value'		=> $margin,
                'default'		=> '',
                'placeholder'		=> '5px 0',
            );

            $settings_tabs_field->generate_field($args);


            $args = array(
                'id'		=> 'text_align',
                'css_id'		=> $element_index.'_text_align',
                'parent' => $input_name.'[yasr_visitor_votes]',
                'title'		=> __('Text align','post-grid'),
                'details'	=> __('Choose text align.','post-grid'),
                'type'		=> 'select',
                'value'		=> $text_align,
                'default'		=> 'left',
                'args'		=> array('left'=> __('Left', 'post-grid'),'right'=> __('Right', 'post-grid'),'center'=> __('Center', 'post-grid') ),
            );

            $settings_tabs_field->generate_field($args);


            $args = array(
                'id'		=> 'css',
                'css_id'		=> $element_index.'_css',
                'parent' => $input_name.'[yasr_visitor_votes]',
                'title'		=> __('Custom CSS','post-grid'),
                'details'	=> __('Set csutom CSS.','post-grid'),
                'type'		=> 'textarea',
                'value'		=> $css,
                'default'		=> '',
                'placeholder'		=> '',
            );

            $settings_tabs_field->generate_field($args);

            $args = array(
                'id'		=> 'css_hover',
                'css_id'		=> $element_index.'_css_hover',
                'parent' => $input_name.'[yasr_visitor_votes]',
                'title'		=> __('Hover CSS','post-grid'),
                'details'	=> __('Set hover custom CSS.','post-grid'),
                'type'		=> 'textarea',
                'value'		=> $css_hover,
                'default'		=> '',
                'placeholder'		=> '',
            );

            $settings_tabs_field->generate_field($args);


            ob_start();
            ?>
            <textarea readonly type="text"  onclick="this.select();">.element_<?php echo esc_attr($element_index); ?>{}</textarea>
            <?php

            $html = ob_get_clean();

            $args = array(
                'id'		=> 'use_css',
                'title'		=> __('Use of CSS','post-grid'),
                'details'	=> __('Use following class selector to add custom CSS for this element.','post-grid'),
                'type'		=> 'custom_html',
                'html'		=> $html,

            );

            $settings_tabs_field->generate_field($args);

            ?>

        </div>
    </div>
    <?php

}



add_action('post_grid_layout_element_yasr_visitor_votes', 'post_grid_layout_element_yasr_visitor_votes');
function post_grid_layout_element_yasr_visitor_votes($args){

    $element  = isset($args['element']) ? $args['element'] : array();
    $elementIndex  = isset($args['index']) ? $args['index'] : '';
    $post_id = isset($args['post_id']) ? $args['post_id'] : '';

    if(empty($post_id)) return;

    $title = get_the_title($post_id);

    $custom_class = isset($element['custom_class']) ? $element['custom_class'] : '';
    $size = isset($element['size']) ? $element['size'] : 'small';


   // if(!empty($acf_value)):

        ?>
        <div class="element element_<?php echo esc_attr($elementIndex); ?> <?php echo esc_attr($custom_class); ?> yasr_visitor_votes ">
            <?php echo do_shortcode("[yasr_visitor_votes size=$size  postid=$post_id]"); ?>
        </div>
        <?php
   // endif;

}



add_action('post_grid_layout_element_css_yasr_visitor_votes', 'post_grid_layout_element_css_yasr_visitor_votes', 10);
function post_grid_layout_element_css_yasr_visitor_votes($args){


    $index = isset($args['index']) ? $args['index'] : '';
    $element = isset($args['element']) ? $args['element'] : array();
    $layout_id = isset($args['layout_id']) ? $args['layout_id'] : '';

    $color = isset($element['color']) ? $element['color'] : '';
    $font_size = isset($element['font_size']) ? $element['font_size'] : '';
    $margin = isset($element['margin']) ? $element['margin'] : '';
    $text_align = isset($element['text_align']) ? $element['text_align'] : 'left';

    $css = isset($element['css']) ? $element['css'] : '';
    $css_hover = isset($element['css_hover']) ? $element['css_hover'] : '';

    ?>
    <style type="text/css">
        .layout-<?php echo esc_attr($layout_id); ?> .element_<?php echo esc_attr($index); ?>{
        <?php if(!empty($color)): ?>
            color: <?php echo esc_attr($color); ?>;
        <?php endif; ?>
        <?php if(!empty($font_size)): ?>
            font-size: <?php echo esc_attr($font_size); ?>;
        <?php endif; ?>

        <?php if(!empty($margin)): ?>
            margin: <?php echo esc_attr($margin); ?>;
        <?php endif; ?>
        <?php if(!empty($text_align)): ?>
            text-align: <?php echo esc_attr($text_align); ?>;
        <?php endif; ?>
        <?php if(!empty($css)): ?>
        <?php echo esc_attr($css); ?>
        <?php endif; ?>
        }
        <?php if(!empty($css_hover)): ?>
        .layout-<?php echo esc_attr($layout_id); ?> .element_<?php echo esc_attr($index); ?>:hover{
        <?php echo esc_attr($css_hover); ?>
        }
        <?php endif; ?>
    </style>
    <?php
}



add_action('post_grid_layout_element_option_yasr_overall_rating','post_grid_layout_element_option_yasr_overall_rating');
function post_grid_layout_element_option_yasr_overall_rating($parameters){

    $settings_tabs_field = new settings_tabs_field();

    $input_name = isset($parameters['input_name']) ? $parameters['input_name'] : '{input_name}';
    $element_data = isset($parameters['element_data']) ? $parameters['element_data'] : array();
    $element_index = isset($parameters['index']) ? $parameters['index'] : '';

    $size = isset($element_data['size']) ? $element_data['size'] : 'small';
    $readonly = isset($element_data['readonly']) ? $element_data['readonly'] : 'no';
    $show_average = isset($element_data['show_average']) ? $element_data['show_average'] : 'small';

    $color = isset($element_data['color']) ? $element_data['color'] : '';
    $font_size = isset($element_data['font_size']) ? $element_data['font_size'] : '';
    $margin = isset($element_data['margin']) ? $element_data['margin'] : '';
    $text_align = isset($element_data['text_align']) ? $element_data['text_align'] : '';

    $css = isset($element_data['css']) ? $element_data['css'] : '';
    $css_hover = isset($element_data['css_hover']) ? $element_data['css_hover'] : '';



    ?>
    <div class="item">
        <div class="element-title header ">
            <span class="remove" onclick="jQuery(this).parent().parent().remove()"><i class="fas fa-times"></i></span>
            <span class="sort"><i class="fas fa-sort"></i></span>

            <span class="expand"><?php echo __('YASR - Visitor votes','post-grid'); ?></span>
        </div>
        <div class="element-options options">

            <?php


            $args = array(
                'id'		=> 'size',
                'css_id'		=> $element_index.'_text',
                'parent' => $input_name.'[yasr_overall_rating]',
                'title'		=> __('Icon size','post-grid'),
                'details'	=> __('Choose icon size.','post-grid'),
                'type'		=> 'select',
                'value'		=> $text_align,
                'default'		=> 'small',
                'args'		=> array('small'=> __('Small', 'post-grid'),'medium'=> __('Medium', 'post-grid'),'large'=> __('Large', 'post-grid') ),
            );

            $settings_tabs_field->generate_field($args);




            $args = array(
                'id'		=> 'color',
                'css_id'		=> $element_index.'_yasr_overall_rating',
                'parent' => $input_name.'[yasr_overall_rating]',
                'title'		=> __('Color','post-grid'),
                'details'	=> __('Title text color.','post-grid'),
                'type'		=> 'colorpicker',
                'value'		=> $color,
                'default'		=> '',
            );

            $settings_tabs_field->generate_field($args);

            $args = array(
                'id'		=> 'font_size',
                'css_id'		=> $element_index.'_font_size',
                'parent' => $input_name.'[yasr_overall_rating]',
                'title'		=> __('Font size','post-grid'),
                'details'	=> __('Set font size.','post-grid'),
                'type'		=> 'text',
                'value'		=> $font_size,
                'default'		=> '',
                'placeholder'		=> '14px',
            );

            $settings_tabs_field->generate_field($args);



            $args = array(
                'id'		=> 'margin',
                'css_id'		=> $element_index.'_margin',
                'parent' => $input_name.'[yasr_overall_rating]',
                'title'		=> __('Margin','post-grid'),
                'details'	=> __('Set margin.','post-grid'),
                'type'		=> 'text',
                'value'		=> $margin,
                'default'		=> '',
                'placeholder'		=> '5px 0',
            );

            $settings_tabs_field->generate_field($args);


            $args = array(
                'id'		=> 'text_align',
                'css_id'		=> $element_index.'_text_align',
                'parent' => $input_name.'[yasr_overall_rating]',
                'title'		=> __('Text align','post-grid'),
                'details'	=> __('Choose text align.','post-grid'),
                'type'		=> 'select',
                'value'		=> $text_align,
                'default'		=> 'left',
                'args'		=> array('left'=> __('Left', 'post-grid'),'right'=> __('Right', 'post-grid'),'center'=> __('Center', 'post-grid') ),
            );

            $settings_tabs_field->generate_field($args);


            $args = array(
                'id'		=> 'css',
                'css_id'		=> $element_index.'_css',
                'parent' => $input_name.'[yasr_overall_rating]',
                'title'		=> __('Custom CSS','post-grid'),
                'details'	=> __('Set csutom CSS.','post-grid'),
                'type'		=> 'textarea',
                'value'		=> $css,
                'default'		=> '',
                'placeholder'		=> '',
            );

            $settings_tabs_field->generate_field($args);

            $args = array(
                'id'		=> 'css_hover',
                'css_id'		=> $element_index.'_css_hover',
                'parent' => $input_name.'[yasr_overall_rating]',
                'title'		=> __('Hover CSS','post-grid'),
                'details'	=> __('Set hover custom CSS.','post-grid'),
                'type'		=> 'textarea',
                'value'		=> $css_hover,
                'default'		=> '',
                'placeholder'		=> '',
            );

            $settings_tabs_field->generate_field($args);


            ob_start();
            ?>
            <textarea readonly type="text"  onclick="this.select();">.element_<?php echo esc_attr($element_index); ?>{}</textarea>
            <?php

            $html = ob_get_clean();

            $args = array(
                'id'		=> 'use_css',
                'title'		=> __('Use of CSS','post-grid'),
                'details'	=> __('Use following class selector to add custom CSS for this element.','post-grid'),
                'type'		=> 'custom_html',
                'html'		=> $html,

            );

            $settings_tabs_field->generate_field($args);

            ?>

        </div>
    </div>
    <?php

}



add_action('post_grid_layout_element_yasr_overall_rating', 'post_grid_layout_element_yasr_overall_rating');
function post_grid_layout_element_yasr_overall_rating($args){

    $element  = isset($args['element']) ? $args['element'] : array();
    $elementIndex  = isset($args['index']) ? $args['index'] : '';
    $post_id = isset($args['post_id']) ? $args['post_id'] : '';

    if(empty($post_id)) return;

    $title = get_the_title($post_id);

    $custom_class = isset($element['custom_class']) ? $element['custom_class'] : '';
    $size = isset($element['size']) ? $element['size'] : 'small';


    // if(!empty($acf_value)):

    ?>
    <div class="element element_<?php echo esc_attr($elementIndex); ?> <?php echo esc_attr($custom_class); ?> yasr_overall_rating ">
        <?php echo do_shortcode("[yasr_overall_rating size=$size  postid=$post_id]"); ?>
    </div>
    <?php
    // endif;

}



add_action('post_grid_layout_element_css_yasr_overall_rating', 'post_grid_layout_element_css_yasr_overall_rating', 10);
function post_grid_layout_element_css_yasr_overall_rating($args){


    $index = isset($args['index']) ? $args['index'] : '';
    $element = isset($args['element']) ? $args['element'] : array();
    $layout_id = isset($args['layout_id']) ? $args['layout_id'] : '';

    $color = isset($element['color']) ? $element['color'] : '';
    $font_size = isset($element['font_size']) ? $element['font_size'] : '';
    $margin = isset($element['margin']) ? $element['margin'] : '';
    $text_align = isset($element['text_align']) ? $element['text_align'] : 'left';

    $css = isset($element['css']) ? $element['css'] : '';
    $css_hover = isset($element['css_hover']) ? $element['css_hover'] : '';

    ?>
    <style type="text/css">
        .layout-<?php echo esc_attr($layout_id); ?> .element_<?php echo esc_attr($index); ?>{
        <?php if(!empty($color)): ?>
            color: <?php echo esc_attr($color); ?>;
        <?php endif; ?>
        <?php if(!empty($font_size)): ?>
            font-size: <?php echo esc_attr($font_size); ?>;
        <?php endif; ?>

        <?php if(!empty($margin)): ?>
            margin: <?php echo esc_attr($margin); ?>;
        <?php endif; ?>
        <?php if(!empty($text_align)): ?>
            text-align: <?php echo esc_attr($text_align); ?>;
        <?php endif; ?>
        <?php if(!empty($css)): ?>
        <?php echo esc_attr($css); ?>
        <?php endif; ?>
        }
        <?php if(!empty($css_hover)): ?>
        .layout-<?php echo esc_attr($layout_id); ?> .element_<?php echo esc_attr($index); ?>:hover{
        <?php echo esc_attr($css_hover); ?>
        }
        <?php endif; ?>
    </style>
    <?php
}

