88 use Magento \Framework \Event \Observer ;
99 use Magento \Framework \Event \ObserverInterface ;
1010 use Magento \Framework \Message \ManagerInterface ;
11- use Magento \Catalog \ Helper \ Product as ProductHelper ;
11+ use Magento \Store \ Model \ StoreManagerInterface ;
1212
1313 /**
1414 * Product.php - This observer is triggered when the product save event is fired. It then finds
@@ -29,37 +29,37 @@ class Product implements ObserverInterface {
2929 * @var Data _data Instance of the Data helper class
3030 * @var Logger _logger Instance of the custom Logger class
3131 * @var ManagerInterface _messageManager Instance of the ManagerInterface
32- * @var ProductHelper _productHelper Instance of the ProductHelper
3332 * @var Purger _purger Instance of the Purger helper class
33+ * @var StoreManager _storeManager Instance of the StoreManager
3434 */
3535 protected $ _data ;
3636 protected $ _logger ;
3737 protected $ _messageManager ;
38- protected $ _productHelper ;
3938 protected $ _purger ;
39+ protected $ _storeManager ;
4040
4141 /**
4242 * This constructor is overloaded from the parent class in order to use dependency injection
4343 * to get the dependency classes that we need for this module's command actions to execute.
4444 * @param Data data Instance of the Data helper class
4545 * @param Logger logger Instance of the custom Logger class
4646 * @param ManagerInterface messageManager Instance of the ManagerInterface
47- * @param ProductHelper productHelper Instance of the ProductHelper
4847 * @param Purger purger Instance of the Purger helper class
48+ * @param StoreManager storeManager Instance of the StoreManager
4949 */
5050 public function __construct (
5151 Data $ data ,
5252 Logger $ logger ,
5353 ManagerInterface $ messageManager ,
54- ProductHelper $ productHelper ,
55- Purger $ purger
54+ Purger $ purger ,
55+ StoreManagerInterface $ storeManager
5656 ) {
5757 // Save the injected class instances
5858 $ this ->_data = $ data ;
5959 $ this ->_logger = $ logger ;
6060 $ this ->_messageManager = $ messageManager ;
61- $ this ->_productHelper = $ productHelper ;
6261 $ this ->_purger = $ purger ;
62+ $ this ->_storeManager = $ storeManager ;
6363 }
6464
6565 /**
@@ -72,38 +72,44 @@ public function __construct (
7272 public function execute ( Observer $ observer ) {
7373 // Check to see if event is enabled
7474 if ( $ this ->_data ->isEnabled () && $ this ->_data ->shouldPurgeAfterProductSave () ) {
75- // Get the product and the product url specific to store view
76- $ product = $ observer ->getProduct ();
77- $ productUrl = $ this ->_productHelper ->getProductUrl ( $ product ->getId () );
78- $ temp = explode ( "/key/ " , $ productUrl );
79- $ productUrl = reset ( $ temp );
80- // Validate the url
81- $ url = $ this ->_purger ->validateUrl ( $ productUrl );
82- // Loop though responses, after purging store (store so we can use starts with for url)
83- foreach ( $ this ->_purger ->purgeStore ( $ url ) as $ response ) {
84- // Log what we are trying to do
85- $ message = [
86- "status " => $ response ->status ,
87- "action " => "auto_purge:product_save " ,
88- "target " => $ response ->target ,
89- "server " => $ response ->server
90- ];
91- $ this ->_logger ->blame ( $ this ->_data ->getLoggedInUserInfo (), $ message );
92- // Check to see if response was successful
93- if ( $ response ->status == 200 ) {
94- // Add success response message
95- $ targetHtml = "<font color='#79A22E' ><b> $ response ->target </b></font> " ;
96- $ serverHtml = "<font color='#79A22E' ><b> $ response ->server </b></font> " ;
97- $ message = "Successfully purged varnish cache for $ targetHtml on $ serverHtml " ;
98- $ this ->_messageManager ->addSuccess ( $ message );
99- }
100- else {
101- // Otherwise add an error message
102- $ targetHtml = "<font color='#E22626' ><b> $ response ->target </b></font> " ;
103- $ serverHtml = "<font color='#E22626' ><b> $ response ->server </b></font> " ;
104- $ statusHtml = "<font color='#E22626' ><b> $ response ->status </b></font> " ;
105- $ message = "Error Purging varnish cache for $ targetHtml on $ serverHtml with response code $ statusHtml " ;
106- $ this ->_messageManager ->addError ( $ message );
75+ // Get the base url for the current store
76+ $ baseUrl = $ this ->_storeManager ->getStore ()->getBaseUrl ();
77+ // Get the product id and retrieve all rewrites recursively and acyclicly.
78+ $ pid = $ observer ->getProduct ()->getId ();
79+ $ rewrites = $ this ->_purger ->getUrlRewrites ("catalog/product/view/id/ $ pid " );
80+ // Define purge command for each found URL
81+ $ actions = array_fill_keys ( $ rewrites , "purgeUrl " );
82+ $ actions ["catalog/product/view/id/ $ pid/ " ] = "purgeStore " ; // note the trailing slash
83+ // Loop through each rewrite
84+ foreach ( $ actions as $ rewrite => $ command ) {
85+ // Validate the url
86+ $ url = $ this ->_purger ->validateUrl ( $ baseUrl . $ rewrite );
87+ // Loop though responses, after purging store (store so we can use 'starts with' for url)
88+ foreach ( $ this ->_purger ->{ $ command } ( $ url ) as $ response ) {
89+ // Log what we are trying to do
90+ $ message = [
91+ "status " => $ response ->status ,
92+ "action " => "auto_purge:product_save " ,
93+ "target " => $ response ->target ,
94+ "server " => $ response ->server
95+ ];
96+ $ this ->_logger ->blame ( $ this ->_data ->getLoggedInUserInfo (), $ message );
97+ // Check to see if response was successful
98+ if ( $ response ->status == 200 ) {
99+ // Add success response message
100+ $ targetHtml = "<font color='#79A22E' ><b> $ response ->target </b></font> " ;
101+ $ serverHtml = "<font color='#79A22E' ><b> $ response ->server </b></font> " ;
102+ $ message = "Successfully purged varnish cache for $ targetHtml on $ serverHtml " ;
103+ $ this ->_messageManager ->addSuccess ( $ message );
104+ }
105+ else {
106+ // Otherwise add an error message
107+ $ targetHtml = "<font color='#E22626' ><b> $ response ->target </b></font> " ;
108+ $ serverHtml = "<font color='#E22626' ><b> $ response ->server </b></font> " ;
109+ $ statusHtml = "<font color='#E22626' ><b> $ response ->status </b></font> " ;
110+ $ message = "Error Purging varnish cache for $ targetHtml on $ serverHtml with response code $ statusHtml " ;
111+ $ this ->_messageManager ->addError ( $ message );
112+ }
107113 }
108114 }
109115 }
0 commit comments