PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.2.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.2.0
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 / FrontEnd / FrontEnd.php

FrontEnd.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.2.0, at includes/FrontEnd/FrontEnd.php

340 lines 11.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\FrontEnd;
4
5 use WPDeveloper\BetterDocs\Utils\Base;
6 use WPDeveloper\BetterDocs\Utils\Helper;
7 use WPDeveloper\BetterDocs\Core\Settings;
8 use WPDeveloper\BetterDocs\Utils\Enqueue;
9 use WPDeveloper\BetterDocs\Utils\Database;
10 use WPDeveloper\BetterDocs\Dependencies\DI\Container;
11
12 class FrontEnd extends Base {
13 private $container;
14 private $database;
15 /**
16 * Enqueue
17 * @var Enqueue
18 */
19 private $assets;
20 /**
21 * Settings
22 * @var Settings
23 */
24 private $settings;
25 private $widget_attributes = [];
26 private $widget_type = '';
27
28 public function __construct( Container $container, Database $database, Settings $settings ) {
29 $this->container = $container;
30 $this->database = $database;
31 $this->settings = $settings;
32
33 $this->assets = $this->container->get( Enqueue::class );
34
35 add_action( 'init', [ $this, 'init' ] );
36 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
37
38 /**
39 * Update The 'Edit Site' Url In FSE Mode For Betterdocs Templates Only
40 */
41 if ( Helper::is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) && wp_is_block_theme() ) {
42 add_action( 'admin_bar_menu', [$this, 'fse_url_update'], 41, 1 );
43 }
44
45 add_filter( 'betterdocs_layout_filename', [ $this, 'layout_filename' ], 10, 2 );
46
47 add_action( 'betterdocs_docs_before_social', [ $this, 'article_reactions' ] );
48
49 add_action( 'betterdocs_before_render', [ $this, 'before_render' ], 11, 2 );
50 add_action( 'betterdocs_after_render', [ $this, 'after_render' ], 11, 2 );
51
52 //Remove Saliant Theme Script For (Delay Javascript Exection), which causes issue with betterdocs sidebar toggle, issue number (#1234)
53 add_action( 'nectar_hook_before_body_close', [ $this, 'dequeue_saliant_theme_script' ], 99999 );
54
55 //Remove our search selector from from Searchanise plugin for woocommerce, conflicts with betterdocs search (Bug Fix Card -> https://trello.com/c/lXzrtv2f/1313-client-issue-betterdocs-is-conflicting-with-the-searchanise-plugin)
56 add_filter( 'se_load_search_widgets', [ $this, 'exclude_betterdocs_search' ], 10, 1 );
57
58 //Fix Betterdocs Search Issue With HostCluster Theme
59 if ( function_exists( 'hostcluster_search_filter' ) ) {
60 remove_filter( 'pre_get_posts', 'hostcluster_search_filter' );
61 }
62
63 //render authors template
64 add_filter( 'template_include', [$this, 'render_authors_template'], 10, 1 );
65 }
66
67 public function render_authors_template( $template_path ) {
68 $post_type = get_query_var( 'post_type' ) != null ? get_query_var( 'post_type' ) : '';
69 $author_id = get_query_var( 'author' ) != null ? get_query_var( 'author' ) : 0;
70 $author_docs_count = count_user_posts( $author_id, 'docs' );
71
72 if ( $post_type == 'docs' && $author_docs_count > 0 ) {
73 wp_enqueue_style('betterdocs-breadcrumb');
74 $template_path = BETTERDOCS_ABSPATH . 'views/templates/authors/author.php';
75 }
76
77 return $template_path;
78 }
79
80 public function fse_url_update( &$wp_admin_bar ) {
81 $site_edit_node = $wp_admin_bar->get_node( 'site-editor' );
82
83 if ( empty( $site_edit_node ) ) {
84 return;
85 }
86
87 if ( is_post_type_archive( 'docs' ) || is_tax( 'knowledge_base' ) || is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) || is_singular( 'docs' ) ) {
88 $href = isset( $site_edit_node->href ) ? $site_edit_node->href : '';
89 if ( ! empty( $href ) ) {
90 $href = $href . '&lang=' . ICL_LANGUAGE_CODE;
91 $site_edit_node->href = $href;
92 $wp_admin_bar->add_node( $site_edit_node );
93 }
94 }
95 }
96
97 public function exclude_betterdocs_search( $options ) {
98 $options['search_input'] = $options['search_input'] . ':not(.betterdocs-search-field)';
99 return $options;
100 }
101 public function before_render( $widget, $widget_type ) {
102 $this->widget_attributes = isset( $widget->attributes ) ? $widget->attributes : [];
103 $this->widget_type = $widget_type;
104
105 /**
106 * This line of code will run for reactions shortcode, elementor widget and blocks
107 */
108 if ( strpos( $widget->get_name(), 'reactions' ) !== false ) {
109 $this->localize_reactions_data();
110 }
111
112 /**
113 * This line of code will run for Feedback form Shortcode, Elementor widget and blocks
114 */
115 if ( strpos( $widget->get_name(), 'feedback_form' ) ) {
116 $this->localize_feedback_form_data();
117 }
118
119 add_filter( 'betterdocs_nested_terms_args', [ $this, 'terms_args' ], 11, 1 );
120 add_filter( 'betterdocs_nested_docs_args', [ $this, 'docs_args' ], 11, 1 );
121 }
122
123 public function dequeue_saliant_theme_script() {
124 if ( is_singular( 'docs' ) || is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) ) {
125 wp_dequeue_script( 'salient-delay-js' );
126 }
127 }
128
129 public function after_render( $widget, $widget_type ) {
130 remove_filter( 'betterdocs_nested_terms_args', [ $this, 'terms_args' ], 11 );
131 remove_filter( 'betterdocs_nested_docs_args', [ $this, 'docs_args' ], 11 );
132
133 $this->widget_attributes = [];
134 $this->widget_type = '';
135 }
136
137 public function terms_args( $args ) {
138 switch ( $this->widget_type ) {
139 case 'shortcode':
140 if ( isset( $this->widget_attributes['terms_orderby'] ) ) {
141 $args['orderby'] = $this->widget_attributes['terms_orderby'];
142 }
143
144 if ( isset( $this->widget_attributes['terms_order'] ) ) {
145 $args['order'] = $this->widget_attributes['terms_order'];
146 }
147 break;
148 case 'blocks':
149 if ( isset( $this->widget_attributes['orderBy'] ) ) {
150 if ( $this->widget_attributes['orderBy'] == 'doc_category_order' ) {
151 $args['orderby'] = 'meta_value_num';
152 $args['meta_key'] = $this->widget_attributes['orderBy'];
153 } else {
154 $args['orderby'] = $this->widget_attributes['orderBy'];
155 }
156 }
157 if ( isset( $this->widget_attributes['order'] ) ) {
158 $args['order'] = $this->widget_attributes['order'];
159 }
160 break;
161 case 'elementor':
162 $args['orderby'] = $this->widget_attributes['orderby'];
163 $args['order'] = $this->widget_attributes['order'];
164 break;
165 }
166
167 return $args;
168 }
169
170 public function docs_args( $args ) {
171 switch ( $this->widget_type ) {
172 case 'shortcode':
173 if ( isset( $this->widget_attributes['orderby'] ) ) {
174 $args['orderby'] = $this->widget_attributes['orderby'];
175 }
176
177 if ( isset( $this->widget_attributes['order'] ) ) {
178 $args['order'] = $this->widget_attributes['order'];
179 }
180 break;
181 case 'blocks':
182 if ( isset( $this->widget_attributes['postsOrderBy'] ) ) {
183 $args['orderby'] = $this->widget_attributes['postsOrderBy'];
184 }
185 if ( isset( $this->widget_attributes['postsOrder'] ) ) {
186 $args['order'] = $this->widget_attributes['postsOrder'];
187 }
188
189 //This is for archive category block only
190 if ( isset( $this->widget_attributes['orderby'] ) ) {
191 $args['orderby'] = $this->widget_attributes['orderby'];
192 }
193 if ( isset( $this->widget_attributes['order'] ) ) {
194 $args['order'] = $this->widget_attributes['order'];
195 }
196 break;
197 case 'elementor':
198 $args['orderby'] = $this->widget_attributes['post_orderby'];
199 $args['order'] = $this->widget_attributes['post_order'];
200 break;
201 }
202
203 return $args;
204 }
205
206 public function article_reactions() {
207 $args = [];
208 $single_layout = $this->database->get_theme_mod( 'betterdocs_single_layout_select', true );
209 $reactions = $this->database->get_theme_mod( 'betterdocs_post_reactions', true );
210
211 // Get the title tag from customizer
212 $text_tag = $this->database->get_theme_mod( 'betterdocs_reactions_title_tag', 'h5' );
213
214 // Collect reaction values and icons
215 $reactions_data = [
216 'happy' => 'betterdocs_post_reactions_happy',
217 'happy_icon' => 'betterdocs_post_reactions_happy_icon',
218 'normal' => 'betterdocs_post_reactions_normal',
219 'normal_icon' => 'betterdocs_post_reactions_normal_icon',
220 'sad' => 'betterdocs_post_reactions_sad',
221 'sad_icon' => 'betterdocs_post_reactions_sad_icon'
222 ];
223
224 foreach ( $reactions_data as $key => $theme_mod ) {
225 $value = betterdocs()->customizer->defaults->get( $theme_mod );
226 if ( $value ) {
227 $args[ $key ] = $value;
228 } else {
229 $args[ $key ] = false;
230 }
231 }
232
233 // Build the attribute string for the shortcode
234 $attr = '';
235 foreach ( $args as $key => $value ) {
236 $attr .= sprintf( ' %s="%s"', esc_attr( $key ), esc_attr( $value ) );
237 }
238
239 // Render the shortcode based on layout and reactions availability
240 if ( $single_layout == 'layout-8' || $single_layout == 'layout-10' && $reactions ) {
241 echo do_shortcode( '[betterdocs_article_reactions text_tag="' . esc_attr( $text_tag ) . '" layout="layout-2"' . $attr . ']' );
242 } elseif ( $single_layout == 'layout-9' && $reactions ) {
243 echo '';
244 } elseif ( $reactions ) {
245 echo do_shortcode( '[betterdocs_article_reactions text_tag="' . esc_attr( $text_tag ) . '"' . $attr . ']' );
246 }
247 }
248
249
250 public function enqueue_scripts() {
251 if ( is_singular( 'docs' ) ) {
252 wp_enqueue_style( 'betterdocs-single' );
253 wp_enqueue_style( 'betterdocs-article-summary' );
254 wp_enqueue_style( 'betterdocs-encyclopedia' );
255 wp_enqueue_style( 'betterdocs-glossaries' );
256 wp_enqueue_script( 'clipboard' );
257 wp_enqueue_script( 'betterdocs-glossaries' );
258 }
259
260 if ( is_post_type_archive( 'docs' ) ) {
261 wp_enqueue_style( 'betterdocs-category-grid' ); //category grid shortcode is supposed to enqueue this style, but this is called again to fix flicking of UI on Docs Page
262 wp_enqueue_style( 'betterdocs-docs' );
263 }
264
265 if ( is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) ) {
266 wp_enqueue_style( 'betterdocs-doc_category' );
267 }
268
269 if ( is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) || is_singular( 'docs' ) ) {
270 wp_enqueue_style( 'simplebar' );
271 wp_enqueue_script( 'simplebar' );
272 wp_enqueue_script( 'betterdocs-category-grid' );
273 }
274
275 if ( is_post_type_archive( 'docs' ) || is_singular( 'docs' ) || is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) || is_tax( 'knowledge_base' ) ) {
276 wp_enqueue_script( 'betterdocs' );
277 }
278
279 if ( is_tax( 'glossaries' ) ) {
280 wp_enqueue_style( 'betterdocs-encyclopedia' );
281 wp_enqueue_style( 'betterdocs-single' );
282 wp_enqueue_style( 'betterdocs-glossaries' );
283 }
284 }
285
286 public function layout_filename( $filename, $origin_layout ) {
287 $filename = ( $origin_layout === 'layout-2' ) ? 'default' : $filename;
288 return $filename;
289 }
290
291 public function init() {
292 $this->container->get( TemplateLoader::class )->init();
293
294 add_filter( 'betterdocs_articles_args', [ $this, 'article_args' ], 11, 3 );
295 }
296
297 public function article_args( $args, $term_id, $_origin_args ) {
298 if ( null == $term_id || isset( $args['orderby'] ) ) {
299 return $args;
300 }
301
302 $post__in = betterdocs()->query->get_docs_order_by_terms( $term_id );
303
304 if ( ! empty( $post__in ) ) {
305 $args['orderby'] = 'post__in';
306 $args['post__in'] = $post__in;
307 }
308
309 return $args;
310 }
311
312 public function localize_reactions_data() {
313 $this->assets->localize(
314 'betterdocs-reactions',
315 'betterdocsReactionsConfig',
316 [
317 'post_id' => get_the_ID(),
318 'FEEDBACK' => [
319 'DISPLAY' => true,
320 'TEXT' => esc_html__( 'How did you feel?', 'betterdocs' ),
321 'SUCCESS' => betterdocs()->settings->get( 'reaction_feedback_text', __( 'Thanks for your feedback', 'betterdocs' ) ),
322 'URL' => get_rest_url( null, '/betterdocs/v1/feedback' )
323 ]
324 ]
325 );
326 }
327
328 public function localize_feedback_form_data() {
329 $this->assets->localize(
330 'betterdocs',
331 'betterdocsSubmitFormConfig',
332 [
333 'ajax_url' => admin_url( 'admin-ajax.php' ),
334 'post_id' => get_the_ID(),
335 'nonce' => wp_create_nonce( 'betterdocs_submit_data' )
336 ]
337 );
338 }
339 }
340