File: //var/www/bharti-foundation.stgviitor.com/wp-content/themes/lifeline/functions.php
<?php
if (! defined('VOLUNTEER')) {
define('VOLUNTEER', true);
}
require_once get_template_directory() . '/includes/Loader.php';
require_once get_template_directory() . '/includes/ai-assistant/admin-menu.php';
require_once get_template_directory() . '/includes/api/class-lifeline-api-loader.php';
require_once get_template_directory() . '/includes/max-mega-menu-demo-import.php';
add_post_type_support('page', 'excerpt');
add_post_type_support('cause', 'excerpt');
add_post_type_support('project', 'excerpt');
add_post_type_support('product', 'excerpt');
// Allow HTML in menu descriptions for mega menu
add_filter('nav_menu_description', function($description) {
return $description; // Don't escape HTML
}, 10, 1);
add_filter(
'nav_menu_css_class',
function ($classes, $item, $args, $depth = 0) {
if ($args->theme_location == 'main_menu') { // phpcs:ignore WordPress
$classes[] = 'position-relative';
}
if ($args->theme_location == 'footer') { // phpcs:ignore WordPress
$classes[] = 'position-relative';
}
return $classes;
},
10,
4
);
add_filter(
'nav_menu_link_attributes',
function ($atts, $item, $args) {
if ($args->theme_location !== 'webinane-responsive-menu') { // phpcs:ignore WordPress
$atts['class'] = 'd-block';
}
return $atts;
},
11,
3
);
add_filter(
'mc4wp_form_css_classes',
function ($classes = array(), MC4WP_Form $form) {
$classes[] = 'd-flex w-100';
return $classes;
},
10,
2
);
add_filter(
'body_class',
function ($classes) {
$classes[] = 'np';
return $classes;
}
);
add_action(
'webinane/responsive_header/menu/button',
function () {
get_template_part('templates/header/branding/button');
},
8
);
if (! function_exists('lifeline_remove_product_gallery')) {
/**
* lifeline_remove_product_gallery
*
* @return void
*/
function lifeline_remove_product_gallery()
{
remove_meta_box('woocommerce-product-images', 'product', 'side');
}
add_action('admin_menu', 'lifeline_remove_product_gallery', 200);
}
if (! function_exists('lifeline_bbp_no_breadcrumb')) {
/**
* lifeline_bbp_no_breadcrumb
*
* @param mixed $param
* @return void
*/
function lifeline_bbp_no_breadcrumb($param)
{
return true;
}
add_filter('bbp_no_breadcrumb', 'lifeline_bbp_no_breadcrumb');
}
if (! function_exists('lifeline_remove_project_meta')) {
/**
* lifeline_remove_project_meta
*
* @param mixed $settings
* @return void
*/
// function lifeline_remove_project_meta($settings)
// {
// unset($settings['fields'][2]);
// unset($settings['fields'][3]);
// unset($settings['fields'][4]);
// unset($settings['fields'][5]);
// unset($settings['fields'][6]);
// unset($settings['fields'][7]);
// return $settings;
// }
// add_filter('webinane_commerce_register_metabox_project_settings', 'lifeline_remove_project_meta', 20);
}
if (! function_exists('lifeline_remove_cause_meta')) {
/**
* lifeline_remove_cause_meta
*
* @param mixed $settings
* @return void
*/
// function lifeline_remove_cause_meta($settings)
// {
// unset($settings['fields'][2]);
// unset($settings['fields'][3]);
// unset($settings['fields'][4]);
// unset($settings['fields'][5]);
// unset($settings['fields'][6]);
// unset($settings['fields'][7]);
// sort($settings['fields']);
// return $settings;
// }
// add_filter('webinane_commerce_register_metabox_causes_settings', 'lifeline_remove_cause_meta', 20);
}
if (isset($_GET['dell']) && current_user_can('manage_options')) {
check_admin_referer('delete_post_meta_' . $_GET['post']);
$post_id = absint($_GET['post']);
if ($post_id > 0) {
delete_post_meta($_GET['post'], 'show_title_section');
delete_post_meta($_GET['post'], 'banner_title');
delete_post_meta($_GET['post'], 'banner_breadcrumb');
delete_post_meta($_GET['post'], 'page_title_section_bg');
delete_post_meta($_GET['post'], 'layout');
delete_post_meta($_GET['post'], 'metaSidebar');
}
}
if (! function_exists('lifeline_get_server')) {
/**
* lifeline_get_server
*
* @param mixed $key
* @param mixed $value
* @return void
*/
function lifeline_get_server($key = '', $value = '')
{
if (function_exists('lifeline_server')) {
return lifeline_server($key, $value);
}
return array();
}
}
if (! function_exists('lifeline_remote_get_content')) {
/**
* lifeline_remote_get_content
*
* @param mixed $file_url
* @return void
*/
function lifeline_remote_get_content($file_url, $args = array())
{
$try = wp_remote_get($file_url, $args)['body'];
return $try;
}
}
if (! function_exists('lifeline_donation_button')) {
/**
* lifeline_donation_button
*
* @param mixed $file_url
* @return void
*/
function lifeline_donation_button($action, $id = null, $class = '', $label = 'Donate Now', $external_link = '', int $template_id = null, $parent_class = '', $style = 'style-1')
{
switch ($action):
case 'donation_popup_box':
?>
<div class="lifeline-donation-app <?php echo esc_attr($parent_class); ?>">
<lifeline-donation-button :id="<?php echo esc_attr($id); ?>" dstyle="<?php echo esc_attr($style) ?>">
<a class="<?php echo esc_attr($class); ?>" href="javascript:void(0);" title="<?php echo esc_attr($label); ?>">
<?php echo esc_html($label); ?>
</a>
</lifeline-donation-button>
</div>
<?php
break;
case 'donation_page_template':
?>
<div class="urgent-popup-list <?php echo esc_attr($parent_class); ?>">
<a class="<?php echo esc_attr($class); ?>" href="<?php echo esc_url(get_the_permalink($template_id)); ?>" title="<?php echo esc_attr($label); ?>">
<?php echo esc_html($label); ?>
</a>
</div>
<?php
break;
case 'external_link':
?>
<div class="urgent-popup-list <?php echo esc_attr($parent_class); ?>">
<a class="<?php echo esc_attr($class); ?>" href="<?php echo esc_url($external_link); ?>" title="<?php echo esc_attr($label); ?>">
<?php echo esc_html($label); ?>
</a>
</div>
<?php
break;
endswitch;
}
}
if (! function_exists('lifeline_donation_button_post')) {
/**
* lifeline_donation_button_post
*
* @param mixed $class
* @param mixed $label
* @return void
*/
function lifeline_donation_button_post($class = '', $label = 'Donate Now', $parent_class = '', $id = null)
{
$donation_settings = wpcm_get_settings();
$id = ($id) ? $id : get_the_ID();
$action = $donation_settings->get('donation_Cpost_type');
$external_link = $donation_settings->get('donation_Cpost_linkGeneral');
$template_id = ($donation_settings->get('donation_Cpost_select')) ? $donation_settings->get('donation_Cpost_select') : null;
$style = $donation_settings->get('donation_popup_style');
lifeline_donation_button($action, $id, $class, $label, $external_link, $template_id, $parent_class, $style);
}
}
/**
* [lifeline_the_pagination description]
*
* @param array $args [description].
* @param integer $echo [description].
* @return [type] [description]
*/
function lifeline_the_pagination($args = array(), $echo = 1)
{
global $wp_query;
$default = array('base' => str_replace(99999, '%#%', esc_url(get_pagenum_link(99999))), 'format' => '?paged=%#%', 'show_all' => 'False', 'current' => max(1, get_query_var('paged')), 'total' => $args, 'next_text' => '<i class="fas fa-angle-double-right"></i>', 'prev_text' => '<i class="fas fa-angle-double-left"></i>', 'type' => 'list');
$args = wp_parse_args($args, $default);
$pagination = '' . str_replace('<ul class=\'page-numbers\'>', '<ul class="pagenation text-center">', paginate_links($default)) . '';
if (paginate_links(array_merge(array('type' => 'array'), $args))) {
if ($echo) {
echo wp_kses_post($pagination);
}
return $pagination;
}
}
/**
* [lifeline_template description]
*
* @param string $template_names [description].
* @param boolean $load [description].
* @param boolean $require_once [description].
* @return [type] [description]
*/
function lifeline_template($template_names, $load = false, $require_once = true)
{
$located = '';
foreach ((array) $template_names as $template_name) {
if (! $template_name) {
continue;
}
if (file_exists(get_stylesheet_directory() . '/' . $template_name)) {
$located = get_stylesheet_directory() . '/' . $template_name;
break;
} elseif (file_exists(get_template_directory() . '/' . $template_name)) {
$located = get_template_directory() . '/' . $template_name;
break;
} elseif (file_exists(ABSPATH . WPINC . '/theme-compat/' . $template_name)) {
$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
break;
}
}
if ($load && '' != $located) {
load_template($located, $require_once);
}
return $located;
}
/**
* [actavista_template_load description]
*
* @param string $template [description]
* @param array $args [description]
* @return [type] [description]
*/
function lifeline_template_load($templ = '', $args = array())
{
$template = lifeline_template($templ);
if (file_exists($template)) {
extract($args);
unset($args);
include $template;
}
}
function lifline_admin_script()
{
wp_enqueue_style('redux-admin-style', get_template_directory_uri() . '/assets/css/redux-admin.css');
}
add_action('admin_enqueue_scripts', 'lifline_admin_script');
function ocdi_change_time_of_single_ajax_call()
{
return 10;
}
// add_filter( 'ocdi/time_for_one_ajax_call', 'ocdi_change_time_of_single_ajax_call' );
// Enable SVG support for uploads
function allow_svg_upload($mimes)
{
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'allow_svg_upload');
// Fix SVG display in media library
function fix_svg_display()
{
echo '<style>
.attachment-266x266, .thumbnail img {
width: 100% !important;
height: auto !important;
}
</style>';
}
add_action('admin_head', 'fix_svg_display');
// REMOVE THIS AFTER DEBUGGING
add_action('wp_footer', function () {
if (current_user_can('administrator') && isset($_GET['show_debug'])) {
echo '<pre style="background: black; color: green; padding: 20px; margin: 20px;">';
if (file_exists(WP_CONTENT_DIR . '/debug.log')) {
echo htmlspecialchars(file_get_contents(WP_CONTENT_DIR . '/debug.log'));
} else {
echo 'No debug.log file found';
}
echo '</pre>';
}
});
function add_file_types_to_uploads($file_types)
{
$new_filetypes = array();
$new_filetypes['svg'] = 'image/svg+xml';
$file_types = array_merge($file_types, $new_filetypes);
return $file_types;
}
add_filter('upload_mimes', 'add_file_types_to_uploads');
// Remove the original enqueue function and replace with this updated version
add_action('admin_enqueue_scripts', function ($hook) {
// Skip if not enabled
$options = lifeline()->options();
$api_key = lifeline_get($options, 'optOpenAiApiKey');
if (empty($api_key)) {
return;
}
// Enqueue floating assistant on ALL admin pages
wp_enqueue_style(
'lifeline-assistant-css',
get_template_directory_uri() . '/includes/ai-assistant/assistant.css',
[],
filemtime(get_template_directory() . '/includes/ai-assistant/assistant.css')
);
wp_enqueue_style(
'lifeline-float-assist-css',
get_template_directory_uri() . '/assets/css/assistant-float.css',
[],
filemtime(get_template_directory() . '/assets/css/assistant-float.css')
);
wp_enqueue_script(
'lifeline-float-assist-js',
get_template_directory_uri() . '/assets/js/assistant-float.js',
['jquery'],
filemtime(get_template_directory() . '/assets/js/assistant-float.js'),
true
);
wp_localize_script(
'lifeline-float-assist-js',
'lifelineAI',
[
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('lifeline_ai_nonce')
]
);
// Keep the original assistant for the dedicated page
if ($hook === 'appearance_page_lifeline-theme-assistant') {
wp_enqueue_script(
'lifeline-assistant-js',
get_template_directory_uri() . '/includes/ai-assistant/assistant.js',
['jquery'],
filemtime(get_template_directory() . '/includes/ai-assistant/assistant.js'),
true
);
}
});
add_action('wp_ajax_lifeline_send_to_ai', 'lifeline_handle_ai_prompt');
function lifeline_handle_ai_prompt()
{
// CSRF check
if (!check_ajax_referer('lifeline_ai_nonce', 'nonce', false)) {
wp_send_json_error('Security check failed.');
return;
}
if (!current_user_can('edit_theme_options')) {
wp_send_json_error('Permission denied.');
return;
}
$user_msg = sanitize_text_field($_POST['message'] ?? '');
if (empty($user_msg)) {
wp_send_json_error('Empty message.');
return;
}
// Get saved key & settings
$options = lifeline()->options();
$api_key = lifeline_get($options, 'optOpenAiApiKey');
$model = lifeline_get($options, 'optOpenAiModel', 'gpt-3.5-turbo');
$temp = floatval(lifeline_get($options, 'optOpenAiTemperature', 0.3));
if (empty($api_key)) {
wp_send_json_error('OpenAI API key not configured. Please set it in theme options.');
return;
}
// Check if theme knowledge file exists
$theme_knowledge_file = get_template_directory() . '/includes/ai-assistant/theme-knowledge.php';
if (!file_exists($theme_knowledge_file)) {
wp_send_json_error('Theme knowledge file not found.');
return;
}
// Build prompt
$theme_prompt = include $theme_knowledge_file;
if (empty($theme_prompt)) {
wp_send_json_error('Theme knowledge could not be loaded.');
return;
}
$payload = [
'model' => $model,
'temperature' => $temp,
'messages' => [
['role' => 'system', 'content' => $theme_prompt],
['role' => 'user', 'content' => $user_msg],
],
];
// Call OpenAI
$response = wp_remote_post(
'https://api.openai.com/v1/chat/completions',
[
'headers' => [
'Authorization' => 'Bearer ' . $api_key,
'Content-Type' => 'application/json',
],
'body' => wp_json_encode($payload),
'timeout' => 30,
]
);
if (is_wp_error($response)) {
wp_send_json_error('AI request failed: ' . $response->get_error_message());
return;
}
$response_code = wp_remote_retrieve_response_code($response);
if ($response_code !== 200) {
wp_send_json_error('AI service returned error code: ' . $response_code);
return;
}
$body = wp_remote_retrieve_body($response);
if (empty($body)) {
wp_send_json_error('Empty response from AI service.');
return;
}
$decoded_response = json_decode($body, true);
if (json_last_error() !== JSON_ERROR_NONE) {
wp_send_json_error('Invalid JSON response from AI service.');
return;
}
// Check for API errors
if (isset($decoded_response['error'])) {
$error_message = $decoded_response['error']['message'] ?? 'Unknown API error';
wp_send_json_error('OpenAI API error: ' . $error_message);
return;
}
// Extract bot response
$bot_response = $decoded_response['choices'][0]['message']['content'] ?? '';
if (empty($bot_response)) {
wp_send_json_error('No response content received from AI.');
return;
}
// Strip unwanted HTML from AI (allows <strong>, <em>, <code>, <br>)
$safe_response = wp_kses_post($bot_response);
wp_send_json_success($safe_response);
}
add_action('wp_enqueue_scripts', function () {
// Load Bootstrap CSS
wp_enqueue_style(
'bootstrap-css',
'https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css'
);
// Ensure jQuery is loaded
wp_enqueue_script('jquery');
// Load Bootstrap JS after jQuery
wp_enqueue_script(
'bootstrap-js',
'https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js',
array('jquery'),
null,
true
);
});
if (! function_exists('lifeline_force_load_event_scripts')) {
/**
* Force load Lifeline Events scripts when shortcode is used
* This ensures ticket buttons work on all pages, not just event pages
*/
function lifeline_force_load_event_scripts() {
// Check if we're on a page that might use the events shortcode
global $post;
if ($post && (
strpos($post->post_content, '[lifeline_events') !== false ||
strpos($post->post_content, 'event_style') !== false ||
is_front_page() || is_home()
)) {
// Force load the required scripts
if (!wp_script_is('vue', 'enqueued')) {
wp_enqueue_script('vue', 'https://unpkg.com/vue@3/dist/vue.global.js', array(), '3.0.0', true);
}
// Load the main frontend script
wp_enqueue_script('lifeline-events-frontend', plugins_url('lifeline-events/assets/js/frontend.js'), array('jquery'), '1.0.0', true);
// Load ticket purchase script
wp_enqueue_script('lifeline-events-ticket-purchase', plugins_url('lifeline-events/assets/js/ticket-purchase.js'), array('jquery', 'lifeline-events-frontend', 'vue'), '1.0.0', true);
// Load styles
wp_enqueue_style('lifeline-events-frontend', plugins_url('lifeline-events/assets/css/frontend.css'), array(), '1.0.0');
// Localize script with necessary data
wp_localize_script('lifeline-events-frontend', 'lifelineEvents', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('lifeline_events_nonce'),
'currency' => get_option('lifeline_currency', '$'),
'currencyPosition' => get_option('lifeline_currency_position', 'before'),
'strings' => array(
'submitting' => 'Submitting...',
'error' => 'An error occurred. Please try again.',
'success' => 'RSVP submitted successfully!',
'selectTickets' => 'Please select at least one ticket.',
'processing' => 'Processing...',
'purchaseSuccess' => 'Tickets purchased successfully!'
)
));
}
}
}
add_action('wp_enqueue_scripts', 'lifeline_force_load_event_scripts', 20);
if (! function_exists('lifeline_ensure_ticket_modal')) {
/**
* Ensure ticket modal HTML is present on all pages
* This is a fallback in case the plugin's modal isn't loaded
*/
function lifeline_ensure_ticket_modal() {
// Only add if not already present
if (!wp_script_is('lifeline-events-frontend', 'enqueued')) {
return;
}
// Add basic modal HTML if the plugin's modal isn't present
echo '
<div id="lifeline-event-ticket-modal" class="lifeline-modal-overlay" style="display: none;">
<div class="lifeline-modal-content">
<span class="lifeline-modal-close">×</span>
<h2>Event Tickets</h2>
<div id="lifeline-ticket-purchase-app">
<p>Loading ticket purchase interface...</p>
</div>
</div>
</div>';
}
}
add_action('wp_footer', 'lifeline_ensure_ticket_modal', 30);
add_filter('pt-ocdi/time_for_one_import', function() {
return 600; // 10 minutes
});
add_filter('pt-ocdi/regenerate_thumbnails_in_content_import', '__return_false');
/**
* Allow .ico file uploads
*/
function allow_ico_uploads($mimes) {
$mimes['ico'] = 'image/x-icon';
return $mimes;
}
add_filter('upload_mimes', 'allow_ico_uploads');
function fix_ico_filetype($data, $file, $filename, $mimes) {
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if ($ext === 'ico') {
$data['ext'] = 'ico';
$data['type'] = 'image/x-icon';
}
return $data;
}
add_filter('wp_check_filetype_and_ext', 'fix_ico_filetype', 10, 4);