|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Mooore\WordpressIntegrationCms\Observer\Adminhtml; |
| 6 | + |
| 7 | +use Magento\Framework\Event\Observer; |
| 8 | +use Magento\Framework\Event\ObserverInterface; |
| 9 | +use Magento\Framework\Exception\LocalizedException; |
| 10 | +use Mooore\WordpressIntegrationCms\Model\Config; |
| 11 | +use Mooore\WordpressIntegrationCms\Model\RemotePageRepository; |
| 12 | + |
| 13 | +/** |
| 14 | + * This Observer class sets the Wordpress page ID to NULL when the page has been detached and updates the record in the |
| 15 | + * Wordpress environment appropriately. |
| 16 | + * |
| 17 | + * @class PageDetacher |
| 18 | + */ |
| 19 | +class PageDetacher implements ObserverInterface |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var Config |
| 23 | + */ |
| 24 | + private $config; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var RemotePageRepository |
| 28 | + */ |
| 29 | + private $remotePageRepository; |
| 30 | + |
| 31 | + public function __construct(RemotePageRepository $remotePageRepository, Config $config) |
| 32 | + { |
| 33 | + $this->remotePageRepository = $remotePageRepository; |
| 34 | + $this->config = $config; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @param Observer $observer |
| 39 | + * @return void |
| 40 | + */ |
| 41 | + public function execute(Observer $observer) |
| 42 | + { |
| 43 | + $page = $observer->getData('page'); |
| 44 | + if (!array_key_exists('wordpress_page_id', $observer->getData('page')->getData())) { |
| 45 | + $page->setData('wordpress_page_id', null); |
| 46 | + $wordpressSiteAndPageId = $page->getOrigData('wordpress_page_id'); |
| 47 | + if (!$this->config->magentoUrlPushBackEnabled() || !$wordpressSiteAndPageId) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $explodedWordpressSiteAndPageId = explode('_', $page->getOrigData('wordpress_page_id')); |
| 52 | + try { |
| 53 | + $this->remotePageRepository->postMetaData( |
| 54 | + (int) $explodedWordpressSiteAndPageId[0], |
| 55 | + (int) $explodedWordpressSiteAndPageId[1], |
| 56 | + 'mooore_magento_cms_url', |
| 57 | + '' |
| 58 | + ); |
| 59 | + } catch (LocalizedException $exception) { |
| 60 | + $this->messageManager->addErrorMessage($exception->getMessage()); |
| 61 | + return; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments