From a134080f14ec0ffefeaad59c04077c12e4a3eb9d Mon Sep 17 00:00:00 2001 From: Egor Svitin Date: Tue, 19 Feb 2019 10:21:31 +0200 Subject: [PATCH] wordpress FAQ plugin --- .gitignore | 1 + composer.json | 3 +- .../themes/lawcarta/assets/js/faqSearch.js | 5 + htdocs/wp-content/themes/lawcarta/footer.php | 1 + .../wp-content/themes/lawcarta/functions.php | 60 ++++++++++++ .../wp-content/themes/lawcarta/page_fac.php | 96 +++++++++++++++++++ .../lawcarta/single-fac_category_answer.php | 46 +++++++++ .../themes/livecarta/assets/js/faqSearch.js | 5 + 8 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 htdocs/wp-content/themes/lawcarta/assets/js/faqSearch.js create mode 100644 htdocs/wp-content/themes/lawcarta/page_fac.php create mode 100644 htdocs/wp-content/themes/lawcarta/single-fac_category_answer.php create mode 100644 htdocs/wp-content/themes/livecarta/assets/js/faqSearch.js diff --git a/.gitignore b/.gitignore index 291372f..0f91f93 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ htdocs/* !htdocs/wp-config-local-sample.php htdocs/wp-content/* +htdocs/wp-content/plugins/faq !htdocs/wp-content/mu-plugins/ !htdocs/wp-content/plugins/ !htdocs/wp-content/themes/ diff --git a/composer.json b/composer.json index 85326c9..9f3be4f 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "wpackagist-plugin/menu-item-custom-fields": "~1.0.0", "wpackagist-plugin/wp-rest-api-v2-menus": "~0.3.1", "wpackagist-plugin/wp-mail-smtp": "~1.4.1", - "wpackagist-plugin/cf7-field-validation": "~1.1.3" + "wpackagist-plugin/cf7-field-validation": "~1.1.3", + "nsp-code/taxonomy-terms-order": "~1.5.5", }, "repositories": [ { diff --git a/htdocs/wp-content/themes/lawcarta/assets/js/faqSearch.js b/htdocs/wp-content/themes/lawcarta/assets/js/faqSearch.js new file mode 100644 index 0000000..4976296 --- /dev/null +++ b/htdocs/wp-content/themes/lawcarta/assets/js/faqSearch.js @@ -0,0 +1,5 @@ +document.addEventListener("DOMContentLoaded", function(event) { + $('.start-search').on('click', function(){ + $('#search-form').submit(); + }); +}) \ No newline at end of file diff --git a/htdocs/wp-content/themes/lawcarta/footer.php b/htdocs/wp-content/themes/lawcarta/footer.php index e8f1ea1..42bb98a 100644 --- a/htdocs/wp-content/themes/lawcarta/footer.php +++ b/htdocs/wp-content/themes/lawcarta/footer.php @@ -75,6 +75,7 @@ require_once 'inc/walker-nav-menu.php'; + \ No newline at end of file diff --git a/htdocs/wp-content/themes/lawcarta/functions.php b/htdocs/wp-content/themes/lawcarta/functions.php index bd7baa6..2cd102e 100644 --- a/htdocs/wp-content/themes/lawcarta/functions.php +++ b/htdocs/wp-content/themes/lawcarta/functions.php @@ -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; +} diff --git a/htdocs/wp-content/themes/lawcarta/page_fac.php b/htdocs/wp-content/themes/lawcarta/page_fac.php new file mode 100644 index 0000000..684bc9a --- /dev/null +++ b/htdocs/wp-content/themes/lawcarta/page_fac.php @@ -0,0 +1,96 @@ + 'fac_category_type', + 'hide_empty' => true, + 'orderby' => 'term_order', + 'include' => $tIds + ]); + } +} else { + $terms = get_terms( [ + 'taxonomy' => 'fac_category_type', + 'hide_empty' => true, + 'orderby' => 'term_order', + ]); +} + + + + +?> + +
+ + + + +
+
+ +
+
+ name?> +
+
+
+ 'fac_category_answer', + 'numberposts' => -1, + 'suppress_filters' => false, + 'tax_query' => [ + [ + 'taxonomy' => 'fac_category_type', + 'field' => 'id', + 'terms' => $term->term_id, + 'include_children' => false + ] + ] + ]); + foreach ($posts as $post) { + + echo ' + + '; + } + + ?> +
+
+
+ +
+
+
+ + +
+ + + + +
+
+
+

post_name?>

+
+ post_content?> +
+
+
+
+
+