<?php 
if(!function_exists('flat_get_post_page_content')){
    function flat_get_post_page_content( $slug ) {
        $content_post = get_posts(array(
            'name' => $slug,
            'posts_per_page' => 1,
            'post_type' => 'elementor_library',
            'post_status' => 'publish'
        ));
        if (array_key_exists(0, $content_post) == true) {
            $id = $content_post[0]->ID;
            return $id;
        }
    }
}

if(!function_exists('tf_get_template_widget')){
    function tf_get_template_widget($template_name, $args = null, $return = false){
        $template_file = $template_name . '.php';
        $default_folder = plugin_dir_path(__FILE__) . 'templates/';
        $theme_folder = apply_filters('tf_templates_folder', dirname(plugin_basename(__FILE__)));
        $template = locate_template($theme_folder . '/' . $template_file);
        if (!$template) {
            $template = $default_folder . $template_file;
        }
        if ($args && is_array($args)) {
            extract($args);
        }
        if ($return) {
            ob_start();
        }
        if (file_exists($template)) {
            include $template;
        }
        if ($return) {
            return ob_get_clean();
        }
        return null;
    }
}


function themesflat_meta_box_services(){
    add_meta_box( 'meta_box_services', 'Link read more', 'themesflat_meta_box_services_ouput', 'services' );
}

function themesflat_meta_box_services_ouput ( $post ) {
    $link = get_post_meta($post->ID,'_service_btn_link', true);
    $button_text = get_post_meta($post->ID,'_service_btn_text',true);

    echo '<label for="service_btn_text">Button Text</label>';
    echo '<input type="text" id="service_btn_text" name="service_btn_text" value="' . esc_attr($button_text) . '" /><br>';
    echo '<label for="service_btn_link">Link:</label>';
    echo '<input type="text" id="service_btn_link" name="service_btn_link" value="' . esc_attr($link) . '" /><br>';


}

function save_meta_box_services ($post_id) {

    if (!current_user_can('edit_post', $post_id)) {
        return;
    }

    if (isset($_POST['service_btn_text'])) {
        update_post_meta($post_id, '_service_btn_text', sanitize_text_field($_POST['service_btn_text']));
    }

    if (isset($_POST['service_btn_link'])) {
        update_post_meta($post_id, '_service_btn_link', sanitize_text_field($_POST['service_btn_link']));
    }

}

add_action('add_meta_boxes', 'themesflat_meta_box_services');
add_action('save_post', 'save_meta_box_services');

function themesflat_meta_box_portfolios(){
    add_meta_box( 'meta_box_portfolios', 'Infor Portfolio', 'themesflat_meta_box_portfolio_ouput', 'portfolios' );
}

function themesflat_meta_box_portfolio_ouput ( $post ) {
    $type = get_post_meta($post->ID,'_portfolio_type', true);
    $duration = get_post_meta($post->ID,'_portfolio_duration',true);
    $budget = get_post_meta($post->ID,'_portfolio_budget',true);
    $client = get_post_meta($post->ID,'_portfolio_client',true);
    $tasks = get_post_meta($post->ID,'_portfolio_tasks',true);
    $date = get_post_meta($post->ID,'_portfolio_date',true);

    echo '<label for="portfolio_type">Portfolio Type:</label>';
    echo '<input type="text" id="portfolio_type" name="portfolio_type" value="' . esc_attr($type) . '" /><br>';
    echo '<label for="portfolio_duration">Duration:</label>';
    echo '<input type="text" id="portfolio_duration" name="portfolio_duration" value="' . esc_attr($duration) . '" /><br>';
    echo '<label for="portfolio_budget">Budget:</label>';
    echo '<input type="text" id="portfolio_budget" name="portfolio_budget" value="' . esc_attr($budget) . '" /><br>';
    echo '<label for="portfolio_client">Client:</label>';
    echo '<input type="text" id="portfolio_client" name="portfolio_client" value="' . esc_attr($client) . '" /><br>';
    echo '<label for="portfolio_tasks">Tasks:</label>';
    echo '<input type="text" id="portfolio_tasks" name="portfolio_tasks" value="' . esc_attr($tasks) . '" /><br>';
    echo '<label for="portfolio_date">Date:</label>';
    echo '<input type="text" id="portfolio_date" name="portfolio_date" value="' . esc_attr($date) . '" /><br>';

}

function save_meta_box_portfolios ($post_id) {

    if (!current_user_can('edit_post', $post_id)) {
        return;
    }

    if (isset($_POST['portfolio_type'])) {
        update_post_meta($post_id, '_portfolio_type', sanitize_text_field($_POST['portfolio_type']));
    }

    if (isset($_POST['portfolio_duration'])) {
        update_post_meta($post_id, '_portfolio_duration', sanitize_text_field($_POST['portfolio_duration']));
    }

    if (isset($_POST['portfolio_budget'])) {
        update_post_meta($post_id, '_portfolio_budget', sanitize_text_field($_POST['portfolio_budget']));
    }
    if (isset($_POST['portfolio_client'])) {
        update_post_meta($post_id, '_portfolio_client', sanitize_text_field($_POST['portfolio_client']));
    }
    if (isset($_POST['portfolio_tasks'])) {
        update_post_meta($post_id, '_portfolio_tasks', sanitize_text_field($_POST['portfolio_tasks']));
    }
    if (isset($_POST['portfolio_date'])) {
        update_post_meta($post_id, '_portfolio_date', sanitize_text_field($_POST['portfolio_date']));
    }

}
add_action('add_meta_boxes', 'themesflat_meta_box_portfolios');
add_action('save_post', 'save_meta_box_portfolios');

