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 / 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.5, at includes/class-betterdocs-docs-post-type.php

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