Skip to content

Commit 2f82c13

Browse files
Merge branch 'main' of github.com:magento-commerce/facebook-for-magento2 into 1.0.0-release
2 parents 2b0b755 + ab46a63 commit 2f82c13

File tree

182 files changed

+6991
-4472
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

182 files changed

+6991
-4472
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
# Meta Business Extension For Magento2
2+
# Meta Business Extension For Magento2
33

44
## Facebook Connects Businesses with People
55

app/code/Meta/BusinessExtension/Block/Adminhtml/Setup.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,16 @@ public function fetchPixelId($storeId)
119119
*
120120
* @param int $storeId
121121
* @return string|null
122-
* @throws \Magento\Framework\Exception\NoSuchEntityException
123122
*/
124123
public function getExternalBusinessId($storeId)
125124
{
126-
return $this->fbeHelper->getFBEExternalBusinessId($storeId);
125+
$storedExternalId = $this->systemConfig->getExternalBusinessId($storeId);
126+
if ($storedExternalId) {
127+
return $storedExternalId;
128+
}
129+
$storeId = $this->fbeHelper->getStore()->getId();
130+
$this->fbeHelper->log("Store id---" . $storeId);
131+
return uniqid('fbe_magento_' . $storeId . '_');
127132
}
128133

129134
/**
@@ -147,6 +152,8 @@ public function getCleanCacheAjaxRoute()
147152
}
148153

149154
/**
155+
* Get Delete Asset IDs Ajax Route
156+
*
150157
* @return mixed
151158
*/
152159
public function getDeleteAssetIdsAjaxRoute()

app/code/Meta/BusinessExtension/Block/Adminhtml/System/Config/ModuleInfo.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function render(AbstractElement $element)
6767
*
6868
* @param AbstractElement $element
6969
* @return string
70+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
7071
*/
7172
protected function _getElementHtml(AbstractElement $element)
7273
{
@@ -84,6 +85,8 @@ public function getModuleVersion()
8485
}
8586

8687
/**
88+
* Retrieve Store Id
89+
*
8790
* @return mixed
8891
*/
8992
private function getStoreId()
@@ -92,6 +95,8 @@ private function getStoreId()
9295
}
9396

9497
/**
98+
* Retrieve Commerce Account Id
99+
*
95100
* @return string
96101
*/
97102
public function getCommerceAccountId()
@@ -100,6 +105,8 @@ public function getCommerceAccountId()
100105
}
101106

102107
/**
108+
* Retrieve Page Id
109+
*
103110
* @return string
104111
*/
105112
public function getPageId()
@@ -108,6 +115,8 @@ public function getPageId()
108115
}
109116

110117
/**
118+
* Retrieve Catalog Id
119+
*
111120
* @return string
112121
*/
113122
public function getCatalogId()
@@ -116,6 +125,8 @@ public function getCatalogId()
116125
}
117126

118127
/**
128+
* Retrieve Commerce Manager Url
129+
*
119130
* @return string
120131
*/
121132
public function getCommerceManagerUrl()
@@ -124,6 +135,8 @@ public function getCommerceManagerUrl()
124135
}
125136

126137
/**
138+
* Retrieve Catalog Manager Url
139+
*
127140
* @return string
128141
*/
129142
public function getCatalogManagerUrl()
@@ -132,6 +145,8 @@ public function getCatalogManagerUrl()
132145
}
133146

134147
/**
148+
* Retrieve Support Url
149+
*
135150
* @return string
136151
*/
137152
public function getSupportUrl()

app/code/Meta/BusinessExtension/Controller/Adminhtml/Ajax/AbstractAjax.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
use Magento\Backend\App\Action\Context;
2424
use Magento\Framework\Controller\Result\JsonFactory;
2525
use Magento\Security\Model\AdminSessionsManager;
26+
use Magento\Framework\Exception\LocalizedException;
27+
use Magento\Framework\App\Action\HttpPostActionInterface;
2628

