diff --git a/CHANGELOG.md b/CHANGELOG.md index e59330d1d..6303e05ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,30 +1,41 @@ # Changelog +## [1.1.21](https://github.com/mailchimp/mc-magento/releases/tag/1.1.21) - 2020-10-21 +**Fixed** +- Protected functions in HelperData being called from Migration Helper. [\#1189](https://github.com/mailchimp/mc-magento/issues/1189) +- 1.1.20 PHP Fatal error: Uncaught Error: Call to undefined method Ebizmarts_MailChimp_Model_Api_Subscribers_MailchimpTags::getHelper() in /app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers/MailchimpTags.php:275 [\#1177](https://github.com/mailchimp/mc-magento/issues/1177) + +**Changed** +- Set CURL_HTTP_VERSION_1_1 [\#1155](https://github.com/mailchimp/mc-magento/issues/1155) +- Add Mailchimp webhook trigger "Profile Update" [\#1145](https://github.com/mailchimp/mc-magento/issues/1145) +- quoteEscape() method doesn't exist on M1.7 [\#1114](https://github.com/mailchimp/mc-magento/issues/1114) +- Request to delete/unsubscribe customers via Magento in Customer + CreditNote to support Omnivore [\#799](https://github.com/mailchimp/mc-magento/issues/799) + ## [1.1.20](https://github.com/mailchimp/mc-magento/releases/tag/1.1.20) - 2020-03-30 **Fixed** - Not sending emails using Mandrill when new customer register. [\#1159](https://github.com/mailchimp/mc-magento/issues/1159) - Mandrill configuration showing: --- Invalid API KEY --- even if API key is correct [\#1157](https://github.com/mailchimp/mc-magento/issues/1157) - Warning: include(Ebizmarts/MailChimp/Model/Resource/Ecommercesyncdata/Promorules/Collection.php): failed to open stream: No such file or directory in /data/web/jenkins/public/builds/current/lib/Varien/Autoload.php on line 94 [\#1156](https://github.com/mailchimp/mc-magento/issues/1156) - Limit local length of Merge Field to be the same size as in Mailchimp. [\#1140](https://github.com/mailchimp/mc-magento/issues/1140) -- Loading animation remains forever if there are no lists in the account after placing API Key [\#1136](https://github.com/mailchimp/mc-magento/issues/1136) -- Remove the mc_eid logic. [\#1141](https://github.com/mailchimp/mc-magento/issues/1141) -- Javascript error in campaignCatcher.js [\#1147](https://github.com/mailchimp/mc-magento/issues/1147) **Changed** - Only 10 interest group checkboxes showed after checkout [\#1135](https://github.com/mailchimp/mc-magento/issues/1135) -- Add Customer data update based on map fields in webhook [\#1110](https://github.com/mailchimp/mc-magento/issues/1110) -- Add explanation on warning when clicking Resend and Reset buttons with what scope will be deleted. [\#1142](https://github.com/mailchimp/mc-magento/issues/1142) ## [1.1.19](https://github.com/mailchimp/mc-magento/releases/tag/1.1.19) - 2020-03-03 **Fixed** +- Javascript error in campaignCatcher.js [\#1147](https://github.com/mailchimp/mc-magento/issues/1147) +- Remove the mc_eid logic. [\#1141](https://github.com/mailchimp/mc-magento/issues/1141) +- Loading animation remains forever if there are no lists in the account after placing API Key [\#1136](https://github.com/mailchimp/mc-magento/issues/1136) - Serializer error when Magento is compiled. [\#1116](https://github.com/mailchimp/mc-magento/issues/1116) - Missing getWebhooks() function on Lib. [\#1115](https://github.com/mailchimp/mc-magento/issues/1115) - Error in Cron error while executing mailchimp_clear_ecommerce_data [\#1112](https://github.com/mailchimp/mc-magento/issues/1112) - Fix incorrect class declaration in function setEcommerceSyncDataItemValues [\#1103](https://github.com/mailchimp/mc-magento/issues/1103) **Changed** +- Add explanation on warning when clicking Resend and Reset buttons with what scope will be deleted. [\#1142](https://github.com/mailchimp/mc-magento/issues/1142) - Refactor collection filters to avoid doing joins, where, etc outside resources. [\#1119](https://github.com/mailchimp/mc-magento/issues/1119) - Allow translation of texts on app/code/community/Ebizmarts/MailChimp/controllers/CartController.php [\#1118](https://github.com/mailchimp/mc-magento/issues/1118) - Add Customer Interest Groups update based on webhook [\#1111](https://github.com/mailchimp/mc-magento/issues/1111) +- Add Customer data update based on map fields in webhook [\#1110](https://github.com/mailchimp/mc-magento/issues/1110) - Add Mailchimp configuration link at Newsletter -> Mailchimp menu [\#1107](https://github.com/mailchimp/mc-magento/issues/1107) - Log the product id if is not supported by Mailchimp [\#1101](https://github.com/mailchimp/mc-magento/issues/1101) - Exceptions not treated properly. [\#1098](https://github.com/mailchimp/mc-magento/issues/1098) diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 000000000..36b2459f6 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,48 @@ + +Open Software License ("OSL") v. 3.0 + +This Open Software License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work: + +Licensed under the Open Software License version 3.0 + + 1. Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following: + + 1. to reproduce the Original Work in copies, either alone or as part of a collective work; + + 2. to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work; + + 3. to distribute or communicate copies of the Original Work and Derivative Works to the public, with the proviso that copies of Original Work or Derivative Works that You distribute or communicate shall be licensed under this Open Software License; + + 4. to perform the Original Work publicly; and + + 5. to display the Original Work publicly. + + 2. Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works. + + 3. Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work. + + 4. Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor's trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license. + + 5. External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c). + + 6. Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work. + + 7. Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer. + + 8. Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation. + + 9. Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including 'fair use' or 'fair dealing'). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c). + + 10. Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware. + + 11. Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License. + + 12. Attorneys' Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License. + + 13. Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. + + 14. Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. + + 15. Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You. + + 16. Modification of This License. This License is Copyright (C) 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Open Software License" or "OSL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under " or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process. diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php index 60f1fef4a..cd0f2e7b0 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Customer/Edit/Tab/Mailchimp.php @@ -51,6 +51,23 @@ public function getInterest() return $interest; } + /** + * @param $data + * @return string + */ + public function escapeQuote($data) + { + return $this->getHelper()->mcEscapeQuote($data); + } + + /** + * @return Ebizmarts_MailChimp_Helper_Data + */ + public function getHelper() + { + return $this->_helper; + } + /** * @return Mage_Newsletter_Model_Subscriber */ diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Sales/Order/Creditmemo/Unsubscribe.php b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Sales/Order/Creditmemo/Unsubscribe.php new file mode 100644 index 000000000..cd1e2f090 --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/Sales/Order/Creditmemo/Unsubscribe.php @@ -0,0 +1,14 @@ +_campaignName; } + /** + * @param $data + * @return string + */ + public function escapeQuote($data) + { + return $this->getMailChimpHelper()->mcEscapeQuote($data); + } + /** * @return Ebizmarts_MailChimp_Helper_Data */ - protected function getMailChimpHelper() + public function getMailChimpHelper() { return Mage::helper('mailchimp'); } diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/System/Config/Fieldset/Mailchimp/Hint.php b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/System/Config/Fieldset/Mailchimp/Hint.php index 136b88fbb..4f889db49 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/System/Config/Fieldset/Mailchimp/Hint.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/System/Config/Fieldset/Mailchimp/Hint.php @@ -38,7 +38,7 @@ public function getMailChimpVersion() */ public function getMigrationFinishedStatus() { - return $this->makeHelper()->migrationFinished(); + return $this->makeMigrationHelper()->migrationFinished(); } /** @@ -101,4 +101,12 @@ protected function makeHelper() { return Mage::helper('mailchimp'); } + + /** + * @return Ebizmarts_MailChimp_Helper_Migration + */ + protected function makeMigrationHelper() + { + return Mage::helper('mailchimp/migration'); + } } diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/System/Config/Form/Field/Mapfields.php b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/System/Config/Form/Field/Mapfields.php index bd6951a45..bf82f9317 100755 --- a/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/System/Config/Form/Field/Mapfields.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Adminhtml/System/Config/Form/Field/Mapfields.php @@ -69,6 +69,23 @@ public function __construct() ksort($this->_customerAttributes); } + /** + * @param $data + * @return string + */ + public function escapeQuote($data) + { + return $this->getHelper()->mcEscapeQuote($data); + } + + /** + * @return Ebizmarts_MailChimp_Helper_Data + */ + public function getHelper() + { + return $this->_helper; + } + /** * @param string $columnName * @return string diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php index b7a733e63..5d9da9c40 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Subscribe.php @@ -30,6 +30,23 @@ public function __construct() $this->_storeId = Mage::app()->getStore()->getId(); } + /** + * @param $data + * @return string + */ + public function escapeQuote($data) + { + return $this->getHelper()->mcEscapeQuote($data); + } + + /** + * @return Ebizmarts_MailChimp_Helper_Data + */ + public function getHelper() + { + return $this->_helper; + } + /** * Render block HTML * diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php index 2a0d38ccd..54face2e3 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Checkout/Success/Groups.php @@ -81,6 +81,23 @@ public function getMessageAfter() return $message; } + /** + * @param $data + * @return string + */ + public function escapeQuote($data) + { + return $this->getMailChimpHelper()->mcEscapeQuote($data); + } + + /** + * @return Ebizmarts_MailChimp_Helper_Data|Mage_Core_Helper_Abstract + */ + public function getMailChimpHelper() + { + return $this->_helper; + } + /** * @return false|Mage_Core_Model_Abstract */ @@ -101,12 +118,4 @@ protected function getSessionLastRealOrder() { return $this->getMailChimpHelper()->getSessionLastRealOrder(); } - - /** - * @return Ebizmarts_MailChimp_Helper_Data|Mage_Core_Helper_Abstract - */ - protected function getMailChimpHelper() - { - return $this->_helper; - } } diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php b/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php index 90632c4c9..b44d88d0f 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Customer/Newsletter/Index.php @@ -57,23 +57,32 @@ public function getInterest() } /** - * Retrieve email from Customer object in session - * - * @return string Email address + * @param $data + * @return string */ - protected function _getEmail() + public function escapeQuote($data) { - return $this->getCustomerSession()->getCustomer()->getEmail(); + return $this->getMailChimpHelper()->mcEscapeQuote($data); } /** * @return Ebizmarts_MailChimp_Helper_Data */ - protected function getMailChimpHelper() + public function getMailChimpHelper() { return $this->_helper; } + /** + * Retrieve email from Customer object in session + * + * @return string Email address + */ + protected function _getEmail() + { + return $this->getCustomerSession()->getCustomer()->getEmail(); + } + /** * @return Mage_Customer_Model_Session */ diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php b/app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php index b31819dd2..d78557ed8 100644 --- a/app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Group/Type.php @@ -11,9 +11,12 @@ class Ebizmarts_MailChimp_Block_Group_Type extends Mage_Core_Block_Template { protected $_currentInterest; + protected $_helper; public function __construct(array $args = array()) { + $this->_helper = Mage::helper('mailchimp'); + if (isset($args['interests'])) { $this->_currentInterest = $interests = $args['interests']; $type = $interests['interest']['type']; @@ -23,6 +26,23 @@ public function __construct(array $args = array()) parent::__construct($args); } + /** + * @param $data + * @return string + */ + public function escapeQuote($data) + { + return $this->getHelper()->mcEscapeQuote($data); + } + + /** + * @return Ebizmarts_MailChimp_Helper_Data + */ + public function getHelper() + { + return $this->_helper; + } + /** * @return mixed */ diff --git a/app/code/community/Ebizmarts/MailChimp/Block/Popup/Emailcatcher.php b/app/code/community/Ebizmarts/MailChimp/Block/Popup/Emailcatcher.php index 93deaf83b..da29bf78d 100755 --- a/app/code/community/Ebizmarts/MailChimp/Block/Popup/Emailcatcher.php +++ b/app/code/community/Ebizmarts/MailChimp/Block/Popup/Emailcatcher.php @@ -12,6 +12,22 @@ */ class Ebizmarts_MailChimp_Block_Popup_Emailcatcher extends Mage_Core_Block_Template { + /** + * @param $data + * @return string + */ + public function escapeQuote($data) + { + return $this->getHelper()->mcEscapeQuote($data); + } + + /** + * @return Ebizmarts_MailChimp_Helper_Data + */ + public function getHelper() + { + return Mage::helper('mailchimp'); + } protected function _canCancel() { diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php index a1a1e5083..a380470e3 100644 --- a/app/code/community/Ebizmarts/MailChimp/Helper/Data.php +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Data.php @@ -389,21 +389,6 @@ public function encryptData($data) return Mage::helper('core')->encrypt($data); } - /** - * @param $scopeId - * @param null $scope - * @return mixed - * @throws Mage_Core_Exception - */ - public function getWebhookDeleteAction($scopeId, $scope = null) - { - return $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_UNSUBSCRIBE, - $scopeId, - $scope - ); - } - /** * Get local store_id value of the MC store for given scope. * @@ -704,23 +689,6 @@ public function getTwoWaySyncEnabled($scopeId = 0, $scope = null) ); } - /** - * Get webhook Id. - * - * @param int $scopeId - * @param null $scope - * @return mixed - * @throws Mage_Core_Exception - */ - public function getWebhookId($scopeId = 0, $scope = null) - { - return $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_WEBHOOK_ID, - $scopeId, - $scope - ); - } - /** * Get if monkey should be displayed in order grid. * @@ -1038,17 +1006,6 @@ public function isErrorLogEnabled() return $logEnabled; } - /** - * @return string - */ - public function getWebhooksKey() - { - $crypt = hash('md5', (string)$this->getConfig()->getNode('global/crypt/key')); - $key = substr($crypt, 0, (strlen($crypt) / 2)); - - return $key; - } - /** * Reset error messages from Products, Subscribers, Customers, Orders, Quotes * and set them to be sent again for given scope. @@ -1217,6 +1174,15 @@ public function saveLastItemsSent($scopeId, $scope, $filters = null) } } + /** + * @param $data + * @return string + */ + public function mcEscapeQuote($data) + { + return htmlspecialchars($data, ENT_QUOTES, null, false); + } + /** * @param $scopeId * @param $scope @@ -2007,640 +1973,11 @@ public function retrieveAndSaveMCJsUrlInConfig($scopeId, $scope = 'stores') } /** - * Handle data migration for versions that require it. - */ - public function handleMigrationUpdates() - { - $dateHelper = $this->getDateHelper(); - - $initialTime = $dateHelper->getTimestamp(); - $migrateFrom115 = $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_115, - 0, - 'default' - ); - $migrateFrom116 = $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_116, - 0, - 'default' - ); - $migrateFrom1164 = $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1164, - 0, - 'default' - ); - - if ($migrateFrom115) { - $this->_migrateFrom115($initialTime); - } elseif ($migrateFrom116 && !$dateHelper->timePassed($initialTime)) { - $this->_migrateFrom116($initialTime); - } elseif ($migrateFrom1164 && !$dateHelper->timePassed($initialTime)) { - $this->_migrateFrom1164($initialTime); - } - } - - /** - * Migrate data from version 1.1.5 to the mailchimp_ecommerce_sync_data table. - * - * @param $initialTime - * @throws Mage_Core_Exception - */ - protected function _migrateFrom115($initialTime) - { - $dateHelper = $this->getDateHelper(); - $arrayMigrationConfigData = array('115' => true, '116' => false, '1164' => false); - //migrate data from older version to the new schemma - if ($this->isEcommerceEnabled(0)) { - $mailchimpStoreId = $this->getMCStoreId(0); - - //migrate customers - $this->_migrateCustomersFrom115($mailchimpStoreId, $initialTime); - - if (!$dateHelper->timePassed($initialTime)) { - //migrate products - $this->_migrateProductsFrom115($mailchimpStoreId, $initialTime); - - if (!$dateHelper->timePassed($initialTime)) { - //migrate orders - $this->_migrateOrdersFrom115($mailchimpStoreId, $initialTime); - - if (!$dateHelper->timePassed($initialTime)) { - //migrate carts - $finished = $this->_migrateCartsFrom115($mailchimpStoreId, $initialTime); - - if ($finished) { - $this->_migrateFrom115dropColumn($arrayMigrationConfigData); - } - } - } - } - } else { - $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); - } - } - - protected function _migrateFrom115dropColumn($arrayMigrationConfigData) - { - $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); - - //Remove attributes no longer used - $setup = Mage::getResourceModel('catalog/setup', 'catalog_setup'); - - try { - $setup->removeAttribute('catalog_product', 'mailchimp_sync_delta'); - $setup->removeAttribute('catalog_product', 'mailchimp_sync_error'); - $setup->removeAttribute('catalog_product', 'mailchimp_sync_modified'); - $setup->removeAttribute('customer', 'mailchimp_sync_delta'); - $setup->removeAttribute('customer', 'mailchimp_sync_error'); - $setup->removeAttribute('customer', 'mailchimp_sync_modified'); - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - - $coreResource = $this->getCoreResource(); - - try { - $quoteTable = $coreResource->getTableName('sales/quote'); - $connectionQuote = $setup->getConnection(); - $connectionQuote->dropColumn($quoteTable, 'mailchimp_sync_delta'); - $connectionQuote->dropColumn($quoteTable, 'mailchimp_sync_error'); - $connectionQuote->dropColumn($quoteTable, 'mailchimp_deleted'); - $connectionQuote->dropColumn($quoteTable, 'mailchimp_token'); - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - - try { - $orderTable = $coreResource->getTableName('sales/order'); - $connectionOrder = $setup->getConnection(); - $connectionOrder->dropColumn($orderTable, 'mailchimp_sync_delta'); - $connectionOrder->dropColumn($orderTable, 'mailchimp_sync_error'); - $connectionOrder->dropColumn($orderTable, 'mailchimp_sync_modified'); - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - } - - /** - * Migrate Customers from version 1.1.5 to the mailchimp_ecommerce_sync_data table. - * - * @param $mailchimpStoreId - * @param $initialTime - * @throws Mage_Core_Exception - */ - protected function _migrateCustomersFrom115($mailchimpStoreId, $initialTime) - { - try { - $entityType = Mage::getSingleton('eav/config')->getEntityType('customer'); - $attribute = Mage::getModel('customer/attribute')->loadByCode($entityType, 'mailchimp_sync_delta'); - - if ($attribute->getId()) { - $mailchimpTableName = $this->getCoreResource()->getTableName('mailchimp/ecommercesyncdata'); - $customerCollection = Mage::getResourceModel('customer/customer_collection'); - $customerCollection->addAttributeToFilter('mailchimp_sync_delta', array('gt' => '0000-00-00 00:00:00')); - $customerCollection->getSelect()->joinLeft( - array('m4m' => $mailchimpTableName), - "m4m.related_id = e.entity_id AND m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_CUSTOMER - . "' AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'", - array('m4m.*') - ); - $customerCollection->getSelect()->where( - "m4m.mailchimp_sync_delta IS null" - ); - $this->_makeForCollectionItem( - $customerCollection, - $mailchimpStoreId, - $initialTime, - function ($customer, $mailchimpStoreId) { - $customerId = $customer->getEntityId(); - $customerObject = Mage::getModel('customer/customer')->load($customerId); - $syncError = null; - $syncModified = null; - $syncDelta = $customerObject->getMailchimpSyncDelta(); - - if ($customer->getMailchimpSyncError()) { - $syncError = $customer->getMailchimpSyncError(); - } - - if ($customer->getMailchimpSyncModified()) { - $syncModified = $customer->getMailchimpSyncModified(); - } - - $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); - $ecommerceSyncData->saveEcommerceSyncData( - $customerId, - Ebizmarts_MailChimp_Model_Config::IS_CUSTOMER, - $mailchimpStoreId, - $syncDelta, - $syncError, - $syncModified - ); - } - ); - } - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - } - - /** - * Migrate Products from version 1.1.5 to the mailchimp_ecommerce_sync_data table. - * - * @param $mailchimpStoreId - * @param $initialTime - * @throws Mage_Core_Exception - */ - protected function _migrateProductsFrom115($mailchimpStoreId, $initialTime) - { - try { - $entityType = Mage_Catalog_Model_Product::ENTITY; - $attributeCode = 'mailchimp_sync_delta'; - $attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entityType, $attributeCode); - - if ($attribute->getId()) { - $mailchimpTableName = $this->getCoreResource()->getTableName('mailchimp/ecommercesyncdata'); - $productCollection = Mage::getResourceModel('catalog/product_collection'); - $productCollection->addAttributeToFilter('mailchimp_sync_delta', array('gt' => '0000-00-00 00:00:00')); - $productCollection->getSelect()->joinLeft( - array('m4m' => $mailchimpTableName), - "m4m.related_id = e.entity_id AND m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_PRODUCT - . "' AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'", - array('m4m.*') - ); - $productCollection->getSelect()->where("m4m.mailchimp_sync_delta IS null"); - $this->_makeForCollectionItem( - $productCollection, - $mailchimpStoreId, - $initialTime, - function ($product, $mailchimpStoreId) { - $productId = $product->getEntityId(); - $_resource = Mage::getResourceSingleton('catalog/product'); - $syncDelta = $_resource->getAttributeRawValue( - $productId, - 'mailchimp_sync_delta', - $this->getMageApp()->getStore() - ); - $syncError = null; - $syncModified = null; - - if ($product->getMailchimpSyncError()) { - $syncError = $product->getMailchimpSyncError(); - } - - if ($product->getMailchimpSyncModified()) { - $syncModified = $product->getMailchimpSyncModified(); - } - - $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); - $ecommerceSyncData->saveEcommerceSyncData( - $productId, - Ebizmarts_MailChimp_Model_Config::IS_PRODUCT, - $mailchimpStoreId, - $syncDelta, - $syncError, - $syncModified - ); - } - ); - } - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - } - - /** - * Migrate Orders from version 1.1.5 to the mailchimp_ecommerce_sync_data table. - * - * @param $mailchimpStoreId - * @param $initialTime - * @throws Mage_Core_Exception - */ - protected function _migrateOrdersFrom115($mailchimpStoreId, $initialTime) - { - try { - $resource = $this->getCoreResource(); - $readConnection = $resource->getConnection('core_read'); - $tableName = $resource->getTableName('sales/order'); - $orderFields = $readConnection->describeTable($tableName); - - if (isset($orderFields['mailchimp_sync_delta'])) { - $mailchimpTableName = $resource->getTableName('mailchimp/ecommercesyncdata'); - $orderCollection = Mage::getResourceModel('sales/order_collection'); - - $orderCollection->getSelect()->joinLeft( - array('m4m' => $mailchimpTableName), - "m4m.related_id = main_table.entity_id AND m4m.type = '" - . Ebizmarts_MailChimp_Model_Config::IS_ORDER . - "' AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'", - array('m4m.*') - ); - $orderCollection->getSelect() - ->where( - "m4m.mailchimp_sync_delta IS NULL AND main_table.mailchimp_sync_delta > '0000-00-00 00:00:00'" - ); - $this->_makeForCollectionItem( - $orderCollection, - $mailchimpStoreId, - $initialTime, - function ($order, $mailchimpStoreId) { - $orderId = $order->getEntityId(); - $syncError = null; - $syncModified = null; - $orderObject = $this->getSalesOrderModel()->load($orderId); - $syncDelta = $orderObject->getMailchimpSyncDelta(); - - if ($order->getMailchimpSyncError()) { - $syncError = $order->getMailchimpSyncError(); - } - - if ($order->getMailchimpSyncModified()) { - $syncModified = $order->getMailchimpSyncModified(); - } - - $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); - $ecommerceSyncData->saveEcommerceSyncData( - $orderId, - Ebizmarts_MailChimp_Model_Config::IS_ORDER, - $mailchimpStoreId, - $syncDelta, - $syncError, - $syncModified - ); - } - ); - } - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - } - - /** - * Migrate Carts from version 1.1.5 to the mailchimp_ecommerce_sync_data table. - * - * @param $mailchimpStoreId - * @param $initialTime - * @return bool - * @throws Mage_Core_Exception - */ - protected function _migrateCartsFrom115($mailchimpStoreId, $initialTime) - { - try { - $resource = $this->getCoreResource(); - $readConnection = $resource->getConnection('core_read'); - $tableName = $resource->getTableName('sales/quote'); - $quoteFields = $readConnection->describeTable($tableName); - - if (isset($quoteFields['mailchimp_sync_delta'])) { - $mailchimpTableName = $resource->getTableName('mailchimp/ecommercesyncdata'); - $quoteCollection = Mage::getResourceModel('sales/quote_collection'); - $quoteCollection->getSelect()->joinLeft( - array('m4m' => $mailchimpTableName), - "m4m.related_id = main_table.entity_id AND m4m.type = '" - . Ebizmarts_MailChimp_Model_Config::IS_QUOTE - . "' AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'", - array('m4m.*') - ); - // be sure that the quotes are already in mailchimp and not deleted - $quoteCollection->getSelect() - ->where( - "m4m.mailchimp_sync_delta IS NULL AND main_table.mailchimp_sync_delta > '0000-00-00 00:00:00'" - ); - $finished = $this->_makeForCollectionItem( - $quoteCollection, - $mailchimpStoreId, - $initialTime, - function ($quote, $mailchimpStoreId) { - $quoteId = $quote->getEntityId(); - $syncError = null; - $syncDeleted = null; - $token = null; - $quoteObject = $this->getSalesOrderModel()->load($quoteId); - $syncDelta = $quoteObject->getMailchimpSyncDelta(); - - if ($quote->getMailchimpSyncError()) { - $syncError = $quote->getMailchimpSyncError(); - } - - if ($quote->getMailchimpSyncDeleted()) { - $syncDeleted = $quote->getMailchimpSyncDeleted(); - } - - if ($quote->getMailchimpToken()) { - $token = $quote->getMailchimpToken(); - } - - $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); - $ecommerceSyncData->saveEcommerceSyncData( - $quoteId, - Ebizmarts_MailChimp_Model_Config::IS_QUOTE, - $mailchimpStoreId, - $syncDelta, - $syncError, - null, - $syncDeleted, - $token - ); - } - ); - } else { - $finished = true; - } - - return $finished; - } catch (Exception $e) { - $this->logError( - $this->__( - 'Unexpected error happened during migration from version 1.1.5 to 1.1.6.' - . 'Please contact our support at ' - ) . 'mailchimp@ebizmarts-desk.zendesk.com' - . $this->__(' See error details below.') - ); - $this->logError($e->getMessage()); - - return false; - } - } - - /** - * Delete config data for migration from 1.1.5. - */ - protected function delete115MigrationConfigData() - { - $this->getConfig()->deleteConfig( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_115, - 'default', - 0 - ); - } - - /** - * Helper function for data migration from version 1.1.5. - * - * @param $collection - * @param $mailchimpStoreId - * @param $initialTime - * @param Closure $callback - * @return bool - */ - protected function _makeForCollectionItem($collection, $mailchimpStoreId, $initialTime, Closure $callback) - { - $dateHelper = $this->getDateHelper(); - $finished = false; - - if (!$collection->getSize()) { - $finished = true; - } - - $collection->setPageSize(100); - - $pages = $collection->getLastPageNumber(); - $currentPage = 1; - - do { - $collection->setCurPage($currentPage); - $this->_loadItemCollection($collection); - - foreach ($collection as $collectionItem) { - $callback($collectionItem, $mailchimpStoreId); - } - - $currentPage++; - // clear collection, - // if not done, the same page will be loaded each loop - // - will also free memory - $collection->clear(); - - if ($dateHelper->timePassed($initialTime)) { - break; - } - - if ($currentPage == $pages) { - $finished = true; - } - } while ($currentPage <= $pages); - - return $finished; - } - - /** - * @param $collection - */ - protected function _loadItemCollection($collection) - { - $collection->load(); - } - - /** - * Migrate data from version 1.1.6. - * - * @param $initialTime - */ - protected function _migrateFrom116($initialTime) - { - $this->_setIsSyncingIfFinishedInAllStores(true); - $finished = $this->_migrateOrdersFrom116($initialTime); - - if ($finished) { - $this->_setIsSyncingIfFinishedInAllStores(false); - $arrayMigrationConfigData = array('115' => false, '116' => true, '1164' => false); - $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); - } - } - - /** - * Modify is_syncing value if initial sync finished for all stores. - * - * @param $syncValue - */ - protected function _setIsSyncingIfFinishedInAllStores($syncValue) - { - $stores = $this->getMageApp()->getStores(); - - foreach ($stores as $storeId => $store) { - $ecommEnabled = $this->isEcomSyncDataEnabled($storeId); - - if ($ecommEnabled) { - $this->setIsSyncingIfFinishedPerScope($syncValue, $storeId); - } - } - } - - /** - * Update Order ids to the Increment id in MailChimp. - * - * @param $initialTime - * @return bool + * @return Ebizmarts_MailChimp_Helper_Webhook */ - protected function _migrateOrdersFrom116($initialTime) + protected function getWebhookHelper() { - $dateHelper = $this->getDateHelper(); - $finished = false; - - if (!$dateHelper->timePassed($initialTime)) { - $finished = true; - $stores = $this->getMageApp()->getStores(); - - foreach ($stores as $storeId => $store) { - if ($this->isEcomSyncDataEnabled($storeId)) { - Mage::getModel('mailchimp/api_batches')->replaceAllOrders($initialTime, $storeId); - } - - if ($dateHelper->timePassed($initialTime)) { - $finished = false; - break; - } - } - } - - return $finished; - } - - /** - * Return if migration has finished checking the config values. - * - * @return bool - * @throws Mage_Core_Exception - */ - public function migrationFinished() - { - $migrationFinished = false; - - $migrateFrom115 = $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_115, - 0, - 'default' - ); - - $migrateFrom116 = $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_116, - 0, - 'default' - ); - - $migrateFrom1164 = $this->getConfigValueForScope( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1164, - 0, - 'default' - ); - - if (!$migrateFrom115 && !$migrateFrom116 && !$migrateFrom1164) { - $migrationFinished = true; - } - - return $migrationFinished; - } - - /** - * Delete config data for migration from 1.1.6. - */ - public function delete116MigrationConfigData() - { - $stores = $this->getMageApp()->getStores(); - $this->getConfig()->deleteConfig( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_116, - 'default', - 0 - ); - - foreach ($stores as $storeId => $store) { - $this->getConfig()->deleteConfig( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_LAST_ORDER_ID, - 'stores', - $storeId - ); - } - } - - /** - * Migrate data from version 1.1.6.4. - * - * @param $initialTime - */ - protected function _migrateFrom1164($initialTime) - { - $dateHelper = $this->getDateHelper(); - - if (!$dateHelper->timePassed($initialTime)) { - $writeConnection = $this->getCoreResource()->getConnection('core_write'); - $resource = Mage::getResourceModel('mailchimp/ecommercesyncdata'); - $writeConnection->update($resource->getMainTable(), array('batch_id' => '1'), "batch_id = 0"); - $arrayMigrationConfigData = array('115' => false, '116' => false, '1164' => true); - $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); - } - } - - /** - * Delete config data for migration from 1.1.6.4. - */ - protected function delete1164MigrationConfigData() - { - $this->getConfig()->deleteConfig( - Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1164, - 'default', - 0 - ); - } - - /** - * @param $arrayMigrationConfigData - */ - public function handleDeleteMigrationConfigData($arrayMigrationConfigData) - { - foreach ($arrayMigrationConfigData as $migrationConfigData => $value) { - if ($migrationConfigData == '115' && $value) { - $this->delete115MigrationConfigData(); - } - - if ($migrationConfigData == '116' && $value) { - $this->delete116MigrationConfigData(); - } - - if ($migrationConfigData == '1164' && $value) { - $this->delete1164MigrationConfigData(); - } - } - - $this->getConfig()->cleanCache(); + return Mage::helper('mailchimp/webhook'); } /** @@ -2756,197 +2093,6 @@ public function loadListCustomer($listId, $email) return $customer; } - /** - * @param $scopeId - * @param string $scope - */ - public function handleWebhookChange($scopeId, $scope = 'stores') - { - $webhookScope = $this->getRealScopeForConfig( - Ebizmarts_MailChimp_Model_Config::GENERAL_LIST, - $scopeId, - $scope - ); - $listId = $this->getGeneralList($scopeId, $scope); - $this->deleteCurrentWebhook($webhookScope['scope_id'], $webhookScope['scope'], $listId); - - if ($this->isSubscriptionEnabled($scopeId, $scope)) { - $this->createNewWebhook($webhookScope['scope_id'], $webhookScope['scope'], $listId); - } - } - - /** - * @param $scopeId - * @param $scope - * @param $listId - * @throws Mage_Core_Exception - */ - protected function deleteCurrentWebhook($scopeId, $scope, $listId) - { - try { - $api = $this->getApi($scopeId, $scope); - $webhookId = $this->getWebhookId($scopeId, $scope); - $apiKey = $this->getApiKey($scopeId, $scope); - - if ($webhookId && $apiKey && $listId) { - try { - $api->getLists()->getWebhooks()->delete($listId, $webhookId); - } catch (MailChimp_Error $e) { - $this->logError($e->getFriendlyMessage()); - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - - $this->getConfig() - ->deleteConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_WEBHOOK_ID, $scope, $scopeId); - } else { - $webhookUrl = $this->getWebhookUrl($scopeId, $scope); - try { - if ($listId) { - $this->_deletedWebhooksByListId($api, $listId, $webhookUrl); - } - } catch (MailChimp_Error $e) { - $this->logError($e->getFriendlyMessage()); - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - } - } catch (Ebizmarts_MailChimp_Helper_Data_ApiKeyException $e) { - $this->logError($e->getMessage()); - } - } - - protected function _deletedWebhooksByListId($api, $listId, $webhookUrl) - { - $webhooks = $api->getLists()->getWebhooks()->getAll($listId); - - foreach ($webhooks['webhooks'] as $webhook) { - if (strpos($webhook['url'], $webhookUrl) !== false) { - $this->deleteWebhookFromList($api->getLists()->getWebhooks(), $listId, $webhook['id']); - } - } - } - - /** - * @param $apiWebhook - * @param $listId - * @param $webhookId - */ - public function deleteWebhookFromList($apiWebhook, $listId, $webhookId) - { - $apiWebhook->delete($listId, $webhookId); - } - - /** - * Returns true on successful creation, or error message if it fails - */ - public function createNewWebhook($scopeId, $scope, $listId) - { - $hookUrl = $this->getWebhookUrl(); - - try { - $api = $this->getApi($scopeId, $scope); - - if ($this->getTwoWaySyncEnabled($scopeId, $scope)) { - $events = array( - 'subscribe' => true, - 'unsubscribe' => true, - 'profile' => false, - 'cleaned' => true, - 'upemail' => true, - 'campaign' => false - ); - $sources = array( - 'user' => true, - 'admin' => true, - 'api' => true - ); - } else { - $events = array( - 'subscribe' => true, - 'unsubscribe' => true, - 'profile' => false, - 'cleaned' => false, - 'upemail' => false, - 'campaign' => false - ); - $sources = array( - 'user' => true, - 'admin' => true, - 'api' => false - ); - } - - try { - $response = $api->getLists()->getWebhooks()->getAll($listId); - $createWebhook = true; - - if (isset($response['total_items']) && $response['total_items'] > 0) { - foreach ($response['webhooks'] as $webhook) { - if ($webhook['url'] == $hookUrl) { - $createWebhook = false; - break; - } - } - } - - if ($createWebhook) { - $newWebhook = $api->getLists()->getWebhooks()->add($listId, $hookUrl, $events, $sources); - $newWebhookId = $newWebhook['id']; - $configValues = array(array(Ebizmarts_MailChimp_Model_Config::GENERAL_WEBHOOK_ID, $newWebhookId)); - $this->saveMailchimpConfig($configValues, $scopeId, $scope); - - return true; - } else { - return $this->__('The webhook already exists.'); - } - } catch (MailChimp_Error $e) { - $errorMessage = $e->getFriendlyMessage(); - $this->logError($errorMessage); - $textToCompare = 'The resource submitted could not be validated. ' - . 'For field-specific details, see the \'errors\' array.'; - - if ($e->getMailchimpDetails() == $textToCompare) { - $errorMessage = 'Your store could not be accessed by MailChimp\'s Api. ' - . 'Please confirm the URL: ' . $hookUrl - . ' is accessible externally to allow the webhook creation.'; - $this->logError($errorMessage); - } - - return $this->__($errorMessage); - } catch (Exception $e) { - $this->logError($e->getMessage()); - } - } catch (Ebizmarts_MailChimp_Helper_Data_ApiKeyException $e) { - $this->logError($e->getMessage()); - } - } - - /** - * @return string - */ - protected function getWebhookUrl() - { - $store = $this->getMageApp()->getDefaultStoreView(); - $webhooksKey = $this->getWebhooksKey(); - //Generating Webhooks URL - $url = Ebizmarts_MailChimp_Model_ProcessWebhook::WEBHOOKS_PATH; - $hookUrl = $store->getUrl( - $url, - array( - 'wkey' => $webhooksKey, - '_nosid' => true, - '_secure' => true, - ) - ); - - if (false != strstr($hookUrl, '?', true)) { - $hookUrl = strstr($hookUrl, '?', true); - } - - return $hookUrl; - } - /** * @param $path * @param $scopeId @@ -4967,7 +4113,7 @@ protected function getSalesOrderModel() /** * @return Ebizmarts_MailChimp_Helper_Date */ - protected function getDateHelper() + public function getDateHelper() { return Mage::helper('mailchimp/date'); } diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Migration.php b/app/code/community/Ebizmarts/MailChimp/Helper/Migration.php new file mode 100644 index 000000000..ace3e9cd4 --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Migration.php @@ -0,0 +1,816 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 3/20/2020 11:14 AM + * @file: Webhook.php + */ +class Ebizmarts_MailChimp_Helper_Migration extends Mage_Core_Helper_Abstract +{ + /** + * @var Ebizmarts_MailChimp_Helper_Data + */ + protected $_helper; + + /** + * @var Ebizmarts_MailChimp_Helper_Date + */ + protected $_dateHelper; + + /** + * @var Ebizmarts_MailChimp_Helper_Webhook + */ + protected $_webhookHelper; + + public function __construct() + { + $this->_helper = Mage::helper('mailchimp'); + $this->_dateHelper = Mage::helper('mailchimp/date'); + $this->_webhookHelper = Mage::helper('mailchimp/webhook'); + } + + /** + * Handle data migration for versions that require it. + */ + public function handleMigrationUpdates() + { + $helper = $this->getHelper(); + $dateHelper = $this->getDateHelper(); + + $initialTime = $dateHelper->getTimestamp(); + $migrateFrom115 = $helper->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_115, + 0, + 'default' + ); + $migrateFrom116 = $helper->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_116, + 0, + 'default' + ); + $migrateFrom1164 = $helper->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1164, + 0, + 'default' + ); + $migrateFrom1120 = $helper->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, + 0, + 'default' + ); + + if ($migrateFrom115) { + $this->_migrateFrom115($initialTime); + } elseif ($migrateFrom116 && !$dateHelper->timePassed($initialTime)) { + $this->_migrateFrom116($initialTime); + } elseif ($migrateFrom1164 && !$dateHelper->timePassed($initialTime)) { + $this->_migrateFrom1164($initialTime); + } elseif ($migrateFrom1120 && !$dateHelper->timePassed($initialTime)) { + $this->_migrateFrom1120($initialTime); + } + } + + /** + * Migrate data from version 1.1.5 to the mailchimp_ecommerce_sync_data table. + * + * @param $initialTime + * @throws Mage_Core_Exception + */ + protected function _migrateFrom115($initialTime) + { + $helper = $this->getHelper(); + $dateHelper = $this->getDateHelper(); + $arrayMigrationConfigData = array('115' => true, '116' => false, '1164' => false); + //migrate data from older version to the new schemma + if ($helper->isEcommerceEnabled(0)) { + $mailchimpStoreId = $this->getMCStoreId(0); + + //migrate customers + $this->_migrateCustomersFrom115($mailchimpStoreId, $initialTime); + + if (!$dateHelper->timePassed($initialTime)) { + //migrate products + $this->_migrateProductsFrom115($mailchimpStoreId, $initialTime); + + if (!$dateHelper->timePassed($initialTime)) { + //migrate orders + $this->_migrateOrdersFrom115($mailchimpStoreId, $initialTime); + + if (!$dateHelper->timePassed($initialTime)) { + //migrate carts + $finished = $this->_migrateCartsFrom115($mailchimpStoreId, $initialTime); + + if ($finished) { + $this->_migrateFrom115dropColumn($arrayMigrationConfigData); + } + } + } + } + } else { + $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); + } + } + + /** + * Helper function for data migration from version 1.1.5. + * + * @param $collection + * @param $mailchimpStoreId + * @param $initialTime + * @param Closure $callback + * @return bool + */ + protected function _makeForCollectionItem($collection, $mailchimpStoreId, $initialTime, Closure $callback) + { + $dateHelper = $this->getDateHelper(); + $finished = false; + + if (!$collection->getSize()) { + $finished = true; + } + + $collection->setPageSize(100); + + $pages = $collection->getLastPageNumber(); + $currentPage = 1; + + do { + $collection->setCurPage($currentPage); + $this->_loadItemCollection($collection); + + foreach ($collection as $collectionItem) { + $callback($collectionItem, $mailchimpStoreId); + } + + $currentPage++; + // clear collection, + // if not done, the same page will be loaded each loop + // - will also free memory + $collection->clear(); + + if ($dateHelper->timePassed($initialTime)) { + break; + } + + if ($currentPage == $pages) { + $finished = true; + } + } while ($currentPage <= $pages); + + return $finished; + } + + /** + * @param $collection + */ + protected function _loadItemCollection($collection) + { + $collection->load(); + } + + protected function _migrateFrom115dropColumn($arrayMigrationConfigData) + { + $helper = $this->getHelper(); + $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); + + //Remove attributes no longer used + $setup = Mage::getResourceModel('catalog/setup', 'catalog_setup'); + + try { + $setup->removeAttribute('catalog_product', 'mailchimp_sync_delta'); + $setup->removeAttribute('catalog_product', 'mailchimp_sync_error'); + $setup->removeAttribute('catalog_product', 'mailchimp_sync_modified'); + $setup->removeAttribute('customer', 'mailchimp_sync_delta'); + $setup->removeAttribute('customer', 'mailchimp_sync_error'); + $setup->removeAttribute('customer', 'mailchimp_sync_modified'); + } catch (Exception $e) { + $helper->logError($e->getMessage()); + } + + $coreResource = $helper->getCoreResource(); + + try { + $quoteTable = $coreResource->getTableName('sales/quote'); + $connectionQuote = $setup->getConnection(); + $connectionQuote->dropColumn($quoteTable, 'mailchimp_sync_delta'); + $connectionQuote->dropColumn($quoteTable, 'mailchimp_sync_error'); + $connectionQuote->dropColumn($quoteTable, 'mailchimp_deleted'); + $connectionQuote->dropColumn($quoteTable, 'mailchimp_token'); + } catch (Exception $e) { + $helper->logError($e->getMessage()); + } + + try { + $orderTable = $coreResource->getTableName('sales/order'); + $connectionOrder = $setup->getConnection(); + $connectionOrder->dropColumn($orderTable, 'mailchimp_sync_delta'); + $connectionOrder->dropColumn($orderTable, 'mailchimp_sync_error'); + $connectionOrder->dropColumn($orderTable, 'mailchimp_sync_modified'); + } catch (Exception $e) { + $helper->logError($e->getMessage()); + } + } + + /** + * @return Ebizmarts_MailChimp_Model_Ecommercesyncdata + */ + protected function getMailchimpEcommerceSyncDataModel() + { + return Mage::getModel('mailchimp/ecommercesyncdata'); + } + + /** + * Migrate Customers from version 1.1.5 to the mailchimp_ecommerce_sync_data table. + * + * @param $mailchimpStoreId + * @param $initialTime + * @throws Mage_Core_Exception + */ + protected function _migrateCustomersFrom115($mailchimpStoreId, $initialTime) + { + $helper = $this->getHelper(); + + try { + $entityType = Mage::getSingleton('eav/config')->getEntityType('customer'); + $attribute = Mage::getModel('customer/attribute')->loadByCode($entityType, 'mailchimp_sync_delta'); + + if ($attribute->getId()) { + $mailchimpTableName = $helper->getCoreResource()->getTableName('mailchimp/ecommercesyncdata'); + $customerCollection = Mage::getResourceModel('customer/customer_collection'); + $customerCollection->addAttributeToFilter('mailchimp_sync_delta', array('gt' => '0000-00-00 00:00:00')); + $customerCollection->getSelect()->joinLeft( + array('m4m' => $mailchimpTableName), + "m4m.related_id = e.entity_id AND m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_CUSTOMER + . "' AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'", + array('m4m.*') + ); + $customerCollection->getSelect()->where( + "m4m.mailchimp_sync_delta IS null" + ); + $this->_makeForCollectionItem( + $customerCollection, + $mailchimpStoreId, + $initialTime, + function ($customer, $mailchimpStoreId) { + $customerId = $customer->getEntityId(); + $customerObject = Mage::getModel('customer/customer')->load($customerId); + $syncError = null; + $syncModified = null; + $syncDelta = $customerObject->getMailchimpSyncDelta(); + + if ($customer->getMailchimpSyncError()) { + $syncError = $customer->getMailchimpSyncError(); + } + + if ($customer->getMailchimpSyncModified()) { + $syncModified = $customer->getMailchimpSyncModified(); + } + + $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); + $ecommerceSyncData->saveEcommerceSyncData( + $customerId, + Ebizmarts_MailChimp_Model_Config::IS_CUSTOMER, + $mailchimpStoreId, + $syncDelta, + $syncError, + $syncModified + ); + } + ); + } + } catch (Exception $e) { + $helper->logError($e->getMessage()); + } + } + + /** + * Migrate Products from version 1.1.5 to the mailchimp_ecommerce_sync_data table. + * + * @param $mailchimpStoreId + * @param $initialTime + * @throws Mage_Core_Exception + */ + protected function _migrateProductsFrom115($mailchimpStoreId, $initialTime) + { + $helper = $this->getHelper(); + + try { + $entityType = Mage_Catalog_Model_Product::ENTITY; + $attributeCode = 'mailchimp_sync_delta'; + $attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entityType, $attributeCode); + + if ($attribute->getId()) { + $mailchimpTableName = $helper->getCoreResource()->getTableName('mailchimp/ecommercesyncdata'); + $productCollection = Mage::getResourceModel('catalog/product_collection'); + $productCollection->addAttributeToFilter('mailchimp_sync_delta', array('gt' => '0000-00-00 00:00:00')); + $productCollection->getSelect()->joinLeft( + array('m4m' => $mailchimpTableName), + "m4m.related_id = e.entity_id AND m4m.type = '" . Ebizmarts_MailChimp_Model_Config::IS_PRODUCT + . "' AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'", + array('m4m.*') + ); + $productCollection->getSelect()->where("m4m.mailchimp_sync_delta IS null"); + $this->_makeForCollectionItem( + $productCollection, + $mailchimpStoreId, + $initialTime, + function ($product, $mailchimpStoreId) { + $productId = $product->getEntityId(); + $_resource = Mage::getResourceSingleton('catalog/product'); + $syncDelta = $_resource->getAttributeRawValue( + $productId, + 'mailchimp_sync_delta', + $helper->getMageApp()->getStore() + ); + $syncError = null; + $syncModified = null; + + if ($product->getMailchimpSyncError()) { + $syncError = $product->getMailchimpSyncError(); + } + + if ($product->getMailchimpSyncModified()) { + $syncModified = $product->getMailchimpSyncModified(); + } + + $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); + $ecommerceSyncData->saveEcommerceSyncData( + $productId, + Ebizmarts_MailChimp_Model_Config::IS_PRODUCT, + $mailchimpStoreId, + $syncDelta, + $syncError, + $syncModified + ); + } + ); + } + } catch (Exception $e) { + $helper->logError($e->getMessage()); + } + } + + /** + * @return Mage_Sales_Model_Order + */ + protected function getSalesOrderModel() + { + return Mage::getModel('sales/order'); + } + + /** + * Migrate Orders from version 1.1.5 to the mailchimp_ecommerce_sync_data table. + * + * @param $mailchimpStoreId + * @param $initialTime + * @throws Mage_Core_Exception + */ + protected function _migrateOrdersFrom115($mailchimpStoreId, $initialTime) + { + $helper = $this->getHelper(); + + try { + $resource = $helper->getCoreResource(); + $readConnection = $resource->getConnection('core_read'); + $tableName = $resource->getTableName('sales/order'); + $orderFields = $readConnection->describeTable($tableName); + + if (isset($orderFields['mailchimp_sync_delta'])) { + $mailchimpTableName = $resource->getTableName('mailchimp/ecommercesyncdata'); + $orderCollection = Mage::getResourceModel('sales/order_collection'); + + $orderCollection->getSelect()->joinLeft( + array('m4m' => $mailchimpTableName), + "m4m.related_id = main_table.entity_id AND m4m.type = '" + . Ebizmarts_MailChimp_Model_Config::IS_ORDER . + "' AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'", + array('m4m.*') + ); + $orderCollection->getSelect() + ->where( + "m4m.mailchimp_sync_delta IS NULL AND main_table.mailchimp_sync_delta > '0000-00-00 00:00:00'" + ); + $this->_makeForCollectionItem( + $orderCollection, + $mailchimpStoreId, + $initialTime, + function ($order, $mailchimpStoreId) { + $orderId = $order->getEntityId(); + $syncError = null; + $syncModified = null; + $orderObject = $this->getSalesOrderModel()->load($orderId); + $syncDelta = $orderObject->getMailchimpSyncDelta(); + + if ($order->getMailchimpSyncError()) { + $syncError = $order->getMailchimpSyncError(); + } + + if ($order->getMailchimpSyncModified()) { + $syncModified = $order->getMailchimpSyncModified(); + } + + $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); + $ecommerceSyncData->saveEcommerceSyncData( + $orderId, + Ebizmarts_MailChimp_Model_Config::IS_ORDER, + $mailchimpStoreId, + $syncDelta, + $syncError, + $syncModified + ); + } + ); + } + } catch (Exception $e) { + $helper->logError($e->getMessage()); + } + } + + /** + * Migrate Carts from version 1.1.5 to the mailchimp_ecommerce_sync_data table. + * + * @param $mailchimpStoreId + * @param $initialTime + * @return bool + * @throws Mage_Core_Exception + */ + protected function _migrateCartsFrom115($mailchimpStoreId, $initialTime) + { + $helper = $this->getHelper(); + + try { + $resource = $helper->getCoreResource(); + $readConnection = $resource->getConnection('core_read'); + $tableName = $resource->getTableName('sales/quote'); + $quoteFields = $readConnection->describeTable($tableName); + + if (isset($quoteFields['mailchimp_sync_delta'])) { + $mailchimpTableName = $resource->getTableName('mailchimp/ecommercesyncdata'); + $quoteCollection = Mage::getResourceModel('sales/quote_collection'); + $quoteCollection->getSelect()->joinLeft( + array('m4m' => $mailchimpTableName), + "m4m.related_id = main_table.entity_id AND m4m.type = '" + . Ebizmarts_MailChimp_Model_Config::IS_QUOTE + . "' AND m4m.mailchimp_store_id = '" . $mailchimpStoreId . "'", + array('m4m.*') + ); + // be sure that the quotes are already in mailchimp and not deleted + $quoteCollection->getSelect() + ->where( + "m4m.mailchimp_sync_delta IS NULL AND main_table.mailchimp_sync_delta > '0000-00-00 00:00:00'" + ); + $finished = $this->_makeForCollectionItem( + $quoteCollection, + $mailchimpStoreId, + $initialTime, + function ($quote, $mailchimpStoreId) { + $quoteId = $quote->getEntityId(); + $syncError = null; + $syncDeleted = null; + $token = null; + $quoteObject = $this->getSalesOrderModel()->load($quoteId); + $syncDelta = $quoteObject->getMailchimpSyncDelta(); + + if ($quote->getMailchimpSyncError()) { + $syncError = $quote->getMailchimpSyncError(); + } + + if ($quote->getMailchimpSyncDeleted()) { + $syncDeleted = $quote->getMailchimpSyncDeleted(); + } + + if ($quote->getMailchimpToken()) { + $token = $quote->getMailchimpToken(); + } + + $ecommerceSyncData = $this->getMailchimpEcommerceSyncDataModel(); + $ecommerceSyncData->saveEcommerceSyncData( + $quoteId, + Ebizmarts_MailChimp_Model_Config::IS_QUOTE, + $mailchimpStoreId, + $syncDelta, + $syncError, + null, + $syncDeleted, + $token + ); + } + ); + } else { + $finished = true; + } + + return $finished; + } catch (Exception $e) { + $helper->logError( + $helper->__( + 'Unexpected error happened during migration from version 1.1.5 to 1.1.6.' + . 'Please contact our support at ' + ) . 'mailchimp@ebizmarts-desk.zendesk.com' + . $helper->__(' See error details below.') + ); + $helper->logError($e->getMessage()); + + return false; + } + } + + /** + * Delete config data for migration from 1.1.5. + */ + protected function delete115MigrationConfigData() + { + $helper = $this->getHelper(); + $helper->getConfig()->deleteConfig( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_115, + 'default', + 0 + ); + } + + /** + * Modify is_syncing value if initial sync finished for all stores. + * + * @param $syncValue + */ + protected function _setIsSyncingIfFinishedInAllStores($syncValue) + { + $stores = $this->getMageApp()->getStores(); + + foreach ($stores as $storeId => $store) { + $ecommEnabled = $this->isEcomSyncDataEnabled($storeId); + + if ($ecommEnabled) { + $this->setIsSyncingIfFinishedPerScope($syncValue, $storeId); + } + } + } + + /** + * Migrate data from version 1.1.6. + * + * @param $initialTime + */ + protected function _migrateFrom116($initialTime) + { + $this->_setIsSyncingIfFinishedInAllStores(true); + $finished = $this->_migrateOrdersFrom116($initialTime); + + if ($finished) { + $this->_setIsSyncingIfFinishedInAllStores(false); + $arrayMigrationConfigData = array('115' => false, '116' => true, '1164' => false); + $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); + } + } + + /** + * Update Order ids to the Increment id in MailChimp. + * + * @param $initialTime + * @return bool + */ + protected function _migrateOrdersFrom116($initialTime) + { + $helper = $this->getHelper(); + $dateHelper = $this->getDateHelper(); + $finished = false; + + if (!$dateHelper->timePassed($initialTime)) { + $finished = true; + $stores = $helper->getMageApp()->getStores(); + + foreach ($stores as $storeId => $store) { + if ($helper->isEcomSyncDataEnabled($storeId)) { + Mage::getModel('mailchimp/api_batches')->replaceAllOrders($initialTime, $storeId); + } + + if ($dateHelper->timePassed($initialTime)) { + $finished = false; + break; + } + } + } + + return $finished; + } + + /** + * Return if migration has finished checking the config values. + * + * @return bool + * @throws Mage_Core_Exception + */ + public function migrationFinished() + { + $helper = $this->getHelper(); + $migrationFinished = false; + + $migrateFrom115 = $helper->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_115, + 0, + 'default' + ); + + $migrateFrom116 = $helper->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_116, + 0, + 'default' + ); + + $migrateFrom1164 = $helper->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1164, + 0, + 'default' + ); + + $migrateFrom1120 = $helper->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, + 0, + 'default' + ); + + if (!$migrateFrom115 && !$migrateFrom116 && !$migrateFrom1164 && !$migrateFrom1120) { + $migrationFinished = true; + } + + return $migrationFinished; + } + + /** + * Delete config data for migration from 1.1.6. + */ + public function delete116MigrationConfigData() + { + $helper = $this->getHelper(); + $stores = $helper->getMageApp()->getStores(); + $helper->getConfig()->deleteConfig( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_116, + 'default', + 0 + ); + + foreach ($stores as $storeId => $store) { + $helper->getConfig()->deleteConfig( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_LAST_ORDER_ID, + 'stores', + $storeId + ); + } + } + + /** + * Migrate data from version 1.1.6.4. + * + * @param $initialTime + */ + protected function _migrateFrom1164($initialTime) + { + $helper = $this->getHelper(); + $dateHelper = $this->getDateHelper(); + + if (!$dateHelper->timePassed($initialTime)) { + $writeConnection = $helper->getCoreResource()->getConnection('core_write'); + $resource = Mage::getResourceModel('mailchimp/ecommercesyncdata'); + $writeConnection->update($resource->getMainTable(), array('batch_id' => '1'), "batch_id = 0"); + $arrayMigrationConfigData = array('115' => false, '116' => false, '1164' => true); + $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); + } + } + + /** + * Delete config data for migration from 1.1.6.4. + */ + protected function delete1164MigrationConfigData() + { + $helper = $this->getHelper(); + + $helper->getConfig()->deleteConfig( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1164, + 'default', + 0 + ); + } + + /** + * Migrate data from version 1.1.21. + * + * @param $initialTime + */ + protected function _migrateFrom1120($initialTime) + { + $helper = $this->getHelper(); + $dateHelper = $this->getDateHelper(); + $webhookHelper = $this->getWebhookHelper(); + + if (!$dateHelper->timePassed($initialTime)) { + // Get all stores data. + $stores = $helper->getMageApp()->getStores(); + + $events = array( + 'subscribe' => true, + 'unsubscribe' => true, + 'profile' => true, + 'cleaned' => true, + 'upemail' => true, + 'campaign' => false + ); + + $sources = array( + 'user' => true, + 'admin' => true, + 'api' => false + ); + + foreach ($stores as $storeId => $store) { + // Gets the ListId and WebhookId for the iterated store. + $listId = $helper->getGeneralList($scopeId, $scope); + $webhookId = $webhookHelper->getWebhookId($scopeId, $scope); + + // Edits the webhook with the new $event array. + $helper + ->getApi($storeId, $store) + ->getLists() + ->getWebhooks() + ->edit($listId, $webhookId, null, $events, $sources); + + if ($dateHelper->timePassed($initialTime)) { + $finished = false; + break; + } + } + + $arrayMigrationConfigData = array('115' => false, '116' => false, '1164' => false, '1120' => true); + $this->handleDeleteMigrationConfigData($arrayMigrationConfigData); + } + } + + /** + * Delete config data for migration from 1.1.21. + */ + protected function delete1120MigrationConfigData() + { + $helper = $this->getHelper(); + + $helper->getConfig()->deleteConfig( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, + 'default', + 0 + ); + } + + /** + * @param $arrayMigrationConfigData + */ + public function handleDeleteMigrationConfigData($arrayMigrationConfigData) + { + $helper = $this->getHelper(); + foreach ($arrayMigrationConfigData as $migrationConfigData => $value) { + if ($migrationConfigData == '115' && $value) { + $this->delete115MigrationConfigData(); + } + + if ($migrationConfigData == '116' && $value) { + $this->delete116MigrationConfigData(); + } + + if ($migrationConfigData == '1164' && $value) { + $this->delete1164MigrationConfigData(); + } + + if ($migrationConfigData == '1120' && $value) { + $this->delete1120MigrationConfigData(); + } + } + + $helper->getConfig()->cleanCache(); + } + + /** + * @var Ebizmarts_MailChimp_Helper_Data + */ + protected function getHelper() + { + return $this->_helper; + } + + /** + * @var Ebizmarts_MailChimp_Helper_Date + */ + protected function getDateHelper() + { + return $this->_dateHelper; + } + + /** + * @var Ebizmarts_MailChimp_Helper_Webhook + */ + protected function getWebhookHelper() + { + return $this->_webhookHelper; + } +} diff --git a/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php b/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php new file mode 100644 index 000000000..6f0bb5599 --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/Helper/Webhook.php @@ -0,0 +1,280 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + * @date: 3/20/2020 11:14 AM + * @file: Webhook.php + */ +class Ebizmarts_MailChimp_Helper_Webhook extends Mage_Core_Helper_Abstract +{ + /** + * @var Ebizmarts_MailChimp_Helper_Data + */ + protected $_helper; + + public function __construct() + { + $this->_helper = Mage::helper('mailchimp'); + } + + /** + * @param $scopeId + * @param null $scope + * @return mixed + * @throws Mage_Core_Exception + */ + public function getWebhookDeleteAction($scopeId, $scope = null) + { + $helper = $this->getHelper(); + return $helper->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_UNSUBSCRIBE, + $scopeId, + $scope + ); + } + + /** + * Get webhook Id. + * + * @param int $scopeId + * @param null $scope + * @return mixed + * @throws Mage_Core_Exception + */ + public function getWebhookId($scopeId = 0, $scope = null) + { + $helper = $this->getHelper(); + return $helper->getConfigValueForScope( + Ebizmarts_MailChimp_Model_Config::GENERAL_WEBHOOK_ID, + $scopeId, + $scope + ); + } + + /** + * @return string + */ + public function getWebhooksKey() + { + $helper = $this->getHelper(); + $crypt = hash('md5', (string)$helper->getConfig()->getNode('global/crypt/key')); + $key = substr($crypt, 0, (strlen($crypt) / 2)); + + return $key; + } + + /** + * @param $scopeId + * @param string $scope + */ + public function handleWebhookChange($scopeId, $scope = 'stores') + { + $helper = $this->getHelper(); + $webhookScope = $helper->getRealScopeForConfig( + Ebizmarts_MailChimp_Model_Config::GENERAL_LIST, + $scopeId, + $scope + ); + $listId = $helper->getGeneralList($scopeId, $scope); + $this->deleteCurrentWebhook($webhookScope['scope_id'], $webhookScope['scope'], $listId); + + if ($helper->isSubscriptionEnabled($scopeId, $scope)) { + $this->createNewWebhook($webhookScope['scope_id'], $webhookScope['scope'], $listId); + } + } + + /** + * @param $scopeId + * @param $scope + * @param $listId + * @throws Mage_Core_Exception + */ + protected function deleteCurrentWebhook($scopeId, $scope, $listId) + { + $helper = $this->getHelper(); + + try { + $api = $helper->getApi($scopeId, $scope); + $webhookId = $this->getWebhookId($scopeId, $scope); + $apiKey = $helper->getApiKey($scopeId, $scope); + + if ($webhookId && $apiKey && $listId) { + try { + $api->getLists()->getWebhooks()->delete($listId, $webhookId); + } catch (MailChimp_Error $e) { + $helper->logError($e->getFriendlyMessage()); + } catch (Exception $e) { + $helper->logError($e->getMessage()); + } + + $helper->getConfig() + ->deleteConfig(Ebizmarts_MailChimp_Model_Config::GENERAL_WEBHOOK_ID, $scope, $scopeId); + } else { + $webhookUrl = $this->getWebhookUrl($scopeId, $scope); + try { + if ($listId) { + $this->_deletedWebhooksByListId($api, $listId, $webhookUrl); + } + } catch (MailChimp_Error $e) { + $helper->logError($e->getFriendlyMessage()); + } catch (Exception $e) { + $helper->logError($e->getMessage()); + } + } + } catch (Ebizmarts_MailChimp_Helper_Data_ApiKeyException $e) { + $helper->logError($e->getMessage()); + } + } + + /** + * @param $api + * @param $listId + * @param $webhookUrl + */ + protected function _deletedWebhooksByListId($api, $listId, $webhookUrl) + { + $webhooks = $api->getLists()->getWebhooks()->getAll($listId); + + foreach ($webhooks['webhooks'] as $webhook) { + if (strpos($webhook['url'], $webhookUrl) !== false) { + $this->deleteWebhookFromList($api->getLists()->getWebhooks(), $listId, $webhook['id']); + } + } + } + + /** + * @param $apiWebhook + * @param $listId + * @param $webhookId + */ + public function deleteWebhookFromList($apiWebhook, $listId, $webhookId) + { + $apiWebhook->delete($listId, $webhookId); + } + + /** + * Returns true on successful creation, or error message if it fails + */ + public function createNewWebhook($scopeId, $scope, $listId) + { + //TODO: cambiar llamadas de este metodo + $helper = $this->getHelper(); + $hookUrl = $this->getWebhookUrl(); + + try { + $api = $helper->getApi($scopeId, $scope); + + if ($helper->getTwoWaySyncEnabled($scopeId, $scope)) { + $events = array( + 'subscribe' => true, + 'unsubscribe' => true, + 'profile' => true, + 'cleaned' => true, + 'upemail' => true, + 'campaign' => false + ); + $sources = array( + 'user' => true, + 'admin' => true, + 'api' => true + ); + } else { + $events = array( + 'subscribe' => true, + 'unsubscribe' => true, + 'profile' => true, + 'cleaned' => false, + 'upemail' => false, + 'campaign' => false + ); + $sources = array( + 'user' => true, + 'admin' => true, + 'api' => false + ); + } + + try { + $response = $api->getLists()->getWebhooks()->getAll($listId); + $createWebhook = true; + + if (isset($response['total_items']) && $response['total_items'] > 0) { + foreach ($response['webhooks'] as $webhook) { + if ($webhook['url'] == $hookUrl) { + $createWebhook = false; + break; + } + } + } + + if ($createWebhook) { + $newWebhook = $api->getLists()->getWebhooks()->add($listId, $hookUrl, $events, $sources); + $newWebhookId = $newWebhook['id']; + $configValues = array(array(Ebizmarts_MailChimp_Model_Config::GENERAL_WEBHOOK_ID, $newWebhookId)); + $helper->saveMailchimpConfig($configValues, $scopeId, $scope); + + return true; + } else { + return $this->__('The webhook already exists.'); + } + } catch (MailChimp_Error $e) { + $errorMessage = $e->getFriendlyMessage(); + $helper->logError($errorMessage); + $textToCompare = 'The resource submitted could not be validated. ' + . 'For field-specific details, see the \'errors\' array.'; + + if ($e->getMailchimpDetails() == $textToCompare) { + $errorMessage = 'Your store could not be accessed by MailChimp\'s Api. ' + . 'Please confirm the URL: ' . $hookUrl + . ' is accessible externally to allow the webhook creation.'; + $helper->logError($errorMessage); + } + + return $helper->__($errorMessage); + } catch (Exception $e) { + $helper->logError($e->getMessage()); + } + } catch (Ebizmarts_MailChimp_Helper_Data_ApiKeyException $e) { + $helper->logError($e->getMessage()); + } + } + + /** + * @return string + */ + protected function getWebhookUrl() + { + $helper = $this->getHelper(); + $store = $helper->getMageApp()->getDefaultStoreView(); + $webhooksKey = $this->getWebhooksKey(); + //Generating Webhooks URL + $url = Ebizmarts_MailChimp_Model_ProcessWebhook::WEBHOOKS_PATH; + $hookUrl = $store->getUrl( + $url, + array( + 'wkey' => $webhooksKey, + '_nosid' => true, + '_secure' => true, + ) + ); + + if (false != strstr($hookUrl, '?', true)) { + $hookUrl = strstr($hookUrl, '?', true); + } + + return $hookUrl; + } + + /** + * @return Ebizmarts_MailChimp_Helper_Data + */ + protected function getHelper() + { + return $this->_helper; + } +} diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/ItemSynchronizer.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/ItemSynchronizer.php index f471b46f2..be4081140 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/ItemSynchronizer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/ItemSynchronizer.php @@ -177,7 +177,7 @@ protected function addSyncDataToken($id, $token) protected function markSyncDataAsModified($id) { - $this->_updateSyncData($id, null, null, 1); + $this->_updateSyncData($id, null, null, 1, null, null, null, true); } protected function markSyncDataAsDeleted($id, $syncedFlag = null) diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers/MailchimpTags.php b/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers/MailchimpTags.php index dc6dc570e..af777e86d 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers/MailchimpTags.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Api/Subscribers/MailchimpTags.php @@ -38,6 +38,10 @@ class Ebizmarts_MailChimp_Model_Api_Subscribers_MailchimpTags * @var Ebizmarts_MailChimp_Helper_Date */ protected $_mcDateHelper; + /** + * @var Ebizmarts_MailChimp_Helper_Webhook + */ + protected $_mcWebhookHelper; /** * @var Mage_Sales_Model_Order */ @@ -52,6 +56,7 @@ public function __construct() { $this->setMailChimpHelper(); $this->setMailChimpDateHelper(); + $this->setMailChimpWebhookHelper(); $this->_interestGroupHandle = Mage::getModel('mailchimp/api_subscribers_InterestGroupHandle'); } @@ -272,7 +277,8 @@ public function processMergeFields($data, $subscribe = false) */ protected function _addSubscriberData($subscriber, $fname, $lname, $email, $listId) { - $helper = $this->getHelper(); + $helper = $this->getMailchimpHelper(); + $webhookHelper = $this->getMailchimpWebhookHelper(); $scopeArray = $helper->getFirstScopeFromConfig( Ebizmarts_MailChimp_Model_Config::GENERAL_LIST, $listId @@ -293,7 +299,7 @@ protected function _addSubscriberData($subscriber, $fname, $lname, $email, $list if ($member['status'] == 'subscribed') { $helper->subscribeMember($subscriber); } else if ($member['status'] == 'unsubscribed') { - if (!$helper->getWebhookDeleteAction($subscriber->getStoreId())) { + if (!$webhookHelper->getWebhookDeleteAction($subscriber->getStoreId())) { $helper->unsubscribeMember($subscriber); } } @@ -784,6 +790,22 @@ protected function setMailChimpDateHelper() $this->_mcDateHelper = Mage::helper('mailchimp/date'); } + /** + * @return Ebizmarts_MailChimp_Helper_Webhook + */ + public function getMailchimpWebhookHelper() + { + return $this->_mcWebhookHelper; + } + + /** + * @param $mageMCWebhookHelper + */ + protected function setMailChimpWebhookHelper() + { + $this->_mcWebhookHelper = Mage::helper('mailchimp/webhook'); + } + /** * @param $key * @return bool diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Config.php b/app/code/community/Ebizmarts/MailChimp/Model/Config.php index 4bb82041b..67f822764 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Config.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Config.php @@ -32,6 +32,7 @@ class Ebizmarts_MailChimp_Model_Config const GENERAL_MIGRATE_FROM_115 = 'mailchimp/general/migrate_from_115'; const GENERAL_MIGRATE_FROM_116 = 'mailchimp/general/migrate_from_116'; const GENERAL_MIGRATE_FROM_1164 = 'mailchimp/general/migrate_from_1164'; + const GENERAL_MIGRATE_FROM_1120 = 'mailchimp/general/migrate_from_1120'; const GENERAL_MIGRATE_LAST_ORDER_ID = 'mailchimp/general/migrate_last_order_id'; const GENERAL_SUBSCRIBER_AMOUNT = 'mailchimp/general/subscriber_batch_amount'; const GENERAL_TIME_OUT = 'mailchimp/general/connection_timeout'; diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Cron.php b/app/code/community/Ebizmarts/MailChimp/Model/Cron.php index bc1c0f657..1205ee7a8 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/Cron.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Cron.php @@ -16,18 +16,23 @@ class Ebizmarts_MailChimp_Model_Cron * @var Ebizmarts_MailChimp_Helper_Data */ protected $_mailChimpHelper; + /** + * @var Ebizmarts_MailChimp_Helper_Migration + */ + protected $_mailChimpMigrationHelper; public function __construct() { $this->_mailChimpHelper = Mage::helper('mailchimp'); + $this->_mailChimpMigrationHelper = Mage::helper('mailchimp/migration'); } public function syncEcommerceBatchData() { - if ($this->getHelper()->migrationFinished()) { + if ($this->getMigrationHelper()->migrationFinished()) { Mage::getModel('mailchimp/api_batches')->handleEcommerceBatches(); } else { - $this->getHelper()->handleMigrationUpdates(); + $this->getMigrationHelper()->handleMigrationUpdates(); } } @@ -55,4 +60,9 @@ protected function getHelper() { return $this->_mailChimpHelper; } + + protected function getMigrationHelper() + { + return $this->_mailChimpMigrationHelper; + } } diff --git a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php index 987ce823e..1bf4c4bc3 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/Observer.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/Observer.php @@ -440,27 +440,32 @@ public function newOrder(Varien_Event_Observer $observer) $this->removeCampaignData(); $items = $order->getAllItems(); - foreach ($items as $item) { - if ($this->isBundleItem($item) || $this->isConfigurableItem($item)) { - continue; - } + try { + foreach ($items as $item) { + if ($this->isBundleItem($item) || $this->isConfigurableItem($item)) { + continue; + } - $mailchimpStoreId = $helper->getMCStoreId($storeId); - $productId = $item->getProductId(); - $dataProduct = $this->getMailchimpEcommerceSyncDataModel()->getEcommerceSyncDataItem( - $productId, - Ebizmarts_MailChimp_Model_Config::IS_PRODUCT, - $mailchimpStoreId - ); + $mailchimpStoreId = $helper->getMCStoreId($storeId); + $productId = $item->getProductId(); + $dataProduct = $this->getMailchimpEcommerceSyncDataModel()->getEcommerceSyncDataItem( + $productId, + Ebizmarts_MailChimp_Model_Config::IS_PRODUCT, + $mailchimpStoreId + ); - $isMarkedAsDeleted = $dataProduct->getMailchimpSyncDeleted(); + $isMarkedAsDeleted = $dataProduct->getMailchimpSyncDeleted(); + $isMarkedAsModified = $dataProduct->getMailchimpSyncModified(); - if (!$isMarkedAsDeleted) { - $apiProducts = $this->makeApiProduct(); - $apiProducts->setMailchimpStoreId($mailchimpStoreId); - $apiProducts->setMagentoStoreId($storeId); - $apiProducts->update($productId); + if (!$isMarkedAsDeleted && !$isMarkedAsModified) { + $apiProducts = $this->makeApiProduct(); + $apiProducts->setMailchimpStoreId($mailchimpStoreId); + $apiProducts->setMagentoStoreId($storeId); + $apiProducts->update($productId); + } } + } catch (Exception $e) { + $helper->logError($e->getMessage()); } } @@ -777,6 +782,29 @@ public function newCreditMemo(Varien_Event_Observer $observer) return $observer; } + /** + * If "unsubscribe" checkbox is checked, ubsubscribes the customer. + * + * @param Varien_Event_Observer $observer + * @return Varien_Event_Observer + */ + public function createCreditmemo($observer) + { + $mailchimpUnsubscribe = $this->getRequest()->getParam('mailchimp_unsubscribe'); + + if ($this->isUnsubscribeChecked($mailchimpUnsubscribe)) { + $creditMemo = $observer->getEvent()->getCreditmemo(); + $helper = $this->makeHelper(); + $order = $creditMemo->getOrder(); + $email = $order->getCustomerEmail(); + $subscriberModel = $this->getSubscriberModel(); + $subscriber = $subscriberModel->loadByEmail($email); + $helper->unsubscribeMember($subscriber); + } + + return $observer; + } + /** * Set the products included in the credit memo to be updated on MailChimp on the next cron job run. * @@ -1186,6 +1214,18 @@ public function handleCustomerGroups($subscriberEmail, $params, $storeId, $custo return $subscriber; } + /** + * @return boolean + */ + protected function isUnsubscribeChecked($mailchimpUnsubscribe) + { + if ($mailchimpUnsubscribe === 'on') { + return true; + } + + return false; + } + /** * @return mixed */ diff --git a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/Active.php b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/Active.php index c69624bc0..1e304d835 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/Active.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/Active.php @@ -16,6 +16,7 @@ class Ebizmarts_MailChimp_Model_System_Config_Backend_Active extends Mage_Core_M protected function _afterSave() { $helper = $this->makeHelper(); + $webhookHelper = $this->makeWebhookHelper(); $scopeId = $this->getScopeId(); $scope = $this->getScope(); $groups = $this->getData('groups'); @@ -32,7 +33,7 @@ protected function _afterSave() if ($this->isValueChanged() && $this->getValue()) { if ($apiKey && $listId) { - $helper->createNewWebhook($scopeId, $scope, $listId); + $webhookHelper->createNewWebhook($scopeId, $scope, $listId); } else { $configValue = array(array(Ebizmarts_MailChimp_Model_Config::GENERAL_ACTIVE, false)); $helper->saveMailchimpConfig($configValue, $scopeId, $scope); @@ -50,6 +51,14 @@ protected function makeHelper() return Mage::helper('mailchimp'); } + /** + * @return Ebizmarts_MailChimp_Helper_Webhook + */ + protected function makeWebhookHelper() + { + return Mage::helper('mailchimp/webhook'); + } + /** * @return Mage_Adminhtml_Model_Session */ diff --git a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/List.php b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/List.php index 3ebcc265f..4705db344 100644 --- a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/List.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Backend/List.php @@ -17,6 +17,7 @@ protected function _afterSave() $groups = $this->getData('groups'); $helper = $this->getMailchimpHelper(); $dateHelper = $this->getMailchimpDateHelper(); + $webhookHelper = $this->getMailchimpWebhookHelper(); $scopeId = $this->getScopeId(); $scope = $this->getScope(); $valueChanged = $this->isValueChanged(); @@ -53,7 +54,7 @@ protected function _afterSave() } if ($apiKey && $moduleIsActive && $valueChanged) { - $helper->handleWebhookChange($scopeId, $scope); + $webhookHelper->handleWebhookChange($scopeId, $scope); } } @@ -73,6 +74,14 @@ protected function getMailchimpDateHelper() return Mage::helper('mailchimp/date'); } + /** + * @return Ebizmarts_MailChimp_Helper_Webhook + */ + protected function getMailchimpWebhookHelper() + { + return Mage::helper('mailchimp/webhook'); + } + /** * @return Mage_Adminhtml_Model_Session */ diff --git a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/Account.php b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/Account.php index 18c539e5e..3be9278b2 100755 --- a/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/Account.php +++ b/app/code/community/Ebizmarts/MailChimp/Model/System/Config/Source/Account.php @@ -26,6 +26,11 @@ class Ebizmarts_MailChimp_Model_System_Config_Source_Account */ protected $_helper; + /** + * @var Ebizmarts_MailChimp_Helper_Migration + */ + protected $_migrationHelper; + const USERNAME_KEY = 0; const TOTAL_ACCOUNT_SUB_KEY = 1; const TOTAL_LIST_SUB_KEY = 2; @@ -56,6 +61,7 @@ public function __construct($params) { $mcStore = null; $helper = $this->_helper = $this->makeHelper(); + $migrationHelper = $this->_migrationHelper = $this->makeMigrationHelper(); $scopeArray = $helper->getCurrentScope(); $apiKey = (empty($params)) ? $helper->getApiKey($scopeArray['scope_id'], $scopeArray['scope']) @@ -138,6 +144,7 @@ public function __construct($params) public function toOptionArray() { $helper = $this->_helper; + $migrationHelper = $this->_migrationHelper; $scopeArray = $helper->getCurrentScope(); if (is_array($this->_accountDetails)) { $totalAccountSubscribersText = $helper->__('Total Account Subscribers:'); @@ -208,7 +215,7 @@ public function toOptionArray() ); } - if (!$helper->migrationFinished() + if (!$migrationHelper->migrationFinished() && $helper->isEcommerceEnabled($scopeArray['scope_id'], $scopeArray['scope']) ) { $storeMigrationText = $helper->__( @@ -239,6 +246,14 @@ protected function makeHelper() return Mage::helper('mailchimp'); } + /** + * @return Ebizmarts_MailChimp_Helper_Migration + */ + protected function makeMigrationHelper() + { + return Mage::helper('mailchimp/migration'); + } + /** * @param $mailchimpStoreId * @return string diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php index 6b27da40b..164447d7b 100644 --- a/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/Adminhtml/MailchimpController.php @@ -15,10 +15,12 @@ class Ebizmarts_MailChimp_Adminhtml_MailchimpController extends Mage_Adminhtml_C { protected $_helper; + protected $_webhookHelper; public function preDispatch() { - $this->_helper = $this->makeHelper(); + $this->_helper = Mage::helper('mailchimp'); + $this->_webhookHelper = Mage::helper('mailchimp/webhook'); return parent::preDispatch(); } @@ -60,13 +62,14 @@ public function resendSubscribersAction() public function createWebhookAction() { $helper = $this->getHelper(); + $webhookHelper = $this->getWebhookHelper(); $mageApp = $helper->getMageApp(); $request = $mageApp->getRequest(); $scope = $request->getParam('scope'); $scopeId = $request->getParam('scope_id'); $listId = $helper->getGeneralList($scopeId); - $message = $helper->createNewWebhook($scopeId, $scope, $listId); + $message = $webhookHelper->createNewWebhook($scopeId, $scope, $listId); $mageApp->getResponse()->setBody($message); } @@ -194,14 +197,17 @@ protected function _isAllowed() /** * @return Ebizmarts_MailChimp_Helper_Data */ - protected function makeHelper() + protected function getHelper() { - return Mage::helper('mailchimp'); + return $this->_helper; } - protected function getHelper() + /** + * @return Ebizmarts_MailChimp_Helper_Webhook + */ + protected function getWebhookHelper() { - return $this->_helper; + return $this->_webhookHelper; } /** diff --git a/app/code/community/Ebizmarts/MailChimp/controllers/WebhookController.php b/app/code/community/Ebizmarts/MailChimp/controllers/WebhookController.php index dd743ebdf..4bb67f85a 100755 --- a/app/code/community/Ebizmarts/MailChimp/controllers/WebhookController.php +++ b/app/code/community/Ebizmarts/MailChimp/controllers/WebhookController.php @@ -13,6 +13,7 @@ class Ebizmarts_MailChimp_WebhookController extends Mage_Core_Controller_Front_Action { protected $_mailchimpHelper = null; + protected $_mailchimpWebhookHelper = null; /** * @return Ebizmarts_MailChimp_Helper_Data|Mage_Core_Helper_Abstract @@ -26,6 +27,18 @@ protected function getHelper() return $this->_mailchimpHelper; } + /** + * @return Ebizmarts_MailChimp_Helper_Webhook + */ + protected function getWebhookHelper() + { + if (!$this->_mailchimpWebhookHelper) { + $this->_mailchimpWebhookHelper = Mage::helper('mailchimp/webhook'); + } + + return $this->_mailchimpWebhookHelper; + } + /** * Entry point for all webhook operations */ @@ -36,6 +49,7 @@ public function indexAction() $moduleName = $request->getModuleName(); $data = $request->getPost(); $helper = $this->getHelper(); + $webhookHelper = $this->getWebhookHelper(); if ($moduleName == 'monkey') { if (isset($data['data']['list_id'])) { @@ -60,7 +74,7 @@ public function indexAction() return $this; } - $myKey = $helper->getWebhooksKey(); + $myKey = $webhookHelper->getWebhooksKey(); //Validate "wkey" GET parameter if ($myKey == $requestKey) { @@ -84,6 +98,7 @@ public function indexAction() protected function _deleteWebhook($storeId, $listId) { $helper = $this->getHelper(); + $webhookHelper = $this->getWebhookHelper(); try { $api = $helper->getApi($storeId); @@ -97,7 +112,7 @@ protected function _deleteWebhook($storeId, $listId) $webhooks = $api->getLists()->getWebhooks()->getAll($listId); foreach ($webhooks['webhooks'] as $webhook) { if (strpos($webhook['url'], 'monkey/webhook') !== false) { - $helper->deleteWebhookFromList($api->getLists()->getWebhooks(), $listId, $webhook['id']); + $webhookHelper->deleteWebhookFromList($api->getLists()->getWebhooks(), $listId, $webhook['id']); } } } catch (MailChimp_Error $e) { diff --git a/app/code/community/Ebizmarts/MailChimp/etc/config.xml b/app/code/community/Ebizmarts/MailChimp/etc/config.xml index b7dfab680..976edf55a 100644 --- a/app/code/community/Ebizmarts/MailChimp/etc/config.xml +++ b/app/code/community/Ebizmarts/MailChimp/etc/config.xml @@ -2,10 +2,10 @@ - 1.1.20 + 1.1.21 - 1.1.20 + 1.1.21 @@ -84,6 +84,14 @@ + + + + mailchimp/observer + createCreditmemo + + + diff --git a/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php new file mode 100644 index 000000000..f895cba0a --- /dev/null +++ b/app/code/community/Ebizmarts/MailChimp/sql/mailchimp_setup/mysql4-upgrade-1.1.20-1.1.21.php @@ -0,0 +1,31 @@ +getCollection() + ->addFieldToFilter('path', 'mailchimp/general/webhook_id'); + + /* If webhook is created, edites it and place the new "event" variable */ + if ($configDataCollection->getSize()) { + // Sets the migration flag to edit webhooks. + Mage::helper('mailchimp') + ->saveMailChimpConfig( + array( + array( + Ebizmarts_MailChimp_Model_Config::GENERAL_MIGRATE_FROM_1120, + 1) + ), + 0, + 'default' + ); + } +} catch (Exception $e) { + Mage::log($e->getMessage(), null, 'MailChimp_Errors.log', true); +} + +$installer->endSetup(); diff --git a/app/design/adminhtml/default/default/layout/ebizmarts/mailchimp.xml b/app/design/adminhtml/default/default/layout/ebizmarts/mailchimp.xml index f8a81e0ec..574ec0414 100644 --- a/app/design/adminhtml/default/default/layout/ebizmarts/mailchimp.xml +++ b/app/design/adminhtml/default/default/layout/ebizmarts/mailchimp.xml @@ -58,6 +58,11 @@ + + + + + diff --git a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml index 8276f3d8f..c148d1021 100644 --- a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml +++ b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/customer/tab/mailchimp.phtml @@ -4,7 +4,7 @@ $interest = $this->getInterest();
- quoteEscape($this->__('Mailchimp Information')) ?> + escapeQuote($this->__('Mailchimp Information')) ?>
diff --git a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/creditmemo/create/unsubscribe.phtml b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/creditmemo/create/unsubscribe.phtml new file mode 100644 index 000000000..1119a0737 --- /dev/null +++ b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/creditmemo/create/unsubscribe.phtml @@ -0,0 +1,4 @@ +

