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 / Plugin.php

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

368 lines 9.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;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly
7 }
8
9 use WPDeveloper\BetterDocs\Core\Admin;
10 use WPDeveloper\BetterDocs\Core\Query;
11 use WPDeveloper\BetterDocs\Core\Roles;
12 use WPDeveloper\BetterDocs\Utils\Views;
13 use WPDeveloper\BetterDocs\Core\BaseAPI;
14 use WPDeveloper\BetterDocs\Core\Install;
15 use WPDeveloper\BetterDocs\Core\Request;
16 use WPDeveloper\BetterDocs\Core\Rewrite;
17 use WPDeveloper\BetterDocs\Core\Scripts;
18 use WPDeveloper\BetterDocs\Utils\Helper;
19 use WPDeveloper\BetterDocs\Core\Settings;
20 use WPDeveloper\BetterDocs\Utils\Enqueue;
21 use WPDeveloper\BetterDocs\Editors\Editor;
22 use WPDeveloper\BetterDocs\Utils\Database;
23 use WPDeveloper\BetterDocs\Admin\Analytics;
24 use WPDeveloper\BetterDocs\Admin\ReportEmail;
25 use WPDeveloper\BetterDocs\FrontEnd\FrontEnd;
26 use WPDeveloper\BetterDocs\Core\ShortcodeFactory;
27 use WPDeveloper\BetterDocs\FrontEnd\TemplateTags;
28 use WPDeveloper\BetterDocs\Admin\Customizer\Customizer;
29 use WPDeveloper\BetterDocs\Dependencies\DI\ContainerBuilder;
30
31 final class Plugin {
32 private static $_instance = null;
33
34 /**
35 * Create a plugin instance.
36 *
37 * @since 2.5.0
38 * @param mixed ...$args
39 *
40 * @return static
41 *
42 * @suppress PHP0441
43 */
44 public static function get_instance() {
45 if ( static::$_instance == null ) {
46 static::$_instance = new self();
47
48 do_action( 'betterdocs_loaded' );
49 }
50
51 return static::$_instance;
52 }
53
54 /**
55 * Assets manager
56 *
57 * @var Enqueue
58 */
59 public $assets;
60
61 /**
62 * View manager
63 *
64 * @var Views
65 */
66 public $views;
67
68 /**
69 * Container Manager
70 *
71 * @var \WPDeveloper\BetterDocs\Dependencies\DI\Container
72 */
73 public $container;
74
75 /**
76 * Editor Manager
77 * @var Editor
78 */
79 public $editor;
80
81 /**
82 * Helper class
83 * @var Helper
84 */
85 public $helper;
86
87 /**
88 * Helper class
89 * @var Database
90 */
91 public $database;
92
93 /**
94 * Helper class
95 * @var Settings
96 */
97 public $settings;
98
99 /**
100 * Helper class
101 * @var TemplateTags
102 */
103 public $template_helper;
104
105 /**
106 * Customizer class
107 * @var Customizer
108 */
109 public $customizer;
110
111 /**
112 * Query class
113 * @var Query
114 */
115 public $query;
116
117 /**
118 * Rewrite Class
119 * @var Rewrite
120 */
121 public $rewrite;
122
123 /**
124 * Request Class
125 * @var Request
126 */
127 public $request;
128
129 /**
130 * Analytics Class
131 * @var Analytics
132 */
133 public $analytics;
134
135 /**
136 * Plugin Version
137 * @var string
138 */
139 public $version = '2.5.2';
140
141 /**
142 * Plugin DB Version
143 * @var string
144 */
145 public $db_version = '1.0.0';
146
147 public function __construct() {
148 $this->define_constants();
149
150 do_action( 'betterdocs_init_before' );
151 $this->setup_container();
152 /**
153 * Register activation and deactivation hooks
154 * and version updates check
155 */
156 $this->container->get( Install::class );
157
158 $this->initialize();
159
160 add_action( 'init', [$this, 'init'], 0 );
161
162 /**
163 * For admin only
164 */
165 add_action( 'admin_init', [$this, 'admin_init'], 0 );
166
167 /**
168 * For AJAX only
169 */
170 $this->ajax();
171 }
172
173 private function define_constants() {
174 $this->define( 'BETTERDOCS_VERSION', $this->version );
175 $this->define( 'BETTERDOCS_DB_VERSION', $this->db_version );
176 $this->define( 'BETTERDOCS_ABSPATH', dirname( BETTERDOCS_PLUGIN_FILE ) . '/' );
177 $this->define( 'BETTERDOCS_ABSURL', plugin_dir_url( BETTERDOCS_PLUGIN_FILE ) );
178 $this->define( 'BETTERDOCS_PLUGIN_BASENAME', plugin_basename( BETTERDOCS_PLUGIN_FILE ) );
179 $this->define( 'BETTERDOCS_BLOCKS_DIRECTORY', BETTERDOCS_ABSPATH . 'assets/blocks/' );
180 $this->define( 'BETTERDOCS_ROOT_DIR_PATH', plugin_dir_path( BETTERDOCS_PLUGIN_FILE ) );
181 $this->define( 'BETTERDOCS_FSE_TEMPLATES_PATH', BETTERDOCS_ROOT_DIR_PATH . 'views/templates/fse' );
182
183 /**
184 * Third Party Constants
185 * @since 2.5.0
186 *
187 * WPML compatibility with Polylang
188 */
189 if ( Helper::is_plugin_active( 'polylang/polylang.php' ) ) {
190 define( 'PLL_WPML_COMPAT', false );
191 }
192 }
193
194 /**
195 * Define constant if not already set.
196 *
197 * @param string $name Constant name.
198 * @param string|bool $value Constant value.
199 */
200 private function define( $name, $value ) {
201 if ( ! defined( $name ) ) {
202 define( $name, $value );
203 }
204 }
205
206 public function setup_container() {
207 $config_array = require_once BETTERDOCS_ABSPATH . 'includes/config.php';
208 $config = apply_filters( 'betterdocs_container_config', $config_array );
209 $builder = new ContainerBuilder();
210
211 $builder->addDefinitions( $config );
212 $this->container = $builder->build();
213 }
214
215 public function initialize() {
216 $this->container->get( Scripts::class );
217 $this->rewrite = $this->container->get( Rewrite::class );
218 $this->request = $this->container->get( Request::class );
219 $this->query = $this->container->get( Query::class );
220
221 $this->rewrite->init();
222 $this->request->init();
223
224 $this->assets = $this->container->get( Enqueue::class );
225 $this->views = $this->container->get( Views::class );
226 $this->helper = $this->container->get( Helper::class );
227 $this->database = $this->container->get( Database::class );
228 $this->settings = $this->container->get( Settings::class );
229 $this->analytics = $this->container->get( Analytics::class );
230
231 $this->template_helper = $this->container->get( TemplateTags::class );
232 $this->customizer = $this->container->get( Customizer::class );
233 $this->editor = $this->container->get( Editor::class );
234
235 /**
236 * Initialize all editors.
237 * Elementor, Gutenberg/BlockEditor
238 */
239 add_action( 'init', [$this->editor, 'init'] );
240
241 /**
242 * Initialize API
243 */
244 add_action( 'rest_api_init', [$this, 'api_initialization'] );
245 }
246
247 /**
248 * Initialize the BetterDocs Plugin
249 *
250 * @since 2.5.0
251 * @return void
252 */
253 public function init() {
254 /**
255 * Setup localization.
256 */
257 $this->load_plugin_textdomain();
258
259 $this->container->get( Admin::class );
260 $this->container->get( Roles::class );
261 $this->container->get( ReportEmail::class );
262
263 /**
264 * Initialize Shortcode
265 * Make sure you have listed out all shortcode in shortcode factory.
266 */
267 $this->container->get( ShortcodeFactory::class )->init();
268
269 $this->container->get( FrontEnd::class );
270
271 do_action( 'betterdocs_init' );
272 }
273
274 /**
275 * Hooked with `admin_init` action.
276 * @return void
277 */
278 public function admin_init() {
279 /**
280 * Maybe Redirect
281 * for setup related settings.
282 */
283 $this->maybe_redirect();
284 }
285
286 /**
287 * For AJAX Only
288 * @return void
289 */
290 public function ajax() {
291 }
292
293 /**
294 * Load plugins textdomain `betterdocs` into actions.
295 * @return void
296 */
297 public function load_plugin_textdomain( $textdomain = 'betterdocs', $plugin_file = BETTERDOCS_PLUGIN_FILE ) {
298 $locale = determine_locale();
299
300 /**
301 * Filter to adjust the BetterDocs locale to use for translations.
302 */
303 $locale = apply_filters( 'plugin_locale', $locale, $textdomain );
304
305 unload_textdomain( $textdomain );
306 load_textdomain( $textdomain, WP_LANG_DIR . "/$textdomain-" . $locale . '.mo' );
307 load_plugin_textdomain( $textdomain, false, plugin_basename( dirname( $plugin_file ) ) . '/languages' );
308 }
309
310 /**
311 * Is Pro Plugin Is Installed?
312 * @return bool
313 */
314 public function is_pro_installed() {
315 return $this->helper->get_plugins( 'betterdocs-pro/betterdocs-pro.php' );
316 }
317
318 /**
319 * Is Pro Plugin Is Active?
320 * @return bool
321 */
322 public function is_pro_active() {
323 return $this->helper->is_plugin_active( 'betterdocs-pro/betterdocs-pro.php' );
324 }
325
326 /**
327 * Summary of maybe_redirect
328 * @return void
329 */
330 public function maybe_redirect() {
331 // Bail if no activation transient is set.
332 if ( ! $this->database->get_transient( 'betterdocs_maybe_redirect' ) ) {
333 return;
334 }
335
336 // Delete the activation transient.
337 $this->database->delete_transient( 'betterdocs_maybe_redirect' );
338
339 if ( ! is_multisite() ) {
340 wp_safe_redirect( add_query_arg( ['page' => 'betterdocs-setup'], admin_url( 'admin.php' ) ) );
341 }
342 }
343
344 /**
345 * Get all the API initialized.
346 * @return void
347 */
348 public function api_initialization() {
349 $_api_classes = scandir( __DIR__ . DIRECTORY_SEPARATOR . 'REST' );
350
351 if ( ! empty( $_api_classes ) && is_array( $_api_classes ) ) {
352 foreach ( $_api_classes as $class ) {
353 if ( $class == '.' || $class == '..' ) {
354 continue;
355 }
356
357 $classname = basename( $class, '.php' );
358 $classname = '\\' . __NAMESPACE__ . "\\REST\\$classname";
359 $_api_class = $this->container->get( $classname );
360
361 if ( $_api_class instanceof BaseAPI ) {
362 $_api_class->register();
363 }
364 }
365 }
366 }
367 }
368