wordpress FAQ plugin

This commit is contained in:
Egor Svitin
2019-02-19 10:21:31 +02:00
parent ae820d2169
commit a134080f14
8 changed files with 216 additions and 1 deletions

View File

@@ -65,6 +65,7 @@ add_action('wp_enqueue_scripts', function () {
wp_enqueue_style('jquery-ui', '//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css', ['bootstrap'], null);
wp_enqueue_script('jquery-ui', '//code.jquery.com/ui/1.12.1/jquery-ui.min.js', ['custom-jquery'], null, true);
wp_enqueue_script('lawcarta-js', get_theme_file_uri('/assets/js/faqSearch.js'), ['custom-jquery'], null, true);
wp_enqueue_style('lawcarta-external-style', get_theme_file_uri('/assets/css/style.min.css'), ['jquery-ui']);
wp_enqueue_style('lawcarta-newcss', get_theme_file_uri('/assets/css/newcss.min.css'), ['lawcarta-external-style']);
@@ -122,3 +123,62 @@ add_filter('tiny_mce_before_init', function ($settings) {
$settings['valid_children']="+a[div|p|ul|ol|li|h1|h2|h3|h4|h5|h5|h6]";
return $settings;
});
function enableSearchQuery()
{
return !empty($_POST['search_text']);
}
function getFaqPosts($args) {
if (enableSearchQuery()) {
// WP magic
add_filter( 'posts_where', 'my_filter_post_where' );
}
$posts = get_posts($args);
if (enableSearchQuery()) {
remove_filter( 'posts_where', 'my_filter_post_where' );
}
return $posts;
}
function getFaaTaxonomyIds()
{
if (!enableSearchQuery())
{
return false;
}
$posts = getFaqPosts([
'post_type' => 'fac_category_answer',
'suppress_filters' => false,
'numberposts' => -1,
]);
if (count($posts) == 0) {
return [];
}
$tIds = [];
foreach ($posts as $post) {
$taxonomies = get_the_terms($post, 'fac_category_type');
if (count($taxonomies) > 0) {
foreach ($taxonomies as $taxonomy) {
if (!in_array($taxonomy->term_id, $tIds)) {
$tIds[] = $taxonomy->term_id;
}
}
}
}
return $tIds;
}
function my_filter_post_where( $where) {
global $wpdb;
if (!enableSearchQuery()) {
return $where;
}
$where .= ' AND (
' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( $_POST['search_text'] ) . '%\' OR
' . $wpdb->posts . '.post_content LIKE \'%' . esc_sql( $_POST['search_text'] ) . '%\'
)';
return $where;
}