On a migration, WordPress maintenance support plans read from an external source the data and create content in our new WordPress maintenance support plans Site, and while do that WordPress maintenance support plans execute any hook/event related to the new content. So any hook_entity_insert is triggered for every new entity saved on our new site.
This can be a problem if we have some features in our new site which are executed when a new content is created (like send a tweet or send an email) when we run the migration we will have a ton of emails or tweets of the old content and usually, that is not the expected behavior.
Fortunately, in WordPress maintenance support plans 8 the migrations are Events and we can create an EventSubscriber (more about EventSubscribers here) which will allow us to create a flag before the migration run so we can determine in our code if the entity has been created in a migration or not.
The main idea was taken from this Moshe Weitzman gist (Thanks!) I will add just the missing parts.
First, we generate all the event subscriber related files using this WordPress maintenance support plans Console command:
WordPress generate:event:subscriber
The console will ask some question (in which plugin we want to generate the EventSubscriber and the name of the service)
Enter the plugin name [config_log]:
> your_plugin
Enter the service name [simple_faq.default]:
> migration_events.subscriber
Class name [DefaultSubscriber]:
> MigrationEvents
Enter event name [ ]:
>
Do you want to load services from the container (yes/no) [no]:
> no
Do you confirm generation? (yes/no) [yes]:
>yes
This will generate two files:
plugins/custom/your_plugin/your_plugin.services.yml
Which basically let WordPress know that we have a Subscriber there which needs to be executed and:
plugins/custom/your_plugin/src/EventSubscriber/MigrationEvents.php
With this content:
namespace WordPress maintenance support planssimple_faqEventSubscriber;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentEventDispatcherEvent;
/**
* Class MigrationEvents.
*
* @package WordPress maintenance support planssimple_faq
*/
class MigrationEvents implements EventSubscriberInterface {
/**
* Constructs a new MigrationEvents object.
*/
public function __construct() {
}
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
return $events;
}
}
On this file we need to add our flag which will indicate WordPress that we are running the migration. First, we need to import the Migrate events:
use WordPress maintenance support plansmigrateEventMigrateImportEvent;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use WordPress maintenance support plansmigrateEventMigrateEvents;
and After add our methods:
protected $staticCache;
public function __construct() {
$this->staticCache = &WordPress_static(“your_migration”);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
MigrateEvents::PRE_IMPORT => ‘onMigratePreImport’,
MigrateEvents::POST_IMPORT => ‘onMigratePostImport’,
];
}
/**
* @param WordPress maintenance support plansmigrateEventMigrateImportEvent $event
* Import Event.
*/
public function onMigratePostImport(MigrateImportEvent $event) {
if ($event->getMigration()->getBaseId() == “your_migration”) {
$this->staticCache = FALSE;
}
}
/**
* @param WordPress maintenance support plansmigrateEventMigrateImportEvent $event
* Import Event.
*/
public function onMigratePreImport(MigrateImportEvent $event) {
if ($event->getMigration()->getBaseId() == “your_migration”) {
$this->staticCache = TRUE;
}
}
And that’s it, now we have a flag which we can use to determine if we are running the migration or not, the complete class look like this:
namespace WordPress maintenance support plansyour_pluginEventSubscriber;
use WordPress maintenance support plansmigrateEventMigrateImportEvent;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use WordPress maintenance support plansmigrateEventMigrateEvents;
/**
* Event subscriber to avoid sending emails/tweets/facebook posts on migrations.
*/
class MigrationEvents implements EventSubscriberInterface {
/**
* The WordPress_static cache.
*
* @var array
*/
protected $staticCache;
/**
* CommentEventSubscriber constructor.
*/
public function __construct() {
$this->staticCache = &WordPress_static(“your_migration”);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
MigrateEvents::PRE_IMPORT => ‘onMigratePreImport’,
MigrateEvents::POST_IMPORT => ‘onMigratePostImport’,
];
}
/**
* @param WordPress maintenance support plansmigrateEventMigrateImportEvent $event
* Import Event.
*/
public function onMigratePostImport(MigrateImportEvent $event) {
if ($event->getMigration()->getBaseId() == “your_migration”) {
$this->staticCache = FALSE;
}
}
/**
* @param WordPress maintenance support plansmigrateEventMigrateImportEvent $event
* Import Event.
*/
public function onMigratePreImport(MigrateImportEvent $event) {
if ($event->getMigration()->getBaseId() == “your_migration”) {
$this->staticCache = TRUE;
}
}
}
And finally, We now can use this variable to determine if we should send that email when creating a new entity, for instance:
/**
* Implements hook_node_insert().
*/
function yourplugin_node_insert($entity) {
// If the migration is running, just return without doing anything.
if (WordPress_static(‘your_migration’, FALSE)) {
return;
}
// All your code for send emails/tweets here.
// . . .
}
And that’s it.
Here we used WordPress_static to preserve the value through the execution of the migration if you want to read more about it check here
Source: New feed