Completed
Pull Request — master (#95)
by Stéphane
08:28
created

options.php ➔ wpmautic_script_location()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 0
dl 0
loc 50
ccs 12
cts 12
cp 1
crap 5
rs 8.7797
c 0
b 0
f 0
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="http://..."
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 3
	$position     = wpmautic_option( 'script_location', '' );
128
	$allowed_tags = array(
129 3
		'br'   => array(),
130
		'code' => array(),
131
	);
132
133
	?>
134 3
	<fieldset id="wpmautic_script_location">
135
		<label>
136
			<input
137
				type="radio"
138
				name="wpmautic_options[script_location]"
139
				value="disabled"
140
				<?php
141 3
				if ( 'disabled' === $position ) :
142
					?>
143
					checked<?php endif; ?>
144 3
			/>
145
			<?php echo wp_kses( __( 'Tracking script will not be loaded when rendering the page; this means that Mautic tracking capability is disabled but you can still use shortcodes to embed Mautic dynamic contents. Use this option to comply with GDPR regulation rules, then load the script by yourself if user accepted tracking.', 'wp-mautic' ), $allowed_tags ); ?>
146 3
		</label>
147
		<br/>
148
		<label>
149
			<input
150
				type="radio"
151
				name="wpmautic_options[script_location]"
152
				value="header"
153
				<?php
154 3
				if ( 'footer' !== $position && 'disabled' !== $position ) :
155
					?>
156
					checked<?php endif; ?>
157 3
			/>
158
			<?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 ); ?>
159 3
		</label>
160
		<br/>
161
		<label>
162 3
			<input
163
				type="radio"
164
				name="wpmautic_options[script_location]"
165
				value="footer"
166
				<?php
167
				if ( 'footer' === $position ) :
168 4
					?>
169
					checked<?php endif; ?>
170
			/>
171 4
			<?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 ); ?>
172
		</label>
173
	</fieldset>
174
	<?php
175
}
176
177 4
/**
178
 * Define the input field for Mautic fallback flag
179
 */
180 4 View Code Duplication
function wpmautic_fallback_activated() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181
	$flag = wpmautic_option( 'fallback_activated', false );
182
183 4
	?>
184
	<input
185 4
		id="wpmautic_fallback_activated"
186
		name="wpmautic_options[fallback_activated]"
187
		type="checkbox"
188
		value="1"
189
		<?php
190
		if ( true === $flag ) :
191 1
			?>
192
			checked<?php endif; ?>
193
	/>
194 1
	<label for="wpmautic_fallback_activated">
195
		<?php esc_html_e( 'Activate the tracking image when JavaScript is disabled', 'wp-mautic' ); ?>
196
	</label>
197
	<?php
198
}
199
200 1
/**
201
 * Define the input field for Mautic logged user tracking flag
202
 */
203 1 View Code Duplication
function wpmautic_track_logged_user() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
	$flag = wpmautic_option( 'track_logged_user', false );
205
206 1
	?>
207
	<input
208 1
		id="wpmautic_track_logged_user"
209
		name="wpmautic_options[track_logged_user]"
210
		type="checkbox"
211
		value="1"
212
		<?php
213
		if ( true === $flag ) :
214
			?>
215
			checked<?php endif; ?>
216
	/>
217 6
	<label for="wpmautic_track_logged_user">
218
		<?php esc_html_e( 'Track user information for logged-in users', 'wp-mautic' ); ?>
219 6
	</label>
220 2
	<?php
221 4
}
222
223 6
/**
224 6
 * Validate base URL input value
225 2
 *
226 4
 * @param  array $input Input data.
227 6
 * @return array
228 1
 */
229
function wpmautic_options_validate( $input ) {
230
	$options = get_option( 'wpmautic_options' );
231 6
232 1
	$input['base_url'] = isset( $input['base_url'] )
233 5
		? trim( $input['base_url'], " \t\n\r\0\x0B/" )
234 6
		: '';
235
236 6
	$options['base_url']        = esc_url_raw( trim( $input['base_url'], " \t\n\r\0\x0B/" ) );
237
	$options['script_location'] = isset( $input['script_location'] )
238 6
		? trim( $input['script_location'] )
239
		: 'header';
240
	if ( ! in_array( $options['script_location'], array( 'header', 'footer', 'disabled' ), true ) ) {
241
		$options['script_location'] = 'header';
242
	}
243
244
	$options['fallback_activated'] = isset( $input['fallback_activated'] ) && '1' === $input['fallback_activated']
245
		? true
246
		: false;
247
	$options['track_logged_user']  = isset( $input['track_logged_user'] ) && '1' === $input['track_logged_user']
248
		? true
249
		: false;
250
251
	return $options;
252
}
253