// Hide render sidebar container css
remove_filter( 'render_block', 'wp_render_layout_support_flag', 10, 2 );
remove_filter( 'render_block', 'gutenberg_render_layout_support_flag', 10, 2 );
remove_filter( 'render_block', 'wp_render_layout_support_flag', 10, 2 );

add_filter( 'render_block', function( $block_content, $block ) {
    if ( $block['blockName'] === 'core/group' ) {
        return $block_content;
    }

    return wp_render_layout_support_flag( $block_content, $block );
}, 10, 2 );


function custom_elementor_template_fullwidth_single_event() {
    if ( is_singular( 'events' ) ) {
        require_once THEMESFLAT_PATH . '/templates/event/info-event.php';
    }
}
add_action('elementor/page_templates/header-footer/before_content', 'custom_elementor_template_fullwidth_single_event');

function custom_elementor_template_fullwidth_single_portfolio() {
    if ( is_singular( 'portfolios' ) ) {
        require_once THEMESFLAT_PATH . '/templates/portfolio2/info-portfolio.php';
    }
}
add_action('elementor/page_templates/header-footer/before_content', 'custom_elementor_template_fullwidth_single_portfolio');

add_filter('wpcf7_autop_or_not', '__return_false'); 

remove_action( 'wpcf7_init', 'wpcf7_add_form_tag_submit', 10, 0 );
add_action( 'wpcf7_init', 'themesflat_custom_submit_cf7', 10, 0 );

function themesflat_custom_submit_cf7() {
	wpcf7_add_form_tag( 'submit', 'themesflat_custom_submit_cf7_handlers' );
}

function themesflat_custom_submit_cf7_handlers( $tag ) {
	$class = wpcf7_form_controls_class( $tag->type, 'has-spinner' );

	$atts = array();

	$atts['class'] = $tag->get_class_option( $class );
	$atts['id'] = $tag->get_id_option();
	$atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true );

	$value = isset( $tag->values[0] ) ? $tag->values[0] : '';

	$atts['type'] = 'submit';


	$atts = wpcf7_format_atts( $atts );

	$html = sprintf( '<button type="submit" %1$s class="wpcf7-form-control has-spinner wpcf7-submit">'.html_entity_decode($value).'</button>', $atts );

	return $html;
}


add_filter('wp_list_categories', 'cat_count_span'); 
function cat_count_span($links) 
{ 
    $links = str_replace(' (', ' (', $links); $links = str_replace(')', ')', $links);
    return $links; 
}
function custom_user_profile_fields($user) {
    ?>
    <h3>Social links available</h3></h3>
    <table class="form-table">
        <tr>
            <th><label for="linkedin_link">Linkedin</label></th>
            <td>
                <input type="text" name="linkedin_link" id="linkedin_link" value="<?php echo esc_attr(get_user_meta($user->ID, 'linkedin_link', true)); ?>" class="regular-text" /><br />
                <span class="description">Type your linkedin profile link here</span>
            </td>
        </tr>
        <tr>
            <th><label for="facebook_link">Facebook</label></th>
            <td>
                <input type="text" name="facebook_link" id="facebook_link" value="<?php echo esc_attr(get_user_meta($user->ID, 'facebook_link', true)); ?>" class="regular-text" /><br />
                <span class="description">Type your facebook profile link here</span>
            </td>
        </tr>
        <tr>
            <th><label for="twitter_link">Twitter</label></th>
            <td>
                <input type="text" name="twitter_link" id="twitter_link" value="<?php echo esc_attr(get_user_meta($user->ID, 'twitter_link', true)); ?>" class="regular-text" /><br />
                <span class="description">Type your twitter profile link here</span>
            </td>
        </tr>
        <tr>
            <th><label for="youtobe_link">Youtobe</label></th>
            <td>
                <input type="text" name="youtobe_link" id="youtobe_link" value="<?php echo esc_attr(get_user_meta($user->ID, 'youtobe_link', true)); ?>" class="regular-text" /><br />
                <span class="description">Type your youtobe profile link here</span>
            </td>
        </tr>
        <tr>
            <th><label for="pinterest_link">Pinterest</label></th>
            <td>
                <input type="text" name="pinterest_link" id="pinterest_link" value="<?php echo esc_attr(get_user_meta($user->ID, 'pinterest_link', true)); ?>" class="regular-text" /><br />
                <span class="description">Type your pinterest profile link here</span>
            </td>
        </tr>
    </table>
    <?php
}

function save_custom_user_profile_fields($user_id) {
    if (current_user_can('edit_user', $user_id)) {
        update_user_meta($user_id, 'linkedin_link', sanitize_text_field($_POST['linkedin_link']));
        update_user_meta($user_id, 'facebook_link', sanitize_text_field($_POST['facebook_link']));
        update_user_meta($user_id, 'twitter_link', sanitize_text_field($_POST['twitter_link']));
        update_user_meta($user_id, 'youtobe_link', sanitize_text_field($_POST['youtobe_link']));
        update_user_meta($user_id, 'pinterest_link', sanitize_text_field($_POST['pinterest_link']));
    }
}

add_action('show_user_profile', 'custom_user_profile_fields');
add_action('edit_user_profile', 'custom_user_profile_fields');
add_action('personal_options_update', 'save_custom_user_profile_fields');
add_action('edit_user_profile_update', 'save_custom_user_profile_fields');