27-
abstract class AbstractAjax extends Action
29+
abstract class AbstractAjax extends Action implements HttpPostActionInterface
2830
{
2931
/**
3032
* @var JsonFactory
@@ -37,6 +39,8 @@ abstract class AbstractAjax extends Action
3739
private $fbeHelper;
3840

3941
/**
42+
* Construct
43+
*
4044
* @param Context $context
4145
* @param JsonFactory $resultJsonFactory
4246
* @param FBEHelper $fbeHelper
@@ -51,9 +55,16 @@ public function __construct(
5155
$this->fbeHelper = $fbeHelper;
5256
}
5357

58+
/**
59+
* Execute for json
60+
*
61+
* @return array
62+
*/
5463
abstract public function executeForJson();
5564

5665
/**
66+
* Execute function
67+
*
5768
* @throws Exception
5869
*/
5970
public function execute()
@@ -64,16 +75,16 @@ public function execute()
6475
->createObject(AdminSessionsManager::class)
6576
->getCurrentSession();
6677
if (!$adminSession && $adminSession->getStatus() != 1) {
67-
throw new Exception('Oops, this endpoint is for logged in admin and ajax only!');
78+
throw new LocalizedException(__('Oops, this endpoint is for logged in admin and ajax only!'));
6879
} else {
6980
try {
7081
$json = $this->executeForJson();
7182
return $result->setData($json);
7283
} catch (Exception $e) {
7384
$this->fbeHelper->logCritical($e->getMessage());
74-
throw new Exception(
75-
'Oops, there was error while processing your request.' .
76-
' Please contact admin for more details.'
85+
throw new LocalizedException(
86+
__('Oops, there was error while processing your request.' .
87+
' Please contact admin for more details.')
7788
);
7889
}
7990
}

app/code/Meta/BusinessExtension/Controller/Adminhtml/Ajax/CleanCache.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public function __construct(
5353
$this->systemConfig = $systemConfig;
5454
}
5555

56+
/**
57+
* @inheritDoc
58+
*/
5659
public function executeForJson()
5760
{
5861
try {

app/code/Meta/BusinessExtension/Controller/Adminhtml/Ajax/Fbaamsettings.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
use Magento\Framework\Controller\Result\JsonFactory;
2222
use Meta\BusinessExtension\Helper\FBEHelper;
2323

24-
/**
25-
* @SuppressWarnings(PHPMD)
26-
*/
2724
class Fbaamsettings extends AbstractAjax
2825
{
2926
/**

app/code/Meta/BusinessExtension/Controller/Adminhtml/Ajax/Fbdeleteasset.php

Lines changed: 114 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,66 +17,163 @@
1717

1818
namespace Meta\BusinessExtension\Controller\Adminhtml\Ajax;
1919

20-
use Magento\Backend\App\Action\Context;
2120
use Magento\Framework\Controller\Result\JsonFactory;
21+
use Magento\Framework\Exception\LocalizedException;
2222
use Meta\BusinessExtension\Helper\FBEHelper;
23+
use Magento\Framework\App\Action\HttpDeleteActionInterface;
24+
use Magento\Framework\App\RequestInterface;
25+
use Magento\Security\Model\AdminSessionsManager;
26+
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
27+
use Magento\Framework\App\ResourceConnection;
2328

24-
class Fbdeleteasset extends AbstractAjax
29+
class Fbdeleteasset implements HttpDeleteActionInterface
2530
{
26-
public const DELETE_SUCCESS_MESSAGE = "You have successfully deleted Meta Business Extension.
27-
The pixel installed on your website is now deleted.";
31+
public const DELETE_SUCCESS_MESSAGE = "You have successfully deleted Meta Business Extension." .
32+
" The pixel installed on your website is now deleted.";
2833

2934
private const DELETE_FAILURE_MESSAGE = "There was a problem deleting the connection.
30-
Please try again.";
35+
Please try again.";
36+
37+
/**
38+
* @var JsonFactory
39+
*/
40+
private $resultJsonFactory;
3141

3242
/**
3343
* @var FBEHelper
3444
*/
3545
private $fbeHelper;
3646

3747
/**
38-
* Construct
39-
*
40-
* @param Context $context
48+
* @var AdminSessionsManager
49+
*/
50+
private $adminSessionManager;
51+
52+
/**
53+
* @var ResourceConnection
54+
*/
55+
private $resourceConnection;
56+
57+
/**
58+
* @var SystemConfig
59+
*/
60+
private $systemConfig;
61+
62+
/**
63+
* @var RequestInterface
64+
*/
65+
private RequestInterface $request;
66+
67+
/**
4168
* @param JsonFactory $resultJsonFactory
4269
* @param FBEHelper $fbeHelper
70+
* @param AdminSessionsManager $adminSessionManager
71+
* @param ResourceConnection $resourceConnection
72+
* @param SystemConfig $systemConfig
73+
* @param RequestInterface $request
4374
*/
4475
public function __construct(
45-
Context $context,
4676
JsonFactory $resultJsonFactory,
47-
FBEHelper $fbeHelper
77+
FBEHelper $fbeHelper,
78+
AdminSessionsManager $adminSessionManager,
79+
ResourceConnection $resourceConnection,
80+
SystemConfig $systemConfig,
81+
RequestInterface $request
4882
) {
49-
parent::__construct($context, $resultJsonFactory, $fbeHelper);
83+
$this->resultJsonFactory = $resultJsonFactory;
5084
$this->fbeHelper = $fbeHelper;
85+
$this->adminSessionManager = $adminSessionManager;
86+
$this->resourceConnection = $resourceConnection;
87+
$this->systemConfig = $systemConfig;
88+
$this->request = $request;
89+
}
90+
91+
/**
92+
* Execute
93+
*
94+
* @throws LocalizedException
95+
*/
96+
public function execute()
97+
{
98+
$result = $this->resultJsonFactory->create();
99+
// TODO : Move all String objects to constants.
100+
$adminSession = $this->adminSessionManager->getCurrentSession();
101+
if (!$adminSession && $adminSession->getStatus() != 1) {
102+
throw new LocalizedException('Oops, this endpoint is for logged in admin and ajax only!');
103+
} else {
104+
try {
105+
$json = $this->executeForJson();
106+
return $result->setData($json);
107+
} catch (\Exception $e) {
108+
$this->fbeHelper->logCritical($e->getMessage());
109+
throw new LocalizedException(
110+
'Oops, there was error while processing your request.' .
111+
' Please contact admin for more details.'
112+
);
113+
}
114+
}
51115
}
52116

53117
/**
54118
* Execute for json
55119
*
56-
* @return array
120+
* Run actual processing after request validation.
121+
* Only public to allow for more direct unit testing.
122+
*
123+
* @inheritdoc
57124
*/
58125
public function executeForJson()
59126
{
60-
$storeId = $this->getRequest()->getParam('storeId');
127+
$storeId = $this->request->getParam('storeId');
61128
if ($storeId === null) {
62129
return [
63130
'success' => false,
64-
'error_message' => __(self::DELETE_FAILURE_MESSAGE)
131+
'error_message' => __('' . self::DELETE_FAILURE_MESSAGE)
65132
];
66133
}
67134
try {
68-
$this->fbeHelper->deleteConfigKeys($storeId);
135+
$this->deleteConfigKeys($storeId);
69136
$response = [
70137
'success' => true,
71-
'message' => __(self::DELETE_SUCCESS_MESSAGE),
138+
'message' => __('' . self::DELETE_SUCCESS_MESSAGE),
72139
];
73140
} catch (\Exception $e) {
74141
$this->fbeHelper->log($e->getMessage());
75142
$response = [
76143
'success' => false,
77-
'error_message' => __(self::DELETE_FAILURE_MESSAGE),
144+
'error_message' => __(
145+
"There was a problem deleting the connection. Please try again."
146+
),
78147
];
79148
}
80149
return $response;
81150
}
151+
152+
/**
153+
* Delete config keys
154+
*
155+
* @param string|int|null $storeId Store ID to delete from.
156+
* @return array
157+
*/
158+
private function deleteConfigKeys($storeId)
159+
{
160+
$this->systemConfig->deleteConfig(
161+
SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_EXTERNAL_BUSINESS_ID,
162+
$storeId
163+
)
164+
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_INSTALLED, $storeId)
165+
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_ACCESS_TOKEN, $storeId)
166+
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_PAGE_ID, $storeId)
167+
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_PAGE_ACCESS_TOKEN, $storeId)
168+
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_COMMERCE_ACCOUNT_ID, $storeId)
169+
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_PIXEL_ID, $storeId)
170+
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_PIXEL_AAM_SETTINGS, $storeId)
171+
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_PROFILES, $storeId)
172+
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_CATALOG_ID, $storeId)
173+
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_FEED_ID, $storeId)
174+
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_API_VERSION, $storeId)
175+
->deleteConfig(SystemConfig::XML_PATH_FACEBOOK_BUSINESS_EXTENSION_API_VERSION_LAST_UPDATE, $storeId);
176+
177+
return $this;
178+
}
82179
}

app/code/Meta/BusinessExtension/Controller/Adminhtml/Ajax/Fbpixel.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
use Magento\Store\Model\ScopeInterface;
2424
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
2525

26-
/**
27-
* @SuppressWarnings(PHPMD)
28-
*/
2926
class Fbpixel extends AbstractAjax
3027
{
3128
/**

app/code/Meta/BusinessExtension/Controller/Adminhtml/Ajax/Fbprofiles.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
2424
use Magento\Store\Model\ScopeInterface;
2525

26-
/**
27-
* @SuppressWarnings(PHPMD)
28-
*/
2926
class Fbprofiles extends AbstractAjax
3027
{
3128
/**

app/code/Meta/BusinessExtension/Controller/Adminhtml/Ajax/Fbtoken.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
2424
use Magento\Store\Model\ScopeInterface;
2525

26-
/**
27-
* @SuppressWarnings(PHPMD)
28-
*/
2926
class Fbtoken extends AbstractAjax
3027
{
3128
/**

0 commit comments

Comments
 (0)