PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.6.1
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.6.1
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Shortcodes / CategoryGrid.php

CategoryGrid.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.6.1, at includes/Shortcodes/CategoryGrid.php

235 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:disable WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- shortcode exposes user-driven exclusion attribute.
3 namespace WPDeveloper\BetterDocs\Shortcodes;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9
10 use WPDeveloper\BetterDocs\Utils\Helper;
11 use WPDeveloper\BetterDocs\Core\Shortcode;
12
13 class CategoryGrid extends Shortcode {
14 protected $layout_class = 'layout-1';
15
16 /**
17 * A list of deprecated attributes.
18 * @var array<string, string>
19 */
20 protected $deprecated_attributes = [
21 'category' => 'taxonomy',
22 'posts_per_grid' => 'posts_per_page',
23 'icon' => 'show_icon',
24 'post_counter' => 'show_count'
25 ];
26
27 public function get_name() {
28 return 'betterdocs_category_grid';
29 }
30
31 public function get_style_depends() {
32 return [ 'betterdocs-category-grid' ];
33 }
34
35 public function get_script_depends() {
36 return [ 'betterdocs-category-grid' ];
37 }
38
39 /**
40 * Summary of default_attributes
41 * @return array
42 */
43 public function default_attributes() {
44 return [
45 'sidebar_list' => false,
46 'taxonomy' => 'doc_category',
47 'show_icon' => true,
48 'category_icon' => '',
49 'masonry' => false,
50 'posts_per_page' => $this->settings->get( 'posts_number', 0 ),
51 'orderby' => $this->settings->get( 'alphabetically_order_post', 'betterdocs_order' ),
52 'order' => $this->settings->get( 'docs_order', 'ASC' ),
53 'show_count' => $this->settings->get( 'post_count' ),
54 'count_suffix' => '',
55 'count_suffix_singular' => '',
56 'column' => $this->settings->get( 'column_number' ),
57 'nested_subcategory' => $this->settings->get( 'nested_subcategory' ),
58 'terms' => '',
59 'terms_orderby' => '',
60 'terms_order' => '',
61 'terms_include' => '',
62 'terms_exclude' => '',
63 'terms_offset' => '',
64 'kb_slug' => '',
65 'multiple_knowledge_base' => false,
66 'disable_customizer_style' => false,
67 'title_tag' => 'h2',
68 'category_title_link' => false,
69 'layout_type' => '',
70 'list_icon_url' => '',
71 'list_icon_name' => 'list',
72 'show_list_icon' => true,
73 'sidebar_layout' => '',
74 'wrapper_class' => []
75 ];
76 }
77
78 public function generate_attributes() {
79 $attributes = [
80 'class' => [
81 'betterdocs-category-grid-inner-wrapper',
82 $this->layout_class
83 ]
84 ];
85
86 $masonry = (bool) $this->settings->get( 'masonry_layout', false );
87 if ( $this->has( 'masonry' ) ) {
88 $masonry = (bool) $this->attributes['masonry'];
89 }
90
91 if ( ! is_singular( 'docs' ) && ! is_tax( 'doc_category' ) && ! is_tax( 'doc_tag' ) ) {
92 if ( $this->attributes['sidebar_list'] == true ) {
93 $attributes['class'][] = 'layout-flex';
94 } elseif ( $masonry == true ) {
95 wp_enqueue_script( 'masonry' );
96 $attributes['class'][] = 'masonry';
97 } else {
98 $attributes['class'][] = 'layout-flex';
99 }
100 if ( $this->attributes['sidebar_list'] == true ) {
101 $_column_val = 1;
102 } elseif ( $this->isset( 'column' ) ) {
103 $_column_val = $this->attributes['column'];
104 } else {
105 $_column_val = $this->settings->get( 'column_number' );
106 }
107
108 $attributes['class'][] = 'docs-col-' . $_column_val;
109 $attributes['data-column_desktop'] = esc_html( $_column_val );
110 $attributes['style'] = "--column: $_column_val;";
111
112 if ( $this->isset( 'disable_customizer_style', false ) ) {
113 $attributes['class'][] = 'single-kb';
114 }
115 }
116
117
118 return $attributes;
119 }
120
121 public function header_layout_sequence( $sequence, $layout, $widget_type, $args ) {
122 return [ 'category_icon', 'category_title', 'category_counts', 'collapse_icon' ];
123 }
124
125 public function render( $atts, $content = null ) {
126 if ( (bool) $this->attributes['sidebar_list'] ) {
127 add_filter( 'betterdocs_header_layout_sequence', [ $this, 'header_layout_sequence' ], 10, 4 );
128 }
129
130 $this->views( 'layouts/base' );
131
132 if ( (bool) $this->attributes['sidebar_list'] ) {
133 remove_filter( 'betterdocs_header_layout_sequence', [ $this, 'header_layout_sequence' ], 10 );
134 }
135 }
136
137 public function view_params() {
138 $exploremore_btn = $this->settings->get( 'exploremore_btn' );
139 $button_text = $this->settings->get( 'exploremore_btn_txt' );
140 $category_title_link = isset( $this->attributes['category_title_link'] ) ? $this->attributes['category_title_link'] : '';
141
142 $show_button = false;
143 if ( $this->attributes['posts_per_page'] == -1 ) {
144 $show_button = false;
145 } elseif ( $exploremore_btn && ! is_singular( 'docs' ) && Helper::get_tax() != 'doc_category' && ! is_tax( 'doc_tag' ) ) {
146 $show_button = true;
147 }
148
149 $terms_query = $this->query->terms_query(
150 [
151 'taxonomy' => $this->attributes['taxonomy'],
152 'multiple_kb' => $this->attributes['multiple_knowledge_base'],
153 'kb_slug' => $this->attributes['kb_slug'],
154 'terms' => $this->attributes['terms'],
155 'order' => $this->attributes['terms_order'],
156 'orderby' => $this->attributes['terms_orderby'],
157 'nested_subcategory' => $this->attributes['nested_subcategory']
158 ]
159 );
160
161 if ( $this->attributes['terms_include'] ) {
162 $terms_query['include'] = $this->attributes['terms_include'];
163 }
164
165 if ( $this->attributes['terms_exclude'] ) {
166 $terms_query['exclude'] = $this->attributes['terms_exclude'];
167 }
168
169 if ( $this->attributes['terms_offset'] ) {
170 $terms_query['offset'] = (int) $this->attributes['terms_offset'];
171 }
172
173 $inner_wrapper_attr = $this->generate_attributes();
174
175 $docs_query = [
176 'orderby' => $this->attributes['orderby'],
177 'order' => $this->attributes['order'],
178 'posts_per_page' => $this->attributes['posts_per_page']
179 ];
180
181 /**
182 * Add This Attribute When Using Outside Betterdocs Templates Only
183 */
184 if ( $this->attributes['multiple_knowledge_base'] && ( ! empty( $this->attributes['kb_slug'] ) ) && ( ! betterdocs()->helper->is_templates() ) ) {
185 $inner_wrapper_attr['data-mkb-slug'] = $this->attributes['kb_slug'];
186 }
187
188 // Prepare wrapper attributes with custom classes
189 $wrapper_attr_classes = [ 'betterdocs-category-grid-wrapper' ];
190
191 // Add custom wrapper classes if provided
192 if ( $this->isset( 'wrapper_class' ) ) {
193 $wrapper_classes = $this->attributes['wrapper_class'];
194
195 // Handle both array and string formats
196 if ( is_string( $wrapper_classes ) ) {
197 // Split by comma or space and clean up
198 $wrapper_classes = preg_split( '/[,\s]+/', $wrapper_classes );
199 $wrapper_classes = array_filter( array_map( 'trim', $wrapper_classes ) );
200 } elseif ( is_array( $wrapper_classes ) ) {
201 $wrapper_classes = array_filter( $wrapper_classes );
202 }
203
204 if ( ! empty( $wrapper_classes ) ) {
205 $wrapper_attr_classes = array_merge( $wrapper_attr_classes, $wrapper_classes );
206 }
207 }
208
209 return [
210 'wrapper_attr' => [ 'class' => $wrapper_attr_classes ],
211 'inner_wrapper_attr' => $inner_wrapper_attr,
212 'layout' => 'default',
213 'widget_type' => 'category-grid',
214 'terms_query_args' => $terms_query,
215 'docs_query_args' => $docs_query,
216 'nested_docs_query_args' => $docs_query,
217 'list_icon_name' => ($this->attributes['list_icon_name'] ?? 'list') == 'list' ? 'list' : [ 'value' => $this->attributes['list_icon_name'] ?? 'list' ],
218 'show_header' => true,
219 'show_list' => true,
220 'show_title' => true,
221 'show_button' => $show_button,
222 'button_text' => $button_text,
223 'show_button_icon' => true,
224 'button_icon_position' => true,
225 'title_tag' => $this->attributes['title_tag'] ?? 'h2',
226 'button_icon' => true,
227 'category_title_link' => $category_title_link,
228 'layout_type' => $this->attributes['layout_type'] ?? '',
229 'list_icon_url' => $this->attributes['list_icon_url'] ?? '',
230 'show_list_icon' => $this->attributes['show_list_icon'] ?? true,
231 'sidebar_layout' => $this->attributes['sidebar_layout'] ?? ''
232 ];
233 }
234 }
235