PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.1.3
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.1.3
5.13.0 5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / classes / WpMatomo / Admin / TrackingSettings / Forms.php
matomo / classes / WpMatomo / Admin / TrackingSettings Last commit date
Forms.php 1 year ago
Forms.php
266 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 * @package matomo
8 * Code Based on
9 * @author Andr&eacute; Br&auml;kling
10 * https://github.com/braekling/matomo
11 *
12 */
13
14 namespace WpMatomo\Admin\TrackingSettings;
15
16 use Piwik\Config;
17 use WpMatomo;
18 use WpMatomo\Admin\TrackingSettings;
19 use WpMatomo\Bootstrap;
20 use WpMatomo\Settings;
21
22 if ( ! defined( 'ABSPATH' ) ) {
23 exit; // if accessed directly
24 }
25
26 /**
27 * phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
28 */
29 class Forms {
30 /**
31 * @var Settings
32 */
33 private $settings;
34
35 public function __construct( $settings ) {
36 $this->settings = $settings;
37 }
38
39 /**
40 * Show an option's description
41 *
42 * @param string $id option id
43 * @param string $description option description
44 * @param boolean $hide_description set to false to show description initially (default: false)
45 *
46 * @return string full description HTML
47 */
48 public function get_description( $id, $description, $hide_description = false ) {
49 return sprintf( '<p class="description' . ( $hide_description ? ' hidden' : '' ) . '" id="%1$s-desc">%2$s</p>', esc_attr( $id ), $description );
50 }
51
52 /**
53 * Show a checkbox option
54 *
55 * @param string $id option id
56 * @param string $name descriptive option name
57 * @param string $description option description
58 * @param boolean $is_hidden set to true to initially hide the option (default: false)
59 * @param string $group_name define a class name to access a group of option rows by javascript (default: empty)
60 * @param boolean $hide_description $hideDescription set to false to show description initially (default: true)
61 * @param string $on_change javascript for onchange event (default: empty)
62 */
63 public function show_checkbox( $id, $name, $description, $is_hidden = false, $group_name = '', $hide_description = false, $on_change = '' ) {
64 printf(
65 '<tr class="' . esc_attr( $group_name ) . ( $is_hidden ? ' hidden' : '' ) . '">'
66 . '<th scope="row"><label for="%2$s">%s:</label></th>'
67 . '<td>'
68 . '<label class="matomo-toggle">'
69 . '<input type="checkbox" value="1"' . ( $this->settings->get_global_option( $id ) ? ' checked="checked"' : '' ) . ' onchange="jQuery(\'#%s\').val(this.checked?1:0);%s" />'
70 . '<span class="slider"></span>'
71 . '</label>'
72 . '<input id="%2$s" type="hidden" name="' . esc_attr( TrackingSettings::FORM_NAME ) . '[%2$s]" value="' . (int) $this->settings->get_global_option( $id ) . '" /> %s'
73 . '</td>'
74 . '</tr>',
75 esc_html( $name ),
76 esc_attr( $id ),
77 $on_change,
78 $this->get_description( $id, $description, $hide_description )
79 );
80 }
81
82 /**
83 * Show a textarea option
84 *
85 * @param string $id option id
86 * @param string $name descriptive option name
87 * @param int $rows number of rows to show
88 * @param string $description option description
89 * @param boolean $is_hidden set to true to initially hide the option (default: false)
90 * @param string $group_name define a class name to access a group of option rows by javascript (default: empty)
91 * @param boolean $hide_description $hideDescription set to false to show description initially (default: false)
92 * @param string $on_change javascript for onchange event (default: empty)
93 * @param boolean $is_readonly set textarea to read only (default: false)
94 * @param boolean $global set to false if the textarea shows a site-specific option (default: true)
95 */
96 public function show_textarea( $id, $name, $rows, $description, $is_hidden, $group_name, $hide_description = false, $on_change = '', $is_readonly = false, $global = true, $exclude_name = false ) {
97 printf(
98 '<tr class="' . esc_attr( $group_name ) . ( $is_hidden ? ' hidden' : '' ) . '"><th scope="row"><label for="%2$s">%s:</label></th><td><textarea cols="80" rows="' . esc_attr( $rows ) . '" id="%s" ' . ( $exclude_name ? '' : 'name="' . esc_attr( TrackingSettings::FORM_NAME ) . '[%2$s]' ) . '" onchange="%s" spellcheck="false"' . ( $is_readonly ? ' readonly="readonly"' : '' ) . '>%s</textarea> %s</td></tr>',
99 esc_html( $name ),
100 esc_attr( $id ),
101 $on_change,
102 ( $global ? $this->settings->get_global_option( $id ) : $this->settings->get_option( $id ) ),
103 $this->get_description( $id, $description, $hide_description )
104 );
105 }
106
107 /**
108 * Show a simple text
109 *
110 * @param string $text Text to show
111 */
112 public function show_text( $text, $group_name = '' ) {
113 printf( '<tr class="%s"><td colspan="2"><p>%s</p></td></tr>', esc_attr( $group_name ), esc_html( $text ) );
114 }
115
116 /**
117 * Show a simple text
118 *
119 * @param string $text Text to show
120 */
121 public function show_headline( $text, $group_name = '' ) {
122 printf( '<tr class="%s"><td colspan="2"><h3>%s</h3></td></tr>', esc_attr( $group_name ), esc_html( $text ) );
123 }
124
125 /**
126 * Show an input option
127 *
128 * @param string $id option id
129 * @param string $name descriptive option name
130 * @param string $description option description
131 * @param boolean $is_hidden set to true to initially hide the option (default: false)
132 * @param string $group_name define a class name to access a group of option rows by javascript (default: empty)
133 * @param string $row_name define a class name to access the specific option row by javascript (default: empty)
134 * @param boolean $hide_description $hideDescription set to false to show description initially (default: false)
135 * @param boolean $wide Create a wide box (default: false)
136 */
137 public function show_input( $id, $name, $description, $is_hidden = false, $group_name = '', $row_name = false, $hide_description = false, $wide = false ) {
138 printf( '<tr class="%s%s"%s><th scope="row"><label for="%5$s">%s:</label></th><td><input ' . ( $wide ? 'class="matomo-wide" ' : '' ) . 'name="' . esc_attr( TrackingSettings::FORM_NAME ) . '[%s]" id="%5$s" value="%s" /> %s</td></tr>', $is_hidden ? 'hidden ' : '', $group_name ? $group_name : '', $row_name ? ' id="' . $group_name . '-' . $row_name . '"' : '', esc_html( $name ), esc_attr( $id ), htmlentities( $this->settings->get_global_option( $id ), ENT_QUOTES, 'UTF-8', false ), ! empty( $description ) ? $this->get_description( $id, $description, $hide_description ) : '' );
139 }
140
141 /**
142 * Show a select box option
143 *
144 * @param string $id option id
145 * @param string $name descriptive option name
146 * @param array $options list of options to show array[](option id => descriptive name)
147 * @param string $description option description
148 * @param string $on_change javascript for onchange event (default: empty)
149 * @param boolean $is_hidden set to true to initially hide the option (default: false)
150 * @param string $group_name define a class name to access a group of option rows by javascript (default: empty)
151 * @param boolean $hide_description $hideDescription set to false to show description initially (default: false)
152 * @param boolean $global set to false if the textarea shows a site-specific option (default: true)
153 */
154 public function show_select( $id, $name, $options = [], $description = '', $on_change = '', $is_hidden = false, $group_name = '', $hide_description = false, $global = true ) {
155 $options_list = '';
156
157 if ( 'tracker_debug' === $id && ! WpMatomo::is_safe_mode() && ! $this->settings->is_network_enabled() ) {
158 Bootstrap::do_bootstrap();
159 if ( Config::getInstance()->Tracker['debug'] ) {
160 $default = 'always';
161 } elseif ( Config::getInstance()->Tracker['debug_on_demand'] ) {
162 $default = 'on_demand';
163 } else {
164 $default = 'disabled';
165 }
166 } else {
167 $default = $global ? $this->settings->get_global_option( $id ) : $this->settings->get_option( $id );
168 }
169 if ( is_array( $options ) ) {
170 foreach ( $options as $key => $value ) {
171 // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
172 $options_list .= sprintf( '<option value="%s"' . ( $key == $default ? ' selected="selected"' : '' ) . '>%s</option>', esc_attr( $key ), esc_html( $value ) );
173 }
174 }
175 $script_change = '';
176 if ( $on_change ) {
177 // we make sure it will select the right settings by default
178 $script_change .= '<script type="text/javascript">setTimeout(function () { jQuery("#' . esc_js( $id ) . '").change(); }, 800);</script>';
179 }
180 printf( '<tr class="' . esc_attr( $group_name ) . ( $is_hidden ? ' hidden' : '' ) . '"><th scope="row"><label for="%3$s">%s:%s</label></th><td><select name="' . esc_attr( TrackingSettings::FORM_NAME ) . '[%s]" id="%3$s" onchange="%s">%s</select> %s</td></tr>', esc_html( $name ), $script_change, esc_attr( $id ), $on_change, $options_list, $this->get_description( $id, $description, $hide_description ) );
181 }
182
183 /**
184 * Show a set of radio buttons
185 *
186 * @param string $id option id
187 * @param string $name descriptive option name
188 * @param array $options list of options to show array[](option id => descriptive name)
189 * @param string|string[] $description option description
190 * @param string $on_change javascript for onchange event (default: empty)
191 * @param boolean $is_hidden set to true to initially hide the option (default: false)
192 * @param string $group_name define a class name to access a group of option rows by javascript (default: empty)
193 * @param boolean $hide_description $hideDescription set to false to show description initially (default: false)
194 * @param boolean $global set to false if the textarea shows a site-specific option (default: true)
195 */
196 public function show_radio( $id, $name, $options = [], $description = '', $on_change = '', $is_hidden = false, $group_name = '', $hide_description = false, $global = true ) {
197 $button_list = [];
198
199 $default = $global ? $this->settings->get_global_option( $id ) : $this->settings->get_option( $id );
200 if ( is_array( $options ) ) {
201 foreach ( $options as $key => $info ) {
202 $label = $info;
203 $disabled = false;
204 $tooltip = null;
205
206 if ( is_array( $info ) ) {
207 $disabled = $info['disabled'];
208 $tooltip = isset( $info['tooltip'] ) ? $info['tooltip'] : null;
209 $label = $info['name'];
210 }
211
212 $radio_id = esc_attr( $id . '_' . $key );
213
214 $desc = '';
215 if ( is_array( $description ) && isset( $description[ $key ] ) ) {
216 $margin_bottom = array_key_last( $options ) === $key ? '' : 'margin-bottom:1em;';
217 $desc = '<p class="description" style="' . $margin_bottom . 'margin-left:1.5em;">' . $description[ $key ] . '</p>';
218 }
219
220 $button_list[] = sprintf(
221 '<span%s><input type="radio" id="%s" name="%s" value="%s" %s %s onchange="%s" />'
222 . '<label for="%s" style="vertical-align:baseline;font-weight:bold;">%s</label></span>'
223 . $desc,
224 ( $tooltip ? ' title="' . esc_attr( $tooltip ) . '"' : '' ),
225 $radio_id,
226 esc_attr( TrackingSettings::FORM_NAME ) . '[' . esc_attr( $id ) . ']',
227 esc_attr( $key ),
228 // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
229 ( $key == $default ? 'checked="checked"' : '' ),
230 ( $disabled ? 'disabled="disabled"' : '' ),
231 esc_attr( $on_change ),
232 $radio_id,
233 esc_html( $label )
234 );
235 }
236 }
237 $script_change = '';
238 if ( $on_change ) {
239 // we make sure it will select the right settings by default
240 $script_change .= '<script type="text/javascript">setTimeout(function () { jQuery("#' . esc_js( $id ) . '").change(); }, 800);</script>';
241 }
242 printf(
243 '<tr class="' . esc_attr( $group_name ) . ( $is_hidden ? ' hidden' : '' ) . '">'
244 . '<th scope="row"><label>%s:%s</label></th>'
245 . '<td><div id="%s" style="display:inline-block">%s</div> %s</td>'
246 . '</tr>',
247 esc_html( $name ),
248 $script_change,
249 esc_attr( $id ),
250 implode( is_array( $description ) ? '' : '<br/>', $button_list ),
251 is_array( $description ) ? '' : $this->get_description( $id, $description, $hide_description )
252 );
253 }
254
255 /**
256 * Show an info box
257 *
258 * @param string $type box style (e.g., updated, error)
259 * @param string $icon box icon, see https://developer.wordpress.org/resource/dashicons/
260 * @param string $content box message
261 */
262 public function show_box( $type, $icon, $content ) {
263 printf( '<tr><td colspan="2"><div class="%s"><p><span class="dashicons dashicons-%s"></span> %s</p></div></td></tr>', esc_attr( $type ), esc_attr( $icon ), esc_html( $content ) );
264 }
265 }
266