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

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

227 lines 7.7 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\Views;
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
37 add_action( 'wp_enqueue_scripts', [$this, 'enqueue_scripts'] );
38
39 add_action( 'template_redirect', [$this, 'template_redirect'] );
40 add_filter( 'betterdocs_layout_filename', [$this, 'layout_filename'], 10, 2 );
41
42 add_action( 'betterdocs_docs_before_social', [$this, 'article_reactions'] );
43
44 add_action('betterdocs_before_render', [ $this, 'before_render' ], 11, 2 );
45 add_action('betterdocs_after_render', [ $this, 'after_render' ], 11, 2 );
46 }
47
48 public function before_render( $widget, $widget_type ) {
49 $this->widget_attributes = isset( $widget->attributes ) ? $widget->attributes : [];
50 $this->widget_type = $widget_type;
51
52 /**
53 * This line of code will run for reactions shortcode, elementor widget and blocks
54 */
55 if( strpos( $widget->get_name(), 'reactions' ) !== false ) {
56 $this->localize_reactions_data();
57 }
58
59 /**
60 * This line of code will run for Feedback form Shortcode, Elementor widget and blocks
61 */
62 if( strpos( $widget->get_name(), 'feedback_form' ) ){
63 $this->localize_feedback_form_data();
64 }
65
66 add_filter('betterdocs_nested_terms_args', [$this, 'terms_args'], 11, 1);
67 add_filter('betterdocs_nested_docs_args', [$this, 'docs_args'], 11, 1);
68 }
69
70 public function after_render( $widget, $widget_type ){
71 remove_filter('betterdocs_nested_terms_args', [$this, 'terms_args'], 11);
72 remove_filter('betterdocs_nested_docs_args', [$this, 'docs_args'], 11);
73
74 $this->widget_attributes = [];
75 $this->widget_type = '';
76 }
77
78 public function terms_args( $args ){
79 switch( $this->widget_type ) {
80 case 'shortcode':
81 if( isset( $this->widget_attributes['terms_orderby'] ) ) {
82 $args['orderby'] = $this->widget_attributes['terms_orderby'];
83 }
84
85 if( isset( $this->widget_attributes['terms_order'] ) ) {
86 $args['order'] = $this->widget_attributes['terms_order'];
87 }
88 break;
89 case 'blocks':
90 $args['orderby'] = $this->widget_attributes['orderBy'];
91 $args['order'] = $this->widget_attributes['order'];
92 break;
93 case 'elementor':
94 $args['orderby'] = $this->widget_attributes['orderby'];
95 $args['order'] = $this->widget_attributes['order'];
96 break;
97 }
98
99 return $args;
100 }
101
102 public function docs_args( $args ){
103 switch( $this->widget_type ) {
104 case 'shortcode':
105 if( isset( $this->widget_attributes['orderby'] ) ) {
106 $args['orderby'] = $this->widget_attributes['orderby'];
107 }
108
109 if( isset( $this->widget_attributes['order'] ) ) {
110 $args['order'] = $this->widget_attributes['order'];
111 }
112 break;
113 case 'blocks':
114 $args['orderby'] = $this->widget_attributes['postsOrderBy'];
115 $args['order'] = $this->widget_attributes['postsOrder'];
116 break;
117 case 'elementor':
118 $args['orderby'] = $this->widget_attributes['post_orderby'];
119 $args['order'] = $this->widget_attributes['post_order'];
120 break;
121 }
122
123 return $args;
124 }
125
126 public function article_reactions() {
127 if ( $this->database->get_theme_mod( 'betterdocs_post_reactions', true ) ) {
128 echo do_shortcode( '[betterdocs_article_reactions]' );
129 }
130 }
131
132 public function enqueue_scripts() {
133 if( is_singular( 'docs' ) ) {
134 wp_enqueue_style( 'betterdocs-single' );
135 wp_enqueue_script( 'clipboard' );
136 }
137
138 if( is_post_type_archive( 'docs' ) ) {
139 wp_enqueue_style( 'betterdocs-docs' );
140 }
141
142 if( is_tax( 'doc_category' ) || is_tax( 'doc_tag') ) {
143 wp_enqueue_style( 'betterdocs-doc_category' );
144 }
145
146 if( is_tax( 'doc_category' ) || is_tax( 'doc_tag') || is_singular( 'docs' ) ) {
147 wp_enqueue_style( 'simplebar' );
148 wp_enqueue_script( 'simplebar' );
149 wp_enqueue_script( 'betterdocs-category-grid' );
150 }
151
152 if( is_post_type_archive('docs') || is_singular( 'docs' ) || is_tax( 'doc_category') || is_tax( 'doc_tag') || is_tax( 'knowledge_base') ) {
153 wp_enqueue_script( 'betterdocs' );
154 }
155 }
156
157 public function layout_filename( $filename, $origin_layout ) {
158 $filename = ( $origin_layout === 'layout-2' ) ? 'default' : $filename;
159 return $filename;
160 }
161
162 public function init() {
163 $this->container->get( TemplateLoader::class )->init();
164
165 // add_filter( 'single_template', [$this, 'single_template'], 99 );
166 add_filter( 'betterdocs_articles_args', [$this, 'article_args'], 11, 3 );
167 }
168
169 public function template_redirect() {
170 // global $wp_query;
171 // dump( get_queried_object() );
172 // die;
173 }
174
175 public function single_template( $template ) {
176 if ( ! is_singular( 'docs' ) ) {
177 return $template;
178 }
179
180 $layout = $this->database->get_theme_mod( 'betterdocs_single_layout_select', 'layout-1' );
181 $layout = 'templates/single/' . $layout;
182
183 $eligible_template = $this->container->get( Views::class )->path( $layout, 'templates/single/layout-1' );
184
185 if ( file_exists( $eligible_template ) ) {
186 $template = &$eligible_template;
187 }
188
189 return apply_filters( 'betterdocs_single_template', $template );
190 }
191
192 public function article_args( $args, $term_id, $_origin_args ) {
193 if ( null == $term_id || isset( $args['orderby'] ) ) {
194 return $args;
195 }
196
197 $post__in = betterdocs()->query->get_docs_order_by_terms( $term_id );
198
199 if( ! empty( $post__in ) ) {
200 $args['orderby'] = 'post__in';
201 $args['post__in'] = $post__in;
202 }
203
204 return $args;
205 }
206
207 public function localize_reactions_data(){
208 $this->assets->localize( 'betterdocs-reactions', 'betterdocsReactionsConfig', [
209 'post_id' => get_the_ID(),
210 'FEEDBACK' => array(
211 'DISPLAY' => true,
212 'TEXT' => esc_html__('How did you feel?', 'betterdocs'),
213 'SUCCESS' => esc_html__('Thanks for your feedback', 'betterdocs'),
214 'URL' => get_rest_url(null, '/betterdocs/v1/feedback'),
215 ),
216 ] );
217 }
218
219 public function localize_feedback_form_data() {
220 $this->assets->localize( 'betterdocs', 'betterdocsSubmitFormConfig', [
221 'ajax_url' => admin_url( 'admin-ajax.php' ),
222 'post_id' => get_the_ID(),
223 'nonce' => wp_create_nonce( 'betterdocs_submit_data' )
224 ] );
225 }
226 }
227