1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Plugin Name: WP Mautic |
4
|
|
|
* Plugin URI: https://github.com/mautic/mautic-wordpress |
5
|
|
|
* Description: This plugin will allow you to add Mautic (Free Open Source Marketing Automation) tracking to your site |
6
|
|
|
* Version: 2.1.0 |
7
|
|
|
* Author: Mautic community |
8
|
|
|
* Author URI: http://mautic.org |
9
|
|
|
* Text Domain: wp-mautic |
10
|
|
|
* License: GPL2 |
11
|
|
|
* |
12
|
|
|
* @package wp-mautic |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
// Prevent direct access to this file. |
16
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
17
|
|
|
header( 'HTTP/1.0 403 Forbidden' ); |
18
|
|
|
echo 'This file should not be accessed directly!'; |
19
|
|
|
exit; // Exit if accessed directly. |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
// Store plugin directory. |
23
|
|
|
define( 'VPMAUTIC_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
24
|
|
|
// Store plugin main file path. |
25
|
|
|
define( 'VPMAUTIC_PLUGIN_FILE', __FILE__ ); |
26
|
|
|
|
27
|
|
|
add_action( 'admin_menu', 'wpmautic_settings' ); |
28
|
|
|
add_action( 'plugins_loaded', 'wpmautic_injector' ); |
29
|
|
|
|
30
|
|
|
include_once( VPMAUTIC_PLUGIN_DIR . '/shortcodes.php' ); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Declare option page |
34
|
|
|
*/ |
35
|
|
|
function wpmautic_settings() { |
36
|
1 |
|
include_once( VPMAUTIC_PLUGIN_DIR . '/options.php' ); |
37
|
|
|
|
38
|
1 |
|
add_options_page( |
39
|
1 |
|
__( 'WP Mautic Settings', 'wp-mautic' ), |
40
|
1 |
|
__( 'WPMautic', 'wp-mautic' ), |
41
|
1 |
|
'manage_options', |
42
|
1 |
|
'wpmautic', |
43
|
1 |
|
'wpmautic_options_page' |
44
|
|
|
); |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Settings Link in the ``Installed Plugins`` page |
49
|
|
|
* |
50
|
|
|
* @param array $links array of plugin action links. |
51
|
|
|
* @param string $file Path to the plugin file relative to the plugins directory. |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
function wpmautic_plugin_actions( $links, $file ) { |
56
|
1 |
|
if ( plugin_basename( VPMAUTIC_PLUGIN_FILE ) === $file && function_exists( 'admin_url' ) ) { |
57
|
1 |
|
$settings_link = sprintf( |
58
|
1 |
|
'<a href="%s">%s</a>', |
59
|
1 |
|
admin_url( 'options-general.php?page=wpmautic' ), |
60
|
1 |
|
__( 'Settings' ) |
61
|
|
|
); |
62
|
|
|
// Add the settings link before other links. |
63
|
1 |
|
array_unshift( $links, $settings_link ); |
64
|
|
|
} |
65
|
1 |
|
return $links; |
66
|
|
|
} |
67
|
|
|
add_filter( 'plugin_action_links', 'wpmautic_plugin_actions', 10, 2 ); |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Retrieve one of the wpmautic options but sanitized |
71
|
|
|
* |
72
|
|
|
* @param string $option Option name to be retrieved (base_url, script_location). |
73
|
|
|
* @param mixed $default Default option value return if not exists. |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
* |
77
|
|
|
* @throws InvalidArgumentException Thrown when the option name is not given. |
78
|
|
|
*/ |
79
|
|
|
function wpmautic_option( $option, $default = null ) { |
80
|
43 |
|
$options = get_option( 'wpmautic_options' ); |
81
|
|
|
|
82
|
|
|
switch ( $option ) { |
83
|
43 |
|
case 'script_location': |
84
|
12 |
|
return ! isset( $options[ $option ] ) ? 'header' : $options[ $option ]; |
85
|
39 |
|
case 'fallback_activated': |
86
|
13 |
|
return isset( $options[ $option ] ) ? (bool) $options[ $option ] : true; |
87
|
|
|
default: |
88
|
34 |
|
if ( ! isset( $options[ $option ] ) ) { |
89
|
9 |
|
if ( isset( $default ) ) { |
90
|
8 |
|
return $default; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
throw new InvalidArgumentException( 'You must give a valid option name !' ); |
94
|
|
|
} |
95
|
|
|
|
96
|
25 |
|
return $options[ $option ]; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Apply JS tracking to the right place depending script_location. |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
function wpmautic_injector() { |
106
|
7 |
|
$script_location = wpmautic_option( 'script_location' ); |
107
|
7 |
|
if ( 'header' === $script_location ) { |
108
|
5 |
|
add_action( 'wp_head', 'wpmautic_inject_script' ); |
109
|
|
|
} else { |
110
|
2 |
|
add_action( 'wp_footer', 'wpmautic_inject_script' ); |
111
|
|
|
} |
112
|
|
|
|
113
|
7 |
|
if ( true === wpmautic_option( 'fallback_activated', false ) ) { |
114
|
6 |
|
add_action( 'wp_footer', 'wpmautic_inject_noscript' ); |
115
|
|
|
} |
116
|
7 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Writes Tracking JS to the HTML source |
120
|
|
|
* |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
|
function wpmautic_inject_script() { |
124
|
6 |
|
$base_url = wpmautic_option( 'base_url', '' ); |
125
|
6 |
|
if ( empty( $base_url ) ) { |
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
|
129
|
6 |
|
$attrs = wpmautic_get_tracking_attributes(); |
130
|
|
|
|
131
|
|
|
?><script type="text/javascript"> |
132
|
|
|
(function(w,d,t,u,n,a,m){w['MauticTrackingObject']=n; |
133
|
|
|
w[n]=w[n]||function(){(w[n].q=w[n].q||[]).push(arguments)},a=d.createElement(t), |
134
|
|
|
m=d.getElementsByTagName(t)[0];a.async=1;a.src=u;m.parentNode.insertBefore(a,m) |
135
|
|
|
})(window,document,'script','<?php echo esc_url( $base_url ); ?>/mtc.js','mt'); |
136
|
|
|
|
137
|
|
|
mt('send', 'pageview'<?php echo count( $attrs ) > 0?', ' . wp_json_encode( $attrs ):'' ?>); |
138
|
|
|
</script> |
139
|
|
|
<?php |
140
|
6 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Writes Tracking image fallback to the HTML source |
144
|
|
|
* This is a separated function because <noscript> tags are not allowed in header ! |
145
|
|
|
* |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
function wpmautic_inject_noscript() { |
149
|
6 |
|
$base_url = wpmautic_option( 'base_url', '' ); |
150
|
6 |
|
if ( empty( $base_url ) ) { |
151
|
|
|
return; |
152
|
|
|
} |
153
|
|
|
|
154
|
6 |
|
$url_query = wpmautic_get_url_query(); |
155
|
6 |
|
$payload = rawurlencode( base64_encode( serialize( $url_query ) ) ); |
156
|
|
|
?> |
157
|
6 |
|
<noscript> |
158
|
|
|
<img src="<?php echo esc_url( $base_url ); ?>/mtracking.gif?d=<?php echo esc_attr( $payload ); ?>" style="display:none;" alt="" /> |
159
|
|
|
</noscript> |
160
|
|
|
<?php |
161
|
6 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Builds and returns additional data for URL query |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
function wpmautic_get_url_query() { |
169
|
10 |
|
global $wp; |
|
|
|
|
170
|
10 |
|
$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) ); |
171
|
|
|
|
172
|
10 |
|
$attrs = wpmautic_get_tracking_attributes(); |
173
|
|
|
|
174
|
10 |
|
$attrs['language'] = get_locale(); |
175
|
10 |
|
$attrs['page_url'] = $current_url; |
176
|
10 |
|
$attrs['page_title'] = function_exists( 'wp_get_document_title' ) |
177
|
10 |
|
? wp_get_document_title() |
178
|
|
|
: wp_title( '»', false ); |
179
|
10 |
|
$attrs['referrer'] = function_exists( 'wp_get_raw_referer' ) |
180
|
10 |
|
? wp_get_raw_referer() |
181
|
|
|
: null; |
182
|
10 |
|
if ( false === $attrs['referrer'] ) { |
183
|
8 |
|
$attrs['referrer'] = $current_url; |
184
|
|
|
} |
185
|
|
|
|
186
|
10 |
|
return $attrs; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Create custom query parameters to be injected inside tracking |
191
|
|
|
* |
192
|
|
|
* @return array |
193
|
|
|
*/ |
194
|
|
|
function wpmautic_get_tracking_attributes() { |
195
|
14 |
|
$attrs = array(); |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Update / add data to be send withing Mautic tracker |
199
|
|
|
* |
200
|
|
|
* Default data only contains the 'language' key but every added key to the |
201
|
|
|
* array will be sent to Mautic. |
202
|
|
|
* |
203
|
|
|
* @since 2.1.0 |
204
|
|
|
* |
205
|
|
|
* @param array $attrs Attributes to be filters, default ['language' => get_locale()] |
206
|
|
|
*/ |
207
|
14 |
|
return apply_filters( 'wpmautic_tracking_attributes', $attrs ); |
208
|
|
|
} |
209
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.