options.php ➔ wpmautic_admin_init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 39
rs 9.296
c 0
b 0
f 0
ccs 31
cts 31
cp 1
crap 1
1
<?php
2
/**
3
 * Option page definition
4
 *
5
 * @package wp-mautic
6
 */
7
8
// Prevent direct access to this file.
9
if ( ! defined( 'ABSPATH' ) ) {
10
	header( 'HTTP/1.0 403 Forbidden' );
11
	echo 'This file should not be accessed directly!';
12
	exit; // Exit if accessed directly.
13
}
14
15
/**
16
 * HTML for the Mautic option page
17
 */
18
function wpmautic_options_page() {
19
	?>
20 2
	<div>
21
		<h2><?php esc_html_e( 'WP Mautic', 'wp-mautic' ); ?></h2>
22
		<p><?php esc_html_e( 'Add Mautic tracking capabilities to your website.', 'wp-mautic' ); ?></p>
23
		<form action="options.php" method="post">
24
			<?php settings_fields( 'wpmautic' ); ?>
25
			<?php do_settings_sections( 'wpmautic' ); ?>
26
			<?php submit_button(); ?>
27 2
		</form>
28
		<h3><?php esc_html_e( 'Shortcode Examples:', 'wp-mautic' ); ?></h3>
29
		<ul>
30
			<li><?php esc_html_e( 'Mautic Form Embed:', 'wp-mautic' ); ?> <code>[mautic type="form" id="1"]</code></li>
31
			<li><?php esc_html_e( 'Mautic Dynamic Content:', 'wp-mautic' ); ?> <code>[mautic type="content" slot="slot_name"]<?php esc_html_e( 'Default Text', 'wp-mautic' ); ?>[/mautic]</code></li>
32
		</ul>
33
		<h3><?php esc_html_e( 'Quick Links', 'wp-mautic' ); ?></h3>
34
		<ul>
35
			<li>
36
				<a href="https://github.com/mautic/mautic-wordpress#mautic-wordpress-plugin" target="_blank"><?php esc_html_e( 'Plugin docs', 'wp-mautic' ); ?></a>
37
			</li>
38
			<li>
39
				<a href="https://github.com/mautic/mautic-wordpress/issues" target="_blank"><?php esc_html_e( 'Plugin support', 'wp-mautic' ); ?></a>
40
			</li>
41
			<li>
42
				<a href="https://mautic.org" target="_blank"><?php esc_html_e( 'Mautic project', 'wp-mautic' ); ?></a>
43
			</li>
44
			<li>
45
				<a href="http://docs.mautic.org/" target="_blank"><?php esc_html_e( 'Mautic docs', 'wp-mautic' ); ?></a>
46
			</li>
47
			<li>
48
				<a href="https://www.mautic.org/community/" target="_blank"><?php esc_html_e( 'Mautic forum', 'wp-mautic' ); ?></a>
49
			</li>
50
		</ul>
51
	</div>
52
	<?php
53 2
}
54
55
/**
56
 * Define admin_init hook logic
57
 */
58
function wpmautic_admin_init() {
59 1
	register_setting( 'wpmautic', 'wpmautic_options', 'wpmautic_options_validate' );
60
61 1
	add_settings_section(
62 1
		'wpmautic_main',
63 1
		__( 'Main Settings', 'wp-mautic' ),
64 1
		'wpmautic_section_text',
65 1
		'wpmautic'
66
	);
67
68 1
	add_settings_field(
69 1
		'wpmautic_base_url',
70 1
		__( 'Mautic URL', 'wp-mautic' ),
71 1
		'wpmautic_base_url',
72 1
		'wpmautic',
73 1
		'wpmautic_main'
74
	);
75 1
	add_settings_field(
76 1
		'wpmautic_script_location',
77 1
		__( 'Tracking script location', 'wp-mautic' ),
78 1
		'wpmautic_script_location',
79 1
		'wpmautic',
80 1
		'wpmautic_main'
81
	);
82 1
	add_settings_field(
83 1
		'wpmautic_fallback_activated',
84 1
		__( 'Tracking image', 'wp-mautic' ),
85 1
		'wpmautic_fallback_activated',
86 1
		'wpmautic',
87 1
		'wpmautic_main'
88
	);
89 1
	add_settings_field(
90 1
		'wpmautic_track_logged_user',
91 1
		__( 'Logged user', 'wp-mautic' ),
92 1
		'wpmautic_track_logged_user',
93 1
		'wpmautic',
94 1
		'wpmautic_main'
95
	);
96 1
}
97
add_action( 'admin_init', 'wpmautic_admin_init' );
98
99
/**
100
 * Section text
101
 */
102
function wpmautic_section_text() {
103 1
}
104
105
/**
106
 * Define the input field for Mautic base URL
107
 */
108
function wpmautic_base_url() {
109 3
	$url = wpmautic_option( 'base_url', '' );
110
111
	?>
112 3
	<input
113
		id="wpmautic_base_url"
114
		name="wpmautic_options[base_url]"
115
		size="40"
116
		type="text"
117
		placeholder="https://..."
118
		value="<?php echo esc_url_raw( $url, array( 'http', 'https' ) ); ?>"
119
	/>
120
	<?php
121 3
}
122
123
/**
124
 * Define the input field for Mautic script location
125
 */
