PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.7.3
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.7.3
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 3.7.3, at includes/FrontEnd/FrontEnd.php

284 lines 11.0 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\Core\Settings;
7 use WPDeveloper\BetterDocs\Utils\Enqueue;
8 use WPDeveloper\BetterDocs\Utils\Database;
9 use WPDeveloper\BetterDocs\Utils\Helper;
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
64 public function fse_url_update( &$wp_admin_bar ) {
65 $site_edit_node = $wp_admin_bar->get_node('site-editor');
66
67 if( empty( $site_edit_node ) ) {
68 return;
69 }
70
71 if( is_post_type_archive('docs') || is_tax( 'knowledge_base' ) || is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) || is_singular( 'docs' ) ) {
72 $href = isset( $site_edit_node->href ) ? $site_edit_node->href : '';
73 if( ! empty( $href ) ) {
74 $href = $href . '&lang='.ICL_LANGUAGE_CODE;
75 $site_edit_node->href = $href;
76 $wp_admin_bar->add_node( $site_edit_node );
77 }
78 }
79 }
80
81 public function exclude_betterdocs_search( $options ) {
82 $options['search_input'] = $options['search_input'] . ':not(.betterdocs-search-field)';
83 return $options;
84 }
85 public function before_render( $widget, $widget_type ) {
86 $this->widget_attributes = isset( $widget->attributes ) ? $widget->attributes : [];
87 $this->widget_type = $widget_type;
88
89 /**
90 * This line of code will run for reactions shortcode, elementor widget and blocks
91 */
92 if ( strpos( $widget->get_name(), 'reactions' ) !== false ) {
93 $this->localize_reactions_data();
94 }
95
96 /**
97 * This line of code will run for Feedback form Shortcode, Elementor widget and blocks
98 */
99 if ( strpos( $widget->get_name(), 'feedback_form' ) ) {
100 $this->localize_feedback_form_data();
101 }
102
103 add_filter( 'betterdocs_nested_terms_args', [$this, 'terms_args'], 11, 1 );
104 add_filter( 'betterdocs_nested_docs_args', [$this, 'docs_args'], 11, 1 );
105 }
106
107 public function dequeue_saliant_theme_script() {
108 if ( is_singular( 'docs' ) || is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) ) {
109 wp_dequeue_script( 'salient-delay-js' );
110 }
111 }
112
113 public function after_render( $widget, $widget_type ) {
114 remove_filter( 'betterdocs_nested_terms_args', [$this, 'terms_args'], 11 );
115 remove_filter( 'betterdocs_nested_docs_args', [$this, 'docs_args'], 11 );
116
117 $this->widget_attributes = [];
118 $this->widget_type = '';
119 }
120
121 public function terms_args( $args ) {
122 switch ( $this->widget_type ) {
123 case 'shortcode':
124 if ( isset( $this->widget_attributes['terms_orderby'] ) ) {
125 $args['orderby'] = $this->widget_attributes['terms_orderby'];
126 }
127
128 if ( isset( $this->widget_attributes['terms_order'] ) ) {
129 $args['order'] = $this->widget_attributes['terms_order'];
130 }
131 break;
132 case 'blocks':
133 if ( isset( $this->widget_attributes['orderBy'] ) ) {
134 if ( $this->widget_attributes['orderBy'] == 'doc_category_order' ) {
135 $args['orderby'] = 'meta_value_num';
136 $args['meta_key'] = $this->widget_attributes['orderBy'];
137 } else {
138 $args['orderby'] = $this->widget_attributes['orderBy'];
139 }
140 }
141 if ( isset( $this->widget_attributes['order'] ) ) {
142 $args['order'] = $this->widget_attributes['order'];
143 }
144 break;
145 case 'elementor':
146 $args['orderby'] = $this->widget_attributes['orderby'];
147 $args['order'] = $this->widget_attributes['order'];
148 break;
149 }
150
151 return $args;
152 }
153
154 public function docs_args( $args ) {
155 switch ( $this->widget_type ) {
156 case 'shortcode':
157 if ( isset( $this->widget_attributes['orderby'] ) ) {
158 $args['orderby'] = $this->widget_attributes['orderby'];
159 }
160
161 if ( isset( $this->widget_attributes['order'] ) ) {
162 $args['order'] = $this->widget_attributes['order'];
163 }
164 break;
165 case 'blocks':
166 if ( isset( $this->widget_attributes['postsOrderBy'] ) ) {
167 $args['orderby'] = $this->widget_attributes['postsOrderBy'];
168 }
169 if ( isset( $this->widget_attributes['postsOrder'] ) ) {
170 $args['order'] = $this->widget_attributes['postsOrder'];
171 }
172
173 //This is for archive category block only
174 if ( isset( $this->widget_attributes['orderby'] ) ) {
175 $args['orderby'] = $this->widget_attributes['orderby'];
176 }
177 if ( isset( $this->widget_attributes['order'] ) ) {
178 $args['order'] = $this->widget_attributes['order'];
179 }
180 break;
181 case 'elementor':
182 $args['orderby'] = $this->widget_attributes['post_orderby'];
183 $args['order'] = $this->widget_attributes['post_order'];
184 break;
185 }
186
187 return $args;
188 }
189
190 public function article_reactions() {
191 $single_layout = $this->database->get_theme_mod( 'betterdocs_single_layout_select', true );
192 $reactions = $this->database->get_theme_mod( 'betterdocs_post_reactions', true );
193
194 if ( $single_layout == 'layout-8' && $reactions ) {
195 echo do_shortcode( '[betterdocs_article_reactions layout="layout-2"]' );
196 } else if ( $single_layout == 'layout-9' && $reactions ) {
197 echo '';
198 } else if ( $reactions ) {
199 echo do_shortcode( '[betterdocs_article_reactions]' );
200 }
201 }
202
203 public function enqueue_scripts() {
204 if ( is_singular( 'docs' ) ) {
205 wp_enqueue_style( 'betterdocs-single' );
206 wp_enqueue_style( 'betterdocs-encyclopedia' );
207 wp_enqueue_style( 'betterdocs-glossaries' );
208 wp_enqueue_script( 'clipboard' );
209 wp_enqueue_script( 'betterdocs-glossaries' );
210 }
211
212 if ( is_post_type_archive( 'docs' ) ) {
213 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
214 wp_enqueue_style( 'betterdocs-docs' );
215 }
216
217 if ( is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) ) {
218 wp_enqueue_style( 'betterdocs-doc_category' );
219 }
220
221 if ( is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) || is_singular( 'docs' ) ) {
222 wp_enqueue_style( 'simplebar' );
223 wp_enqueue_script( 'simplebar' );
224 wp_enqueue_script( 'betterdocs-category-grid' );
225 }
226
227 if ( is_post_type_archive( 'docs' ) || is_singular( 'docs' ) || is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) || is_tax( 'knowledge_base' ) ) {
228 wp_enqueue_script( 'betterdocs' );
229 }
230
231 if(is_tax('glossaries')){
232 wp_enqueue_style('betterdocs-encyclopedia');
233 wp_enqueue_style('betterdocs-single');
234 wp_enqueue_style('betterdocs-glossaries');
235 }
236 }
237
238 public function layout_filename( $filename, $origin_layout ) {
239 $filename = ( $origin_layout === 'layout-2' ) ? 'default' : $filename;
240 return $filename;
241 }
242
243 public function init() {
244 $this->container->get( TemplateLoader::class )->init();
245
246 add_filter( 'betterdocs_articles_args', [$this, 'article_args'], 11, 3 );
247 }
248
249 public function article_args( $args, $term_id, $_origin_args ) {
250 if ( null == $term_id || isset( $args['orderby'] ) ) {
251 return $args;
252 }
253
254 $post__in = betterdocs()->query->get_docs_order_by_terms( $term_id );
255
256 if ( ! empty( $post__in ) ) {
257 $args['orderby'] = 'post__in';
258 $args['post__in'] = $post__in;
259 }
260
261 return $args;
262 }
263
264 public function localize_reactions_data() {
265 $this->assets->localize( 'betterdocs-reactions', 'betterdocsReactionsConfig', [
266 'post_id' => get_the_ID(),
267 'FEEDBACK' => [
268 'DISPLAY' => true,
269 'TEXT' => esc_html__( 'How did you feel?', 'betterdocs' ),
270 'SUCCESS' => betterdocs()->settings->get( 'reaction_feedback_text', __( 'Thanks for your feedback', 'betterdocs' ) ),
271 'URL' => get_rest_url( null, '/betterdocs/v1/feedback' )
272 ]
273 ] );
274 }
275
276 public function localize_feedback_form_data() {
277 $this->assets->localize( 'betterdocs', 'betterdocsSubmitFormConfig', [
278 'ajax_url' => admin_url( 'admin-ajax.php' ),
279 'post_id' => get_the_ID(),
280 'nonce' => wp_create_nonce( 'betterdocs_submit_data' )
281 ] );
282 }
283 }
284