-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTrackingCodeTags.php
More file actions
61 lines (47 loc) · 1.62 KB
/
TrackingCodeTags.php
File metadata and controls
61 lines (47 loc) · 1.62 KB
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
<?php
namespace Simonridley\TrackingCodeManager;
use Illuminate\Support\Facades\Request;
use Statamic\Facades\YAML;
use Statamic\Tags\Tags;
use Illuminate\Support\Arr;
class TrackingCodeTags extends Tags
{
protected $channels = ['head', 'body', 'footer'];
protected static $handle = 'tracking_code_manager';
public function wildcard($tag)
{
if ($this->isSupportedChannel($tag)) {
return $this->createLink($tag);
}
}
public function createLink(string $tag): string
{
$values = collect(YAML::file(__DIR__.'/../content/tracking-codes.yaml')->parse())
->merge(YAML::file(base_path('content/tracking-codes.yaml'))->parse())
->all();
if(!isset($values['enabled']) || $values['enabled'] == false || !isset($values['scripts'])){
return '';
}
$filtered = Arr::where($values['scripts'], function ($value, $key) use($tag) {
return $value['position'] == $tag && $value['enabled'] == true;
});
$scripts = Arr::pluck($filtered, 'script');
$output = '<!-- tracking code manager '.$tag.' scripts -->';
foreach($scripts as $script){
if (is_array($script)) {
$script = $script['code'];
}
$output .= $script;
}
$output .= '<!-- end tracking code manager '.$tag.' scripts -->';
return $output;
return '';
}
protected function isSupportedChannel(string $channel): bool
{
if (in_array($channel, $this->channels)) {
return true;
}
return false;
}
}