1 | <?php |
||
2 | /** |
||
3 | * Plugin Name: WP Mautic |
||
4 | * Plugin URI: https://github.com/mautic/mautic-wordpress |
||
5 | * Contributors: mautic,hideokamoto,shulard,escopecz,dbhurley,macbookandrew |
||
6 | * Description: This plugin will allow you to add Mautic (Free Open Source Marketing Automation) tracking to your site |
||
7 | * Version: 2.4.2 |
||
8 | * Requires at least: 4.6 |
||
9 | * Tested up to: 5.7 |
||
10 | * Author: Mautic community |
||
11 | * Author URI: http://mautic.org |
||
12 | * Text Domain: wp-mautic |
||
13 | * License: GPLv2 or later |
||
14 | * License URI: http://www.gnu.org/licenses/gpl-2.0.html |
||
15 | * |
||
16 | * @package wp-mautic |
||
17 | */ |
||
18 | |||
19 | // Prevent direct access to this file. |
||
20 | if ( ! defined( 'ABSPATH' ) ) { |
||
21 | header( 'HTTP/1.0 403 Forbidden' ); |
||
22 | echo 'This file should not be accessed directly!'; |
||
23 | exit; // Exit if accessed directly. |
||
24 | } |
||
25 | |||
26 | // Store plugin directory. |
||
27 | define( 'VPMAUTIC_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
||
28 | // Store plugin main file path. |
||
29 | define( 'VPMAUTIC_PLUGIN_FILE', __FILE__ ); |
||
30 | |||
31 | add_action( 'admin_menu', 'wpmautic_settings' ); |
||
32 | add_action( 'plugins_loaded', 'wpmautic_injector' ); |
||
33 | |||
34 | require_once VPMAUTIC_PLUGIN_DIR . '/shortcodes.php'; |
||
35 | |||
36 | /** |
||
37 | * Declare option page |
||
38 | */ |
||
39 | function wpmautic_settings() { |
||
40 | 1 | include_once VPMAUTIC_PLUGIN_DIR . '/options.php'; |
|
41 | |||
42 | 1 | add_options_page( |
|
43 | 1 | __( 'WP Mautic Settings', 'wp-mautic' ), |
|
44 | 1 | __( 'WPMautic', 'wp-mautic' ), |
|
45 | 1 | 'manage_options', |
|
46 | 1 | 'wpmautic', |
|
47 | 1 | 'wpmautic_options_page' |
|
48 | ); |
||
49 | 1 | } |
|
50 | |||
51 | /** |
||
52 | * Settings Link in the ``Installed Plugins`` page |
||
53 | * |
||
54 | * @param array $links array of plugin action links. |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | function wpmautic_plugin_actions( $links ) { |
||
59 | 1 | if ( function_exists( 'admin_url' ) ) { |
|
60 | 1 | $settings_link = sprintf( |
|
61 | 1 | '<a href="%s">%s</a>', |
|
62 | 1 | admin_url( 'options-general.php?page=wpmautic' ), |
|
63 | 1 | __( 'Settings' ) |
|
64 | ); |
||
65 | // Add the settings link before other links. |
||
66 | 1 | array_unshift( $links, $settings_link ); |
|
67 | } |
||
68 | 1 | return $links; |
|
69 | } |
||
70 | add_filter( 'plugin_action_links_' . plugin_basename( VPMAUTIC_PLUGIN_FILE ), 'wpmautic_plugin_actions', 10, 2 ); |
||
71 | |||
72 | /** |
||
73 | * Retrieve one of the wpmautic options but sanitized |
||
74 | * |
||
75 | * @param string $option Option name to be retrieved (base_url, script_location). |
||
76 | * @param mixed $default Default option value return if not exists. |
||
77 | * |
||
78 | * @return string |
||
79 | * |
||
80 | * @throws InvalidArgumentException Thrown when the option name is not given. |
||
81 | */ |
||
82 | function wpmautic_option( $option, $default = null ) { |
||
83 | 53 | $options = get_option( 'wpmautic_options' ); |
|
84 | |||
85 | 53 | switch ( $option ) { |
|
86 | 53 | case 'script_location': |
|
87 | 15 | return ! isset( $options[ $option ] ) ? 'header' : $options[ $option ]; |
|
88 | 47 | case 'fallback_activated': |
|
89 | 13 | return isset( $options[ $option ] ) ? (bool) $options[ $option ] : true; |
|
90 | 42 | case 'track_logged_user': |
|
91 | 16 | return isset( $options[ $option ] ) ? (bool) $options[ $option ] : false; |
|
92 | default: |
||
93 | 35 | if ( ! isset( $options[ $option ] ) ) { |
|
94 | 9 | if ( isset( $default ) ) { |
|
95 | 8 | return $default; |
|
96 | } |
||
97 | |||
98 | 1 | throw new InvalidArgumentException( 'You must give a valid option name !' ); |
|
99 | } |
||
100 | |||
101 | 26 | return $options[ $option ]; |
|
102 | } |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Apply JS tracking to the right place depending script_location. |
||
107 | * |
||
108 | * @return void |
||
109 | */ |
||
110 | function wpmautic_injector() { |
||
111 | 8 | $script_location = wpmautic_option( 'script_location' ); |
|
112 | 8 | if ( 'header' === $script_location ) { |
|
113 | 5 | add_action( 'wp_head', 'wpmautic_inject_script' ); |
|
114 | } else { |
||
115 | 3 | add_action( 'wp_footer', 'wpmautic_inject_script' ); |
|
116 | } |
||
117 | |||
118 | 8 | if ( 'disabled' !== $script_location && true === wpmautic_option( 'fallback_activated', false ) ) { |
|
119 | 6 | add_action( 'wp_footer', 'wpmautic_inject_noscript' ); |
|
120 | } |
||
121 | 8 | } |
|
122 | |||
123 | /** |
||
124 | * Generate the mautic script URL to be used outside of the plugin when |
||
125 | * necessary |
||
126 | * |
||
127 | * @return string |
||
128 | */ |
||
129 | function wpmautic_base_script() { |
||
130 | 7 | $base_url = wpmautic_option( 'base_url', '' ); |
|
131 | 7 | if ( empty( $base_url ) ) { |
|
132 | return; |
||
133 | } |
||
134 | |||
135 | 7 | return $base_url . '/mtc.js'; |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * Writes Tracking JS to the HTML source |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | function wpmautic_inject_script() { |
||
144 | // Load the Mautic tracking library mtc.js if it is not disabled. |
||
145 | 7 | $base_url = wpmautic_base_script(); |
|
146 | 7 | $script_location = wpmautic_option( 'script_location' ); |
|
147 | 7 | $attrs = wpmautic_get_tracking_attributes(); |
|
148 | ?> |
||
149 | 7 | <script type="text/javascript" > |
|
150 | function wpmautic_send(){ |
||
151 | if ('undefined' === typeof mt) { |
||
152 | if (console !== undefined) { |
||
153 | console.warn('WPMautic: mt not defined. Did you load mtc.js ?'); |
||
154 | } |
||
155 | return false; |
||
156 | } |
||
157 | // Add the mt('send', 'pageview') script with optional tracking attributes. |
||
158 | mt('send', 'pageview'<?php echo count( $attrs ) > 0 ? ', ' . wp_json_encode( $attrs ) : ''; ?>); |
||
159 | } |
||
160 | |||
161 | <?php |
||
162 | // Mautic is not configured, or user disabled automatic tracking on page load (GDPR). |
||
163 | 7 | if ( ! empty( $base_url ) && 'disabled' !== $script_location ) : |
|
164 | ?> |
||
165 | 6 | (function(w,d,t,u,n,a,m){w['MauticTrackingObject']=n; |
|
166 | w[n]=w[n]||function(){(w[n].q=w[n].q||[]).push(arguments)},a=d.createElement(t), |
||
167 | m=d.getElementsByTagName(t)[0];a.async=1;a.src=u;m.parentNode.insertBefore(a,m) |
||
168 | })(window,document,'script','<?php echo esc_url( $base_url ); ?>','mt'); |
||
169 | |||
170 | wpmautic_send(); |
||
171 | <?php |
||
172 | endif; |
||
173 | ?> |
||
174 | 7 | </script> |
|
175 | <?php |
||
176 | 7 | } |
|
177 | |||
178 | /** |
||
179 | * Writes Tracking image fallback to the HTML source |
||
180 | * This is a separated function because <noscript> tags are not allowed in header ! |
||
181 | * |
||
182 | * @return void |
||
183 | */ |
||
184 | function wpmautic_inject_noscript() { |
||
185 | 6 | $base_url = wpmautic_option( 'base_url', '' ); |
|
186 | 6 | if ( empty( $base_url ) ) { |
|
187 | return; |
||
188 | } |
||
189 | |||
190 | 6 | $url_query = wpmautic_get_url_query(); |
|
191 | 6 | $payload = rawurlencode( base64_encode( serialize( $url_query ) ) ); |
|
192 | ?> |
||
193 | 6 | <noscript> |
|
194 | <img src="<?php echo esc_url( $base_url ); ?>/mtracking.gif?d=<?php echo esc_attr( $payload ); ?>" style="display:none;" alt="<?php echo esc_attr__( 'Mautic Tags', 'wp-mautic' ); ?>" /> |
||
195 | </noscript> |
||
196 | <?php |
||
197 | 6 | } |
|
198 | |||
199 | /** |
||
200 | * Builds and returns additional data for URL query |
||
201 | * |
||
202 | * @return array |
||
203 | */ |
||
204 | function wpmautic_get_url_query() { |
||
205 | 10 | global $wp; |
|
206 | 10 | $current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) ); |
|
207 | |||
208 | 10 | $attrs = wpmautic_get_tracking_attributes(); |
|
209 | |||
210 | 10 | $attrs['language'] = get_locale(); |
|
211 | 10 | $attrs['page_url'] = $current_url; |
|
212 | 10 | $attrs['page_title'] = function_exists( 'wp_get_document_title' ) |
|
213 | 10 | ? wp_get_document_title() |
|
214 | : wp_title( '»', false ); |
||
215 | 10 | $attrs['referrer'] = function_exists( 'wp_get_raw_referer' ) |
|
216 | 10 | ? wp_get_raw_referer() |
|
217 | : null; |
||
218 | 10 | if ( false === $attrs['referrer'] ) { |
|
219 | 8 | $attrs['referrer'] = $current_url; |
|
220 | } |
||
221 | |||
222 | 10 | return $attrs; |
|
223 | } |
||
224 | |||
225 | /** |
||
226 | * Create custom query parameters to be injected inside tracking |
||
227 | * |
||
228 | * @return array |
||
229 | */ |
||
230 | function wpmautic_get_tracking_attributes() { |
||
231 | 15 | $attrs = wpmautic_get_user_query(); |
|
232 | |||
233 | /** |
||
234 | * Update / add data to be send withing Mautic tracker |
||
235 | * |
||
236 | * Default data only contains the 'language' key but every added key to the |
||
237 | * array will be sent to Mautic. |
||
238 | * |
||
239 | * @since 2.1.0 |
||
240 | * |
||
241 | * @param array $attrs Attributes to be filters, default ['language' => get_locale()] |
||
242 | */ |
||
243 | 15 | return apply_filters( 'wpmautic_tracking_attributes', $attrs ); |
|
244 | } |
||
245 | |||
246 | /** |
||
247 | * Extract logged user informations to be send within Mautic tracker |
||
248 | * |
||
249 | * @return array |
||
250 | */ |
||
251 | function wpmautic_get_user_query() { |
||
252 | 15 | $attrs = array(); |
|
253 | |||
254 | if ( |
||
255 | 15 | true === wpmautic_option( 'track_logged_user', false ) && |
|
256 | 15 | is_user_logged_in() |
|
257 | ) { |
||
258 | $current_user = wp_get_current_user(); |
||
259 | $attrs['email'] = $current_user->user_email; |
||
260 | $attrs['firstname'] = $current_user->user_firstname; |
||
261 | $attrs['lastname'] = $current_user->user_lastname; |
||
262 | |||
263 | // Following Mautic fields has to be created manually and the fields must match these names. |
||
264 | $attrs['wp_user'] = $current_user->user_login; |
||
265 | $attrs['wp_alias'] = $current_user->display_name; |
||
266 | $attrs['wp_registration_date'] = date( |
||
267 | 'Y-m-d', |
||
268 | strtotime( $current_user->user_registered ) |
||
269 | ); |
||
270 | } |
||
271 | |||
272 | 15 | return $attrs; |
|
273 | } |
||
274 |