- This plugin will create a laravel application
app()(a DI container https://code.tutsplus.com/tutorials/digging-in-to-laravels-ioc-container--cms-22167) contains everything we need. - Each plugin or theme will act as a Service Provider https://laravel.com/docs/7.x/providers
- All Laravel Base Providers are loaded in WP_Application (similar to Applications)
- Main Service Providers (Configured Service Providers) should be loaded when Kernel do the bootstrapping all bootstrappers
- We don't load the configuration from the file so we skip the LoadConfiguration bootstrapper, and LoadEnvironmentVariables should be skipped as well as we don't need WP_Application to load environment variables from .env files
- On each Service Provider, we should put the configs for that Service Provider to the 'config' instance of the application to avoid a big array. Each should come with a filter to allow other plugins to tweak the configs.
- WP Application bootstrapping happens at the action
after_setup_theme, to have the themefunctions.phploaded as well. The theme functions.php is loaded right before the actionafter_setup_themehappens. app()(helper function for gettingWP_Application::$instance) needs to skip the WordPress template render and skip the main query when the URL is working on wp_app mode (domain.com/wp-app)- app() should skip the WordPress rest api as well on wp_api mode (domain.com/wp-api)
- Yivic Base is loaded as a MU plugin (this should be the choice), normal plugin or a dependency of plugins or themes.
- At the first time the site receive the web request, the plugin needs to:
- Perform the folder prepare (the Yivic Base plugin needs the Laravel folder structure)
- Redirect the request to the general setup page (with certain condition because the setup phase needs special permission).
- If the setup cannot be done on general setup page, it would redirect the request to the Admin setup page (which requires the Admin access and provide better error messages)
- If the Admin have the WP CLI access, we need to perform the following command:
wp yivic-base prepare # to setup the correct folder structure wp yivic-base wp-app:setup # to run the migrations, copy needed assets - When Yivic Base plugin loaded, the WP_Application instance would be initialized and the Yivic_Base_WP_Plugin would be initialized next to work as the service provider for WP_Application. At Yivic_Base_WP_Plugin, we created several hooks for WP App based on the WP Hooks:
- The const
YIVIC_BASE_SETUP_HOOK_NAMEdefines the moment when we set up the WP App. yivic_base_wp_app_loadedis the event when the WP App is loaded, we should use this event to init WP Plugins, WP Themes.- The
manipulate_hooksmethod of the WP_Plugin, WP_Theme would happen at this stage so hooks registered here can interfere yivic_base_wp_app_registeredis the action happens when the WP App and all service providers registered. We use this event to register WP Plugins, WP Themes to the WP Applicationyivic_base_wp_app_bootedis the action happens when the WP App and all service providers are booted
- The const
- Here are stages of the WP App via a request:
yivic_base_wp_app_bootstrapis the action for the first event for putting thesapp()to the business (happens atafter_theme_setup). At this stage, we add a handler to bootstrap theapp(). We init the Kernel services (for Console and Http) and the Error Handler service and bind them to the Service Container (WP_Application::$instance).yivic_base_wp_app_initis the equivalent instance of the WPinitaction. We use another name to know that, this is forapp(). At this stage, theapp()should have all Service Providers registered and booted and of course, all available things from the actioninitof WordPress as well.yivic_base_wp_app_complete_executionis the action happens to complete the request handing. (We usually terminate theapp()here)
Yivic Base plugin will split WordPress into 3 modes:
- Normal WordPress workflow
- WP App mode: use Laravel to handle the request and response with Laravel
- WP Api mode: same with WP App but for API only, no HTML rendering
- All behaviors of WordPress must be kept
- At
yivic_base_wp_app_initaction, we need to do the following: - Because we don't let the Laravel kernel to handle the request and use the Laravel response as the main response, but we need to have the
requestandresponseinstance for some reason (especially for start the session via the middlewareStartSesssion). We capture the current request and let it go through all needed middleware to have several Laravel features ready. - We need to synchronize the WP logged user to Laravel session (
Auth::user()should have data). - We interfere the
template_includefilter skip the usage of WPlocate_templateto use Blade template to compile and render the HTML. Therefore, we can use Blade template syntax on the WP template file. - At
yivic_base_wp_app_complete_executionaction, we need to call the kernel'sterminate()method for theapp()to have needed events to trigger several actions (e.g. logging or Telescope logging, Telescope send the logging to database via the terminate event)
app()should be registered, booted and bootstrapped at the hookyivic_base_wp_app_init- We specify the WP App mode by the request uri prefix e.g.
<domain>/wp-app/abc. - At the action
wp_loadedwe skip the WP handling for the request and started to use the Laravel Http kernel to handle the request and send Laravel headers and response. As we use thewp_loadedto switch to WP App mode (https://wp-kama.com/hooks/actions-order), so no WP loop working and the WP main query would not work either. The reason we usewp_loadedis to wait for WP widgets, navigations - The action
shutdownwould be invoked as WP register the callback to execute that hook viaregister_shutdown_functionso we don't need to explicitly call that action - We use this action
App_Const::ACTION_WP_APP_REGISTER_ROUTESto register WP App mode routes. All WP App mode routes are prefixed withwp-app::.
- Same workflow of WP App
- Instead of using
wp_loadedaction, we use the very late handler on the actioninit(for other plugins to complete execution asinitis the main action plugins use to start) and we skip several middleware as we don't want to use Cookie, Session as we want it stateless (we should keepSubstituteBindingsmiddleware) - We use this action
App_Const::ACTION_WP_API_REGISTER_ROUTESto register WP Api mode routes. All WP Api mode routes are prefixed withwp-api::.