PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 2.2.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v2.2.2
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 / public / class-betterdocs-public.php

class-betterdocs-public.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 2.2.2, at public/class-betterdocs-public.php

569 lines 24.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 use \Elementor\Plugin;
3 /**
4 * The public-facing functionality of the plugin.
5 *
6 * @link https://wpdeveloper.com
7 * @since 1.0.0
8 *
9 * @package BetterDocs
10 * @subpackage BetterDocs/public
11 */
12
13 /**
14 * The public-facing functionality of the plugin.
15 *
16 * Defines the plugin name, version, and two examples hooks for how to
17 * enqueue the public-facing stylesheet and JavaScript.
18 *
19 * @package BetterDocs
20 * @subpackage BetterDocs/public
21 * @author WPDeveloper <support@wpdeveloper.com>
22 */
23 class BetterDocs_Public {
24
25 /**
26 * The ID of this plugin.
27 *
28 * @since 1.0.0
29 * @access private
30 * @var string $plugin_name The ID of this plugin.
31 */
32 private $plugin_name;
33
34 /**
35 * The version of this plugin.
36 *
37 * @since 1.0.0
38 * @access private
39 * @var string $version The current version of this plugin.
40 */
41 private $version;
42
43 /**
44 * Initialize the class and set its properties.
45 *
46 * @since 1.0.0
47 * @param string $plugin_name The name of the plugin.
48 * @param string $version The version of this plugin.
49 */
50 public function __construct( $plugin_name, $version )
51 {
52 $this->plugin_name = $plugin_name;
53 $this->version = $version;
54 add_action( 'init', array( $this, 'public_hooks' ) );
55 add_action('betterdocs_before_shortcode_load', array( $this, 'enqueue_styles'));
56 add_action('betterdocs_before_shortcode_load', array( $this, 'enqueue_scripts'));
57 add_action('betterdocs_docs_before_social', array($this, 'betterdocs_article_reactions'));
58 }
59
60 /**
61 * Register the stylesheets for the public-facing side of the site.
62 *
63 * @since 1.0.0
64 */
65 public function register_styles()
66 {
67 /**
68 * This function is provided for demonstration purposes only.
69 *
70 * An instance of this class should be passed to the run() function
71 * defined in BetterDocs_Loader as all of the hooks are defined
72 * in that particular class.
73 *
74 * The BetterDocs_Loader will then create the relationship
75 * between the defined hooks and the functions defined in this
76 * class.
77 */
78 wp_register_style( 'simplebar', plugin_dir_url(__FILE__) . 'css/simplebar.css', array(), $this->version, 'all' );
79 wp_register_style($this->plugin_name, BETTERDOCS_PUBLIC_URL . 'css/betterdocs-public.css', array(), $this->version, 'all');
80 wp_register_style('betterdocs-category-grid', BETTERDOCS_URL . 'includes/elementor/assets/betterdocs-category-grid.css', array(), $this->version, 'all');
81 wp_register_style('betterdocs-category-box', BETTERDOCS_URL . 'includes/elementor/assets/betterdocs-category-box.css', array(), $this->version, 'all');
82
83 }
84
85 public function enqueue_styles() {
86 wp_enqueue_style($this->plugin_name);
87 wp_enqueue_style('simplebar');
88 }
89
90 /**
91 * Register the JavaScript for the public-facing side of the site.
92 *
93 * @since 1.0.0
94 */
95 public function register_scripts()
96 {
97 /**
98 * This function is provided for demonstration purposes only.
99 *
100 * An instance of this class should be passed to the run() function
101 * defined in BetterDocs_Loader as all of the hooks are defined
102 * in that particular class.
103 *
104 * The BetterDocs_Loader will then create the relationship
105 * between the defined hooks and the functions defined in this
106 * class.
107 */
108 wp_register_script('simplebar', plugin_dir_url(__FILE__) . 'js/simplebar.js', array( 'jquery' ), $this->version, true);
109 wp_register_script('clipboard', BETTERDOCS_PUBLIC_URL . 'js/clipboard.min.js', array( 'jquery' ), $this->version, true);
110 wp_register_script($this->plugin_name, BETTERDOCS_PUBLIC_URL . 'js/betterdocs-public.js', array( 'jquery' ), $this->version, true);
111 wp_localize_script($this->plugin_name, 'betterdocspublic', array(
112 'ajax_url' => admin_url( 'admin-ajax.php' ),
113 'post_id' => get_the_ID(),
114 'copy_text' => esc_html__('Copied','betterdocs'),
115 'sticky_toc_offset' => BetterDocs_DB::get_settings('sticky_toc_offset'),
116 'nonce' => wp_create_nonce( 'betterdocs_submit_data' )
117 ));
118 wp_register_script('betterdocs-category-grid', BETTERDOCS_URL . 'includes/elementor/assets/betterdocs-category-grid.js', [ 'jquery' ], '1.0.0', true);
119 }
120
121 public function enqueue_scripts()
122 {
123 wp_enqueue_script('simplebar');
124 wp_enqueue_script($this->plugin_name);
125 wp_enqueue_script('clipboard');
126 wp_localize_script($this->plugin_name, 'betterdocspublic', array(
127 'ajax_url' => admin_url( 'admin-ajax.php' ),
128 'post_id' => get_the_ID(),
129 'copy_text' => esc_html__('Copied','betterdocs'),
130 'sticky_toc_offset' => BetterDocs_DB::get_settings('sticky_toc_offset'),
131 'nonce' => wp_create_nonce( 'betterdocs_submit_data' ),
132 'search_letter_limit' => BetterDocs_DB::get_settings('search_letter_limit'),
133 'FEEDBACK' => array(
134 'DISPLAY' => true,
135 'TEXT' => esc_html__('How did you feel?', 'betterdocs'),
136 'SUCCESS' => esc_html__('Thanks for your feedback', 'betterdocs'),
137 'URL' => home_url() . '?rest_route=/betterdocs/feedback',
138 ),
139 ));
140 }
141
142 /**
143 * Load assets only for BetterDocs templates and shortcodes
144 *
145 */
146 public function load_assets()
147 {
148 $this->register_styles();
149 $this->register_scripts();
150 if (BetterDocs_Helper::is_templates() == true) {
151 $this->enqueue_styles();
152 $this->enqueue_scripts();
153 }
154 }
155
156 public function betterdocs_article_reactions($reactions = '')
157 {
158 $post_reactions = get_theme_mod('betterdocs_post_reactions', true);
159
160 if ($post_reactions == true) {
161 $reactions = do_shortcode('[betterdocs_article_reactions]');
162 }
163
164 return $reactions;
165 }
166
167 /**
168 * Register the JavaScript for the public-facing side of the site.
169 *
170 * @since 1.0.0
171 */
172 public function public_hooks()
173 {
174 add_filter( 'archive_template', array( $this, 'get_docs_archive_template' ), 99 );
175 add_filter( 'single_template', array( $this, 'get_docs_single_template' ), 99);
176 add_filter( 'betterdocs_single_post_nav', array( $this, 'singledoc_navigation_ordering' ), 9, 1 );
177 $defaults = betterdocs_generate_defaults();
178 if( is_array( $defaults ) && $defaults['betterdocs_docs_layout_select'] === 'layout-2' ) {
179 add_filter( 'betterdocs_doc_page_cat_icon_size2_default', 80 );
180 }
181 if( is_admin() ) {
182 add_filter('plugin_action_links_' . BETTERDOCS_BASENAME, array($this, 'insert_plugin_links'));
183 }
184 }
185
186 function singledoc_navigation_ordering( $nav_markup ) {
187 $doc_ids = array();
188 $docs_orderby = BetterDocs_DB::get_settings('alphabetically_order_post');
189 $docs_order = BetterDocs_DB::get_settings('docs_order');
190 $multiple_kb = BetterDocs_DB::get_settings('multiple_kb');
191 $kb_slug = class_exists('BetterDocs_Multiple_Kb') ? BetterDocs_Multiple_Kb::kb_slug() : '';
192 $current_term_object = isset( get_the_terms( get_the_ID(), 'doc_category' )[0] ) ? get_the_terms( get_the_ID(), 'doc_category' )[0] : '' ;
193 $current_term_id = $current_term_object != '' ? $current_term_object->term_id : '';
194 $current_term_slug = $current_term_object != '' ? $current_term_object->slug : '';
195 $list_args = BetterDocs_Helper::list_query_arg('docs', $multiple_kb, $current_term_slug, -1, $docs_orderby, $docs_order, $kb_slug);
196 $args = apply_filters('betterdocs_articles_args', $list_args, $current_term_id);
197 $query = new WP_Query( $args );
198
199 if( $query->have_posts() ) {
200 while( $query->have_posts() ) {
201 $query->the_post();
202 array_push( $doc_ids, get_the_ID() );
203 }
204 }
205 wp_reset_query();
206
207 $doc_id_keys = array_flip( $doc_ids );
208 $current_doc_index = isset( $doc_id_keys[get_the_ID()] ) ? $doc_id_keys[get_the_ID()] : '';
209
210 if( $current_doc_index != '' || $current_doc_index === 0 ) {
211 $nav_markup = '';
212 $next_post_index = $current_doc_index + 1;
213 $previous_post_index = $current_doc_index - 1;
214
215 if( isset( $doc_ids[$previous_post_index] ) ) {
216 $nav_markup .= '<a rel="prev" class="next-post" href="' . get_post_permalink( $doc_ids[$previous_post_index] ) . '"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="42px" viewBox="0 0 50 50" version="1.1"><g id="surface1"><path style=" " d="M 11.957031 13.988281 C 11.699219 14.003906 11.457031 14.117188 11.28125 14.308594 L 1.015625 25 L 11.28125 35.691406 C 11.527344 35.953125 11.894531 36.0625 12.242188 35.976563 C 12.589844 35.890625 12.867188 35.625 12.964844 35.28125 C 13.066406 34.933594 12.972656 34.5625 12.71875 34.308594 L 4.746094 26 L 48 26 C 48.359375 26.003906 48.695313 25.816406 48.878906 25.503906 C 49.058594 25.191406 49.058594 24.808594 48.878906 24.496094 C 48.695313 24.183594 48.359375 23.996094 48 24 L 4.746094 24 L 12.71875 15.691406 C 13.011719 15.398438 13.09375 14.957031 12.921875 14.582031 C 12.753906 14.203125 12.371094 13.96875 11.957031 13.988281 Z "></path></g></svg>' . wp_kses( get_the_title( $doc_ids[$previous_post_index] ), BETTERDOCS_KSES_ALLOWED_HTML ) . '</a>';
217 }
218
219 if( isset( $doc_ids[$next_post_index] ) ) {
220 $nav_markup .= '<a rel="next" class="next-post" href="' . get_post_permalink( $doc_ids[$next_post_index] ) . '">' . wp_kses( get_the_title( $doc_ids[$next_post_index] ), BETTERDOCS_KSES_ALLOWED_HTML ) . '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="42px" viewBox="0 0 50 50" version="1.1"><g id="surface1"><path style=" " d="M 38.035156 13.988281 C 37.628906 13.980469 37.257813 14.222656 37.09375 14.59375 C 36.933594 14.96875 37.015625 15.402344 37.300781 15.691406 L 45.277344 24 L 2.023438 24 C 1.664063 23.996094 1.328125 24.183594 1.148438 24.496094 C 0.964844 24.808594 0.964844 25.191406 1.148438 25.503906 C 1.328125 25.816406 1.664063 26.003906 2.023438 26 L 45.277344 26 L 37.300781 34.308594 C 36.917969 34.707031 36.933594 35.339844 37.332031 35.722656 C 37.730469 36.105469 38.363281 36.09375 38.746094 35.691406 L 49.011719 25 L 38.746094 14.308594 C 38.5625 14.109375 38.304688 13.996094 38.035156 13.988281 Z "></path></g></svg></a>';
221 }
222 }
223
224 return $nav_markup;
225 }
226
227 /**
228 * Get Archive Template for the docs base directory.
229 *
230 * @since 1.0.0
231 */
232 public function get_docs_archive_template($template)
233 {
234 $docs_layout = get_theme_mod('betterdocs_docs_layout_select', 'layout-1');
235 $tax = BetterDocs_Helper::get_tax();
236 if (is_post_type_archive('docs')) {
237 $docs_layout = get_theme_mod('betterdocs_docs_layout_select', 'layout-1');
238
239 if ( $docs_layout === 'layout-2' ) {
240 $template = BETTERDOCS_PUBLIC_PATH . 'partials/archive-template/category-box.php';
241 } else {
242 $template = BETTERDOCS_PUBLIC_PATH . 'partials/archive-template/category-list.php';
243 }
244 } elseif ($tax === 'doc_category') {
245 $template = BETTERDOCS_PUBLIC_PATH . 'partials/doc-category-templates/category-template-1.php';
246 } else if (is_tax('doc_tag')) {
247 $template = BETTERDOCS_PUBLIC_PATH . 'betterdocs-tag-template.php';
248 } else if ($tax === 'knowledge_base' && $docs_layout === 'layout-2') {
249 $template = BETTERDOCS_PUBLIC_PATH . 'partials/archive-template/category-box.php';
250 } else if ($tax === 'knowledge_base') {
251 $template = BETTERDOCS_PUBLIC_PATH . 'partials/archive-template/category-list.php';
252 }
253 return apply_filters('betterdocs_archive_template', $template);
254 }
255
256 /**
257 * Get Single Page Template for docs base directory.
258 *
259 * @param int $single_template Overirde single templates.
260 *
261 * @since 1.0.0
262 */
263 public function get_docs_single_template($single_template)
264 {
265 if (is_singular('docs')) {
266 $layout_select = get_theme_mod('betterdocs_single_layout_select', 'layout-1');
267 if ($layout_select === 'layout-4') {
268 $single_template = BETTERDOCS_PUBLIC_PATH . 'partials/template-single/layout-4.php';
269 } elseif ($layout_select === 'layout-5') {
270 $single_template = BETTERDOCS_PUBLIC_PATH . 'partials/template-single/layout-5.php';
271 } else {
272 $single_template = BETTERDOCS_PUBLIC_PATH . 'partials/template-single/layout-1.php';
273 }
274 }
275 return apply_filters('betterdocs_single_template', $single_template);
276 }
277
278 /**
279 * Archive Page Sidebar Layout Renderer
280 */
281 public static function archive_sidebar_loader( $layout ) {
282 $template_path = BETTERDOCS_PUBLIC_PATH . 'partials/sidebars/sidebar-1.php';
283 if( $layout == 'layout-1' ) {
284 $template_path = BETTERDOCS_PUBLIC_PATH . 'partials/sidebars/sidebar-1.php';
285 } else if( $layout == 'layout-4' ) {
286 $template_path = BETTERDOCS_PUBLIC_PATH . 'partials/sidebars/sidebar-4.php';
287 } else if( $layout == 'layout-5' ) {
288 $template_path = BETTERDOCS_PUBLIC_PATH . 'partials/sidebars/sidebar-5.php';
289 }
290 return apply_filters( 'betterdocs_archive_sidebar_template', $template_path );
291 }
292
293 /**
294 * Get supported heading tag from settings
295 *
296 * @since 1.0.0
297 */
298 public static function htag_support()
299 {
300 $supported_tag = BetterDocs_DB::get_settings('supported_heading_tag');
301 if( ! empty( $supported_tag ) && $supported_tag !== 'off' ) {
302 $tags = implode(',',$supported_tag);
303 }
304 return $tags;
305 }
306
307 public static function list_hierarchy($post_content, $toc_hierarchy, $htag_support)
308 {
309 // Whether or not the TOC should be built flat or hierarchical.
310
311 preg_match_all( '/(<h(['.$htag_support.']{1})[^>]*>).*<\/h\2>/msuU', $post_content, $matches, PREG_SET_ORDER );
312 $html = '';
313 if ($matches) {
314 $tag_arr = explode(',', $htag_support);
315 $last_tag_support = end($tag_arr);
316 $first_match = $matches[0][2];
317 $current_match = $first_match; // headings can't be larger than h6 but 100 as a default to be sure
318 $numbered_items = array();
319 $depth_match = array( 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0);
320 $numbered_items_min = null;
321 $level_depth = 1;
322 $depth_match[$first_match] = $level_depth;
323 $max_match = 1;
324 $max_depth = 1;
325 if ($toc_hierarchy == '1') {
326 $html .= '<ul class="toc-list betterdocs-hierarchial-toc">';
327 // find the minimum heading to establish our baseline
328 foreach ($matches as $i => $match) {
329 if ($current_match < $first_match) {
330 $current_match = $first_match;
331 } else if ($current_match > $matches[ $i ][2]) {
332 $current_match = (int) $matches[ $i ][2];
333 }
334 if($matches[ $i ][2] > $max_match){
335 $max_match = $matches[ $i ][2];
336 }
337 }
338
339 $numbered_items[ $current_match ] = 0;
340 $numbered_items_min = $current_match;
341 foreach ($matches as $i => $match) {
342 $level = $matches[ $i ][2];
343
344 if ($depth_match[(int) $matches[ $i ][2]] != 0) {
345 $depth_match[(int) $matches[ $i ][2]] = $level_depth;
346 }
347
348 $depth_status = ($last_tag_support == $level) ? 'stop' : 'continue';
349
350 if ($current_match == (int) $matches[ $i ][2]) {
351 $html .= '<li class="betterdocs-toc-heading-level-' . $current_match . '">';
352 }
353
354 // start lists
355 if ($current_match != (int) $matches[ $i ][2]) {
356 $diff = $current_match - (int) $matches[ $i ][2];
357 for ($current_match; $current_match < (int) $matches[ $i ][2]; $current_match = $current_match - $diff) {
358 if ($depth_status == 'continue') {
359 $level_depth++;
360 }
361 $depth_match[(int) $matches[ $i ][2]] = $level_depth;
362 if (($matches[ $i ][2] == $max_match)) {
363 $max_depth = $level_depth;
364 }
365 $numbered_items[ $current_match + 1 ] = 0;
366 $html .= '<ul class="betterdocs-toc-list-level-' . $level . '"><li class="betterdocs-toc-heading-level-' . $level . '">';
367 }
368 }
369
370 $title = isset( $matches[ $i ]['alternate'] ) ? $matches[ $i ]['alternate'] : $matches[ $i ][0];
371 $title = strip_tags( $title );
372 $has_id = preg_match('/id=(["\'])(.*?)\1[\s>]/si', $matches[ $i ][0], $matched_ids);
373 $id = $has_id ? $matched_ids[2] : $i . '-toc-title';
374 $dynamic_toc_title_switch = BetterDocs_DB::get_settings('toc_dynamic_title');
375 $id = $dynamic_toc_title_switch === 1 || $dynamic_toc_title_switch != 'off' ? sanitize_title($title) : $id;
376 $html .= '<a href="#' . $id . '">' . $title . '</a>';
377
378 // end lists
379 if ($i != count($matches) - 1) {
380 $next_match = (int) $matches[ $i + 1 ][2];
381 $diff = $current_match - $next_match;
382 $level_depth_diff = $level_depth - $depth_match[$next_match];
383
384 if ($current_match > $next_match && $level_depth == 1) {
385 for ($current_match; $current_match > $next_match; $current_match-- ) {
386 $html .= '</li>';
387 $numbered_items[ $current_match ] = 0;
388 $level_depth--;
389 }
390 } else if ($current_match > $next_match && $diff > 1 && $level_depth > $level_depth_diff) {
391 for ($current_match; $current_match > $next_match; $current_match = $current_match - $diff) {
392 for ($k = 1; $k <= $level_depth_diff; $k++) {
393 $html .= '</li></ul>';
394 $numbered_items[ $current_match ] = 0;
395 }
396 $level_depth = $level_depth - $level_depth_diff;
397 }
398 } else if ($current_match > $next_match && $diff > $max_depth) {
399 for ($current_match; $current_match > $next_match; $current_match = $current_match - $max_depth) {
400 $html .= '</li></ul>';
401 $numbered_items[ $current_match ] = 0;
402 $level_depth--;
403 }
404 } else if ($current_match > $next_match) {
405 for ($current_match; $current_match > $next_match; $current_match--) {
406 $html .= '</li></ul>';
407 $numbered_items[ $current_match ] = 0;
408 $level_depth--;
409 }
410 }
411
412 if ($level_depth < 1) {
413 $level_depth = 1;
414 }
415
416 if ($current_match == $next_match) {
417 $html .= '</li>';
418 }
419 } else {
420 // this is the last item, make sure we close off all tags
421 for ($current_match; $current_match >= $numbered_items_min; $current_match--) {
422 $html .= '</li>';
423 if ( $current_match != $numbered_items_min) {
424 $html .= '</ul>';
425 }
426 }
427 }
428 }
429
430 $html .= '</ul>';
431 } else {
432 $html .= '<ul class="toc-list">';
433
434 foreach ($matches as $i => $match) {
435 $count = $i + 1;
436 $title = isset( $matches[ $i ]['alternate'] ) ? $matches[ $i ]['alternate'] : $matches[ $i ][0];
437 $title = strip_tags( apply_filters( 'ez_toc_title', $title ), apply_filters( 'ez_toc_title_allowable_tags', '' ) );
438 $html .= '<li>';
439 $title = isset( $matches[ $i ]['alternate'] ) ? $matches[ $i ]['alternate'] : $matches[ $i ][0];
440 $title = strip_tags( $title );
441 $has_id = preg_match('/id=(["\'])(.*?)\1[\s>]/si', $matches[ $i ][0], $matched_ids);
442 $id = $has_id ? $matched_ids[2] : $i . '-toc-title';
443 $dynamic_toc_title_switch = BetterDocs_DB::get_settings('toc_dynamic_title');
444 $id = $dynamic_toc_title_switch === 1 || $dynamic_toc_title_switch != 'off' ? sanitize_title( $title ) : $id;
445 $html .= '<a href="#'.$id.'">' . $title . '</a>';
446 $html .= '</li>';
447 }
448
449 $html .= '</ul>';
450 }
451 }
452 return $html;
453 }
454
455 public static function betterdocs_toc(
456 $content,
457 $htags,
458 $toc_hierarchy,
459 $list_number,
460 $collapsible,
461 $toc_title=''
462 ) {
463 $html = '';
464 if (!empty(self::list_hierarchy($content, $toc_hierarchy, $htags))) {
465 $toc_class = array( 'betterdocs-toc' );
466 if (empty($toc_title)) {
467 $toc_title = BetterDocs_DB::get_settings('toc_title') ? BetterDocs_DB::get_settings('toc_title') : esc_html__( 'Table of Contents', 'betterdocs' );
468 $toc_title = stripslashes($toc_title);
469 }
470
471 if ($collapsible == '1') {
472 $toc_class[] = 'collapsible-sm';
473 $collapsible_arrow = "<svg class='angle-icon angle-up' aria-hidden='true' focusable='false' data-prefix='fas' data-icon='angle-up' class='svg-inline--fa fa-angle-up fa-w-10' role='img' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 320 512'><path fill='currentColor' d='M177 159.7l136 136c9.4 9.4 9.4 24.6 0 33.9l-22.6 22.6c-9.4 9.4-24.6 9.4-33.9 0L160 255.9l-96.4 96.4c-9.4 9.4-24.6 9.4-33.9 0L7 329.7c-9.4-9.4-9.4-24.6 0-33.9l136-136c9.4-9.5 24.6-9.5 34-.1z'></path></svg><svg class='angle-icon angle-down' aria-hidden='true' focusable='false' data-prefix='fas' data-icon='angle-down' class='svg-inline--fa fa-angle-down fa-w-10' role='img' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 320 512'><path fill='currentColor' d='M143 352.3L7 216.3c-9.4-9.4-9.4-24.6 0-33.9l22.6-22.6c9.4-9.4 24.6-9.4 33.9 0l96.4 96.4 96.4-96.4c9.4-9.4 24.6-9.4 33.9 0l22.6 22.6c9.4 9.4 9.4 24.6 0 33.9l-136 136c-9.2 9.4-24.4 9.4-33.8 0z'></path></svg>";
474 } else {
475 $collapsible_arrow = null;
476 }
477
478 if ($list_number == '1') {
479 $toc_class[] = 'toc-list-number';
480 }
481
482 $html = '<div class="' . implode( ' ', $toc_class ) . '">
483 <span class="toc-title">' . $toc_title . $collapsible_arrow . '</span>';
484 $html .= self::list_hierarchy($content, $toc_hierarchy, $htags);
485 $html .= '</div>';
486 }
487 return $html;
488 }
489
490 /**
491 * Return table of content list before single post the_content
492 *
493 * @since 1.0.0
494 */
495 public static function betterdocs_the_content ($content, $htgs, $enable_toc) {
496 if ($enable_toc == '1' && preg_match_all( '/(<h(['.$htgs.']{1})[^>]*>).*<\/h\2>/msuU', $content, $matches, PREG_SET_ORDER)) {
497 $index = 0;
498 $content = preg_replace_callback( '#<(h['.$htgs.'])(.*?)>(.*?)</\1>#si', function ($matches) use (&$index) {
499 $tag = $matches[1];
500 //$title = strip_tags($matches[3]);
501 preg_match('/id=(["\'])(.*?)\1[\s>]/si', $matches[0], $matched_ids);
502 $id = !empty($matched_ids[2]) ? $matched_ids[2] : $index . '-toc-title';
503 $dynamic_toc_title_switch = BetterDocs_DB::get_settings('toc_dynamic_title');
504 $id = $dynamic_toc_title_switch === 1 || $dynamic_toc_title_switch != 'off' ? sanitize_title( $matches[3] ) : $id;
505 $index++;
506
507 $title_link_ctc = BetterDocs_DB::get_settings('title_link_ctc');
508 if ($title_link_ctc == 1) {
509 $hash_link = '<a href="#'.$id.'" class="batterdocs-anchor" data-clipboard-text="'. get_permalink() .'#'. $id .'" data-title="'.esc_html__('Copy URL','betterdocs').'">#</a>';
510 } else {
511 $hash_link = '';
512 }
513
514 return sprintf('<%s%s class="betterdocs-content-heading" id="%s">%s %s</%s>', $tag, $matches[2], $id, $matches[3], $hash_link, $tag);
515 }, $content );
516 }
517
518 return '<div id="betterdocs-single-content" class="betterdocs-content">'. $content . '</div>';
519 }
520
521 public static function get_last_post_id()
522 {
523 global $wpdb;
524
525 $query = "SELECT ID FROM $wpdb->posts ORDER BY ID DESC LIMIT 0,1";
526
527 $result = $wpdb->get_results($query);
528 $row = $result[0];
529 $id = $row->ID;
530
531 return $id;
532 }
533
534 /**
535 * Insert quick action link to plugin page
536 *
537 * @since 1.1.5
538 */
539 public function insert_plugin_links($links)
540 {
541 $links[] = sprintf('<a href="admin.php?page=betterdocs-settings">' . esc_html__('Settings','betterdocs') . '</a>');
542 return $links;
543 }
544
545 public static function search()
546 {
547 $output = betterdocs_generate_output();
548 $live_search = BetterDocs_DB::get_settings('live_search');
549 $search_placeholder = BetterDocs_DB::get_settings('search_placeholder');
550 $search_heading = $search_subheading = $heading_tag = $subheading_tag = '';
551 if ( $output['betterdocs_live_search_heading_switch'] == true ) {
552 $search_heading_switch = $output['betterdocs_live_search_heading_switch'];
553 $search_heading = $output['betterdocs_live_search_heading'];
554 $search_subheading = $output['betterdocs_live_search_subheading'];
555 $heading_tag = $output['betterdocs_live_search_heading_tag'];
556 $subheading_tag = $output['betterdocs_live_search_subheading_tag'];
557 }
558
559 if ( $live_search == 1 ) {
560 $html = '<div class="betterdocs-search-form-wrap">'. do_shortcode( '[betterdocs_search_form
561 placeholder="'.$search_placeholder.'"
562 heading="'.$search_heading.'"
563 subheading="'.$search_subheading.'" heading_tag="'.$heading_tag.'" subheading_tag="'.$subheading_tag.'"]').'</div>';
564
565 return apply_filters('betterdocs_search_section', $html);
566 }
567 }
568 }
569