+ + +

diff --git a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/view/monkey.phtml b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/view/monkey.phtml index b1dde8c54..58bf30bef 100644 --- a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/view/monkey.phtml +++ b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/sales/order/view/monkey.phtml @@ -1,7 +1,7 @@ isReferred()) : ?>
-

quoteEscape($this->__('MailChimp')) ?>

+

escapeQuote($this->__('MailChimp')) ?>

@@ -16,7 +16,7 @@ isDataAvailable()): ?> - quoteEscape( + escapeQuote( $this->__("Yaay! Recovered by Mailchimp's campaign.") . "
" . $this->__("Name") . @@ -27,7 +27,7 @@ ); ?>
- quoteEscape( + escapeQuote( $this->__("The campaign does not belong to the audience configured for ") . $this->getStoreCodeFromOrder() . $this->__(" view.") diff --git a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/system/config/form/field/array_dropdown.phtml b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/system/config/form/field/array_dropdown.phtml index b9b7622c4..37da08225 100644 --- a/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/system/config/form/field/array_dropdown.phtml +++ b/app/design/adminhtml/default/default/template/ebizmarts/mailchimp/system/config/form/field/array_dropdown.phtml @@ -14,28 +14,28 @@ if (!$this->_addAfter) { $colspan = $colspan > 1 ? 'colspan="' . $colspan . '"' : ''; ?> -
+
- + _columns as $columnName => $column): ?> - + - + - - + -
quoteEscape($column['label']) ?>escapeQuote($column['label']) ?> quoteEscape($colspan) ?>>escapeQuote($colspan) ?>>
+
quoteEscape($colspan) ?>> + escapeQuote($colspan) ?>>
- +
-
+