Skip to content

Commit

Permalink
Merge pull request #39 from trustpilot/scope-param
Browse files Browse the repository at this point in the history
Scope param
  • Loading branch information
Povilas Kačinskas committed Jul 15, 2019
2 parents af039d6 + 0b61e8a commit 53bd5b6
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 83 deletions.
3 changes: 2 additions & 1 deletion Block/Head.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public function getPreviewCssUrl()

public function getInstallationKey()
{
$scope = $this->_helper->getScope();
$storeId = $this->_helper->getWebsiteOrStoreId();
return $this->_helper->getKey($storeId);
return $this->_helper->getKey($scope, $storeId);
}
}
3 changes: 2 additions & 1 deletion Block/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Magento\Framework\View\Element\Template;
use Trustpilot\Reviews\Helper\Data;
use Trustpilot\Reviews\Helper\OrderData;
use Magento\Store\Model\ScopeInterface as StoreScopeInterface;

class Success extends Template
{
Expand Down Expand Up @@ -38,7 +39,7 @@ public function getOrder()
$order = $this->_salesFactory->load($orderId);
$storeId = $order->getStoreId();

$general_settings = json_decode($this->_helper->getConfig('master_settings_field', $storeId))->general;
$general_settings = json_decode($this->_helper->getConfig('master_settings_field', $storeId, StoreScopeInterface::SCOPE_STORES))->general;
$data = $this->_orderData->getInvitation($order, 'magento2_success', \Trustpilot\Reviews\Model\Config::WITH_PRODUCT_DATA);

try {
Expand Down
16 changes: 8 additions & 8 deletions Block/System/Config/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ public function getIntegrationAppUrl()
return $this->_helper->getIntegrationAppUrl();
}

public function getSettings($storeId) {
return base64_encode($this->_helper->getConfig('master_settings_field', $storeId));
public function getSettings($scope, $storeId) {
return base64_encode($this->_helper->getConfig('master_settings_field', $storeId, $scope));
}

public function getPageUrls($storeId) {
return base64_encode(json_encode($this->_helper->getPageUrls($storeId)));
public function getPageUrls($scope, $storeId) {
return base64_encode(json_encode($this->_helper->getPageUrls($storeId, $scope)));
}

public function getCustomTrustBoxes($storeId)
public function getCustomTrustBoxes($scope, $storeId)
{
$customTrustboxes = $this->_helper->getConfig('custom_trustboxes', $storeId);
$customTrustboxes = $this->_helper->getConfig('custom_trustboxes', $storeId, $scope);
if ($customTrustboxes) {
return $customTrustboxes;
}
Expand All @@ -54,8 +54,8 @@ public function getStoreInformation() {
return $this->_helper->getStoreInformation();
}

public function getPastOrdersInfo($storeId) {
$info = $this->_pastOrders->getPastOrdersInfo($storeId);
public function getPastOrdersInfo($scope, $storeId) {
$info = $this->_pastOrders->getPastOrdersInfo($scope, $storeId);
$info['basis'] = 'plugin';
return json_encode($info);
}
Expand Down
16 changes: 8 additions & 8 deletions Block/Trustpilot.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ public function getIntegrationAppUrl()
return $this->_helper->getIntegrationAppUrl();
}

public function getSettings($storeId) {
return base64_encode($this->_helper->getConfig('master_settings_field', $storeId));
public function getSettings($scope, $storeId) {
return base64_encode($this->_helper->getConfig('master_settings_field', $storeId, $scope));
}

public function getPageUrls($storeId) {
return base64_encode(json_encode($this->_helper->getPageUrls($storeId)));
public function getPageUrls($scope, $storeId) {
return base64_encode(json_encode($this->_helper->getPageUrls($storeId, $scope)));
}

public function getCustomTrustBoxes($storeId)
public function getCustomTrustBoxes($scope, $storeId)
{
$customTrustboxes = $this->_helper->getConfig('custom_trustboxes', $storeId);
$customTrustboxes = $this->_helper->getConfig('custom_trustboxes', $storeId, $scope);
if ($customTrustboxes) {
return $customTrustboxes;
}
Expand All @@ -58,8 +58,8 @@ public function getStoreInformation() {
return $this->_helper->getStoreInformation();
}

public function getPastOrdersInfo($storeId) {
$info = $this->_pastOrders->getPastOrdersInfo($storeId);
public function getPastOrdersInfo($scope, $storeId) {
$info = $this->_pastOrders->getPastOrdersInfo($scope, $storeId);
$info['basis'] = 'plugin';
return json_encode($info);
}
Expand Down
6 changes: 3 additions & 3 deletions Controller/Adminhtml/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ public function execute()
case 'handle_past_orders':
if (array_key_exists('sync', $post)) {
$this->_pastOrders->sync($post["sync"], $scope, $scopeId);
$output = $this->_pastOrders->getPastOrdersInfo($scopeId);
$output = $this->_pastOrders->getPastOrdersInfo($scope, $scopeId);
$output['basis'] = 'plugin';
$output['pastOrders']['showInitial'] = false;
$this->getResponse()->setBody(json_encode($output));
break;
} else if (array_key_exists('resync', $post)) {
$this->_pastOrders->resync($scope, $scopeId);
$output = $this->_pastOrders->getPastOrdersInfo($scopeId);
$output = $this->_pastOrders->getPastOrdersInfo($scope, $scopeId);
$output['basis'] = 'plugin';
$this->getResponse()->setBody(json_encode($output));
break;
} else if (array_key_exists('issynced', $post)) {
$output = $this->_pastOrders->getPastOrdersInfo($scopeId);
$output = $this->_pastOrders->getPastOrdersInfo($scope, $scopeId);
$output['basis'] = 'plugin';
$this->getResponse()->setBody(json_encode($output));
break;
Expand Down
72 changes: 36 additions & 36 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\Store\Model\ScopeInterface as StoreScopeInterface;
use \Magento\Store\Model\StoreManagerInterface;
use \Magento\Framework\App\Cache\TypeListInterface;
use \Magento\Framework\App\Cache\Frontend\Pool;
use \Magento\Framework\Api\SearchCriteriaBuilder;
use \Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory as ConfigCollectionFactory;
use \Magento\Store\Model\StoreRepository;
use Magento\Framework\App\Config\ReinitableConfigInterface;

class Data extends AbstractHelper
{
Expand All @@ -28,30 +26,26 @@ class Data extends AbstractHelper
protected $_categoryCollectionFactory;
protected $_productCollectionFactory;
protected $_configWriter;
protected $_cacheTypeList;
protected $_cacheFrontendPool;
protected $_attributeFactory;
protected $_searchCriteriaBuilder;
protected $_attributeRepository;
protected $_configCollectionFactory;
protected $_logger;
protected $_httpClient;
protected $_storeRepository;
protected $_integrationAppUrl;
protected $_reinitableConfig;

public function __construct(
Context $context,
StoreManagerInterface $storeManager,
CategoryCollectionFactory $categoryCollectionFactory,
ProductCollectionFactory $productCollectionFactory,
WriterInterface $configWriter,
TypeListInterface $cacheTypeList,
Pool $cacheFrontendPool,
SearchCriteriaBuilder $searchCriteriaBuilder,
AttributeRepositoryInterface $attributeRepository,
ConfigCollectionFactory $configCollectionFactory,
TrustpilotHttpClient $httpClient,
StoreRepository $storeRepository
StoreRepository $storeRepository,
ReinitableConfigInterface $reinitableConfig
) {
$this->_storeManager = $storeManager;
$this->_categoryCollectionFactory = $categoryCollectionFactory;
Expand All @@ -61,13 +55,11 @@ public function __construct(
$this->_configWriter = $configWriter;
parent::__construct($context);
$this->_request = $context->getRequest();
$this->_cacheTypeList = $cacheTypeList;
$this->_cacheFrontendPool = $cacheFrontendPool;
$this->_configCollectionFactory = $configCollectionFactory;
$this->_logger = $context->getLogger();
$this->_httpClient = $httpClient;
$this->_storeRepository = $storeRepository;
$this->_integrationAppUrl = \Trustpilot\Reviews\Model\Config::TRUSTPILOT_INTEGRATION_APP_URL;
$this->_reinitableConfig = $reinitableConfig;
}

public function getIntegrationAppUrl()
Expand All @@ -82,9 +74,9 @@ public function getIntegrationAppUrl()
return $domainName;
}

public function getKey($storeId)
public function getKey($scope, $storeId)
{
return trim(json_decode(self::getConfig('master_settings_field', $storeId))->general->key);
return trim(json_decode(self::getConfig('master_settings_field', $storeId, $scope))->general->key);
}

private function getDefaultConfigValues($key)
Expand Down Expand Up @@ -157,15 +149,37 @@ public function getScope()
return 'default';
}

public function getConfig($config, $storeId)
public function getConfig($config, $storeId, $scope = null)
{
$this->_reinitableConfig->reinit();

$path = self::TRUSTPILOT_SETTINGS . $config;

$setting = $this->scopeConfig->getValue($path, $this->getScope(), $storeId);
if ($scope === null) {
$scope = $this->getScope();
} elseif ($scope === 'store') {
$scope = 'stores';
} elseif ($scope === 'website') {
$scope = 'websites';
}

$setting = $this->scopeConfig->getValue($path, $scope, $storeId);

return $setting ? $setting : $this->getDefaultConfigValues($config);
}

public function setConfig($config, $value, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0)
{
if ($scope === 'store') {
$scope = 'stores';
} elseif ($scope === 'website') {
$scope = 'websites';
}
$this->_configWriter->save(self::TRUSTPILOT_SETTINGS . $config, $value, $scope, $scopeId);

$this->_reinitableConfig->reinit();
}

public function getVersion() {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface');
Expand All @@ -176,13 +190,13 @@ public function getVersion() {
}
}

public function getPageUrls($storeId)
public function getPageUrls($scope, $storeId)
{
$pageUrls = new \stdClass();
$pageUrls->landing = $this->getPageUrl('trustpilot_trustbox_homepage', $storeId);
$pageUrls->category = $this->getPageUrl('trustpilot_trustbox_category', $storeId);
$pageUrls->product = $this->getPageUrl('trustpilot_trustbox_product', $storeId);
$customPageUrls = json_decode($this->getConfig('page_urls', $storeId));
$customPageUrls = json_decode($this->getConfig('page_urls', $storeId, $scope));
$urls = (object) array_merge((array) $customPageUrls, (array) $pageUrls);
return $urls;
}
Expand Down Expand Up @@ -308,31 +322,17 @@ private function loadAttributeValue($product, $selector)
} catch(\Exception $e) {
return '';
}
}

public function setConfig($config, $value, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0)
{
if ($scope === 'store') {
$scope = 'stores';
} elseif ($scope === 'website') {
$scope = 'websites';
}
$this->_configWriter->save(self::TRUSTPILOT_SETTINGS . $config, $value, $scope, $scopeId);

$this->_cacheTypeList->cleanType('block_html');
foreach ($this->_cacheFrontendPool as $cacheFrontend) {
$cacheFrontend->getBackend()->clean();
}
}
}

public function log($message, $exception)
{
$scope = $this->getScope();
$storeId = $this->getWebsiteOrStoreId();
$this->_logger->error($message, ['exception' => $exception]);
$log = array(
'platform' => 'Magento2',
'version' => \Trustpilot\Reviews\Model\Config::TRUSTPILOT_PLUGIN_VERSION,
'key' => $this->getKey($storeId),
'key' => $this->getKey($scope, $storeId),
'message' => $message,
);
$this->_httpClient->postLog($log, $storeId);
Expand Down
3 changes: 2 additions & 1 deletion Helper/OrderData.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use \Magento\Store\Model\StoreManagerInterface;
use Trustpilot\Reviews\Helper\Data;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Store\Model\ScopeInterface as StoreScopeInterface;

class OrderData extends AbstractHelper
{
Expand Down Expand Up @@ -121,7 +122,7 @@ public function getProducts($order)
{
$products = array();
try {
$settings = json_decode($this->_helper->getConfig('master_settings_field', $order->getStoreId()));
$settings = json_decode($this->_helper->getConfig('master_settings_field', $order->getStoreId(), StoreScopeInterface::SCOPE_STORES));
$skuSelector = $settings->skuSelector;
$gtinSelector = $settings->gtinSelector;
$mpnSelector = $settings->mpnSelector;
Expand Down
26 changes: 13 additions & 13 deletions Helper/PastOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public function sync($period_in_days, $scope, $storeId)
$this->_helper->setConfig('sync_in_progress', 'true', $scope, $storeId);
$this->_helper->setConfig("show_past_orders_initial", 'false', $scope, $storeId);
try {
$key = $this->_helper->getKey($storeId);
$key = $this->_helper->getKey($scope, $storeId);
$collect_product_data = \Trustpilot\Reviews\Model\Config::WITHOUT_PRODUCT_DATA;
if (!is_null($key)) {
$this->_helper->setConfig('past_orders', 0, $scope, $storeId);
$pageId = 1;
$sales_collection = $this->getSalesCollection($period_in_days, $storeId);
$sales_collection = $this->getSalesCollection($period_in_days, $scope, $storeId);
$post_batch = $this->getInvitationsForPeriod($sales_collection, $collect_product_data, $pageId);
while ($post_batch) {
set_time_limit(30);
Expand Down Expand Up @@ -74,8 +74,8 @@ public function resync($scope, $storeId)
{
$this->_helper->setConfig('sync_in_progress', 'true', $scope, $storeId);
try {
$key = $this->_helper->getKey($storeId);
$failed_orders_object = json_decode($this->_helper->getConfig('failed_orders', $scope, $storeId));
$key = $this->_helper->getKey($scope, $storeId);
$failed_orders_object = json_decode($this->_helper->getConfig('failed_orders', $storeId, $scope));
$collect_product_data = \Trustpilot\Reviews\Model\Config::WITHOUT_PRODUCT_DATA;
if (!is_null($key)) {
$failed_orders_array = array();
Expand Down Expand Up @@ -126,13 +126,13 @@ private function trustpilotGetOrdersByIds($collect_product_data, $order_ids) {
return $invitations;
}

public function getPastOrdersInfo($storeId)
public function getPastOrdersInfo($scope, $storeId)
{
$syncInProgress = $this->_helper->getConfig('sync_in_progress', $storeId);
$showInitial = $this->_helper->getConfig('show_past_orders_initial', $storeId);
$syncInProgress = $this->_helper->getConfig('sync_in_progress', $storeId, $scope);
$showInitial = $this->_helper->getConfig('show_past_orders_initial', $storeId, $scope);
if ($syncInProgress === 'false') {
$synced_orders = (int) $this->_helper->getConfig('past_orders', $storeId);
$failed_orders = json_decode($this->_helper->getConfig('failed_orders', $storeId));
$synced_orders = (int) $this->_helper->getConfig('past_orders', $storeId, $scope);
$failed_orders = json_decode($this->_helper->getConfig('failed_orders', $storeId, $scope));

$failed_orders_result = array();
foreach ($failed_orders as $key => $value) {
Expand Down Expand Up @@ -162,12 +162,12 @@ public function getPastOrdersInfo($storeId)
}
}

private function getSalesCollection($period_in_days, $storeId) {
private function getSalesCollection($period_in_days, $scope, $storeId) {
$date = new \DateTime();
$args = array(
'date_created' => $date->setTimestamp(time() - (86400 * $period_in_days))->format('Y-m-d'),
'limit' => 20,
'past_order_statuses' => json_decode($this->_helper->getConfig('master_settings_field', $storeId))->pastOrderStatuses
'past_order_statuses' => json_decode($this->_helper->getConfig('master_settings_field', $storeId, $scope))->pastOrderStatuses
);

$collection = $this->_orders->getCollection()
Expand Down Expand Up @@ -195,8 +195,8 @@ private function getInvitationsForPeriod($sales_collection, $collect_product_dat

private function handleTrustpilotResponse($response, $post_batch, $scope, $storeId)
{
$synced_orders = (int) $this->_helper->getConfig('past_orders', $storeId);
$failed_orders = json_decode($this->_helper->getConfig('failed_orders', $storeId));
$synced_orders = (int) $this->_helper->getConfig('past_orders', $storeId, $scope);
$failed_orders = json_decode($this->_helper->getConfig('failed_orders', $storeId, $scope));

$data = array();
if (isset($response['data']))
Expand Down
2 changes: 1 addition & 1 deletion Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Config
const TRUSTPILOT_GENERAL_CONFIGURATION = 'general';
const TRUSTPILOT_TRUSTBOX_CONFIGURATION = 'trustbox';
const TRUSTPILOT_INTEGRATION_KEY = 'key';
const TRUSTPILOT_PLUGIN_VERSION = '2.6.435';
const TRUSTPILOT_PLUGIN_VERSION = '2.6.437';
const TRUSTPILOT_SCRIPT = 'TrustpilotScriptUrl';
const TRUSTPILOT_INTEGRATION_APP = 'IntegrationAppUrl';
const TRUSTPILOT_WIDGET_SCRIPT = 'WidgetScriptUrl';
Expand Down
Loading

0 comments on commit 53bd5b6

Please sign in to comment.