forked from PrestaShop/ps_sharebuttons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps_sharebuttons.php
204 lines (171 loc) · 7.43 KB
/
ps_sharebuttons.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* 2007-2016 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2016 PrestaShop SA
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_')) {
exit;
}
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
class Ps_Sharebuttons extends Module implements WidgetInterface
{
protected static $networks = ['Facebook', 'Twitter', 'Pinterest'];
private $templateFile;
public function __construct()
{
$this->name = 'ps_sharebuttons';
$this->author = 'PrestaShop';
$this->version = '2.0.1';
$this->need_instance = 0;
$this->ps_versions_compliancy = ['min' => '1.7.1.0', 'max' => _PS_VERSION_];
$this->_directory = dirname(__FILE__);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->trans('Social media share buttons', [], 'Modules.Sharebuttons.Admin');
$this->description = $this->trans('Displays social media sharing buttons (Twitter, Facebook and Pinterest) on every product page.', [], 'Modules.Sharebuttons.Admin');
$this->templateFile = 'module:ps_sharebuttons/views/templates/hook/ps_sharebuttons.tpl';
}
public function install()
{
return parent::install()
&& Configuration::updateValue('PS_SC_TWITTER', 1)
&& Configuration::updateValue('PS_SC_FACEBOOK', 1)
&& Configuration::updateValue('PS_SC_PINTEREST', 1)
&& $this->registerHook('displayProductButtons')
;
}
public function getConfigFieldsValues()
{
$values = [];
foreach (self::$networks as $network) {
$values['PS_SC_'.Tools::strtoupper($network)] = (int) Tools::getValue('PS_SC_'.Tools::strtoupper($network), Configuration::get('PS_SC_'.Tools::strtoupper($network)));
}
return $values;
}
public function getContent()
{
$output = '';
if (Tools::isSubmit('submitSocialSharing')) {
foreach (self::$networks as $network) {
Configuration::updateValue('PS_SC_'.Tools::strtoupper($network), (int) Tools::getValue('PS_SC_'.Tools::strtoupper($network)));
}
$this->_clearCache($this->templateFile);
$output .= $this->displayConfirmation($this->trans('Settings updated.', [], 'Admin.Notifications.Success'));
Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true).'&conf=6&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name);
}
$helper = new HelperForm();
$helper->submit_action = 'submitSocialSharing';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = ['fields_value' => $this->getConfigFieldsValues()];
$fields = [];
foreach (self::$networks as $network) {
$fields[] = [
'type' => 'switch',
'label' => $network,
'name' => 'PS_SC_'.Tools::strtoupper($network),
'values' => [
[
'id' => Tools::strtolower($network).'_active_on',
'value' => 1,
'label' => $this->trans('Enabled', [], 'Admin.Global'),
],
[
'id' => Tools::strtolower($network).'_active_off',
'value' => 0,
'label' => $this->trans('Disabled', [], 'Admin.Global'),
],
),
);
}
return $output.$helper->generateForm([
[
'form' => [
'legend' => [
'title' => $this->displayName,
'icon' => 'icon-share',
],
'input' => $fields,
'submit' => [
'title' => $this->trans('Save', [], 'Admin.Actions'),
],
],
],
]);
}
public function renderWidget($hookName, array $params)
{
$key = 'ps_sharebuttons|' . $params['product']['id_product'];
if (!empty($params['product']['id_product_attribute'])) {
$key .= '|' . $params['product']['id_product_attribute'];
}
if (!$this->isCached($this->templateFile, $this->getCacheId($key))) {
$this->smarty->assign($this->getWidgetVariables($hookName, $params));
}
return $this->fetch($this->templateFile, $this->getCacheId($key));
}
public function getWidgetVariables($hookName, array $params)
{
if (!method_exists($this->context->controller, 'getProduct')) {
return;
}
$product = $this->context->controller->getProduct();
if (!Validate::isLoadedObject($product)) {
return;
}
$social_share_links = [];
$sharing_url = addcslashes($this->context->link->getProductLink($product), "'");
$sharing_name = addcslashes($product->name, "'");
$image_cover_id = $product->getCover($product->id);
if (is_array($image_cover_id) && isset($image_cover_id['id_image'])) {
$image_cover_id = (int) $image_cover_id['id_image'];
} else {
$image_cover_id = 0;
}
$sharing_img = addcslashes($this->context->link->getImageLink($product->link_rewrite, $image_cover_id), "'");
if (Configuration::get('PS_SC_FACEBOOK')) {
$social_share_links['facebook'] = [
'label' => $this->trans('Share', [], 'Modules.Sharebuttons.Shop'),
'class' => 'facebook',
'url' => 'http://www.facebook.com/sharer.php?u='.$sharing_url,
];
}
if (Configuration::get('PS_SC_TWITTER')) {
$social_share_links['twitter'] = [
'label' => $this->trans('Tweet', [], 'Modules.Sharebuttons.Shop'),
'class' => 'twitter',
'url' => 'https://twitter.com/intent/tweet?text='.$sharing_name.' '.$sharing_url,
];
}
if (Configuration::get('PS_SC_PINTEREST')) {
$social_share_links['pinterest'] = [
'label' => $this->trans('Pinterest', [], 'Modules.Sharebuttons.Shop'),
'class' => 'pinterest',
'url' => 'http://www.pinterest.com/pin/create/button/?media='.$sharing_img.'&url='.$sharing_url,
];
}
return [
'social_share_links' => $social_share_links,
];
}
}