PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.5.3
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.5.3
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 / Editors / BlockEditor / Blocks / CategoryBox.php

CategoryBox.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.5.3, at includes/Editors/BlockEditor/Blocks/CategoryBox.php

274 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\Editors\BlockEditor\Blocks;
4
5 use WPDeveloper\BetterDocs\Editors\BlockEditor\Block;
6 use WPDeveloper\BetterDocs\Traits\CategoryBox as CategoryBoxTraits;
7
8 class CategoryBox extends Block {
9 use CategoryBoxTraits;
10
11 protected $editor_styles = [
12 'betterdocs-fontawesome',
13 'betterdocs-blocks-editor',
14 'betterdocs-blocks-category-box'
15 ];
16
17 protected $frontend_styles = [
18 'betterdocs-fontawesome',
19 'betterdocs-blocks-category-box',
20 'betterdocs-docs'
21 ];
22
23 /**
24 * unique name of block
25 * @return string
26 */
27 public function get_name() {
28 return 'categorybox';
29 }
30
31 public function get_default_attributes() {
32 return [
33 'blockId' => '',
34 'categories' => [],
35 'selectKB' => '',
36 'includeCategories' => '',
37 'excludeCategories' => '',
38 'boxPerPage' => 9,
39 'orderBy' => 'doc_category_order',
40 'order' => 'asc',
41 'layout' => 'default',
42 'showIcon' => true,
43 'showTitle' => true,
44 'titleTag' => 'h2',
45 'showCount' => true,
46 'prefix' => '',
47 'suffix' => __( 'Docs', 'betterdocs' ),
48 'suffixSingular' => __( 'Doc', 'betterdocs' ),
49 'colRange' => 3,
50 'TABcolRange' => 2,
51 'MOBcolRange' => 1,
52 'layout4Col' => 4,
53 'showLastUpdatedTime' => true
54 ];
55 }
56
57 public function view_params() {
58 $attributes = &$this->attributes;
59
60 // Sanitize the layout attribute to prevent path traversal (LFI).
61 $attributes['layout'] = sanitize_file_name( $attributes['layout'] );
62
63 $terms_object = [
64 'taxonomy' => 'doc_category',
65 'order' => $attributes['order'],
66 'orderby' => $attributes['orderBy'],
67 'number' => isset( $attributes['boxPerPage'] ) ? $attributes['boxPerPage'] : 5,
68 'hide_empty' => true
69 ];
70
71 if ( 'doc_category_order' === $attributes['orderBy'] ) {
72 // Use betterdocs_order which handles fallback logic automatically
73 $terms_object['orderby'] = 'betterdocs_order';
74 }
75
76 $includes = $this->string_to_array( $attributes['includeCategories'] );
77 $excludes = $this->string_to_array( $attributes['excludeCategories'] );
78 $styles = '';
79
80 if ( ! empty( $includes ) ) {
81 $terms_object['include'] = array_diff( $includes, (array) $excludes );
82 }
83
84 if ( ! empty( $excludes ) ) {
85 $terms_object['exclude'] = $excludes;
86 }
87
88 $_wrapper_classes = [
89 'betterdocs-category-box-wrapper',
90 'betterdocs-blocks-grid',
91 'betterdocs-box-' . $attributes['layout']
92 ];
93
94 $_inner_wrapper_classes = [
95 'betterdocs-category-box-inner-wrapper',
96 'layout-flex',
97 $attributes['layout'] === 'default' ? 'layout-1' : $attributes['layout'],
98 'betterdocs-column-' . $attributes['colRange'],
99 'betterdocs-column-tablet-' . $attributes['TABcolRange'],
100 'betterdocs-column-mobile-' . $attributes['MOBcolRange']
101 ];
102
103 if ( $attributes['layout'] == 'layout-4' ) {
104 $_inner_wrapper_classes = [
105 'betterdocs-category-box-inner-wrapper',
106 'layout-4',
107 'docs-col-4',
108 'single-kb',
109 'betterdocs-categories-folder',
110 'layout-4',
111 'single-kb'
112 ];
113 $column = is_tax( 'doc_category' ) ? 3 : $attributes['layout4Col'];
114 $term_count = $this->get_terms_count_conditionally( $terms_object );
115 $reminder = $term_count % $column;
116 $styles .= "--column: $column;";
117 $styles .= "--count: $term_count;";
118 $styles .= "--reminder: $reminder;";
119 }
120
121 $wrapper_attr = [
122 'class' => $_wrapper_classes
123 ];
124
125 $inner_wrapper_attr = [
126 'class' => $_inner_wrapper_classes,
127 'style' => $styles,
128 'data-column_desktop' => $attributes['colRange'],
129 'data-column_tab' => $attributes['TABcolRange'],
130 'data-column_mobile' => $attributes['MOBcolRange']
131 ];
132
133 $default_multiple_kb = betterdocs()->settings->get( 'multiple_kb' );
134
135 $select_kb = isset( $attributes['selectKB'] ) ? $attributes['selectKB'] : '';
136 if ( is_string( $select_kb ) && $select_kb !== '' ) {
137 $decoded = json_decode( $select_kb, true );
138 $select_kb = is_array( $decoded ) ? $decoded : [];
139 }
140 $kb_slug = is_array( $select_kb ) && isset( $select_kb['value'] ) ? $select_kb['value'] : '';
141
142 if ( is_tax( 'knowledge_base' ) && $default_multiple_kb == 1 ) {
143 $object = get_queried_object();
144 $kb_slug = $object->slug;
145 }
146
147 if ( ! empty( $kb_slug ) && $default_multiple_kb == 1 ) {
148 $terms_object['meta_query'] = [
149 'relation' => 'OR',
150 [
151 'key' => 'doc_category_knowledge_base',
152 'value' => $kb_slug,
153 'compare' => 'LIKE'
154 ]
155 ];
156 }
157
158 $_params = [
159 'wrapper_attr' => $wrapper_attr,
160 'inner_wrapper_attr' => $inner_wrapper_attr,
161 'terms_query_args' => betterdocs()->query->terms_query( $terms_object ),
162 'widget_type' => 'category-box',
163 'multiple_knowledge_base' => ( $default_multiple_kb && ! empty( $kb_slug ) ) ? true : false,
164 'kb_slug' => $kb_slug,
165 'nested_subcategory' => false,
166 'show_header' => true,
167 'show_description' => false,
168 'title_tag' => $this->attributes['titleTag']
169 ];
170
171 if ( $attributes['layout'] === 'layout-2' ) {
172 $_params['count_prefix'] = '';
173 $_params['count_suffix'] = '';
174 $_params['count_suffix_singular'] = '';
175 }
176
177 if ( $attributes['layout'] == 'layout-4' ) {
178 $column = is_tax( 'doc_category' ) ? 3 : $attributes['layout4Col'];
179 $term_count = $this->get_terms_count_conditionally( $terms_object );
180 $reminder = $term_count % $column;
181 $_params['show_icon'] = $this->attributes['showIcon'];
182 $_params['show_count'] = $this->attributes['showCount'];
183 $_params['total_terms'] = $term_count;
184 $_params['reminder'] = $reminder;
185 $_params['category_icon'] = 'folder';
186 $_params['last_update'] = $this->attributes['showLastUpdatedTime'];
187 $_params['taxonomy'] = 'doc_category';
188 $_params['column'] = $column;
189 unset( $_params['inner_wrapper_attr']['data-column_desktop'] ); //not needed for layout 4
190 unset( $_params['inner_wrapper_attr']['data-column_tab'] ); // not needed for layout 4
191 unset( $_params['inner_wrapper_attr']['data-column_mobile'] ); // not needed for layout 4
192 }
193
194 return $_params;
195 }
196
197 public function render( $attributes, $content ) {
198 $_eligible = ( $attributes['layout'] == 'layout-2' || $attributes['layout'] == 'layout-3' || $attributes['layout'] == 'layout-4' );
199
200 if ( $attributes['layout'] == 'layout-4' ) {
201 add_filter( 'betterdocs_layout_filename', [ $this, 'change_to_layout_four' ], 15, 3 );
202 }
203
204 if ( $attributes['layout'] == 'layout-4' && is_tax( 'doc_category' ) ) {
205 add_filter( 'betterdocs_base_terms_args', [ $this, 'render_child_terms' ], 10, 1 );
206 add_filter( 'betterdocs_terms_query_args', [ $this, 'modify_terms_params' ], 10, 2 );
207 }
208
209 $this->add_filter( $_eligible );
210 $this->views( 'layouts/base' );
211 $this->remove_filter( $_eligible );
212
213 if ( $attributes['layout'] == 'layout-4' ) {
214 remove_filter( 'betterdocs_layout_filename', [ $this, 'change_to_layout_four' ], 15, 3 );
215 }
216
217 }
218
219 public function change_to_layout_four() {
220 return 'layout-4';
221 }
222
223 /**
224 * Modify Terms Params Based On Doc Category Page
225 *
226 * @param array $_query_args
227 * @param array $_origin_args
228 * @return bool|array
229 */
230 public function modify_terms_params( $_query_args, $_origin_args ) {
231 $current_category_id = get_queried_object() != null ? get_queried_object()->term_id : '';
232 $count = count( betterdocs()->query->get_all_child_term_ids( 'doc_category', $current_category_id ) );
233 if ( $count == 0 ) {
234 return false;
235 }
236 return $_query_args;
237 }
238
239 /**
240 * Render The Current Block Child Terms On Doc Category Page
241 *
242 * @param array $args
243 * @return array
244 */
245 public function render_child_terms( $args ) {
246 $current_category_id = get_queried_object() != null ? get_queried_object()->term_id : '';
247 $_nested_categories = betterdocs()->query->get_child_term_ids_by_parent_id( 'doc_category', $current_category_id );
248 if ( ! empty( $_nested_categories ) ) {
249 $args['include'] = $_nested_categories;
250 }
251 return $args;
252 }
253
254 /**
255 * Get the term count based on doc_category page or other page
256 *
257 * @param array $term_params
258 *
259 * @return int
260 */
261 public function get_terms_count_conditionally( $term_params ) {
262 $count = 0;
263
264 if ( is_tax( 'doc_category' ) ) {
265 $current_category_id = get_queried_object() != null ? get_queried_object()->term_id : '';
266 $count = count( betterdocs()->query->get_all_child_term_ids( 'doc_category', $current_category_id ) );
267 } else {
268 $count = count( get_terms( $term_params ) );
269 }
270
271 return $count;
272 }
273 }
274