-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.php
159 lines (146 loc) · 4.2 KB
/
plugin.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
<?php
/**
* Plugin.php
*
* This file is part of the Xpressengine package.
*
* PHP version 7
*
* @category GoogleAnalytics
* @package Xpressengine\Plugins\GoogleAnalytics
* @author XE Developers <developers@xpressengine.com>
* @copyright 2019 Copyright XEHub Corp. <https://www.xehub.io>
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL
* @link https://xpressengine.io
*/
namespace Xpressengine\Plugins\GoogleAnalytics;
use Illuminate\Support\Facades\Artisan;
use Route;
use Validator;
use View;
use XeFrontend;
use XeRegister;
use Xpressengine\Plugin\AbstractPlugin;
use Xpressengine\Translation\Translator;
/**
* Plugin
*
* @category GoogleAnalytics
* @package Xpressengine\Plugins\GoogleAnalytics
* @author XE Developers <developers@xpressengine.com>
* @copyright 2019 Copyright XEHub Corp. <https://www.xehub.io>
* @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html LGPL
* @link https://xpressengine.io
*/
class Plugin extends AbstractPlugin
{
/**
* boot
*
* @return void
*/
public function boot()
{
Validator::extend('ga_json', function ($attribute, $value) {
return 'json' === $value->getClientOriginalExtension();
});
Validator::replacer('ga_json', function ($message, $attribute, $rule, $parameters) {
return xe_trans('validation.mimes', ['attribute' => $attribute, 'values' => 'json']);
});
View::addNamespace('ga', __DIR__ . '/views');
Translator::alias('google_analytics', 'ga');
$this->routes();
$this->intercepts();
}
/**
* register
*
* @return void
*/
public function register()
{
app()->singleton(Handler::class, function ($app) {
return new Handler(
$app,
new Setting($app['xe.config'], $app['xe.storage'], $app['xe.keygen'])
);
}, true);
app()->alias(Handler::class, 'xe.ga');
}
/**
* get setting uri
*
* @return string
*/
public function getSettingsURI()
{
return route('ga::setting.edit');
}
/**
* add route
*
* @return void
*/
private function routes()
{
Route::group(
['namespace' => 'Xpressengine\\Plugins\\GoogleAnalytics\\Controllers', 'as' => 'ga::'],
function () {
require plugins_path('google_analytics/routes.php');
}
);
}
/**
* register intercept
*
* @return void
*/
private function intercepts()
{
intercept(
'Presenter@make',
'googleAnalytics.addScript',
function ($target, $id, $data = [], $mergeData = [], $html = true, $api = false) {
/** @var \Illuminate\Routing\Route $route */
$route = app('router')->current();
if ($route && in_array('settings', $route->middleware()) === false) {
$setting = app('xe.ga')->getSetting();
if ($setting->get('trackingId')) {
XeFrontend::html('ga:tracking')->content(
$this->getTrackingCode($setting->get('trackingId'), $setting->get('domain', 'auto'),$setting->get('allClick'))
)->load();
}
}
return $target($id, $data, $mergeData, $html, $api);
}
);
}
/**
* get tracking code
*
* @param string $trackingId tracking id
* @param string $domain domain
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
private function getTrackingCode($trackingId, $domain = 'auto', $allClick = null)
{
return view('ga::tracking', compact('trackingId', 'domain','allClick'));
}
/**
* uninstall
*
* @return void
*/
public function uninstall()
{
app('xe.ga')->getSetting()->destroy();
}
public function install()
{
Artisan::call('translation:import',[
'name'=>'google_analytics',
'--path' => str_replace(base_path(),'.',self::path('langs/lang.php'))
]);
}
}