PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 2.0.5
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v2.0.5
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 / categorybox.php

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

171 lines 5.4 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_categorybox_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 = 'categorybox/index.js';
25 wp_register_script(
26 'betterdocs-categorybox-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 ),
35 filemtime("$dir/$index_js")
36 );
37
38 $editor_style = 'categorybox/style.css';
39 wp_register_style(
40 'betterdocs-categorybox-block-editor',
41 plugins_url($editor_style, __FILE__),
42 array(),
43 filemtime("$dir/$editor_style"),
44 'all'
45 );
46
47 register_block_type(__DIR__ . '/categorybox', array(
48 'editor_script' => 'betterdocs-categorybox-block-editor',
49 'editor_style' => 'betterdocs-categorybox-block-editor',
50 'render_callback' => 'betterdocs_categorybox_server_side_render'
51 ));
52 }
53 add_action('init', 'betterdocs_categorybox_block_init');
54
55 /**
56 * Category Box Server Side Render
57 */
58 function betterdocs_categorybox_server_side_render(array $attributes)
59 {
60 if (!is_admin()) {
61 wp_enqueue_style("betterdocs-categorybox-block-editor");
62 }
63
64 $attributes = wp_parse_args(
65 $attributes,
66 [
67 'blockId' => '',
68 'categories' => array(),
69 'includeCategories' => '',
70 'excludeCategories' => '',
71 'boxPerPage' => 9,
72 'orderBy' => 'name',
73 'order' => 'asc',
74 'layout' => 'default',
75 'showIcon' => true,
76 'showTitle' => true,
77 'titleTag' => 'h2',
78 'showCount' => true,
79 'prefix' => '',
80 'suffix' => __('articles', 'betterdocs'),
81 'suffixSingular' => __('article', 'betterdocs'),
82 ]
83 );
84
85 $blockId = $attributes['blockId'];
86 $includeCategories = $attributes['includeCategories'];
87 $excludeCategories = $attributes['excludeCategories'];
88 $boxPerPage = $attributes['boxPerPage'];
89 $orderBy = $attributes['orderBy'];
90 $order = $attributes['order'];
91 $layout = $attributes['layout'];
92 $showIcon = $attributes['showIcon'];
93 $showTitle = $attributes['showTitle'];
94 $titleTag = $attributes['titleTag'];
95 $showCount = $attributes['showCount'];
96 $prefix = $attributes['prefix'];
97 $suffix = $attributes['suffix'];
98 $suffixSingular = $attributes['suffixSingular'];
99 $enableNestedSubcategory = false;
100
101 $terms_object = array(
102 'taxonomy' => 'doc_category',
103 'order' => $order,
104 'orderby' => $orderBy,
105 'hide_empty' => true,
106 'number' => $boxPerPage ? $boxPerPage : 9,
107 );
108
109 if ('doc_category_order' === $orderBy) {
110 $terms_object['meta_key'] = 'doc_category_order';
111 $terms_object['orderby'] = 'meta_value_num';
112 }
113
114 $includes = array();
115 if ($includeCategories && $includeCategories !== '[]') {
116 $includes = json_decode($includeCategories, true);
117 $includes = array_map(function ($item) {
118 return $item['value'];
119 }, $includes);
120 }
121
122 $excludes = array();
123 if ($excludeCategories && $excludeCategories !== '[]') {
124 $excludes = json_decode($excludeCategories, true);
125 $excludes = array_map(function ($item) {
126 return $item['value'];
127 }, $excludes);
128 }
129
130 if (is_array($includes) && count($includes)) {
131 $terms_object['include'] = array_diff($includes, (array) $excludes);
132 }
133
134 if (is_array($excludes) && count($excludes)) {
135 $terms_object['exclude'] = $excludes;
136 }
137
138 $taxonomy_objects = get_terms($terms_object);
139
140 $html = '';
141 $default_multiple_kb = false;
142
143 $html .= "<div class='el-betterdocs-category-box-wrapper $blockId'>";
144 $html .= "<div class='el-betterdocs-category-box el-betterdocs-column'>";
145
146
147 if ($taxonomy_objects && !is_wp_error($taxonomy_objects)) {
148 foreach ($taxonomy_objects as $term) {
149 $term_id = $term->term_id;
150 $term_slug = $term->slug;
151 $count = $term->count;
152 $get_term_count = betterdocs_get_postcount($count, $term_id, $enableNestedSubcategory);
153 $term_count = apply_filters('betterdocs_postcount', $get_term_count, $default_multiple_kb, $term_id, $term_slug, $count, $enableNestedSubcategory);
154 if ($term_count > 0) {
155 if ($layout === 'default') {
156 include BETTERDOCS_DIR_PATH . 'includes/gutenberg/template/category-box/Layout_Default.php';
157 } else if ($layout === 'layout-2') {
158 include BETTERDOCS_DIR_PATH . 'includes/gutenberg/template/category-box/Layout_2.php';
159 }
160 }
161 }
162 } else {
163 $html .= '<p class="no-posts-found">' . __('No posts found!', 'betterdocs') . '</p>';
164 }
165
166 $html .= "</div>";
167 $html .= "</div>";
168
169 return $html;
170 }
171