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