| 1 |
<?php |
| 2 |
/** |
| 3 |
* Common functions class |
| 4 |
*/ |
| 5 |
class Qcld_WPBot_Common_Functions { |
| 6 |
|
| 7 |
/** |
| 8 |
* Remove stopwords from search query |
| 9 |
* |
| 10 |
* @param string $query The search query |
| 11 |
* @param array $stopwords Array of stopwords to remove |
| 12 |
* @return string Query with stopwords removed |
| 13 |
*/ |
| 14 |
public static function qcpd_remove_wa_stopwords($query, $stopwords) { |
| 15 |
return preg_replace('/\b('.implode('|',$stopwords).')\b/','',$query); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Get relevant page links based on search query |
| 20 |
* |
| 21 |
* @param string $search_query The search query |
| 22 |
* @return array Array of relevant page links |
| 23 |
*/ |
| 24 |
public static function qcpd_relevant_pagelink($search_query) { |
| 25 |
$stopwords = explode(',', get_option('qlcd_wp_chatbot_stop_words')); |
| 26 |
|
| 27 |
$finalQueryWordsWithoutStopWords = self::qcpd_remove_wa_stopwords(strtolower($search_query), $stopwords); |
| 28 |
|
| 29 |
$cleanWordsWithoutPunctuationMarks = preg_replace('/[\p{P}]/u', '', $finalQueryWordsWithoutStopWords); |
| 30 |
|
| 31 |
$q = trim($cleanWordsWithoutPunctuationMarks); |
| 32 |
|
| 33 |
$links = []; |
| 34 |
|
| 35 |
$post_type_array = get_option('qcld_openai_relevant_post'); |
| 36 |
|
| 37 |
$the_query = new WP_Query(array( |
| 38 |
'post_status' => 'publish', |
| 39 |
'posts_per_page' => 5, |
| 40 |
's' => esc_attr($q), |
| 41 |
'post_type' => $post_type_array |
| 42 |
)); |
| 43 |
|
| 44 |
if($the_query->have_posts()) { |
| 45 |
while($the_query->have_posts()) { |
| 46 |
$the_query->the_post(); |
| 47 |
|
| 48 |
$url = esc_url(get_permalink()); |
| 49 |
$link = '<a href=' . $url . ' target="_blank">' . get_the_title() . '</a>'; |
| 50 |
array_push($links, $link); |
| 51 |
} |
| 52 |
wp_reset_postdata(); |
| 53 |
} |
| 54 |
|
| 55 |
$links = array_unique($links); |
| 56 |
return $links; |
| 57 |
} |
| 58 |
|
| 59 |
} |
| 60 |
|