($open_multiple_faqs || $open_multiple_faqs === 'yes') ? 'yes' : 'no',
'admin_url' => admin_url(),
'ajax_url' => admin_url( 'admin-ajax.php'),
);
wp_localize_script('thfaqf-public-script', 'thfaqf_public_var', $faqf_var);
}
public function faq_shortcode($atts){
$args = shortcode_atts( array(
'id' => '',
), $atts );
$invalid_ids = array();
$faq_post_ids = explode(',', $args['id']);
ob_start();
foreach($faq_post_ids as $key => $faq_post_id){
$faq_post_id = absint($faq_post_id);
$post_status = get_post_status($faq_post_id);
$post_type = get_post_type($faq_post_id);
if($post_type == 'faq' && $post_status == 'publish'){
$this->faq_list($faq_post_id,'faq');
}else{
array_push($invalid_ids, $faq_post_id);
}
}
$invalid_ids = implode(',', $invalid_ids);
// Escape output to prevent XSS
echo !empty($invalid_ids) ? esc_html('[FAQ id="'.$invalid_ids.'"]') : '';
return ob_get_clean();
}
public function faq_layout_shortcode($atts){
ob_start();
$sh_args = shortcode_atts( array(
'category' => '',
'limit' => '',
), $atts );
$faq_category = isset($sh_args['category']) ? sanitize_text_field($sh_args['category']) : '';
$limit = isset($sh_args['limit']) ? intval($sh_args['limit']) : '';
$category_array = explode (",", $faq_category);
$pst_args = array(
'post_type' => 'faq',
'post_status' => 'publish',
'posts_per_page' => $limit,
'tax_query' => array(
array(
'taxonomy' => 'faq_category',
'field' => 'slug',
'terms' => $category_array,
),
),
'order' => 'ASC',
'orderby' => 'title',
);
$loop = new WP_Query( $pst_args );
$this->prepare_faq_layout($loop);
wp_reset_postdata();
return ob_get_clean();
}
public function prepare_faq_layout($loop){
$theme_wrapper_class = $this->get_theme_wrapper_class();
?>
have_posts()) : $loop->the_post();
$faq_index1++;
$post_id = absint(get_the_ID());
?>
faq_search_option($show_updated_date);
}
if ($show_updated_date) {
$last_updated = get_the_modified_date(get_option('date_format'), $post_id);
?>
have_posts()) : $loop->the_post();
$tab_index++;
$tab_title = get_the_title(get_the_ID());
$tab_title = $tab_title ? $tab_title : 'Title '.$tab_index;
$tab_id = absint(get_the_ID());
?>
have_posts()) : $loop->the_post();
$faq_index++;
$post_id = absint(get_the_ID());
?>
faq_list($post_id,'layout'); ?>
prepare_faq_settings($global_settings, $local_settings);
$border_radius = isset($settings['faq_border_radius']) ? $settings['faq_border_radius'] : 0;
$faq_title_icon = isset($settings['icon_picker']) ? $settings['icon_picker'] : 0;
$enable_icon_options = isset($settings['enable_disable_title_icons']) ?$settings['enable_disable_title_icons']: false;
$border_radius = is_numeric($border_radius) ? $border_radius : 0;
$title_color = isset($settings['title_color']) ? $settings['title_color'] : false;
$title_bg = isset($settings['title_bg_color']) ? $settings['title_bg_color'] : false;
$content_color = isset($settings['content_color']) ? $settings['content_color'] : false;
$content_bg = isset($settings['content_bg_color']) ? $settings['content_bg_color'] : false;
$expnd_icon_color = isset($settings['expnd_icon_color']) ? $settings['expnd_icon_color'] : false;
$title_active_color = isset($settings['title_active_color']) ? $settings['title_active_color'] : false;
$enable_search_option = isset($individual_settings['enable_disable_search_option']) ? $individual_settings['enable_disable_search_option'] : false;
$faq_count = isset($settings['thfaq_count']) ? $settings['thfaq_count'] : false;
$show_updated_date = THFAQF_Utils::get_setting_value($settings, 'show_updated_date');
$accordion_display_mode = THFAQF_Utils::get_setting_value($settings, 'accordion_display_mode');
$accordion_display_mode = $accordion_display_mode ? $accordion_display_mode : 'open_first';
$fist_item_active_class = $accordion_display_mode === 'open_first' ? ' thfaqf-active' : '';
$theme_wrapper_class = $this->get_theme_wrapper_class();
$item_wrapper_style = '';
$item_title_style = 'border-width: 1px; border-style: solid; margin-bottom: -1px;';
$item_content_style = 'border-width: 1px; border-style: solid;';
$title_text_style = '';
$item_expnd_icon_style = '';
$user_id = $current_user->ID;
$tottal_faq_count = is_array($faqs) ? count($faqs) : 0;
$visible_faq_count =!empty((int)$faq_count) ? (int)$faq_count : $tottal_faq_count;
$load_button_display_none = $visible_faq_count >= $tottal_faq_count ? 'hide-faqf-group' : '';
if($border_radius){
$item_wrapper_style .= 'border-radius: '.esc_attr($border_radius).'px;';
}
if($title_bg){
$item_title_style .= ' background-color: '.esc_attr($title_bg).'; border-color: '.esc_attr($title_bg).';';
}
if($title_color){
$title_text_style .= ' color: '.esc_attr($title_color).';';
}
if($content_bg){
$item_content_style .= ' background-color: '.esc_attr($content_bg).'; border-color: '.esc_attr($content_bg).';';
}
if($content_color){
$item_content_style .= ' color: '.esc_attr($content_color).';';
}
if($expnd_icon_color){
$item_expnd_icon_style = ' color: '.esc_attr($expnd_icon_color).';';
}
$this->get_additional_css_for_faqs($post_id);
?>
'.esc_html(get_the_title($post_id)).'' : '';
echo (($enable_search_option === 'yes' || $enable_search_option === true) && $type !== 'layout') ? wp_kses_post((string) $this->faq_search_option($show_updated_date)) : '';
if(($show_updated_date === "yes" || $show_updated_date == 1) && $type != 'layout'){
$last_updated = get_the_modified_date(get_option('date_format'), $post_id);
echo '
'. esc_html($last_updated).'
';
}
$index = 0;
echo '
';
foreach($faqs as $key => $faq_item){
$item_wrapper_class = $index === 0 ? $fist_item_active_class : '';
$like_user_ids = !empty($faq_item['like_user_ids']) ? $faq_item['like_user_ids'] : '';
$dislike_user_ids = !empty ($faq_item['dislike_user_ids']) ? $faq_item['dislike_user_ids'] : '';
$display_none = $key < $visible_faq_count ? '' : 'thfaqf-div-none';
$this->display_faq_item($faq_item, $item_wrapper_class, $item_wrapper_style, $item_title_style, $item_content_style, $title_text_style,$item_expnd_icon_style,$post_id,$key,$like_user_ids,$dislike_user_ids,$faq_title_icon,$enable_icon_options,$user_id,esc_attr($display_none),$title_active_color);
$index++;
}
echo '
';
$this->display_social_share($post_id, $settings);
$total_count = isset($faqs) ? count($faqs) : 0;
$count = intdiv($total_count, $visible_faq_count);
$mod = fmod($total_count, $visible_faq_count);
$page_count = (($mod < $visible_faq_count) and ($mod > 0) ) ? $count+1 : $count;
$pagination_panel = fmod($page_count,4)>0 ? $count = intdiv($page_count,4)+1 : intdiv($page_count,4);
?>
display_edit_link($post_id); ?>
rate_comment($faq_comment_id);
$c_color = $count_comments>0? 'color:black;': '';
?>
like_option($post_id,$key,$like_user_ids,$dislike_user_ids,$user_id)) : '';
if($enable_comment_box == true){
?>
$faq_single_comment_ids){
$post_status = get_post_status($faq_single_comment_ids);
$post_type = get_post_type($faq_single_comment_ids);
if($post_status == 'publish' and $post_type == 'user-comment'){
array_push($comment_arr,$faq_single_comment_ids);
}
}
return isset($comment_arr) ? count($comment_arr) : 0;
}
public function thfaqf_comment(){
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
$this->thfaqf_error('Invalid request method.');
}
if ( ! check_ajax_referer('thfaqc_nonce', 'wp_thfaqc_nonce', false) ) {
$this->thfaqf_error('Invalid or expired nonce.');
}
if (!empty($_POST['website'])) {
$this->thfaqf_error('Spam detected');
}
if (empty($_SERVER['HTTP_USER_AGENT']) || strlen($_SERVER['HTTP_USER_AGENT']) < 10) {
$this->thfaqf_error('Invalid request');
}
// Token validation
$token = isset($_POST['form_token']) ? sanitize_text_field($_POST['form_token']) : '';
$stored_time = get_transient('thfaq_' . $token);
if (!$token || !$stored_time) {
$this->thfaqf_error('Invalid session.');
}
$time_diff = time() - $stored_time;
if ($time_diff < 3) {
$this->thfaqf_error('Form submitted too quickly.');
}
if ($time_diff > 600) {
delete_transient('thfaq_' . $token);
$this->thfaqf_error('Form expired. Please refresh.');
}
// delete_transient('thfaq_' . $token); // one-time use
// Inputs
$user_name = isset($_POST['user_name']) ? sanitize_text_field(wp_unslash($_POST['user_name'])) : '';
$user_msg = isset($_POST['user_msg']) ? wp_filter_post_kses(wp_unslash($_POST['user_msg'])) : '';
$faq_id = isset($_POST['faq_id']) ? absint($_POST['faq_id']) : 0;
$faq_index = isset($_POST['faq_index']) ? absint($_POST['faq_index']) : 0;
if (!$faq_id) {
$this->thfaqf_error('Invalid FAQ reference.');
}
// Rate limit
$ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
$ua = $_SERVER['HTTP_USER_AGENT'] ?? '';
$rate_key = 'thfaq_comment_' . md5($ip . $faq_id . $ua);
$count = (int) get_transient($rate_key);
if ($count >= 10) {
$this->thfaqf_error('Too many requests. Try again after a few minutes.');
}
set_transient($rate_key, $count + 1, 5 * MINUTE_IN_SECONDS);
// Validation
if (empty($user_name)) {
wp_send_json_error(['name' => 'Name is required.']);
}
if (empty($user_msg)) {
wp_send_json_error(['comment' => 'Comment is required.']);
}
$faq_post = get_post($faq_id);
if (!$faq_post || $faq_post->post_status !== 'publish') {
$this->thfaqf_error('FAQ does not exist.');
}
$faq_data = get_post_meta($faq_id, THFAQF_Utils::OPTION_KEY_FAQ_ITEMS, true);
if (!is_array($faq_data) || !isset($faq_data[$faq_index])) {
$this->thfaqf_error('Invalid FAQ item.');
}
if (!THFAQF_Utils::get_faq_settings('', 'enable_disable_comment')) {
$this->thfaqf_error('Comments are disabled.');
}
// Insert
$post_id = wp_insert_post([
'post_title' => $user_name,
'post_content' => $user_msg,
'post_type' => 'user-comment',
'post_status' => 'draft',
], true);
if (is_wp_error($post_id)) {
$this->thfaqf_error('Failed to save comment.');
}
// Update meta
$faq_comment_string = $faq_data[$faq_index]['faq_comment'] ?? '';
$faq_comments_array = $faq_comment_string ? explode(',', $faq_comment_string) : [];
$faq_comments_array[] = $post_id;
$faq_data[$faq_index]['faq_comment'] = implode(',', $faq_comments_array);
update_post_meta($faq_id, THFAQF_Utils::OPTION_KEY_FAQ_ITEMS, $faq_data);
$message = 'Comment posted successfully.';
wp_send_json_success([
'message' => wp_kses_post($message)
]);
}
public function thfaqf_error($msg) {
wp_send_json_error([
'message' => '' . esc_html($msg) . ''
]);
}
public function display_social_share($post_id, $settings){
$enabled_share_button = THFAQF_Utils::get_setting_value($settings, 'enable_share_button');
if($enabled_share_button === 'yes' || $enabled_share_button == 1){
$post_url = get_post_permalink($post_id);
$social_share_icons = THFAQF_Utils::get_setting_value($settings, 'social_share_options');
$social_share_icons = $social_share_icons ? explode(',', $social_share_icons) : false;
$social_share_html = '';
if(is_array($social_share_icons)){
foreach($social_share_icons as $key){
$link = '';
if($key === 'twitter'){
$url = 'https://twitter.com/home?status='.esc_url($post_url);
$link = '';
}else if($key === 'facebook'){
$url = 'https://www.facebook.com/sharer/sharer.php?u='.esc_url($post_url);
$link = '';
}else if($key === 'email'){
$url = 'mailto:info@example.com?&subject=&body='.esc_url($post_url);
$link = '';
}else if($key === 'linkedin'){
$url = 'https://www.linkedin.com/shareArticle?mini=true&url='.esc_url($post_url);
$link = '';
}else if($key === 'pinterest'){
$url = 'https://pinterest.com/pin/create/button/?url='.esc_url($post_url) ;
$link = '';
}else if($key === 'whatsapp'){
$url = 'https://web.whatsapp.com/send?text='. esc_url($post_url);
$link = '';
}
$social_share_html .= $link;
}
}
if($social_share_html){
$social_share_title = THFAQF_Utils::get_setting_value($settings, 'social_share_title');
?>
Edit FAQ
$value) {
if($value){
$global_settings[$key] = $value;
}
}
}
}
return $global_settings;
}
public function get_theme_wrapper_class(){
$current_theme = wp_get_theme();
$current_theme_name = isset($current_theme['Template']) ? $current_theme['Template'] : '';
$wrapper_class = '';
if($current_theme_name){
$wrapper_class = str_replace(' ', '-', strtolower($current_theme_name));
$wrapper_class = 'thfaqf-theme-wrapper-'.$wrapper_class;
}
return $wrapper_class;
}
public function prepare_colour_settings($faq_cleaned_color, $general_settings){
$settings_fields = THFAQF_Utils::get_settings_fields();
$use_post_settings = isset($faq_cleaned_color['use_post_settings']) ? $faq_cleaned_color['use_post_settings'] : '';
if(($use_post_settings == 'yes') and (!empty($faq_cleaned_color))){
$title_color = isset($faq_cleaned_color['title_color'])?$faq_cleaned_color['title_color']:'';
$title_bg_color = isset($faq_cleaned_color['title_bg_color'])?$faq_cleaned_color['title_bg_color']:'';
$content_color = isset($faq_cleaned_color['content_color'])?$faq_cleaned_color['content_color']:'';
$content_bg_color = isset($faq_cleaned_color['content_bg_color'])?$faq_cleaned_color['content_bg_color']:'';
}else{
$title_color = isset($general_settings['title_color'])?$general_settings['title_color']:$settings_fields['title_color']['value'];
$title_bg_color = isset($general_settings['title_bg_color'])?$general_settings['title_bg_color']:$settings_fields['title_bg_color']['value'];
$content_color = isset($general_settings['content_color'])?$general_settings['content_color']:$settings_fields['content_color']['value'];
$content_bg_color = isset($general_settings['content_bg_color'])?$general_settings['content_bg_color']:$settings_fields['content_bg_color']['value'];
}
$data = array(
'title_color' => $title_color,
'title_bg_color' => $title_bg_color,
'content_color' => $content_color,
'content_bg_color' => $content_bg_color,
);
return $data;
}
public function like_option($post_id,$key,$like_user_ids,$dislike_user_ids,$user_id){
ob_start(); // start capturing output
$liked_user = !empty($like_user_ids) ? explode(',', $like_user_ids): array();
$dislikeliked_user = !empty($dislike_user_ids) ? explode(',', $dislike_user_ids): array();
$like_count = is_array($liked_user) ? count($liked_user) : 0;
$dislike_count = is_array($dislikeliked_user) ? count($dislikeliked_user) : 0;
$l_color = in_array($user_id, $liked_user) ? 'color:black' : '';
$d_color = in_array($user_id, $dislikeliked_user) ? 'color:black' : '';
$user_login = $user_id<=0 ? wp_login_url( get_permalink() ) : '#';
?>
'Sorry, your nonce did not verify.');
wp_send_json($responce);
}else{
$user_id = get_current_user_id();
$value = isset($_REQUEST['value']) ? trim($_REQUEST['value']) : false;
$faq_uid = isset($_REQUEST['uid']) ? trim($_REQUEST['uid']) : false;
$post_id = isset($_REQUEST['post_id']) ? trim($_REQUEST['post_id']) : false;
// Sanitize and validate inputs
$value = sanitize_text_field($value);
// Cast numeric values with absint() before use
$faq_uid = absint($faq_uid);
$post_id = absint($post_id);
$faq_data = get_post_meta($post_id, THFAQF_Utils::OPTION_KEY_FAQ_ITEMS, true);
$result = array();
if ($user_id > 0 && is_array($faq_data) && isset($faq_data[$faq_uid])){
$faq_liked_user_string = !empty($faq_data[$faq_uid]['like_user_ids']) ? trim($faq_data[$faq_uid]['like_user_ids']) : false;
$faq_disliked_user_string = !empty($faq_data[$faq_uid]['dislike_user_ids']) ? trim($faq_data[$faq_uid]['dislike_user_ids']) : false;
$faq_liked_user_array = explode(',', $faq_liked_user_string);
$faq_disliked_user_array = explode(',', $faq_disliked_user_string);
if($value =='like'){
if($faq_disliked_user_string){
foreach ($faq_disliked_user_array as $key => $id){
if($user_id == $id){
unset($faq_disliked_user_array[$key]);
}
}
}
if($faq_liked_user_string){
if(!in_array($user_id, $faq_liked_user_array)){
array_push($faq_liked_user_array,$user_id);
}else{
foreach ($faq_liked_user_array as $key => $id){
if($user_id == $id){
unset($faq_liked_user_array[$key]);
}
}
}
}else{
$faq_liked_user_array = array($user_id);
}
}else{
if($faq_liked_user_string){
foreach ($faq_liked_user_array as $key => $id){
if($user_id == $id){
unset($faq_liked_user_array[$key]);
}
}
}
if($faq_disliked_user_string){
if(!in_array($user_id, $faq_disliked_user_array)){
array_push($faq_disliked_user_array,$user_id);
}else{
foreach ($faq_disliked_user_array as $key => $id){
if($user_id == $id){
unset($faq_disliked_user_array[$key]);
}
}
}
}else{
$faq_disliked_user_array = array($user_id);
}
}
$faq_liked_user_array = array_filter($faq_liked_user_array);
$faq_disliked_user_array = array_filter($faq_disliked_user_array);
$user_like = implode(',', $faq_liked_user_array);
$user_dislike = implode(',', $faq_disliked_user_array);
$faq_data[$faq_uid]['like_user_ids'] = $user_like;
$faq_data[$faq_uid]['dislike_user_ids'] = $user_dislike;
$update = update_post_meta($post_id, THFAQF_Utils::OPTION_KEY_FAQ_ITEMS,$faq_data);
$result = array('like_user_ids' => $faq_liked_user_array,'dislike_user_ids' => $faq_disliked_user_array,'current_user_id' => $user_id);
wp_send_json($result);
}
wp_send_json_error(array('message' => 'Unable to update like/dislike status.'));
}
}
public function get_additional_css_for_faqs($id){
$global_settings = THFAQF_Utils::get_faq_settings();
$css = isset($global_settings['thfaq_custom_css']) ? $global_settings['thfaq_custom_css'] : '';
$enable = isset($global_settings['enable_thfaq_custom_css']) ? $global_settings['enable_thfaq_custom_css'] : '';
$local_settings = get_post_meta($id, THFAQF_Utils::OPTION_KEY_FAQ_SETTINGS_POST, true);
$settings = $this->prepare_faq_settings($global_settings, $local_settings);
$title_active_color = isset($settings['title_active_color']) ? $settings['title_active_color'] : '#cc2753';
$tab_active_color = isset($settings['tab_active_color']) ? $settings['tab_active_color'] : '#cc2753';
$tab_bg_color = isset($settings['tab_bg_color']) ? $settings['tab_bg_color'] : '#f5f5f5';
?>
display_comment_box($post_id,$key);?>