Completed
Push — master ( 15b78d...370b29 )
by Stéphane
06:13 queued 01:33
created

options.php ➔ wpmautic_options_validate()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.013

Importance

Changes 0
Metric Value
cc 8
eloc 18
nc 128
nop 1
dl 0
loc 24
ccs 16
cts 17
cp 0.9412
crap 8.013
rs 4.9999
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 18 and the first side effect is on line 10.

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
 * 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 Base 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
		__( 'Fallback 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
129
	?>
130 3
	<fieldset id="wpmautic_script_location">
131
		<label>
132
			<input
133
				type="radio"
134
				name="wpmautic_options[script_location]"
135
				value="header"
136
				<?php if ( 'footer' !== $position ) : ?>checked<?php endif; ?>
137 3
			/>
138
			<?php esc_html_e( 'Embedded within the `wp_head` hook', 'wp-mautic' ); ?>
139 3
		</label>
140
		<br/>
141
		<label>
142
			<input
143
				type="radio"
144
				name="wpmautic_options[script_location]"
145
				value="footer"
146
				<?php if ( 'footer' === $position ) : ?>checked<?php endif; ?>
147 3
			/>
148
			<?php esc_html_e( 'Embedded within the `wp_footer` hook', 'wp-mautic' ); ?>
149 3
		</label>
150
	</fieldset>
151
	<?php
152 3
}
153
154
/**
155
 * Define the input field for Mautic fallback flag
156
 */
157 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...
158 4
	$flag = wpmautic_option( 'fallback_activated', false );
159
160
	?>
161 4
	<input
162
		id="wpmautic_fallback_activated"
163
		name="wpmautic_options[fallback_activated]"
164
		type="checkbox"
165
		value="1"
166
		<?php if ( true === $flag ) : ?>checked<?php endif; ?>
167 4
	/>
168
	<label for="wpmautic_fallback_activated">
169
		<?php esc_html_e( 'Activate it when JavaScript is disabled ?', 'wp-mautic' ); ?>
170 4
	</label>
171
	<?php
172 4
}
173
174
/**
175
 * Define the input field for Mautic logged user tracking flag
176
 */
177 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...
178 1
	$flag = wpmautic_option( 'track_logged_user', false );
179
180
	?>
181 1
	<input
182
		id="wpmautic_track_logged_user"
183
		name="wpmautic_options[track_logged_user]"
184
		type="checkbox"
185
		value="1"
186
		<?php if ( true === $flag ) : ?>checked<?php endif; ?>
187 1
	/>
188
	<label for="wpmautic_track_logged_user">
189
		<?php esc_html_e( 'Track user information when logged ?', 'wp-mautic' ); ?>
190 1
	</label>
191
	<?php
192 1
}
193
194
/**
195
 * Validate base URL input value
196
 *
197
 * @param  array $input Input data.
198
 * @return array
199
 */
200
function wpmautic_options_validate( $input ) {
201 6
	$options = get_option( 'wpmautic_options' );
202
203 6
	$input['base_url'] = isset( $input['base_url'] )
204 2
		? trim( $input['base_url'], " \t\n\r\0\x0B/" )
205 4
		: '';
206
207 6
	$options['base_url'] = esc_url_raw( trim( $input['base_url'], " \t\n\r\0\x0B/" ) );
208 6
	$options['script_location'] = isset( $input['script_location'] )
209 2
		? trim( $input['script_location'] )
210 4
		: 'header';
211 6
	if ( ! in_array( $options['script_location'], array( 'header', 'footer' ), true ) ) {
212 1
		$options['script_location'] = 'header';
213
	}
214
215 6
	$options['fallback_activated'] = isset( $input['fallback_activated'] ) && '1' === $input['fallback_activated']
216 1
		? true
217 5
		: false;
218 6
	$options['track_logged_user'] = isset( $input['track_logged_user'] ) && '1' === $input['track_logged_user']
219
		? true
220 6
		: false;
221
222 6
	return $options;
223
}
224