| @@ -10,8 +10,9 @@ | ||
| 10 | 10 | use WPDeveloper\BetterDocs\Admin\Customizer\Customizer; |
| 11 | 11 | use WPDeveloper\BetterDocs\Admin\HelpScoutMigration; |
| 12 | 12 | use WPDeveloper\BetterDocs\Admin\ReportEmail; |
| 13 | 13 | use WPDeveloper\BetterDocs\Core\Admin; |
| 14 | +use WPDeveloper\BetterDocs\Core\AIActions; | |
| 14 | 15 | use WPDeveloper\BetterDocs\Core\AnalyticsTracker; |
| 15 | 16 | use WPDeveloper\BetterDocs\Core\AnalyticsRetention; |
| 16 | 17 | use WPDeveloper\BetterDocs\Core\BaseAPI; |
| 17 | 18 | use WPDeveloper\BetterDocs\Core\Install; |
| @@ -27,8 +28,11 @@ | ||
| 27 | 28 | use WPDeveloper\BetterDocs\Core\ArticleSummary; |
| 28 | 29 | use WPDeveloper\BetterDocs\Core\ArticleQualityScore; |
| 29 | 30 | use WPDeveloper\BetterDocs\Core\UnifiedMetabox; |
| 30 | 31 | use WPDeveloper\BetterDocs\Core\DocsAISuite; |
| 32 | +use WPDeveloper\BetterDocs\Core\Listen; | |
| 33 | +use WPDeveloper\BetterDocs\Core\MarkdownEndpoint; | |
| 34 | +use WPDeveloper\BetterDocs\Core\MarkdownRenderer; | |
| 31 | 35 | use WPDeveloper\BetterDocs\Dependencies\DI\Container; |
| 32 | 36 | use WPDeveloper\BetterDocs\Dependencies\DI\ContainerBuilder; |
| 33 | 37 | use WPDeveloper\BetterDocs\Editors\Editor; |
| 34 | 38 | use WPDeveloper\BetterDocs\FrontEnd\FrontEnd; |
| @@ -129,12 +133,32 @@ | ||
| 129 | 133 | * @var Analytics |
| 130 | 134 | */ |
| 131 | 135 | public $analytics; |
| 132 | 136 | /** |
| 137 | + * Markdown renderer (HTML -> Markdown for docs) | |
| 138 | + * @var MarkdownRenderer | |
| 139 | + */ | |
| 140 | + public $markdown; | |
| 141 | + /** | |
| 142 | + * `<doc-url>.md` endpoint | |
| 143 | + * @var MarkdownEndpoint | |
| 144 | + */ | |
| 145 | + public $markdown_endpoint; | |
| 146 | + /** | |
| 147 | + * AI Actions registry ("Copy page" split button) | |
| 148 | + * @var AIActions | |
| 149 | + */ | |
| 150 | + public $ai_actions; | |
| 151 | + /** | |
| 152 | + * Listen ("text to audio" player in the doc meta row) | |
| 153 | + * @var Listen | |
| 154 | + */ | |
| 155 | + public $listen; | |
| 156 | + /** | |
| 133 | 157 | * Plugin Version |
| 134 | 158 | * @var string |
| 135 | 159 | */ |
| 136 | - public $version = '4.9.3'; | |
| 160 | + public $version = '4.9.4'; | |
| 137 | 161 | |
| 138 | 162 | /** |
| 139 | 163 | * WriteWithAI Class |
| 140 | 164 | * @var string |
| @@ -153,15 +177,35 @@ | ||
| 153 | 177 | * @var string |
| 154 | 178 | */ |
| 155 | 179 | public $db_version = '1.0.3'; |
| 156 | 180 | |
| 181 | + /** | |
| 182 | + * Listeners attached to each load-time hook when it fired, keyed by hook. | |
| 183 | + * @var array | |
| 184 | + */ | |
| 185 | + private $fired_hook_callbacks = []; | |
| 186 | + | |
| 157 | 187 | public function __construct() { |
| 158 | 188 | $this->define_constants(); |
| 159 | 189 | |
| 160 | 190 | do_action( 'betterdocs_init_before' ); |
| 191 | + $this->fired_hook_callbacks['betterdocs_init_before'] = $this->get_hook_callbacks( 'betterdocs_init_before' ); | |
| 161 | 192 | |
| 162 | 193 | $this->setup_container(); |
| 194 | + | |
| 163 | 195 | /** |
| 196 | + * Add-ons (Pro, AI Chatbot) hook `betterdocs_init_before` to register | |
| 197 | + * their container definitions and `betterdocs_loaded` to boot, and both | |
| 198 | + * fire while this file is loading. When the site's plugin load order puts | |
| 199 | + * an add-on after Free (e.g. after a migration rewrote `active_plugins`), | |
| 200 | + * the add-on hooks in too late — Pro then fatals resolving its own | |
| 201 | + * Utils\Enqueue. Catch those late listeners up once every plugin has | |
| 202 | + * loaded, before anything is resolved on `init`. | |
| 203 | + */ | |
| 204 | + if ( ! did_action( 'plugins_loaded' ) ) { | |
| 205 | + add_action( 'plugins_loaded', [ $this, 'run_late_addon_listeners' ], PHP_INT_MIN ); | |
| 206 | + } | |
| 207 | + /** | |
| 164 | 208 | * Register activation and deactivation hooks |
| 165 | 209 | * and version updates check |
| 166 | 210 | */ |
| 167 | 211 | $this->container->get( Install::class ); |
| @@ -199,8 +243,13 @@ | ||
| 199 | 243 | /** |
| 200 | 244 | * Style Handler For Parsing and Saving Styles as file. |
| 201 | 245 | */ |
| 202 | 246 | ModulesStyleHandler::init(); |
| 247 | + | |
| 248 | + /** | |
| 249 | + * Serve converted GIFs in docs as video. | |
| 250 | + */ | |
| 251 | + \WPDeveloper\BetterDocs\Modules\GifVideo::init(); | |
| 203 | 252 | } |
| 204 | 253 | |
| 205 | 254 | private function define_constants() { |
| 206 | 255 | $this->define( 'BETTERDOCS_VERSION', $this->version ); |
| @@ -249,8 +298,84 @@ | ||
| 249 | 298 | $builder->addDefinitions( $config ); |
| 250 | 299 | $this->container = $builder->build(); |
| 251 | 300 | } |
| 252 | 301 | |
| 302 | + /** | |
| 303 | + * Run the `betterdocs_init_before` and `betterdocs_loaded` listeners that | |
| 304 | + * missed those hooks because their plugin loaded after Free, and add the | |
| 305 | + * definitions they contribute through `betterdocs_container_config` to the | |
| 306 | + * already-built container. | |
| 307 | + * | |
| 308 | + * Only listeners attached after the original fire are run, and only the | |
| 309 | + * config filters they add are applied, so plugins that loaded before Free | |
| 310 | + * are not processed twice. Container::set() also drops any entry already | |
| 311 | + * resolved under the same id, so the add-on's override wins. | |
| 312 | + * | |
| 313 | + * @since 4.9.4 | |
| 314 | + * @return void | |
| 315 | + */ | |
| 316 | + public function run_late_addon_listeners() { | |
| 317 | + $config_filters = $this->get_hook_callbacks( 'betterdocs_container_config' ); | |
| 318 | + | |
| 319 | + if ( $this->run_late_listeners( 'betterdocs_init_before' ) ) { | |
| 320 | + $late_filters = array_diff_key( $this->get_hook_callbacks( 'betterdocs_container_config' ), $config_filters ); | |
| 321 | + | |
| 322 | + $config = []; | |
| 323 | + foreach ( $late_filters as $filter ) { | |
| 324 | + $config = call_user_func( $filter['function'], $config ); | |
| 325 | + } | |
| 326 | + | |
| 327 | + if ( is_array( $config ) ) { | |
| 328 | + foreach ( $config as $id => $definition ) { | |
| 329 | + $this->container->set( $id, $definition ); | |
| 330 | + } | |
| 331 | + } | |
| 332 | + } | |
| 333 | + | |
| 334 | + $this->run_late_listeners( 'betterdocs_loaded' ); | |
| 335 | + } | |
| 336 | + | |
| 337 | + /** | |
| 338 | + * Call the listeners attached to an already-fired hook after it fired. | |
| 339 | + * | |
| 340 | + * @param string $hook Hook name. | |
| 341 | + * @return bool Whether any listener ran. | |
| 342 | + */ | |
| 343 | + private function run_late_listeners( $hook ) { | |
| 344 | + $fired = isset( $this->fired_hook_callbacks[ $hook ] ) ? $this->fired_hook_callbacks[ $hook ] : []; | |
| 345 | + $listeners = array_diff_key( $this->get_hook_callbacks( $hook ), $fired ); | |
| 346 | + | |
| 347 | + foreach ( $listeners as $listener ) { | |
| 348 | + // do_action() with no arguments passes a single empty string. | |
| 349 | + call_user_func_array( $listener['function'], array_slice( [ '' ], 0, (int) $listener['accepted_args'] ) ); | |
| 350 | + } | |
| 351 | + | |
| 352 | + return ! empty( $listeners ); | |
| 353 | + } | |
| 354 | + | |
| 355 | + /** | |
| 356 | + * Callbacks attached to a hook, in run order, keyed by priority and id. | |
| 357 | + * | |
| 358 | + * @param string $hook Hook name. | |
| 359 | + * @return array | |
| 360 | + */ | |
| 361 | + private function get_hook_callbacks( $hook ) { | |
| 362 | + global $wp_filter; | |
| 363 | + | |
| 364 | + $callbacks = []; | |
| 365 | + if ( empty( $wp_filter[ $hook ] ) || ! $wp_filter[ $hook ] instanceof \WP_Hook ) { | |
| 366 | + return $callbacks; | |
| 367 | + } | |
| 368 | + | |
| 369 | + foreach ( $wp_filter[ $hook ]->callbacks as $priority => $items ) { | |
| 370 | + foreach ( $items as $id => $callback ) { | |
| 371 | + $callbacks[ $priority . '|' . $id ] = $callback; | |
| 372 | + } | |
| 373 | + } | |
| 374 | + | |
| 375 | + return $callbacks; | |
| 376 | + } | |
| 377 | + | |
| 253 | 378 | public function initialize() { |
| 254 | 379 | |
| 255 | 380 | /** |
| 256 | 381 | * Setup localization. |
| @@ -275,8 +400,19 @@ | ||
| 275 | 400 | $this->admin = $this->container->get( Admin::class ); |
| 276 | 401 | $this->database = $this->container->get( Database::class ); |
| 277 | 402 | $this->settings = $this->container->get( Settings::class ); |
| 278 | 403 | $this->analytics = $this->container->get( Analytics::class ); |
| 404 | + | |
| 405 | + // AI Actions and the Markdown endpoint. MarkdownEndpoint's constructor | |
| 406 | + // strips a trailing `.md` from REQUEST_URI, so it has to be built before | |
| 407 | + // WP::parse_request() — which this is, since initialize() IS the `init` | |
| 408 | + // priority-0 callback. It also has to be built AFTER $this->settings above, | |
| 409 | + // because that strip consults the setting. | |
| 410 | + $this->markdown = $this->container->get( MarkdownRenderer::class ); | |
| 411 | + $this->markdown_endpoint = $this->container->get( MarkdownEndpoint::class ); | |
| 412 | + $this->ai_actions = $this->container->get( AIActions::class ); | |
| 413 | + $this->listen = $this->container->get( Listen::class ); | |
| 414 | + | |
| 279 | 415 | // Free analytics collectors: the frontend view/scroll tracker and the |
| 280 | 416 | // daily retention purge. Each self-registers its hooks on construct. |
| 281 | 417 | $this->container->get( AnalyticsTracker::class ); |
| 282 | 418 | $this->container->get( AnalyticsRetention::class ); |
| @@ -377,8 +513,9 @@ | ||
| 377 | 513 | if ( null == self::$_instance ) { |
| 378 | 514 | self::$_instance = new self(); |
| 379 | 515 | |
| 380 | 516 | do_action( 'betterdocs_loaded' ); |
| 517 | + self::$_instance->fired_hook_callbacks['betterdocs_loaded'] = self::$_instance->get_hook_callbacks( 'betterdocs_loaded' ); | |
| 381 | 518 | } |
| 382 | 519 | |
| 383 | 520 | return self::$_instance; |
| 384 | 521 | } |