PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 2.0.1
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v2.0.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 / class-betterdocs-docs-post-type.php

class-betterdocs-docs-post-type.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 2.0.1, at includes/class-betterdocs-docs-post-type.php

709 lines 26.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Register docs post type and taxonomies.
5 *
6 * @link https://wpdeveloper.com
7 * @since 1.0.0
8 *
9 * @package BetterDocs
10 * @subpackage BetterDocs/includes
11 */
12
13 use function YoastSEO_Vendor\GuzzleHttp\json_decode;
14
15 /**
16 * Register docs post type and taxonomies class.
17 *
18 *
19 * Also maintains the unique identifier of this plugin as well as the current
20 * version of the plugin.
21 *
22 * @since 1.0.0
23 * @package BetterDocs
24 * @subpackage BetterDocs/includes
25 * @author WPDeveloper <support@wpdeveloper.com>
26 */
27 class BetterDocs_Docs_Post_Type
28 {
29 public static $post_type = 'docs';
30 public static $menu_position = 5;
31 public static $category = 'doc_category';
32 public static $tag = 'doc_tag';
33 public static $docs_archive;
34 public static $docs_slug;
35 public static $cat_slug;
36
37 /**
38 *
39 * Initialize the class and start calling our hooks and filters
40 *
41 * @since 1.0.0
42 *
43 */
44 public static function init()
45 {
46 self::$docs_archive = self::get_docs_archive();
47 self::$docs_slug = self::get_docs_slug();
48 self::$cat_slug = self::docs_category_slug();
49 add_action('init', array(__CLASS__, 'register_post'));
50 add_filter('betterdocs_docs_rewrite', array(__CLASS__, 'docs_rewrite'), 9);
51 add_filter('post_type_link', array(__CLASS__, 'docs_show_permalinks'), 1, 3);
52 add_action('init', array(__CLASS__, 'flush_rewrite_rules'), 99999);
53 add_filter('rest_docs_collection_params', array(__CLASS__, 'add_rest_orderby_params'), 10, 1);
54 add_filter('rest_doc_category_collection_params', array(__CLASS__, 'add_rest_orderby_params_on_doc_category'), 10, 1);
55 add_filter('rest_doc_category_query', array(__CLASS__, 'modify_doc_category_rest_query'), 10, 2);
56 add_action('admin_head', array(__CLASS__, 'admin_order_terms'));
57 // doc category taxonomy media upload hooks
58 add_action('doc_category_add_form_fields', array(__CLASS__, 'add_doc_category_meta'), 10, 2);
59 add_action('doc_category_edit_form_fields', array(__CLASS__, 'update_doc_category_meta'), 10, 2);
60 add_action('created_doc_category', array(__CLASS__, 'save_doc_category_meta'), 10, 2);
61 add_action('edited_doc_category', array(__CLASS__, 'updated_doc_category_meta'), 10, 2);
62 add_action('admin_enqueue_scripts', array(__CLASS__, 'load_media'));
63 add_action('admin_footer', array(__CLASS__, 'add_script'));
64 // add doc category image on rest api
65 add_action('rest_api_init', array(__CLASS__, 'add_doc_category_meta_rest_api'), 10, 2);
66 }
67
68 public static function get_docs_slug()
69 {
70 $builtin_doc_page = BetterDocs_DB::get_settings('builtin_doc_page');
71 $docs_slug = BetterDocs_DB::get_settings('docs_slug');
72 $docs_page = BetterDocs_DB::get_settings('docs_page');
73
74 if ($builtin_doc_page == 1 && $docs_slug) {
75 $docs_post_slug = $docs_slug;
76 } elseif ($builtin_doc_page != 1 && $docs_page) {
77 $post_info = get_post($docs_page);
78 $docs_post_slug = $post_info->post_name;
79 } else {
80 $docs_post_slug = 'docs';
81 }
82
83 return $docs_post_slug;
84 }
85
86 public static function get_docs_archive()
87 {
88 $builtin_doc_page = BetterDocs_DB::get_settings('builtin_doc_page');
89 $docs_slug = BetterDocs_DB::get_settings('docs_slug');
90 $docs_page = BetterDocs_DB::get_settings('docs_page');
91
92 if ($builtin_doc_page == 1 && $docs_slug) {
93 $docs_post_slug = $docs_slug;
94 } elseif ($builtin_doc_page != 1 && $docs_page) {
95 $post_info = get_post($docs_page);
96 $docs_post_slug = $post_info->post_name;
97 } else {
98 $docs_post_slug = 'docs';
99 }
100
101 return $docs_post_slug;
102 }
103
104 public static function docs_category_slug()
105 {
106 $category_slug = BetterDocs_DB::get_settings('category_slug');
107
108 if (empty($category_slug)) {
109 $category_slug = 'docs-category';
110 }
111
112 return $category_slug;
113 }
114
115 /**
116 *
117 * Register post type and taxonomies
118 *
119 * @since 1.0.0
120 *
121 */
122 public static function register_post()
123 {
124 $singular_name = BetterDocs_DB::get_settings('breadcrumb_doc_title');
125
126 /**
127 * Register category taxonomy
128 */
129 $category_labels = array(
130 'name' => esc_html__('Docs Categories', 'betterdocs'),
131 'singular_name' => esc_html__('Docs Category', 'betterdocs'),
132 'all_items' => esc_html__('Docs Categories', 'betterdocs'),
133 'parent_item' => esc_html__('Parent Docs Category', 'betterdocs'),
134 'parent_item_colon' => esc_html__('Parent Docs Category:', 'betterdocs'),
135 'edit_item' => esc_html__('Edit Category', 'betterdocs'),
136 'update_item' => esc_html__('Update Category', 'betterdocs'),
137 'add_new_item' => esc_html__('Add New Docs Category', 'betterdocs'),
138 'new_item_name' => esc_html__('New Docs Name', 'betterdocs'),
139 'menu_name' => esc_html__('Categories', 'betterdocs')
140 );
141
142 $category_args = array(
143 'hierarchical' => true,
144 'public' => true,
145 'labels' => $category_labels,
146 'show_ui' => true,
147 'show_admin_column' => true,
148 'query_var' => true,
149 'show_in_rest' => true,
150 'has_archive' => true,
151 );
152
153 $category_args['rewrite'] = apply_filters('betterdocs_category_rewrite', array('slug' => self::$cat_slug, 'with_front' => false));
154
155 register_taxonomy(self::$category, array(self::$post_type), $category_args);
156
157 /**
158 * Register post type
159 */
160 $labels = array(
161 'name' => ($singular_name) ? $singular_name : 'Docs',
162 'singular_name' => ($singular_name) ? $singular_name : 'Docs',
163 'menu_name' => esc_html__('BetterDocs', 'betterdocs'),
164 'name_admin_bar' => esc_html__('Docs', 'betterdocs'),
165 'add_new' => esc_html__('Add New', 'betterdocs'),
166 'add_new_item' => esc_html__('Add New Docs', 'betterdocs'),
167 'new_item' => esc_html__('New Docs', 'betterdocs'),
168 'edit_item' => esc_html__('Edit Docs', 'betterdocs'),
169 'view_item' => esc_html__('View Docs', 'betterdocs'),
170 'all_items' => esc_html__('All Docs', 'betterdocs'),
171 'search_items' => esc_html__('Search Docs', 'betterdocs'),
172 'parent_item_colorn' => null,
173 'not_found' => esc_html__('No docs found', 'betterdocs'),
174 'not_found_in_trash' => esc_html__('No docs found in trash', 'betterdocs')
175 );
176
177 $betterdocs_articles_caps = apply_filters('betterdocs_articles_caps', 'edit_posts', 'article_roles');
178
179 $args = array(
180 'labels' => $labels,
181 'description' => esc_html__('Add new doc from here', 'betterdocs'),
182 'public' => true,
183 'public_queryable' => true,
184 'exclude_from_search' => false,
185 'show_ui' => true,
186 'show_in_menu' => false,
187 'show_in_admin_bar' => $betterdocs_articles_caps,
188 'query_var' => true,
189 'capability_type' => 'post',
190 'hierarchical' => true,
191 'menu_position' => self::$menu_position,
192 'show_in_rest' => true,
193 'menu_icon' => BETTERDOCS_ADMIN_URL . '/assets/img/betterdocs-icon-white.svg', 100,
194 'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'author', 'revisions', 'custom-fields', 'comments')
195 );
196
197 $builtin_doc_page = BetterDocs_DB::get_settings('builtin_doc_page');
198
199 if ($builtin_doc_page == 'off') {
200 $args['has_archive'] = false;
201 } else {
202 $args['has_archive'] = self::$docs_archive;
203 }
204
205 $args['rewrite'] = apply_filters('betterdocs_docs_rewrite', array('slug' => self::$docs_archive, 'with_front' => false));
206
207 register_post_type(self::$post_type, $args);
208
209 /**
210 * Register tag taxonomy
211 */
212 $tags_labels = array(
213 'name' => esc_html__('Docs Tags', 'betterdocs'),
214 'singular_name' => esc_html__('Tag', 'betterdocs'),
215 'search_items' => esc_html__('Search Tags', 'betterdocs'),
216 'popular_items' => esc_html__('Popular Tags', 'betterdocs'),
217 'all_items' => esc_html__('All Tags', 'betterdocs'),
218 'parent_item' => null,
219 'parent_item_colon' => null,
220 'edit_item' => esc_html__('Edit Tag', 'betterdocs'),
221 'update_item' => esc_html__('Update Tag', 'betterdocs'),
222 'add_new_item' => esc_html__('Add New Tag', 'betterdocs'),
223 'new_item_name' => esc_html__('New Tag Name', 'betterdocs'),
224 'separate_items_with_commas' => esc_html__('Separate tags with commas', 'betterdocs'),
225 'add_or_remove_items' => esc_html__('Add or remove tags', 'betterdocs'),
226 'choose_from_most_used' => esc_html__('Choose from the most used tags', 'betterdocs'),
227 'menu_name' => esc_html__('Tags', 'betterdocs'),
228 );
229
230 $tag_args = array(
231 'hierarchical' => true,
232 'labels' => $tags_labels,
233 'show_ui' => true,
234 'update_count_callback' => '_update_post_term_count',
235 'show_admin_column' => true,
236 'query_var' => true,
237 'show_in_rest' => true
238 );
239
240 $tag_slug = BetterDocs_DB::get_settings('tag_slug');
241
242 if ($tag_slug) {
243 $tag_args['rewrite'] = array('slug' => $tag_slug, 'with_front' => false);
244 } else {
245 $tag_args['rewrite'] = array('slug' => 'docs-tag', 'with_front' => false);
246 }
247
248 register_taxonomy(self::$tag, array(self::$post_type), $tag_args);
249 }
250
251 /**
252 * Add menu_order param to the list of rest api orderby values
253 */
254 public static function add_rest_orderby_params($params)
255 {
256 $params['orderby']['enum'][] = 'menu_order';
257 return $params;
258 }
259
260 /**
261 * Add doc_category_order param to the list of rest api orderby values on doc_category taxonomy
262 */
263 public static function add_rest_orderby_params_on_doc_category($params)
264 {
265 $params['orderby']['enum'][] = 'doc_category_order';
266 return $params;
267 }
268
269 /**
270 * Modify doc_category rest query for doc_category_order meta key
271 */
272 public static function modify_doc_category_rest_query($args, $request)
273 {
274 $order_by = $request->get_param('orderby');
275 if (isset($order_by) && 'doc_category_order' === $order_by) {
276 $args['meta_key'] = $order_by;
277 $args['orderby'] = 'meta_value_num';
278 }
279 return $args;
280 }
281
282 /**
283 * load media for taxonomy category image
284 *
285 * @since 1.0.0
286 */
287 public static function load_media()
288 {
289 wp_enqueue_media();
290 }
291
292 /**
293 * Default the taxonomy's terms' order if it's not set.
294 *
295 * @param string $tax_slug The taxonomy's slug.
296 */
297 public static function default_term_order($tax_slug)
298 {
299 $terms = get_terms($tax_slug, array('hide_empty' => false));
300 $order = self::get_max_taxonomy_order($tax_slug);
301
302 foreach ($terms as $term) {
303 if (!get_term_meta($term->term_id, 'doc_category_order', true)) {
304 update_term_meta($term->term_id, 'doc_category_order', $order);
305 $order++;
306 }
307 }
308 }
309
310 /**
311 * Order the terms on the admin side.
312 */
313 public static function admin_order_terms()
314 {
315 $screen = function_exists('get_current_screen') ? get_current_screen() : '';
316 $screen_id = isset($screen->id) ? $screen->id : '';
317 if (in_array($screen_id, array('toplevel_page_betterdocs-admin', 'betterdocs_page_betterdocs-settings'))) {
318 self::default_term_order('doc_category');
319 }
320
321 if (!isset($_GET['orderby']) && !empty($screen) && !empty($screen->base) && $screen->base === 'edit-tags' && $screen->taxonomy === 'doc_category') {
322 self::default_term_order($screen->taxonomy);
323 add_filter('terms_clauses', array(__CLASS__, 'set_tax_order'), 10, 3);
324 }
325 }
326
327 /**
328 * Get the maximum doc_category_order for this taxonomy. This will be applied to terms that don't have a tax position.
329 */
330 private static function get_max_taxonomy_order($tax_slug)
331 {
332 global $wpdb;
333
334 $max_term_order = $wpdb->get_col(
335 $wpdb->prepare(
336 "SELECT MAX( CAST( tm.meta_value AS UNSIGNED ) )
337 FROM $wpdb->terms t
338 JOIN $wpdb->term_taxonomy tt ON t.term_id = tt.term_id AND tt.taxonomy = '%s'
339 JOIN $wpdb->termmeta tm ON tm.term_id = t.term_id WHERE tm.meta_key = 'doc_category_order'",
340 $tax_slug
341 )
342 );
343
344 $max_term_order = is_array($max_term_order) ? current($max_term_order) : 0;
345
346 return (int) $max_term_order === 0 || empty($max_term_order) ? 1 : (int) $max_term_order + 1;
347 }
348
349 /**
350 * Re-Order the taxonomies based on the doc_category_order value.
351 *
352 * @param array $pieces Array of SQL query clauses.
353 * @param array $taxonomies Array of taxonomy names.
354 * @param array $args Array of term query args.
355 */
356 public static function set_tax_order($pieces, $taxonomies, $args)
357 {
358 foreach ($taxonomies as $taxonomy) {
359 global $wpdb;
360
361 if ($taxonomy === 'doc_category') {
362 $join_statement = " LEFT JOIN $wpdb->termmeta AS term_meta ON t.term_id = term_meta.term_id AND term_meta.meta_key = 'doc_category_order'";
363
364 if (!self::does_substring_exist($pieces['join'], $join_statement)) {
365 $pieces['join'] .= $join_statement;
366 }
367
368 $pieces['orderby'] = 'ORDER BY CAST( term_meta.meta_value AS UNSIGNED )';
369 }
370 }
371
372 return $pieces;
373 }
374
375 /**
376 * Order the taxonomies on the front end.
377 */
378 public static function front_end_order_terms()
379 {
380 if (!is_admin()) {
381 add_filter('terms_clauses', array(__CLASS__, 'set_tax_order'), 10, 3);
382 }
383 }
384
385 /**
386 * Check if a substring exists inside a string.
387 *
388 * @param string $string The main string (haystack) we're searching in.
389 * @param string $substring The substring we're searching for.
390 *
391 * @return bool True if substring exists, else false.
392 */
393 protected static function does_substring_exist($string, $substring)
394 {
395 return strstr($string, $substring) !== false;
396 }
397
398 /**
399 * Default the taxonomy's terms' order if it's not set.
400 *
401 * @param string $tax_slug The taxonomy's slug.
402 */
403 public static function get_manage_docs()
404 {
405 $terms = get_terms('knowledge_base', array('hide_empty' => false));
406
407 if ($terms) {
408 echo '<select name="term_meta[knowledge_base]" id="knowledge_base">';
409 echo '<option> ' . esc_html__('Select an option') . ' </option>';
410
411 foreach ($terms as $term) {
412 echo '<option value="' . $term->slug . '">' . $term->name . '</option>';
413 }
414
415 echo '</select>';
416 }
417 }
418
419 /**
420 * Add a form field in the new category page
421 *
422 * @since 1.0.0
423 */
424 public static function add_doc_category_meta($taxonomy)
425 {
426 do_action('betterdocs_doc_category_add_form_before');
427
428 echo '<div class="form-field term-group">
429 <label for="doc-category-order">' . esc_html__('Order', 'betterdocs') . '</label>
430 <input type="number" id="doc-category-order" style="width:100px" name="term_meta[order]" value="">
431 </div>
432
433 <div class="form-field term-group">
434 <label for="doc-category-image-id">' . esc_html__('Category Icon', 'betterdocs') . '</label>
435 <input type="hidden" id="doc-category-image-id" name="term_meta[image-id]" class="custom_media_url" value="">
436 <div id="doc-category-image-wrapper">
437 <img src="' . BETTERDOCS_ADMIN_URL . 'assets/img/betterdocs-cat-icon.svg" alt="">
438 </div>
439 <p>
440 <input type="button" class="button button-secondary betterdocs_tax_media_button"
441 id="betterdocs_tax_media_button" name="betterdocs_tax_media_button"
442 value="' . esc_html__('Add Image', 'betterdocs') . '" />
443 <input type="button" class="button button-secondary doc_tax_media_remove" id="doc_tax_media_remove"
444 name="doc_tax_media_remove"
445 value="' . esc_html__('Remove Image', 'betterdocs') . '" />
446 </p>
447 </div>';
448 }
449
450 /**
451 * Save the form field
452 *
453 * @since 1.0.0
454 */
455 public static function save_doc_category_meta($term_id)
456 {
457 if (isset($_POST['term_meta'])) {
458 $term_meta = get_option("doc_category_$term_id");
459 $cat_keys = array_keys($_POST['term_meta']);
460 foreach ($cat_keys as $key) {
461 if (isset($_POST['term_meta'][$key])) {
462 add_term_meta($term_id, "doc_category_$key", $_POST['term_meta'][$key]);
463 $term_meta[$key] = $_POST['term_meta'][$key];
464 }
465 }
466 }
467 if (isset($_POST['doc_category_kb'])) {
468 $doc_category_kb = $_POST['doc_category_kb'];
469 update_term_meta($term_id, "doc_category_knowledge_base", $doc_category_kb);
470 }
471 }
472
473 /**
474 * Edit the form field
475 *
476 * @since 1.0.0
477 */
478 public static function update_doc_category_meta($term, $taxonomy)
479 { ?>
480 <?php
481 $term_meta = get_option("doc_category_$term->term_id");
482 $cat_order = get_term_meta($term->term_id, 'doc_category_order', true);
483 $cat_icon_id = get_term_meta($term->term_id, 'doc_category_image-id', true);
484
485 do_action('betterdocs_doc_category_update_form_before', $term);
486
487 ?>
488
489 <tr class="form-field term-group-wrap">
490 <th scope="row">
491 <label for="doc-category-id"><?php esc_html_e('Category Id', 'betterdocs'); ?></label>
492 </th>
493 <td>
494 <input type="text" id="doc-category-id" style="width:100px" name="" value="<?php echo esc_attr($_GET["tag_ID"]) ?>" readonly>
495 </td>
496 </tr>
497 <tr class="form-field term-group-wrap">
498 <th scope="row">
499 <label for="doc-category-order"><?php esc_html_e('Order', 'betterdocs'); ?></label>
500 </th>
501 <td>
502 <input type="number" id="doc-category-order" style="width:100px" name="term_meta[order]" value="<?php echo $cat_order ? esc_attr($cat_order) : ''; ?>">
503 </td>
504 </tr>
505 <tr class="form-field term-group-wrap batterdocs-cat-media-upload">
506 <th scope="row">
507 <label for="doc-category-image-id"><?php esc_html_e('Image', 'betterdocs'); ?></label>
508 </th>
509 <td>
510 <input type="hidden" id="doc-category-image-id" name="term_meta[image-id]" value="<?php echo esc_attr($cat_icon_id); ?>">
511 <div id="doc-category-image-wrapper">
512 <?php
513 if ($cat_icon_id) {
514 echo wp_get_attachment_image($cat_icon_id, 'thumbnail');
515 } else {
516 echo '<img src="' . BETTERDOCS_ADMIN_URL . 'assets/img/betterdocs-cat-icon.svg" alt="">';
517 }
518 ?>
519 </div>
520 <p>
521 <input type="button" class="button button-secondary betterdocs_tax_media_button" id="betterdocs_tax_media_button" name="betterdocs_tax_media_button" value="<?php esc_html_e('Add Image', 'betterdocs'); ?>" />
522 <input type="button" class="button button-secondary doc_tax_media_remove" id="doc_tax_media_remove" name="doc_tax_media_remove" value="<?php esc_html_e('Remove Image', 'betterdocs'); ?>" />
523 </p>
524 </td>
525 </tr>
526 <?php
527 }
528
529 /*
530 * Update the form field value
531 *
532 * @since 1.0.0
533 */
534 public static function updated_doc_category_meta($term_id)
535 {
536 if (isset($_POST['term_meta'])) {
537 $cat_keys = array_keys($_POST['term_meta']);
538 foreach ($cat_keys as $key) {
539 if (isset($_POST['term_meta'][$key])) {
540 update_term_meta($term_id, "doc_category_$key", $_POST['term_meta'][$key]);
541 }
542 }
543 }
544 if (isset($_POST['doc_category_kb'])) {
545 $doc_category_kb = $_POST['doc_category_kb'];
546 update_term_meta($term_id, "doc_category_knowledge_base", $doc_category_kb);
547 }
548 }
549
550 /*
551 * Add script
552 *
553 * @since 1.0.0
554 */
555 public static function add_script()
556 {
557
558 global $current_screen;
559 if ($current_screen->id == 'edit-doc_category') {
560
561 ?>
562 <script>
563 jQuery(document).ready(function($) {
564
565 function betterdocs_media_upload(button_class) {
566 var _custom_media = true,
567 _betterdocs_send_attachment = wp.media.editor.send.attachment;
568 $('body').on('click', button_class, function(e) {
569 var button_id = '#' + $(this).attr('id');
570 var send_attachment_bkp = wp.media.editor.send.attachment;
571 var button = $(button_id);
572 _custom_media = true;
573 wp.media.editor.send.attachment = function(props, attachment) {
574 if (_custom_media) {
575 $('#doc-category-image-id').val(attachment.id);
576 $('#doc-category-image-wrapper').html(
577 '<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />'
578 );
579 $('#doc-category-image-wrapper .custom_media_image').attr('src', attachment
580 .url).css('display', 'block');
581 } else {
582 return _betterdocs_send_attachment.apply(button_id, [props, attachment]);
583 }
584 }
585 wp.media.editor.open(button);
586 return false;
587 });
588 }
589
590 betterdocs_media_upload('.betterdocs_tax_media_button.button');
591
592 $('body').on('click', '.doc_tax_media_remove', function() {
593 $('#doc-category-image-id').val('');
594 $('#doc-category-image-wrapper').html(
595 '<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />'
596 );
597 });
598
599 $(document).ajaxComplete(function(event, xhr, settings) {
600 var queryStringArr = settings.data.split('&');
601 if ($.inArray('action=add-tag', queryStringArr) !== -1) {
602 var xml = xhr.responseXML;
603 $response = $(xml).find('term_id').text();
604 if ($response != "") {
605 // Clear the thumb image
606 $('#doc-category-image-wrapper').html('');
607 }
608 }
609 });
610 });
611 </script>
612 <?php }
613 }
614
615 /**
616 * Filtering post type rewrite rule.
617 *
618 * @param [type] $rewrite
619 * @return array $rewrite
620 */
621 public static function docs_rewrite($rewrite)
622 {
623 $permalink = BetterDocs_DB::get_settings('permalink_structure');
624 if (!empty($permalink)) {
625 if (class_exists('BetterDocs_Multiple_Kb') && BetterDocs_Multiple_Kb::get_multiple_kb() != 1) {
626 $permalink = preg_replace('/%knowledge_base%\/?/', '', $permalink);
627 }
628 $rewrite = array('slug' => trim($permalink, '/'), 'with_front' => false);
629 }
630 return $rewrite;
631 }
632
633 /**
634 * Replacing %doc_category% placeholder in permalink.
635 *
636 * @param [type] $url
637 * @param [type] $post
638 * @param boolean $leavename
639 * @return string $url
640 */
641 public static function docs_show_permalinks($url, $post = null, $leavename = false)
642 {
643 if ($post->post_type != 'docs') {
644 return $url;
645 }
646 $doc_category = 'doc_category';
647 $cat_tag = '%' . $doc_category . '%';
648 $cat_terms = wp_get_object_terms($post->ID, $doc_category);
649
650 if (is_array($cat_terms) && sizeof($cat_terms) > 0) {
651 $doccat_terms = $cat_terms[0]->slug;
652 } else {
653 $doccat_terms = 'uncategorized';
654 }
655
656 if (class_exists('BetterDocs_Multiple_Kb') && BetterDocs_Multiple_Kb::get_multiple_kb() != 1) {
657 $url = preg_replace('/%knowledge_base%\/?/', '', $url);
658 }
659 return str_replace($cat_tag, $doccat_terms, $url);
660 }
661
662 public static function flush_rewrite_rules()
663 {
664 // Get the rewrite rules
665 $rules = get_option('rewrite_rules');
666 if (is_array($rules)) {
667 $rules = implode('', $rules);
668 }
669 if (!strpos($rules, 'docs')) {
670 flush_rewrite_rules();
671 }
672 }
673
674 /**
675 * Resister doc_category image field in rest API
676 */
677 public static function add_doc_category_meta_rest_api()
678 {
679 register_rest_field('doc_category', 'thumbnail', [
680 'get_callback' => array(__CLASS__, 'doc_category_thumbnail_image'),
681 'update_callback' => null,
682 'schema' => [
683 'description' => 'Holds the thumbnail URL of doc category',
684 'type' => 'string',
685 'format' => 'url',
686 ]
687 ]);
688 }
689
690 /**
691 *
692 * add doc_category meta field callback function
693 *
694 * @param [string|array] $object
695 * @return string $url
696 */
697 public static function doc_category_thumbnail_image($object)
698 {
699 $attachment_id = get_term_meta($object['id'], 'doc_category_image-id', true);
700 if (!$attachment_id) {
701 return;
702 }
703 $url = wp_get_attachment_url($attachment_id);
704 return $url;
705 }
706 }
707
708 BetterDocs_Docs_Post_Type::init();
709