PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.8.1
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.8.1
4.9.2 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 All 200 releases
betterdocs / includes / Editors / BlockEditor / Blocks / CategoryBox.php

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

265 lines 9.6 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' => 'name',
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 $terms_object = [
61 'taxonomy' => 'doc_category',
62 'order' => $attributes['order'],
63 'orderby' => $attributes['orderBy'],
64 'number' => isset( $attributes['boxPerPage'] ) ? $attributes['boxPerPage'] : 5,
65 'hide_empty' => true
66 ];
67
68 if ( 'doc_category_order' === $attributes['orderBy'] ) {
69 $terms_object['meta_key'] = 'doc_category_order';
70 $terms_object['orderby'] = 'meta_value_num';
71 }
72
73 $includes = $this->string_to_array( $attributes['includeCategories'] );
74 $excludes = $this->string_to_array( $attributes['excludeCategories'] );
75 $styles = '';
76
77 if ( ! empty( $includes ) ) {
78 $terms_object['include'] = array_diff( $includes, (array) $excludes );
79 }
80
81 if ( ! empty( $excludes ) ) {
82 $terms_object['exclude'] = $excludes;
83 }
84
85 $_wrapper_classes = [
86 'betterdocs-category-box-wrapper',
87 'betterdocs-blocks-grid',
88 'betterdocs-box-' . $attributes['layout']
89 ];
90
91 $_inner_wrapper_classes = [
92 'betterdocs-category-box-inner-wrapper',
93 'layout-flex',
94 $attributes['layout'] === 'default' ? 'layout-1' : $attributes['layout'],
95 "betterdocs-column-" . $attributes['colRange'],
96 "betterdocs-column-tablet-" . $attributes['TABcolRange'],
97 "betterdocs-column-mobile-" . $attributes['MOBcolRange']
98 ];
99
100 if ( $attributes['layout'] == 'layout-4' ) {
101 $_inner_wrapper_classes = [
102 'betterdocs-category-box-inner-wrapper',
103 'layout-4',
104 'docs-col-4',
105 'single-kb',
106 'betterdocs-categories-folder',
107 'layout-4',
108 'single-kb'
109 ];
110 $column = is_tax( 'doc_category' ) ? 3 : $attributes['layout4Col'];
111 $term_count = $this->get_terms_count_conditionally( $terms_object );
112 $reminder = $term_count % $column;
113 $styles .= "--column: $column;";
114 $styles .= "--count: $term_count;";
115 $styles .= "--reminder: $reminder;";
116 }
117
118 $wrapper_attr = [
119 'class' => $_wrapper_classes
120 ];
121
122 $inner_wrapper_attr = [
123 'class' => $_inner_wrapper_classes,
124 'style' => $styles,
125 'data-column_desktop' => $attributes['colRange'],
126 'data-column_tab' => $attributes['TABcolRange'],
127 'data-column_mobile' => $attributes['MOBcolRange']
128 ];
129
130 $default_multiple_kb = betterdocs()->settings->get( 'multiple_kb' );
131 $kb_slug = ! empty( $attributes['selectKB'] ) && isset( $attributes['selectKB'] ) ? json_decode( $attributes['selectKB'] )->value : '';
132
133 if ( is_tax( 'knowledge_base' ) && $default_multiple_kb == 1 ) {
134 $object = get_queried_object();
135 $terms_object['meta_query'] = [
136 'relation' => 'OR',
137 [
138 'key' => 'doc_category_knowledge_base',
139 'value' => $object->slug,
140 'compare' => 'LIKE'
141 ]
142 ];
143 }
144
145 if ( ! empty( $kb_slug ) ) {
146 $terms_object['meta_query'] = [
147 'relation' => 'OR',
148 [
149 'key' => 'doc_category_knowledge_base',
150 'value' => $kb_slug,
151 'compare' => 'LIKE'
152 ]
153 ];
154 }
155
156 $_params = [
157 'wrapper_attr' => $wrapper_attr,
158 'inner_wrapper_attr' => $inner_wrapper_attr,
159 'terms_query_args' => betterdocs()->query->terms_query( $terms_object ),
160 'widget_type' => 'category-box',
161 'multiple_knowledge_base' => $default_multiple_kb,
162 'nested_subcategory' => false,
163 'show_header' => true,
164 'show_description' => false
165 ];
166
167 if ( $attributes['layout'] === 'layout-2' ) {
168 $_params['count_prefix'] = '';
169 $_params['count_suffix'] = '';
170 $_params['count_suffix_singular'] = '';
171 }
172
173 if ( $attributes['layout'] == 'layout-4' ) {
174 $column = is_tax( 'doc_category' ) ? 3 : $attributes['layout4Col'];
175 $term_count = $this->get_terms_count_conditionally( $terms_object );
176 $reminder = $term_count % $column;
177 $_params['show_icon'] = $this->attributes['showIcon'];
178 $_params['show_count'] = $this->attributes['showCount'];
179 $_params['total_terms'] = $term_count;
180 $_params['reminder'] = $reminder;
181 $_params['category_icon'] = 'folder';
182 $_params['last_update'] = $this->attributes['showLastUpdatedTime'];
183 $_params['taxonomy'] = 'doc_category';
184 $_params['column'] = $column;
185 unset( $_params['inner_wrapper_attr']['data-column_desktop'] ); //not needed for layout 4
186 unset( $_params['inner_wrapper_attr']['data-column_tab'] ); // not needed for layout 4
187 unset( $_params['inner_wrapper_attr']['data-column_mobile'] ); // not needed for layout 4
188 }
189
190 return $_params;
191 }
192
193 public function render( $attributes, $content ) {
194 $_eligible = ( $attributes['layout'] == 'layout-2' || $attributes['layout'] == 'layout-3' || $attributes['layout'] == 'layout-4' );
195
196 if ( $attributes['layout'] == 'layout-4' ) {
197 add_filter( 'betterdocs_layout_filename', [$this, 'change_to_layout_four'], 15, 3 );
198 }
199
200 if( $attributes['layout'] == 'layout-4' && is_tax('doc_category') ) {
201 add_filter('betterdocs_base_terms_args', [$this, 'render_child_terms'], 10, 1);
202 add_filter('betterdocs_terms_query_args', [$this, 'modify_terms_params'], 10, 2);
203 }
204
205 $this->add_filter( $_eligible );
206 $this->views( 'layouts/base' );
207 $this->remove_filter( $_eligible );
208 }
209
210 public function change_to_layout_four() {
211 return 'layout-4';
212 }
213
214 /**
215 * Modify Terms Params Based On Doc Category Page
216 *
217 * @param array $_query_args
218 * @param array $_origin_args
219 * @return bool|array
220 */
221 public function modify_terms_params($_query_args, $_origin_args) {
222 $current_category_id = get_queried_object() != null ? get_queried_object()->term_id : '';
223 $count = count( betterdocs()->query->get_all_child_term_ids( 'doc_category', $current_category_id ) );
224 if( $count == 0 ) {
225 return false;
226 }
227 return $_query_args;
228 }
229
230 /**
231 * Render The Current Block Child Terms On Doc Category Page
232 *
233 * @param array $args
234 * @return array
235 */
236 public function render_child_terms($args) {
237 $current_category_id = get_queried_object() != null ? get_queried_object()->term_id : '';
238 $_nested_categories = betterdocs()->query->get_child_term_ids_by_parent_id( 'doc_category', $current_category_id );
239 if( ! empty( $_nested_categories ) ) {
240 $args['include'] = $_nested_categories;
241 }
242 return $args;
243 }
244
245 /**
246 * Get the term count based on doc_category page or other page
247 *
248 * @param array $term_params
249 *
250 * @return int
251 */
252 public function get_terms_count_conditionally( $term_params ) {
253 $count = 0;
254
255 if ( is_tax( 'doc_category' ) ) {
256 $current_category_id = get_queried_object() != null ? get_queried_object()->term_id : '';
257 $count = count( betterdocs()->query->get_all_child_term_ids( 'doc_category', $current_category_id ) );
258 } else {
259 $count = count( get_terms( $term_params ) );
260 }
261
262 return $count;
263 }
264 }
265