126
function wpmautic_script_location() {
127 4
	$position     = wpmautic_option( 'script_location', '' );
128
	$allowed_tags = array(
129 4
		'br'   => array(),
130
		'code' => array(),
131
	);
132
133
	?>
134 4
	<fieldset id="wpmautic_script_location">
135
		<label>
136
			<input
137
				type="radio"
138
				name="wpmautic_options[script_location]"
139
				value="header"
140
				<?php
141 4
				if ( 'footer' !== $position && 'disabled' !== $position ) :
142
					?>
143
					checked<?php endif; ?>
144 4
			/>
145
			<?php echo wp_kses( __( 'Added in the <code>wp_head</code> action.<br/>Inserts the tracking code before the <code>&lt;head&gt;</code> tag; can be slightly slower since page load is delayed until all scripts in <code><head></code> are loaded and processed.', 'wp-mautic' ), $allowed_tags ); ?>
146 4
		</label>
147
		<br/>
148
		<label>
149
			<input
150
				type="radio"
151
				name="wpmautic_options[script_location]"
152
				value="footer"
153
				<?php
154 4
				if ( 'footer' === $position ) :
155
					?>
156
					checked<?php endif; ?>
157 4
			/>
158
			<?php echo wp_kses( __( 'Embedded within the <code>wp_footer</code> action.<br/>Inserts the tracking code before the <code>&lt;/body&gt;</code> tag; slightly better for performance but may track less reliably if users close the page before the script has loaded.', 'wp-mautic' ), $allowed_tags ); ?>
159 4
		</label>
160
		<br />
161
		<label>
162
			<input
163
				type="radio"
164
				name="wpmautic_options[script_location]"
165
				value="disabled"
166
				<?php
167 4
				if ( 'disabled' === $position ) :
168
					?>
169
					checked<?php endif; ?>
170 4
			/>
171
			<?php echo wp_kses( __( 'Visitor will not be tracked when rendering the page. Use this option to comply with GDPR regulations. If the visitor accept cookies you must execute the <code>wpmautic_send()</code> JavaScript function to start tracking.', 'wp-mautic' ), $allowed_tags ); ?>
172 4
			<br/>
173
			<?php echo wp_kses( __( 'However when using shortcodes, a tracking cookie will be added everytime even when tracking is disabled. This is because loading a Mautic resource (javascript or image) generate that cookie.', 'wp-mautic' ), $allowed_tags ); ?>
174 4
		</label>
175
	</fieldset>
176
	<?php
177 4
}
178
179
/**
180
 * Define the input field for Mautic fallback flag
181
 */
182
function wpmautic_fallback_activated() {
183 4
	$flag         = wpmautic_option( 'fallback_activated', false );
184
	$allowed_tags = array(
185 4
		'br'   => array(),
186
		'code' => array(),
187
	);
188
189
	?>
190 4
	<input
191
		id="wpmautic_fallback_activated"
192
		name="wpmautic_options[fallback_activated]"
193
		type="checkbox"
194
		value="1"
195
		<?php
196 4
		if ( true === $flag ) :
197
			?>
198
			checked<?php endif; ?>
199 4
	/>
200
	<label for="wpmautic_fallback_activated">
201
		<?php esc_html_e( 'Activate the tracking image when JavaScript is disabled.', 'wp-mautic' ); ?>
202 4
		<br/>
203
		<?php echo wp_kses( __( 'Be warned, that the tracking image will always generate a cookie on the user browser side. If you want to control cookies and comply to GDPR, you must use JavaScript instead.', 'wp-mautic' ), $allowed_tags ); ?>
204 4
	</label>
205
	<?php
206 4
}
207
208
/**
209
 * Define the input field for Mautic logged user tracking flag
210
 */
211
function wpmautic_track_logged_user() {
212 1
	$flag = wpmautic_option( 'track_logged_user', false );
213
214
	?>
215 1
	<input
216
		id="wpmautic_track_logged_user"
217
		name="wpmautic_options[track_logged_user]"
218
		type="checkbox"
219
		value="1"
220
		<?php
221 1
		if ( true === $flag ) :
222
			?>
223
			checked<?php endif; ?>
224 1
	/>
225
	<label for="wpmautic_track_logged_user">
226
		<?php esc_html_e( 'Track user information for logged-in users', 'wp-mautic' ); ?>
227 1
	</label>
228
	<?php
229 1
}
230
231
/**
232
 * Validate base URL input value
233
 *
234
 * @param  array $input Input data.
235
 * @return array
236
 */
237
function wpmautic_options_validate( $input ) {
238 8
	$options = get_option( 'wpmautic_options' );
239
240 8
	$input['base_url'] = isset( $input['base_url'] )
241 2
		? trim( $input['base_url'], " \t\n\r\0\x0B/" )
242 6
		: '';
243
244 8
	$options['base_url']        = esc_url_raw( trim( $input['base_url'], " \t\n\r\0\x0B/" ) );
245 8
	$options['script_location'] = isset( $input['script_location'] )
246 4
		? trim( $input['script_location'] )
247 4
		: 'header';
248 8
	if ( ! in_array( $options['script_location'], array( 'header', 'footer', 'disabled' ), true ) ) {
249 1
		$options['script_location'] = 'header';
250
	}
251
252 8
	$options['fallback_activated'] = isset( $input['fallback_activated'] ) && '1' === $input['fallback_activated']
253 1
		? true
254 7
		: false;
255 8
	$options['track_logged_user']  = isset( $input['track_logged_user'] ) && '1' === $input['track_logged_user']
256
		? true
257 8
		: false;
258
259 8
	return $options;
260
}
261