Skip to content
This repository was archived by the owner on Sep 1, 2021. It is now read-only.

Commit e951e5c

Browse files
committed
Feature: Make settings and config better for public
This removes dotenv as a depedency for development. The plugin now uses admin settings and a config file (basically makes it more crafty).
1 parent dbea0b0 commit e951e5c

8 files changed

+78
-12
lines changed

FbPixelPlugin.php

+42
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ class FbPixelPlugin extends BasePlugin
55
{
66
public function init()
77
{
8+
if (
9+
craft()->config->get('noop', 'fbpixel') ||
10+
empty($this->getPixelId())
11+
) {
12+
return;
13+
}
14+
815
craft()->fbPixel->listen();
916
}
1017

@@ -18,6 +25,11 @@ public function getDescription()
1825
return Craft::t('Integrates Facebook Pixel with Craft Commerce');
1926
}
2027

28+
public function getDocumentationUrl()
29+
{
30+
return 'https://github.com/moment-inc/craft-fbpixel/wiki';
31+
}
32+
2133
public function getVersion()
2234
{
2335
return '0.0.1';
@@ -32,4 +44,34 @@ public function getDeveloperUrl()
3244
{
3345
return 'http://github.com/Moment-Inc';
3446
}
47+
48+
public function getCpAlerts($path, $fetch)
49+
{
50+
if (empty($this->getPixelId())) {
51+
return ["Enter your pixel id <a href='{$this->getSettingsUrl()}'>here</a> for fbpixel to work."];
52+
}
53+
}
54+
55+
public function getSettingsHtml()
56+
{
57+
return craft()->templates->render('fbpixel/settings', [
58+
'settings' => $this->getSettings()
59+
]);
60+
}
61+
62+
public function getPixelId()
63+
{
64+
if (!empty(craft()->config->get('pixelId', 'fbpixel'))) {
65+
return craft()->config->get('pixelId', 'fbpixel');
66+
}
67+
68+
return $this->getSettings()->pixelId;
69+
}
70+
71+
protected function defineSettings()
72+
{
73+
return [
74+
'pixelId' => array(AttributeType::String),
75+
];
76+
}
3577
}

config.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* FB Pixel Configuration
4+
*
5+
* Completely optional configuration settings for FB Pixel if you want specific control over things.
6+
*
7+
* Don't edit this file, instead copy it to 'craft/config' as 'fbpixel.php' and make
8+
* your changes there.
9+
*/
10+
11+
return [
12+
/**
13+
* fbpixel will by default render on any environment. If you want to shut it off then you can override it per environment (if you have multi environment configs setup).
14+
*/
15+
'noop' => false,
16+
17+
/**
18+
* you have to provide a pixel id in the admin. We give you the option to override that here. Mainly, because we have a test pixel id that we use.
19+
*/
20+
'pixelId' => '',
21+
];

services/FbPixelService.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ class FbPixelService extends BaseApplicationComponent
1111
*/
1212
public function listen()
1313
{
14-
if (getenv('FB_PIXEL_NOOP')) {
15-
return;
16-
}
17-
1814
craft()->templates->hook('fbPixel.renderBase', [
1915
$this, 'renderBase'
2016
]);
@@ -32,7 +28,7 @@ public function listen()
3228
public function renderBase()
3329
{
3430
return $this->renderTemplate('base', [
35-
'fbPixelId' => getenv('FB_PIXEL_ID')
31+
'fbPixelId' => craft()->plugins->getPlugin('fbpixel')->getPixelId(),
3632
]);
3733
}
3834

services/FbPixel_AddPaymentInfoService.php

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class FbPixel_AddPaymentInfoService extends BaseApplicationComponent
77
public function render()
88
{
99
$cart = craft()->commerce_cart->getCart();
10-
1110
$eventData = [
1211
'value' => $cart->totalPrice,
1312
'currency' => 'USD',

services/FbPixel_InitiateCheckoutService.php

-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ public function listen()
1414
public function render()
1515
{
1616
$cart = craft()->commerce_cart->getCart();
17-
1817
$addPaymentInfoRaw = craft()->fbPixel_addPaymentInfo->render();
19-
2018
$item = [
2119
'content_name' => 'Checkout',
2220
'content_ids' => array_map(function($i) { return $i->sku; }, $cart->lineItems),
@@ -26,7 +24,6 @@ public function render()
2624
'currency' => 'USD'
2725
];
2826

29-
3027
return $addPaymentInfoRaw . craft()->fbPixel->renderEvent('InitiateCheckout', $item);
3128
}
3229
}

services/FbPixel_PurchaseService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function checkFlash()
2929

3030
public function addHook()
3131
{
32-
craft()->templates->hook('fbPixel.renderPurchase', [
32+
craft()->templates->hook('fbPixel.renderBase', [
3333
$this, 'renderTemplate'
3434
]);
3535
}

services/FbPixel_ViewContentService.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ class FbPixel_ViewContentService extends BaseApplicationComponent
66
{
77
public function listen()
88
{
9-
craft()->templates->hook('fbPixel.renderViewContent', [
9+
craft()->templates->hook('fbPixel.renderBase', [
1010
$this, 'render'
1111
]);
1212
}
1313

1414
public function render(&$context)
1515
{
16-
return craft()->fbPixel->renderEvent('ViewContent', $context['fbPixelItem']);
16+
if (!empty($context['fbPixelItem'])) {
17+
return craft()->fbPixel->renderEvent('ViewContent', $context['fbPixelItem']);
18+
}
1719
}
1820
}

templates/settings.twig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div class="field">
2+
<div class="heading">
3+
<label>Pixel ID</label>
4+
</div>
5+
6+
<div class="input ltr">
7+
<input class="text code fullwidth" type="text" name="pixelId" value="{{ settings.pixelId }}">
8+
</div>
9+
</div>

0 commit comments

Comments
 (0)