Test Failed
Pull Request — master (#67)
by Stéphane
02:01
created

wpmautic.php ➔ wpmautic_get_tracking_attributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 17.

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.

Loading history...
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
	include_once( VPMAUTIC_PLUGIN_DIR . '/options.php' );
37
38
	add_options_page(
39
		__( 'WP Mautic Settings', 'wp-mautic' ),
40
		__( 'WPMautic', 'wp-mautic' ),
41
		'manage_options',
42
		'wpmautic',
43
		'wpmautic_options_page'
44
	);
45
}
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
	if ( plugin_basename( VPMAUTIC_PLUGIN_FILE ) === $file && function_exists( 'admin_url' ) ) {
57
		$settings_link = sprintf(
58
			'<a href="%s">%s</a>',
59
			admin_url( 'options-general.php?page=wpmautic' ),
60
			__( 'Settings' )
61
		);
62
		// Add the settings link before other links.
63
		array_unshift( $links, $settings_link );
64
	}
65
	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  string $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
	$options = get_option( 'wpmautic_options' );
81
82
	switch ( $option ) {
83
		case 'script_location':
84
			return ! isset( $options[ $option ] ) ? 'header' : $options[ $option ];
85
		case 'fallback_activated':
86
			return isset( $options[ $option ] ) ? (bool) $options[ $option ] : true;
87
		default:
88
			if ( ! isset( $options[ $option ] ) ) {
89
				if ( isset( $default ) ) {
90
					return $default;
91
				}
92
93
				throw new InvalidArgumentException( 'You must give a valid option name !' );
94
			}
95
96
			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
	$script_location = wpmautic_option( 'script_location' );
107
	if ( 'header' === $script_location ) {
108
		add_action( 'wp_head', 'wpmautic_inject_script' );
109
	} else {
110
		add_action( 'wp_footer', 'wpmautic_inject_script' );
111
	}
112
113
	if ( true === wpmautic_option( 'fallback_activated', false ) ) {
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
		add_action( 'wp_footer', 'wpmautic_inject_noscript' );
115
	}
116
}
117
118
/**
119
 * Writes Tracking JS to the HTML source
120
 *
121
 * @return void
122
 */
123
function wpmautic_inject_script() {
124
	$base_url = wpmautic_option( 'base_url', '' );
125
	if ( empty( $base_url ) ) {
126
		return;
127
	}
128
129
	$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
}
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
	$base_url = wpmautic_option( 'base_url', '' );
150
	if ( empty( $base_url ) ) {
151
		return;
152
	}
153
154
	global $wp;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
155
156
	$url_query = wpmautic_get_url_query();
157
	$payload = rawurlencode( base64_encode( serialize( $url_query ) ) );
158
	?>
159
	<noscript>
160
		<img src="<?php echo esc_url( $base_url ); ?>/mtracking.gif?d=<?php echo esc_attr( $payload ); ?>"  style="display:none;" alt="" />
161
	</noscript>
162
	<?php
163
}
164
165
/**
166
 * Builds and returns additional data for URL query
167
 *
168
 * @return array
169
 */
170
function wpmautic_get_url_query() {
171
	global $wp;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
172
	$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );
173
174
	$attrs = wpmautic_get_tracking_attributes();
175
176
	$attrs['language']   = get_locale();
177
	$attrs['page_url']   = $current_url;
178
	$attrs['page_title'] = function_exists( 'wp_get_document_title' )
179
		? wp_get_document_title()
180
		: wp_title( '&raquo;', false );
181
	$attrs['referrer']   = function_exists( 'wp_get_raw_referer' )
182
		? wp_get_raw_referer()
183
		: null;
184
	if ( false === $attrs['referrer'] ) {
185
		$attrs['referrer'] = $current_url;
186
	}
187
188
	return $attrs;
189
}
190
191
/**
192
 * Create custom query parameters to be injected inside tracking
193
 *
194
 * @return array
195
 */
196
function wpmautic_get_tracking_attributes() {
197
	$attrs = array();
198
199
	/**
200
	 * Update / add data to be send withing Mautic tracker
201
	 *
202
	 * Default data only contains the 'language' key but every added key to the
203
	 * array will be sent to Mautic.
204
	 *
205
	 * @since 2.1.0
206
	 *
207
	 * @param array $attrs Attributes to be filters, default ['language' => get_locale()]
208
	 */
209
	return apply_filters( 'wpmautic_tracking_attributes', $attrs );
210
}
211