Completed
Pull Request — master (#88)
by Stéphane
03:05 queued 01:02
created

options.php ➔ wpmautic_options_validate()   B

Complexity

Conditions 8
Paths 128

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.013

Importance

Changes 0
Metric Value
cc 8
nc 128
nop 1
dl 0
loc 24
rs 8.2111
c 0
b 0
f 0
ccs 16
cts 17
cp 0.9412
crap 8.013
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="header"
140
				<?php
141 3
				if ( 'footer' !== $position ) :
142
					?>
143
					checked<?php endif; ?>
144 3
			/>
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 3
		</label>
147
		<br/>
148
		<label>
149
			<input
150
				type="radio"
151
				name="wpmautic_options[script_location]"
152
				value="footer"
153
				<?php
154 3
				if ( 'footer' === $position ) :
155
					?>
156
					checked<?php endif; ?>
157 3
			/>
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 3
		</label>
160
	</fieldset>
161
	<?php
162 3
}
163
164
/**
165
 * Define the input field for Mautic fallback flag
166
 */
167 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...
168 4
	$flag = wpmautic_option( 'fallback_activated', false );
169
170
	?>
171 4
	<input
172
		id="wpmautic_fallback_activated"
173
		name="wpmautic_options[fallback_activated]"
174
		type="checkbox"
175
		value="1"
176
		<?php
177 4
		if ( true === $flag ) :
178
			?>
179
			checked<?php endif; ?>
180 4
	/>
181
	<label for="wpmautic_fallback_activated">
182
		<?php esc_html_e( 'Activate the tracking image when JavaScript is disabled', 'wp-mautic' ); ?>
183 4
	</label>
184
	<?php
185 4
}
186
187
/**
188
 * Define the input field for Mautic logged user tracking flag
189
 */
190 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...
191 1
	$flag = wpmautic_option( 'track_logged_user', false );
192
193
	?>
194 1
	<input
195
		id="wpmautic_track_logged_user"
196
		name="wpmautic_options[track_logged_user]"
197
		type="checkbox"
198
		value="1"
199
		<?php
200 1
		if ( true === $flag ) :
201
			?>
202
			checked<?php endif; ?>
203 1
	/>
204
	<label for="wpmautic_track_logged_user">
205
		<?php esc_html_e( 'Track user information for logged-in users', 'wp-mautic' ); ?>
206 1
	</label>
207
	<?php
208 1
}
209
210
/**
211
 * Validate base URL input value
212
 *
213
 * @param  array $input Input data.
214
 * @return array
215
 */
216
function wpmautic_options_validate( $input ) {
217 6
	$options = get_option( 'wpmautic_options' );
218
219 6
	$input['base_url'] = isset( $input['base_url'] )
220 2
		? trim( $input['base_url'], " \t\n\r\0\x0B/" )
221 4
		: '';
222
223 6
	$options['base_url']        = esc_url_raw( trim( $input['base_url'], " \t\n\r\0\x0B/" ) );
224 6
	$options['script_location'] = isset( $input['script_location'] )
225 2
		? trim( $input['script_location'] )
226 4
		: 'header';
227 6
	if ( ! in_array( $options['script_location'], array( 'header', 'footer' ), true ) ) {
228 1
		$options['script_location'] = 'header';
229
	}
230
231 6
	$options['fallback_activated'] = isset( $input['fallback_activated'] ) && '1' === $input['fallback_activated']
232 1
		? true
233 5
		: false;
234 6
	$options['track_logged_user']  = isset( $input['track_logged_user'] ) && '1' === $input['track_logged_user']
235
		? true
236 6
		: false;
237
238 6
	return $options;
239
}
240