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

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