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

246 lines 9.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\Dependencies\DI\Container;
10
11 class FrontEnd extends Base {
12 private $container;
13 private $database;
14 /**
15 * Enqueue
16 * @var Enqueue
17 */
18 private $assets;
19 /**
20 * Settings
21 * @var Settings
22 */
23 private $settings;
24 private $widget_attributes = [];
25 private $widget_type = '';
26
27 public function __construct( Container $container, Database $database, Settings $settings ) {
28 $this->container = $container;
29 $this->database = $database;
30 $this->settings = $settings;
31
32 $this->assets = $this->container->get( Enqueue::class );
33
34 add_action( 'init', [$this, 'init'] );
35 add_action( 'wp_enqueue_scripts', [$this, 'enqueue_scripts'] );
36
37 add_filter( 'betterdocs_layout_filename', [$this, 'layout_filename'], 10, 2 );
38
39 add_action( 'betterdocs_docs_before_social', [$this, 'article_reactions'] );
40
41 add_action( 'betterdocs_before_render', [$this, 'before_render'], 11, 2 );
42 add_action( 'betterdocs_after_render', [$this, 'after_render'], 11, 2 );
43
44 //Removes Divi Script On Betterdocs Single Doc #1130, issue title -> ( Bug Fix | With the DIVI theme copy #Url button doesn't seem to work )
45 add_action( 'wp_enqueue_scripts', [$this, 'dequeue_divi_script'], 99999 );
46
47 //Remove Saliant Theme Script For (Delay Javascript Exection), which causes issue with betterdocs sidebar toggle, issue number (#1234)
48 add_action( 'nectar_hook_before_body_close', [$this, 'dequeue_saliant_theme_script'], 99999 );
49 }
50
51 public function before_render( $widget, $widget_type ) {
52 $this->widget_attributes = isset( $widget->attributes ) ? $widget->attributes : [];
53 $this->widget_type = $widget_type;
54
55 /**
56 * This line of code will run for reactions shortcode, elementor widget and blocks
57 */
58 if ( strpos( $widget->get_name(), 'reactions' ) !== false ) {
59 $this->localize_reactions_data();
60 }
61
62 /**
63 * This line of code will run for Feedback form Shortcode, Elementor widget and blocks
64 */
65 if ( strpos( $widget->get_name(), 'feedback_form' ) ) {
66 $this->localize_feedback_form_data();
67 }
68
69 add_filter( 'betterdocs_nested_terms_args', [$this, 'terms_args'], 11, 1 );
70 add_filter( 'betterdocs_nested_docs_args', [$this, 'docs_args'], 11, 1 );
71 }
72
73 public function dequeue_divi_script() {
74 if ( is_singular( 'docs' ) ) {
75 wp_dequeue_script( 'divi-custom-script' );
76 }
77 }
78
79 public function dequeue_saliant_theme_script() {
80 if ( is_singular( 'docs' ) ) {
81 wp_dequeue_script( 'salient-delay-js' );
82 }
83 if ( is_tax( 'doc_category' ) ) {
84 wp_dequeue_script( 'salient-delay-js' );
85 }
86 if ( is_tax( 'doc_tag' ) ) {
87 wp_dequeue_script( 'salient-delay-js' );
88 }
89 }
90
91 public function after_render( $widget, $widget_type ) {
92 remove_filter( 'betterdocs_nested_terms_args', [$this, 'terms_args'], 11 );
93 remove_filter( 'betterdocs_nested_docs_args', [$this, 'docs_args'], 11 );
94
95 $this->widget_attributes = [];
96 $this->widget_type = '';
97 }
98
99 public function terms_args( $args ) {
100 switch ( $this->widget_type ) {
101 case 'shortcode':
102 if ( isset( $this->widget_attributes['terms_orderby'] ) ) {
103 $args['orderby'] = $this->widget_attributes['terms_orderby'];
104 }
105
106 if ( isset( $this->widget_attributes['terms_order'] ) ) {
107 $args['order'] = $this->widget_attributes['terms_order'];
108 }
109 break;
110 case 'blocks':
111 if ( isset( $this->widget_attributes['orderBy'] ) ) {
112 if ( $this->widget_attributes['orderBy'] == 'doc_category_order' ) {
113 $args['orderby'] = 'meta_value_num';
114 $args['meta_key'] = $this->widget_attributes['orderBy'];
115 } else {
116 $args['orderby'] = $this->widget_attributes['orderBy'];
117 }
118 }
119 if ( isset( $this->widget_attributes['order'] ) ) {
120 $args['order'] = $this->widget_attributes['order'];
121 }
122 break;
123 case 'elementor':
124 $args['orderby'] = $this->widget_attributes['orderby'];
125 $args['order'] = $this->widget_attributes['order'];
126 break;
127 }
128
129 return $args;
130 }
131
132 public function docs_args( $args ) {
133 switch ( $this->widget_type ) {
134 case 'shortcode':
135 if ( isset( $this->widget_attributes['orderby'] ) ) {
136 $args['orderby'] = $this->widget_attributes['orderby'];
137 }
138
139 if ( isset( $this->widget_attributes['order'] ) ) {
140 $args['order'] = $this->widget_attributes['order'];
141 }
142 break;
143 case 'blocks':
144 if ( isset( $this->widget_attributes['postsOrderBy'] ) ) {
145 $args['orderby'] = $this->widget_attributes['postsOrderBy'];
146 }
147 if ( isset( $this->widget_attributes['postsOrder'] ) ) {
148 $args['order'] = $this->widget_attributes['postsOrder'];
149 }
150
151 //This is for archive category block only
152 if ( isset( $this->widget_attributes['orderby'] ) ) {
153 $args['orderby'] = $this->widget_attributes['orderby'];
154 }
155 if ( isset( $this->widget_attributes['order'] ) ) {
156 $args['order'] = $this->widget_attributes['order'];
157 }
158 break;
159 case 'elementor':
160 $args['orderby'] = $this->widget_attributes['post_orderby'];
161 $args['order'] = $this->widget_attributes['post_order'];
162 break;
163 }
164
165 return $args;
166 }
167
168 public function article_reactions() {
169 if ( $this->database->get_theme_mod( 'betterdocs_post_reactions', true ) ) {
170 echo do_shortcode( '[betterdocs_article_reactions]' );
171 }
172 }
173
174 public function enqueue_scripts() {
175 if ( is_singular( 'docs' ) ) {
176 wp_enqueue_style( 'betterdocs-single' );
177 wp_enqueue_script( 'clipboard' );
178 }
179
180 if ( is_post_type_archive( 'docs' ) ) {
181 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
182 wp_enqueue_style( 'betterdocs-docs' );
183 }
184
185 if ( is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) ) {
186 wp_enqueue_style( 'betterdocs-doc_category' );
187 }
188
189 if ( is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) || is_singular( 'docs' ) ) {
190 wp_enqueue_style( 'simplebar' );
191 wp_enqueue_script( 'simplebar' );
192 wp_enqueue_script( 'betterdocs-category-grid' );
193 }
194
195 if ( is_post_type_archive( 'docs' ) || is_singular( 'docs' ) || is_tax( 'doc_category' ) || is_tax( 'doc_tag' ) || is_tax( 'knowledge_base' ) ) {
196 wp_enqueue_script( 'betterdocs' );
197 }
198 }
199
200 public function layout_filename( $filename, $origin_layout ) {
201 $filename = ( $origin_layout === 'layout-2' ) ? 'default' : $filename;
202 return $filename;
203 }
204
205 public function init() {
206 $this->container->get( TemplateLoader::class )->init();
207
208 add_filter( 'betterdocs_articles_args', [$this, 'article_args'], 11, 3 );
209 }
210
211 public function article_args( $args, $term_id, $_origin_args ) {
212 if ( null == $term_id || isset( $args['orderby'] ) ) {
213 return $args;
214 }
215
216 $post__in = betterdocs()->query->get_docs_order_by_terms( $term_id );
217
218 if ( ! empty( $post__in ) ) {
219 $args['orderby'] = 'post__in';
220 $args['post__in'] = $post__in;
221 }
222
223 return $args;
224 }
225
226 public function localize_reactions_data() {
227 $this->assets->localize( 'betterdocs-reactions', 'betterdocsReactionsConfig', [
228 'post_id' => get_the_ID(),
229 'FEEDBACK' => [
230 'DISPLAY' => true,
231 'TEXT' => esc_html__( 'How did you feel?', 'betterdocs' ),
232 'SUCCESS' => esc_html__( 'Thanks for your feedback', 'betterdocs' ),
233 'URL' => get_rest_url( null, '/betterdocs/v1/feedback' )
234 ]
235 ] );
236 }
237
238 public function localize_feedback_form_data() {
239 $this->assets->localize( 'betterdocs', 'betterdocsSubmitFormConfig', [
240 'ajax_url' => admin_url( 'admin-ajax.php' ),
241 'post_id' => get_the_ID(),
242 'nonce' => wp_create_nonce( 'betterdocs_submit_data' )
243 ] );
244 }
245 }
246