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

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

487 lines 14.2 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;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly
7 }
8
9 use WPDeveloper\BetterDocs\Admin\Analytics;
10 use WPDeveloper\BetterDocs\Admin\Customizer\Customizer;
11 use WPDeveloper\BetterDocs\Admin\HelpScoutMigration;
12 use WPDeveloper\BetterDocs\Admin\ReportEmail;
13 use WPDeveloper\BetterDocs\Core\Admin;
14 use WPDeveloper\BetterDocs\Core\BaseAPI;
15 use WPDeveloper\BetterDocs\Core\Install;
16 use WPDeveloper\BetterDocs\Core\KBMigration;
17 use WPDeveloper\BetterDocs\Core\Query;
18 use WPDeveloper\BetterDocs\Core\Request;
19 use WPDeveloper\BetterDocs\Core\Rewrite;
20 use WPDeveloper\BetterDocs\Core\Roles;
21 use WPDeveloper\BetterDocs\Core\Scripts;
22 use WPDeveloper\BetterDocs\Core\Settings;
23 use WPDeveloper\BetterDocs\Core\ShortcodeFactory;
24 use WPDeveloper\BetterDocs\Core\WriteWithAI;
25 use WPDeveloper\BetterDocs\Core\ArticleSummary;
26 use WPDeveloper\BetterDocs\Core\ArticleQualityScore;
27 use WPDeveloper\BetterDocs\Core\UnifiedMetabox;
28 use WPDeveloper\BetterDocs\Dependencies\DI\Container;
29 use WPDeveloper\BetterDocs\Dependencies\DI\ContainerBuilder;
30 use WPDeveloper\BetterDocs\Editors\Editor;
31 use WPDeveloper\BetterDocs\FrontEnd\FrontEnd;
32 use WPDeveloper\BetterDocs\FrontEnd\SearchExtender;
33 use WPDeveloper\BetterDocs\FrontEnd\TemplateTags;
34 use WPDeveloper\BetterDocs\Modules\StyleHandler as ModulesStyleHandler;
35 use WPDeveloper\BetterDocs\Utils\Database;
36 use WPDeveloper\BetterDocs\Utils\Enqueue;
37 use WPDeveloper\BetterDocs\Utils\Helper;
38 use WPDeveloper\BetterDocs\Utils\Views;
39
40 final class Plugin {
41 private static $_instance = null;
42 /**
43 * Assets manager
44 *
45 * @var Enqueue
46 */
47 public $assets;
48 /**
49 * View manager
50 *
51 * @var Views
52 */
53 public $views;
54 /**
55 * Container Manager
56 *
57 * @var Container
58 */
59 public $container;
60 /**
61 * Editor Manager
62 * @var Editor
63 */
64 public $editor;
65 /**
66 * Helper class
67 * @var Helper
68 */
69 public $helper;
70 /**
71 * KBMigration class
72 * @var KBMigration
73 */
74 public $kbmigration;
75 /**
76 * KBMigration class
77 * @var Admin
78 */
79 public $admin;
80 /**
81 * Helper class
82 * @var Database
83 */
84 public $database;
85 /**
86 * Helper class
87 * @var Settings
88 */
89 public $settings;
90 /**
91 * Helper class
92 * @var TemplateTags
93 */
94 public $template_helper;
95 /**
96 * Article Summary class
97 * @var ArticleSummary
98 */
99 public $article_summary;
100 /**
101 * Customizer class
102 * @var Customizer
103 */
104 public $customizer;
105 /**
106 * Query class
107 * @var Query
108 */
109 public $query;
110 /**
111 * Rewrite Class
112 * @var Rewrite
113 */
114 public $rewrite;
115 /**
116 * Request Class
117 * @var Request
118 */
119 public $request;
120 /**
121 * Analytics Class
122 * @var Analytics
123 */
124 public $analytics;
125 /**
126 * Plugin Version
127 * @var string
128 */
129 public $version = '4.5.5';
130
131 /**
132 * WriteWithAI Class
133 * @var string
134 */
135 public $ai_autowrtie;
136 public $backgroundProccessor;
137
138 /**
139 * ArticleQualityScore Class
140 * @var ArticleQualityScore
141 */
142 public $article_quality_score;
143
144 /**
145 * Plugin DB Version
146 * @var string
147 */
148 public $db_version = '1.0.1';
149
150 public function __construct() {
151 $this->define_constants();
152
153 do_action( 'betterdocs_init_before' );
154
155 $this->setup_container();
156 /**
157 * Register activation and deactivation hooks
158 * and version updates check
159 */
160 $this->container->get( Install::class );
161
162 add_action( 'init', array( $this, 'initialize' ), 0 );
163
164 /**
165 * Initialize API
166 */
167 add_action( 'rest_api_init', array( $this, 'api_initialization' ) );
168
169 /**
170 * For admin only
171 */
172 add_action( 'admin_init', array( $this, 'admin_init' ), 0 );
173
174 /**
175 * For AJAX only
176 */
177 $this->ajax();
178
179 /**
180 * Style Handler For Parsing and Saving Styles as file.
181 */
182 ModulesStyleHandler::init();
183 }
184
185 private function define_constants() {
186 $this->define( 'BETTERDOCS_VERSION', $this->version );
187 $this->define( 'BETTERDOCS_DB_VERSION', $this->db_version );
188 $this->define( 'BETTERDOCS_ABSPATH', dirname( BETTERDOCS_PLUGIN_FILE ) . '/' );
189 $this->define( 'BETTERDOCS_ABSURL', plugin_dir_url( BETTERDOCS_PLUGIN_FILE ) );
190 $this->define( 'BETTERDOCS_PLUGIN_BASENAME', plugin_basename( BETTERDOCS_PLUGIN_FILE ) );
191 $this->define( 'BETTERDOCS_BLOCKS_DIRECTORY', BETTERDOCS_ABSPATH . 'assets/blocks/' );
192 $this->define( 'BETTERDOCS_ROOT_DIR_PATH', plugin_dir_path( BETTERDOCS_PLUGIN_FILE ) );
193 $this->define( 'BETTERDOCS_FSE_TEMPLATES_PATH', BETTERDOCS_ROOT_DIR_PATH . 'views/templates/fse' );
194
195 /**
196 * Third Party Constants
197 * @since 2.5.0
198 *
199 * WPML compatibility with Polylang
200 */
201 if ( Helper::is_plugin_active( 'polylang/polylang.php' ) ) {
202 // Polylang's documented integration constant — must use the upstream-defined name.
203 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound
204 define( 'PLL_WPML_COMPAT', false );
205 }
206 }
207
208 /**
209 * Define constant if not already set.
210 *
211 * @param string $name Constant name.
212 * @param string|bool $value Constant value.
213 */
214 private function define( $name, $value ) {
215 if ( ! defined( $name ) ) {
216 // Caller passes plugin-prefixed names (BETTERDOCS_*); $name comes from a controlled internal allowlist.
217 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.VariableConstantNameFound
218 define( $name, $value );
219 }
220 }
221
222 public function setup_container() {
223 $config_array = require_once BETTERDOCS_ABSPATH . 'includes/config.php';
224 $config = apply_filters( 'betterdocs_container_config', $config_array );
225 $builder = new ContainerBuilder();
226
227 $builder->addDefinitions( $config );
228 $this->container = $builder->build();
229 }
230
231 public function initialize() {
232
233 /**
234 * Setup localization.
235 */
236 $this->load_plugin_textdomain();
237
238 $this->container->get( Scripts::class );
239 $this->rewrite = $this->container->get( Rewrite::class );
240 $this->request = $this->container->get( Request::class );
241 $this->query = $this->container->get( Query::class );
242
243 // Initialize background process
244 $this->backgroundProccessor = $this->container->get( HelpScoutMigration::class );
245
246 $this->rewrite->init();
247 $this->request->init();
248
249 $this->assets = $this->container->get( Enqueue::class );
250 $this->views = $this->container->get( Views::class );
251 $this->helper = $this->container->get( Helper::class );
252 $this->kbmigration = $this->container->get( KBMigration::class );
253 $this->admin = $this->container->get( Admin::class );
254 $this->database = $this->container->get( Database::class );
255 $this->settings = $this->container->get( Settings::class );
256 $this->analytics = $this->container->get( Analytics::class );
257
258 $this->template_helper = $this->container->get( TemplateTags::class );
259 $this->customizer = $this->container->get( Customizer::class );
260 $this->editor = $this->container->get( Editor::class );
261 $this->ai_autowrtie = $this->container->get( WriteWithAI::class );
262 $this->article_summary = $this->container->get( ArticleSummary::class );
263
264 // Initialize unified metabox before individual features
265 $this->container->get( UnifiedMetabox::class );
266 $this->article_quality_score = $this->container->get( ArticleQualityScore::class );
267
268 $this->container->get( Admin::class );
269 $this->container->get( Roles::class );
270 $this->container->get( ReportEmail::class );
271 /**
272 * Initialize Shortcode
273 * Make sure you have listed out all shortcode in shortcode factory.
274 */
275 $this->container->get( ShortcodeFactory::class )->init();
276
277 $this->container->get( FrontEnd::class );
278 $this->container->get( SearchExtender::class );
279
280 do_action( 'betterdocs_init' );
281
282 $this->editor->init();
283 }
284
285 /**
286 * Load plugins textdomain `betterdocs` into actions.
287 * @return void
288 */
289 public function load_plugin_textdomain( $textdomain = 'betterdocs', $plugin_file = BETTERDOCS_PLUGIN_FILE ) {
290 $locale = determine_locale();
291
292 /**
293 * Filter to adjust the BetterDocs locale to use for translations.
294 */
295 // 'plugin_locale' is a WP-core filter; using its documented name is required.
296 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
297 $locale = apply_filters( 'plugin_locale', $locale, $textdomain );
298
299 if ( file_exists( WP_LANG_DIR . "/$textdomain-" . $locale . '.mo' ) ) {
300 unload_textdomain( $textdomain );
301 load_textdomain( $textdomain, WP_LANG_DIR . "/$textdomain-" . $locale . '.mo' );
302 }
303 }
304
305 /**
306 * For AJAX Only
307 * @return void
308 */
309 public function ajax() {
310 }
311
312 /**
313 * Create a plugin instance.
314 *
315 * @param mixed ...$args
316 *
317 * @return static
318 *
319 * @suppress PHP0441
320 * @since 2.5.0
321 */
322 public static function get_instance() {
323 if ( null == self::$_instance ) {
324 self::$_instance = new self();
325
326 do_action( 'betterdocs_loaded' );
327 }
328
329 return self::$_instance;
330 }
331
332 /**
333 * Hooked with `admin_init` action.
334 * @return void
335 */
336 public function admin_init() {
337 /**
338 * Maybe Redirect
339 * for setup related settings.
340 */
341 $this->maybe_redirect();
342 }
343
344 /**
345 * Summary of maybe_redirect
346 * @return void
347 */
348 public function maybe_redirect() {
349 // Bail if no activation transient is set.
350 if ( ! $this->database->get_transient( 'betterdocs_maybe_redirect' ) ) {
351 return;
352 }
353
354 // Delete the activation transient.
355 $this->database->delete_transient( 'betterdocs_maybe_redirect' );
356
357 if ( ! is_multisite() ) {
358 $betterdocs_settings = get_option( 'betterdocs_settings' );
359 if ( $betterdocs_settings ) {
360 wp_safe_redirect( add_query_arg( array( 'page' => 'betterdocs-settings' ), admin_url( 'admin.php' ) ) );
361 } else {
362 wp_safe_redirect( add_query_arg( array( 'page' => 'betterdocs-setup' ), admin_url( 'admin.php' ) ) );
363 }
364 }
365 }
366
367 /**
368 * Is Pro Plugin Is Installed?
369 * @return bool
370 */
371 public function is_pro_installed() {
372 return $this->helper->get_plugins( 'betterdocs-pro/betterdocs-pro.php' );
373 }
374
375 /**
376 * Is Pro Plugin Is Active?
377 * @return bool
378 */
379 public function pro_file() {
380 return WP_PLUGIN_DIR . '/betterdocs-pro/betterdocs-pro.php';
381 }
382
383 public function is_pro_active() {
384 if ( file_exists( $this->pro_file() ) ) {
385 return $this->helper->is_plugin_active( 'betterdocs-pro/betterdocs-pro.php' );
386 }
387
388 return false;
389 }
390
391 public function chatbot_file() {
392 return WP_PLUGIN_DIR . '/betterdocs-ai-chatbot/betterdocs-ai-chatbot.php';
393 }
394
395 public function is_chatbot_active() {
396 if ( file_exists( $this->chatbot_file() ) ) {
397 return $this->helper->is_plugin_active( 'betterdocs-ai-chatbot/betterdocs-ai-chatbot.php' );
398 }
399
400 return false;
401 }
402
403 public function pro_version() {
404 if ( ! $this->is_pro_active() ) {
405 return false;
406 }
407
408 if ( ! function_exists( 'get_plugin_data' ) ) {
409 require_once ABSPATH . 'wp-admin/includes/plugin.php';
410 }
411
412 $plugin_data = get_plugin_data( $this->pro_file() );
413
414 return $plugin_data[ 'Version' ];
415 }
416
417 /**
418 * Get all the API initialized.
419 * @return void
420 */
421 public function api_initialization() {
422 $_api_classes = scandir( __DIR__ . DIRECTORY_SEPARATOR . 'REST' );
423
424 if ( ! empty( $_api_classes ) && is_array( $_api_classes ) ) {
425 foreach ( $_api_classes as $class ) {
426 if ( '.' == $class || '..' == $class || strpos( $class, '.' ) === 0 ) {
427 continue;
428 }
429
430 $classname = basename( $class, '.php' );
431 $classname = '\\' . __NAMESPACE__ . "\\REST\\$classname";
432 $_api_class = $this->container->get( $classname );
433
434 if ( $_api_class instanceof BaseAPI ) {
435 $_api_class->register();
436 }
437 }
438 }
439 }
440
441 public function is_betterdocs_screen( $hook, $admin_check = true ): bool {
442 $screens = array(
443 'toplevel_page_betterdocs-dashboard',
444 'toplevel_page_betterdocs-admin',
445 'admin_page_betterdocs-admin',
446 'betterdocs_page_betterdocs-admin',
447 'betterdocs_page_betterdocs-analytics',
448 'betterdocs_page_betterdocs-settings',
449 'betterdocs_page_betterdocs-faq',
450 'betterdocs_page_betterdocs-glossaries',
451 'betterdocs_page_betterdocs-ai-chatbot'
452 );
453
454 if ( $admin_check ) {
455 if ( in_array( $hook, $screens ) ) {
456 return true;
457 }
458
459 return false;
460 }
461
462 return false;
463 }
464
465 public function get_betterdocs_screen() {
466 $registered_screens = array(
467 'toplevel_page_betterdocs-dashboard',
468 'admin_page_betterdocs-admin',
469 'betterdocs_page_betterdocs-admin',
470 'betterdocs_page_betterdocs-settings',
471 'betterdocs_page_betterdocs-analytics',
472 'betterdocs_page_betterdocs-faq',
473 'betterdocs_page_betterdocs-glossaries',
474 'betterdocs_page_betterdocs-ai-chatbot',
475 'edit-docs'
476 );
477
478 $current_screen_id = get_current_screen() != null ? get_current_screen()->id : '';
479
480 if ( in_array( $current_screen_id, $registered_screens ) ) {
481 return $current_screen_id;
482 }
483
484 return false;
485 }
486 }
487