Skip to content

Commit

Permalink
Re-organize how event handlers are loaded to allow for compatibility …
Browse files Browse the repository at this point in the history
…with Fallback Site plugin

Signed-off-by: Andrew Welch <[email protected]>
  • Loading branch information
khalwat committed Jul 10, 2018
1 parent 064de80 commit e90a88b
Showing 1 changed file with 116 additions and 88 deletions.
204 changes: 116 additions & 88 deletions src/Seomatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,55 +215,8 @@ public function init()
$this->name = Seomatic::$settings->pluginName;
// Determine whether Craft Commerce exists
self::$commerceInstalled = class_exists(CommercePlugin::class);
// Install our event listeners only if our table schema exists
if ($this->tableSchemaExists()) {
$this->installEventListeners();
}
// Handler: EVENT_AFTER_INSTALL_PLUGIN
Event::on(
Plugins::class,
Plugins::EVENT_AFTER_INSTALL_PLUGIN,
function (PluginEvent $event) {
if ($event->plugin === $this) {
// Invalidate our caches after we've been installed
$this->clearAllCaches();
// Send them to our welcome screen
$request = Craft::$app->getRequest();
if ($request->isCpRequest) {
Craft::$app->getResponse()->redirect(UrlHelper::cpUrl(
'seomatic/dashboard',
[
'showWelcome' => true,
]
))->send();
}
}
}
);
// Handler: EVENT_BEFORE_SAVE_PLUGIN_SETTINGS
Event::on(
Plugins::class,
Plugins::EVENT_BEFORE_SAVE_PLUGIN_SETTINGS,
function (PluginEvent $event) {
if ($event->plugin === $this) {
// For all the emojis
if (!Craft::$app->getDb()->getSupportsMb4()) {
$settingsModel = $this->getSettings();
Seomatic::$savingSettings = true;
if ($settingsModel !== null) {
$attributes = $settingsModel->attributes();
if ($attributes !== null) {
foreach ($attributes as $attribute) {
if (\is_string($settingsModel->$attribute)) {
$settingsModel->$attribute = StringHelper::encodeMb4($settingsModel->$attribute);
}
}
}
}
}
}
}
);
// Install our event listeners
$this->installEventListeners();
// We're loaded
Craft::info(
Craft::t(
Expand Down Expand Up @@ -391,48 +344,104 @@ protected function tableSchemaExists(): bool
}

/**
* Install our event listeners. We do it only after we receive the event
* EVENT_AFTER_LOAD_PLUGINS so that any pending db migrations can be run
* before our event listeners kick in
* Install our event listeners.
*/
protected function installEventListeners()
{
// Add in our Twig extensions
Seomatic::$view->registerTwigExtension(new SeomaticTwigExtension);
// Handler: Fields::EVENT_REGISTER_FIELD_TYPES
// Install our event listeners only if our table schema exists
if ($this->tableSchemaExists()) {
// Add in our Twig extensions
Seomatic::$view->registerTwigExtension(new SeomaticTwigExtension);
$request = Craft::$app->getRequest();
// Add in our event listeners that are needed for every request
$this->installGlobalEventListeners();
// Install only for non-console site requests
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
$this->installSiteEventListeners();
}
// Install only for non-console AdminCP requests
if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) {
$this->installCpEventListeners();
}
}
// Handler: EVENT_AFTER_INSTALL_PLUGIN
Event::on(
Fields::class,
Fields::EVENT_REGISTER_FIELD_TYPES,
function (RegisterComponentTypesEvent $event) {
$event->types[] = SeoSettingsField::class;
$event->types[] = Seomatic_MetaField::class;
Plugins::class,
Plugins::EVENT_AFTER_INSTALL_PLUGIN,
function (PluginEvent $event) {
if ($event->plugin === $this) {
// Invalidate our caches after we've been installed
$this->clearAllCaches();
// Send them to our welcome screen
$request = Craft::$app->getRequest();
if ($request->isCpRequest) {
Craft::$app->getResponse()->redirect(UrlHelper::cpUrl(
'seomatic/dashboard',
[
'showWelcome' => true,
]
))->send();
}
}
}
);
// Handler: EVENT_BEFORE_SAVE_PLUGIN_SETTINGS
Event::on(
Plugins::class,
Plugins::EVENT_BEFORE_SAVE_PLUGIN_SETTINGS,
function (PluginEvent $event) {
if ($event->plugin === $this) {
// For all the emojis
if (!Craft::$app->getDb()->getSupportsMb4()) {
$settingsModel = $this->getSettings();
Seomatic::$savingSettings = true;
if ($settingsModel !== null) {
$attributes = $settingsModel->attributes();
if ($attributes !== null) {
foreach ($attributes as $attribute) {
if (\is_string($settingsModel->$attribute)) {
$settingsModel->$attribute = StringHelper::encodeMb4($settingsModel->$attribute);
}
}
}
}
}
}
}
);
}

