<?php
/**
 * Enqueue child styles.
 */
function child_enqueue_styles() {
	wp_enqueue_style( 'child-theme', get_stylesheet_directory_uri() . '/style.css', array(), 100 );
}

add_action( 'wp_enqueue_scripts', 'child_enqueue_styles' );

// shortcode to output current year
function get_current_year() {
    return date('Y');
}

function year_shortcode() {
    return get_current_year();
}
add_shortcode('year', 'year_shortcode');
// end

// Custom font
function my_kadence_custom_fonts( $system_fonts ) {
 
  $system_fonts[ 'trdsp' ] = array(
    'fallback' => 'Verdana, Arial, sans-serif',
    'weights' => array(
      '100',
		'200',
		'300',
		'400',
		'500',
		'600',
		'700',
		'800',
		
		
    ),
  );
  return $system_fonts;
 
}
add_filter( 'kadence_theme_add_custom_fonts', 'my_kadence_custom_fonts' );

// Mail lead creator
function generate_start_mail_button() {
    if (is_singular('lead')) {
        // Get custom field values
        $website = get_field('website');
        $email = get_field('email');
        $city = get_field('city');
        $first_name = get_field('first_name');
        $email_name = get_field('email_name');
        $business_name = get_the_title(); // Assuming the post title is the business name

        // Decode any HTML entities in the business name
        $business_name = html_entity_decode($business_name, ENT_QUOTES, 'UTF-8');

        // Construct the subject and body
        $subject = rawurlencode("$city Business Website Pilot Opportunity for $business_name");
        $body = rawurlencode("Hi $first_name,\n\nI’m Rian, owner of Riverway, a web development agency in Courtenay. We’re exploring an opportunity to set up a presence in $city, and looking for a few $city-based businesses who may be interested in participating in our pilot project to get a new professional quality website.\n\nWhile we’re exploring this opportunity, we’re offering to rebuild a few websites at significantly reduced rates in exchange for feedback. In addition, there’s currently a government grant available for SMEs which could reimburse all or part of that cost. The grant funding is ending very soon, so if this is of interest to you, we’d want to act pretty fast!\n\nIf you’re interested, please reply to this email and we’ll take it from there regarding potential fit, the grant process (if relevant), and next steps.\n\nLooking forward to connecting!\n\n--\nRian Bowden\nOwner, Riverway Online Marketing\n146 Beckensell Ave, Courtenay, BC V9N 2H4, Canada\n+1 (250) 813-0252 | hello@riverway.ca");

        // Replace '+' with '%20' to ensure proper spacing
        $subject = str_replace('+', '%20', $subject);
        $body = str_replace('+', '%20', $body);

        // Create the mailto link
        $mailto_link = "mailto:$email?subject=$subject&body=$body";

        // Return the button HTML with Kadence's standard button class
        return '<a href="' . esc_url($mailto_link) . '" class="wp-block-button__link">Start Mail</a>';
    }
}

add_shortcode('start_mail_button', 'generate_start_mail_button');



// Add custom columns to the Lead post type
function add_lead_custom_columns($columns) {
    // Add new columns after the Title column
    $new_columns = array(
        'cb' => $columns['cb'], // Checkbox for bulk actions
        'title' => $columns['title'], // Post title
        'website' => 'Website',
        'email' => 'Email',
        'city' => 'City',
        'first_name' => 'First Name',
        'email_name' => 'Email Name',
        'sent_date' => 'Sent Date',
        'date' => $columns['date'], // Post date
    );

    return $new_columns;
}
add_filter('manage_lead_posts_columns', 'add_lead_custom_columns');

// Populate the custom columns with data
function fill_lead_custom_columns($column, $post_id) {
    switch ($column) {
        case 'website':
            echo esc_html(get_post_meta($post_id, 'website', true));
            break;
        case 'email':
            echo esc_html(get_post_meta($post_id, 'email', true));
            break;
        case 'city':
            echo esc_html(get_post_meta($post_id, 'city', true));
            break;
        case 'first_name':
            echo esc_html(get_post_meta($post_id, 'first_name', true));
            break;
        case 'email_name':
            echo esc_html(get_post_meta($post_id, 'email_name', true));
            break;
        case 'sent_date':
            $sent_date = get_post_meta($post_id, 'sent_date', true);
            echo !empty($sent_date) ? esc_html(date('F j, Y', strtotime($sent_date))) : 'Not Sent';
            break;
    }
}
add_action('manage_lead_posts_custom_column', 'fill_lead_custom_columns', 10, 2);

// Make the custom columns sortable
function lead_custom_columns_sortable($columns) {
    $columns['website'] = 'website';
    $columns['email'] = 'email';
    $columns['city'] = 'city';
    $columns['first_name'] = 'first_name';
    $columns['email_name'] = 'email_name';
    $columns['sent_date'] = 'sent_date';
    return $columns;
}
add_filter('manage_edit-lead_sortable_columns', 'lead_custom_columns_sortable');
