PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 2.3.6
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v2.3.6
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 / gutenberg / blocks / categorygrid.php

categorygrid.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 2.3.6, at includes/gutenberg/blocks/categorygrid.php

239 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Functions to register client-side assets (scripts and stylesheets) for the
5 * Gutenberg block.
6 *
7 * @package betterdocs
8 */
9
10 /**
11 * Registers all block assets so that they can be enqueued through Gutenberg in
12 * the corresponding context.
13 *
14 * @see https://wordpress.org/gutenberg/handbook/designers-developers/developers/tutorials/block-tutorial/applying-styles-with-stylesheets/
15 */
16 function betterdocs_categorygrid_block_init()
17 {
18 // Skip block registration if Gutenberg is not enabled/merged.
19 if (!function_exists('register_block_type')) {
20 return;
21 }
22 $dir = dirname(__FILE__);
23
24 $index_js = 'categorygrid/index.js';
25 wp_register_script(
26 'betterdocs-categorygrid-block-editor',
27 plugins_url($index_js, __FILE__),
28 array(
29 'wp-blocks',
30 'wp-i18n',
31 'wp-element',
32 'wp-editor',
33 'wp-block-editor',
34 'masonry',
35 'betterdocs-blocks-edit-post'
36 ),
37 filemtime("$dir/$index_js")
38 );
39
40 wp_register_style(
41 'betterdocs-fontawesome-frontend',
42 BETTERDOCS_URL . 'admin/assets/css/font-awesome5.css',
43 array(),
44 BETTERDOCS_VERSION,
45 'all'
46 );
47
48 $editor_style = 'categorygrid/style.css';
49 wp_register_style(
50 'betterdocs-categorygrid-block-editor',
51 plugins_url($editor_style, __FILE__),
52 array('betterdocs-fontawesome-frontend'),
53 filemtime("$dir/$editor_style"),
54 'all'
55 );
56
57 $grid_script = 'categorygrid/assets/js/categorygrid.js';
58 wp_register_script(
59 'betterdocs-categorygrid-js',
60 plugins_url($grid_script, __FILE__),
61 array('masonry'),
62 filemtime("$dir/$grid_script")
63 );
64
65 register_block_type(__DIR__ . '/categorygrid', array(
66 'editor_script' => 'betterdocs-categorygrid-block-editor',
67 'editor_style' => 'betterdocs-categorygrid-block-editor',
68 'render_callback' => 'betterdocs_categorygrid_server_side_render'
69 ));
70 }
71 add_action('init', 'betterdocs_categorygrid_block_init');
72
73 /**
74 * Category Grid Server Side Render
75 */
76 function betterdocs_categorygrid_server_side_render(array $attributes)
77 {
78 if (!is_admin()) {
79 wp_enqueue_style("betterdocs-categorygrid-block-editor");
80 wp_enqueue_script("betterdocs-categorygrid-js");
81 }
82
83 $attributes = wp_parse_args(
84 $attributes,
85 [
86 'blockId' => '',
87 'categories' => array(),
88 'includeCategories' => '',
89 'excludeCategories' => '',
90 'gridPerPage' => 9,
91 'orderBy' => 'name',
92 'order' => 'asc',
93 'layout' => 'default',
94 'showIcon' => true,
95 'showTitle' => true,
96 'titleTag' => 'h2',
97 'showCount' => true,
98 'showIcon' => true,
99 'showTitle' => true,
100 'showList' => true,
101 'layoutMode' => 'grid',
102 'showHeader' => true,
103 'showButton' => true,
104 'buttonText' => esc_html__('Explore More', 'betterdocs'),
105 'listIcon' => 'far fa-file-alt',
106 'postsPerPage' => 5,
107 'postsOrderBy' => 'date',
108 'postsOrder' => 'asc',
109 'enableNestedSubcategory' => false,
110 'postPerSubcategory' => 3,
111 'buttonIconPosition' => 'after',
112 'buttonIcon' => 'fas fa-angle-right',
113 'buttonPosition' => 'after',
114 'showButtonIcon' => true,
115 'colRange' => 3,
116 'TABcolRange' => 2,
117 'MOBcolRange' => 1,
118 'gridSpaceRange' => 10,
119 'TABgridSpaceRange' => 10,
120 'MOBgridSpaceRange' => 10,
121 ]
122 );
123
124 $blockId = $attributes['blockId'];
125 $includeCategories = $attributes['includeCategories'];
126 $excludeCategories = $attributes['excludeCategories'];
127 $gridPerPage = $attributes['gridPerPage'];
128 $orderBy = $attributes['orderBy'];
129 $order = $attributes['order'];
130 $layout = $attributes['layout'];
131 $showIcon = $attributes['showIcon'];
132 $showTitle = $attributes['showTitle'];
133 $titleTag = $attributes['titleTag'];
134 $showCount = $attributes['showCount'];
135 $enableNestedSubcategory = $attributes['enableNestedSubcategory'];
136 $postsPerPage = $attributes['postsPerPage'];
137 $postsOrderBy = $attributes['postsOrderBy'];
138 $postsOrder = $attributes['postsOrder'];
139 $colRange = $attributes['colRange'];
140 $TABcolRange = $attributes['TABcolRange'];
141 $MOBcolRange = $attributes['MOBcolRange'];
142 $showHeader = $attributes['showHeader'];
143 $showList = $attributes['showList'];
144 $listIcon = $attributes['listIcon'];
145 $showButton = $attributes['showButton'];
146 $showButtonIcon = $attributes['showButtonIcon'];
147 $buttonIconPosition = $attributes['buttonIconPosition'];
148 $buttonIcon = $attributes['buttonIcon'];
149 $buttonText = $attributes['buttonText'];
150 $postPerSubcategory = $attributes['postPerSubcategory'];
151 $layoutMode = $attributes['layoutMode'];
152 $gridSpaceRange = $attributes['gridSpaceRange'];
153 $TABgridSpaceRange = $attributes['TABgridSpaceRange'];
154 $MOBgridSpaceRange = $attributes['MOBgridSpaceRange'];
155 $multiple_kb = false;
156
157 // responsive class name
158 $gridColomnDesktopClass = $colRange ? " betterdocs-grid-$colRange" : "betterdocs-grid-3";
159 $gridColomnTabClass = $TABcolRange ? " betterdocs-grid-tablet-$TABcolRange" : "betterdocs-grid-tablet-2";
160 $gridColomnMobileClass = $MOBcolRange ? " betterdocs-grid-mobile-$MOBcolRange" : "betterdocs-grid-mobile-1";
161
162 $terms_object = array(
163 'taxonomy' => 'doc_category',
164 'order' => $order,
165 'orderby' => $orderBy,
166 'number' => $gridPerPage ? $gridPerPage : 5,
167 'hide_empty' => true,
168 );
169
170 if ('doc_category_order' === $orderBy) {
171 $terms_object['meta_key'] = 'doc_category_order';
172 $terms_object['orderby'] = 'meta_value_num';
173 }
174
175 if ($enableNestedSubcategory) {
176 $terms_object['parent'] = 0;
177 }
178
179 $includes = array();
180 if ($includeCategories && $includeCategories !== '[]') {
181 $includes = json_decode($includeCategories, true);
182 $includes = array_map(function ($item) {
183 return $item['value'];
184 }, $includes);
185 }
186
187 $excludes = array();
188 if ($excludeCategories && $excludeCategories !== '[]') {
189 $excludes = json_decode($excludeCategories, true);
190 $excludes = array_map(function ($item) {
191 return $item['value'];
192 }, $excludes);
193 }
194
195 if (is_array($includes) && count($includes)) {
196 $terms_object['include'] = array_diff($includes, (array) $excludes);
197 }
198
199 if (is_array($excludes) && count($excludes)) {
200 $terms_object['exclude'] = $excludes;
201 }
202
203 $taxonomy_objects = get_terms($terms_object);
204
205 $html = '';
206
207
208 $html .= '<div class="betterdocs-block ' . $blockId . $gridColomnDesktopClass . $gridColomnTabClass . $gridColomnMobileClass . '">';
209 $html .= '<div class="betterdocs-category-grid-wrapper">';
210 $html .= '<div class="betterdocs-category-grid ' . $layoutMode . '" data-column="' . $colRange . '" data-tab-column="' . $TABcolRange . '" data-mobile-column="' . $MOBcolRange . '" data-colomn-space="' . $gridSpaceRange . '" data-tab-colomn-space="' . $TABgridSpaceRange . '" data-mobile-colomn-space="' . $MOBgridSpaceRange . '">';
211
212
213 if ($taxonomy_objects && !is_wp_error($taxonomy_objects)) {
214 foreach ($taxonomy_objects as $term) {
215 $term_id = $term->term_id;
216 $term_slug = $term->slug;
217 $count = $term->count;
218 $get_term_count = betterdocs_get_postcount($count, $term_id, $enableNestedSubcategory);
219 $term_count = apply_filters('betterdocs_postcount', $get_term_count, false, $term_id, $term_slug, $count, $enableNestedSubcategory);
220 if ($term_count > 0) {
221 if ($layout === 'default') {
222 include BETTERDOCS_DIR_PATH . 'includes/gutenberg/template/category-grid/Layout_Default.php';
223 } else if ($layout === 'layout-2') {
224 include BETTERDOCS_DIR_PATH . 'includes/gutenberg/template/category-grid/Layout_2.php';
225 }
226 }
227 }
228 } else {
229 $html .= '<p class="no-posts-found">' . __('No posts found!', 'betterdocs') . '</p>';
230 }
231 wp_reset_postdata();
232
233 $html .= '</div>';
234 $html .= '</div>';
235 $html .= '</div>';
236
237 return $html;
238 }
239