/**
* Install global event listeners for all request types
*/
protected function installGlobalEventListeners()
{
// Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS
Event::on(
Plugins::class,
Plugins::EVENT_AFTER_LOAD_PLUGINS,
function () {
// Add in our event listeners that are needed for every request
$this->installGlobalEventListeners();
// Only respond to non-console site requests
// Install these only after all other plugins have loaded
$request = Craft::$app->getRequest();
// Only respond to non-console site requests
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
$this->handleSiteRequest();
}
// AdminCP magic
// Respond to AdminCP requests
if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) {
$this->handleAdminCpRequest();
}
}
);
}

/**
* Install global event listeners
*/
protected function installGlobalEventListeners()
{
// Handler: Fields::EVENT_REGISTER_FIELD_TYPES
Event::on(
Fields::class,
Fields::EVENT_REGISTER_FIELD_TYPES,
function (RegisterComponentTypesEvent $event) {
$event->types[] = SeoSettingsField::class;
$event->types[] = Seomatic_MetaField::class;
}
);
// Handler: TemplateCaches::EVENT_AFTER_DELETE_CACHES
Event::on(
TemplateCaches::class,
Expand Down Expand Up @@ -573,14 +582,45 @@ function (ElementEvent $event) {
}

/**
* Handle site requests
* Install site event listeners for site requests only
*/
protected function handleSiteRequest()
protected function installSiteEventListeners()
{
// Load the sitemap containers
Seomatic::$plugin->sitemaps->loadSitemapContainers();
// Load the frontend template containers
Seomatic::$plugin->frontendTemplates->loadFrontendTemplateContainers();
// Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES
Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_SITE_URL_RULES,
function (RegisterUrlRulesEvent $event) {
Craft::debug(
'UrlManager::EVENT_REGISTER_SITE_URL_RULES',
__METHOD__
);
$path = 'seomatic/seo-file-link/<url:[^\/]+>/<robots:[^\/]+>/<canonical:[^\/]+>/<inline:\d+>/<fileName:[-\w\.*]+>';
$route = Seomatic::$plugin->handle.'/file/seo-file-link';
$event->rules[$path] = ['route' => $route];
}
);
}

/**
* Install site event listeners for AdminCP requests only
*/
protected function installCpEventListeners()
{

}

/**
* Handle site requests. We do it only after we receive the event
* EVENT_AFTER_LOAD_PLUGINS so that any pending db migrations can be run
* before our event listeners kick in
*/
protected function handleSiteRequest()
{
// Handler: ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION
Event::on(
ErrorHandler::class,
Expand Down Expand Up @@ -647,24 +687,12 @@ function () {
}
}
);
// Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES
Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_SITE_URL_RULES,
function (RegisterUrlRulesEvent $event) {
Craft::debug(
'UrlManager::EVENT_REGISTER_SITE_URL_RULES',
__METHOD__
);
$path = 'seomatic/seo-file-link/<url:[^\/]+>/<robots:[^\/]+>/<canonical:[^\/]+>/<inline:\d+>/<fileName:[-\w\.*]+>';
$route = Seomatic::$plugin->handle.'/file/seo-file-link';
$event->rules[$path] = ['route' => $route];
}
);
}

/**
* Handle AdminCP requests
* Handle AdminCP requests. We do it only after we receive the event
* EVENT_AFTER_LOAD_PLUGINS so that any pending db migrations can be run
* before our event listeners kick in
*/
protected function handleAdminCpRequest()
{
Expand Down

0 comments on commit e90a88b

Please sign in to comment.