Forms.php
269 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é Brä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 | * @param boolean $is_global whether the option is global or blog specific |
| 63 | */ |
| 64 | public function show_checkbox( $id, $name, $description, $is_hidden = false, $group_name = '', $hide_description = false, $on_change = '', $is_global = true ) { |
| 65 | $value = $is_global ? $this->settings->get_global_option( $id ) : $this->settings->get_option( $id ); |
| 66 | |
| 67 | printf( |
| 68 | '<tr id="' . esc_attr( $id ) . '-field" class="' . esc_attr( $group_name ) . ( $is_hidden ? ' hidden' : '' ) . '">' |
| 69 | . '<th scope="row"><label for="%2$s">%s:</label></th>' |
| 70 | . '<td>' |
| 71 | . '<label class="matomo-toggle">' |
| 72 | . '<input type="checkbox" value="1"' . ( $value ? ' checked="checked"' : '' ) . ' onchange="jQuery(\'#%s\').val(this.checked?1:0);%s" />' |
| 73 | . '<span class="slider"></span>' |
| 74 | . '</label>' |
| 75 | . '<input id="%2$s" type="hidden" name="' . esc_attr( TrackingSettings::FORM_NAME ) . '[%2$s]" value="' . (int) $this->settings->get_global_option( $id ) . '" /> %s' |
| 76 | . '</td>' |
| 77 | . '</tr>', |
| 78 | esc_html( $name ), |
| 79 | esc_attr( $id ), |
| 80 | $on_change, |
| 81 | $this->get_description( $id, $description, $hide_description ) |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Show a textarea option |
| 87 | * |
| 88 | * @param string $id option id |
| 89 | * @param string $name descriptive option name |
| 90 | * @param int $rows number of rows to show |
| 91 | * @param string $description option description |
| 92 | * @param boolean $is_hidden set to true to initially hide the option (default: false) |
| 93 | * @param string $group_name define a class name to access a group of option rows by javascript (default: empty) |
| 94 | * @param boolean $hide_description $hideDescription set to false to show description initially (default: false) |
| 95 | * @param string $on_change javascript for onchange event (default: empty) |
| 96 | * @param boolean $is_readonly set textarea to read only (default: false) |
| 97 | * @param boolean $global set to false if the textarea shows a site-specific option (default: true) |
| 98 | */ |
| 99 | 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 ) { |
| 100 | printf( |
| 101 | '<tr id="' . esc_attr( $id ) . '-field" 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>', |
| 102 | esc_html( $name ), |
| 103 | esc_attr( $id ), |
| 104 | $on_change, |
| 105 | esc_textarea( $global ? $this->settings->get_global_option( $id ) : $this->settings->get_option( $id ) ), |
| 106 | $this->get_description( $id, $description, $hide_description ) |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Show a simple text |
| 112 | * |
| 113 | * @param string $text Text to show |
| 114 | */ |
| 115 | public function show_text( $text, $group_name = '' ) { |
| 116 | printf( '<tr class="%s"><td colspan="2"><p>%s</p></td></tr>', esc_attr( $group_name ), esc_html( $text ) ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Show a simple text |
| 121 | * |
| 122 | * @param string $text Text to show |
| 123 | */ |
| 124 | public function show_headline( $text, $group_name = '' ) { |
| 125 | printf( '<tr class="%s"><td colspan="2"><h3>%s</h3></td></tr>', esc_attr( $group_name ), esc_html( $text ) ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Show an input option |
| 130 | * |
| 131 | * @param string $id option id |
| 132 | * @param string $name descriptive option name |
| 133 | * @param string $description option description |
| 134 | * @param boolean $is_hidden set to true to initially hide the option (default: false) |
| 135 | * @param string $group_name define a class name to access a group of option rows by javascript (default: empty) |
| 136 | * @param string $row_name define a class name to access the specific option row by javascript (default: empty) |
| 137 | * @param boolean $hide_description $hideDescription set to false to show description initially (default: false) |
| 138 | * @param boolean $wide Create a wide box (default: false) |
| 139 | */ |
| 140 | public function show_input( $id, $name, $description, $is_hidden = false, $group_name = '', $row_name = false, $hide_description = false, $wide = false ) { |
| 141 | printf( '<tr id="' . esc_attr( $id ) . '-field" 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 ) : '' ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Show a select box option |
| 146 | * |
| 147 | * @param string $id option id |
| 148 | * @param string $name descriptive option name |
| 149 | * @param array $options list of options to show array[](option id => descriptive name) |
| 150 | * @param string $description option description |
| 151 | * @param string $on_change javascript for onchange event (default: empty) |
| 152 | * @param boolean $is_hidden set to true to initially hide the option (default: false) |
| 153 | * @param string $group_name define a class name to access a group of option rows by javascript (default: empty) |
| 154 | * @param boolean $hide_description $hideDescription set to false to show description initially (default: false) |
| 155 | * @param boolean $global set to false if the textarea shows a site-specific option (default: true) |
| 156 | */ |
| 157 | public function show_select( $id, $name, $options = [], $description = '', $on_change = '', $is_hidden = false, $group_name = '', $hide_description = false, $global = true ) { |
| 158 | $options_list = ''; |
| 159 | |
| 160 | if ( 'tracker_debug' === $id && ! WpMatomo::is_safe_mode() && ! $this->settings->is_network_enabled() ) { |
| 161 | Bootstrap::do_bootstrap(); |
| 162 | if ( Config::getInstance()->Tracker['debug'] ) { |
| 163 | $default = 'always'; |
| 164 | } elseif ( Config::getInstance()->Tracker['debug_on_demand'] ) { |
| 165 | $default = 'on_demand'; |
| 166 | } else { |
| 167 | $default = 'disabled'; |
| 168 | } |
| 169 | } else { |
| 170 | $default = $global ? $this->settings->get_global_option( $id ) : $this->settings->get_option( $id ); |
| 171 | } |
| 172 | if ( is_array( $options ) ) { |
| 173 | foreach ( $options as $key => $value ) { |
| 174 | // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
| 175 | $options_list .= sprintf( '<option value="%s"' . ( $key == $default ? ' selected="selected"' : '' ) . '>%s</option>', esc_attr( $key ), esc_html( $value ) ); |
| 176 | } |
| 177 | } |
| 178 | $script_change = ''; |
| 179 | if ( $on_change ) { |
| 180 | // we make sure it will select the right settings by default |
| 181 | $script_change .= '<script type="text/javascript">setTimeout(function () { jQuery("#' . esc_js( $id ) . '").change(); }, 800);</script>'; |
| 182 | } |
| 183 | printf( '<tr id="' . esc_attr( $id ) . '-field" 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 ) ); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Show a set of radio buttons |
| 188 | * |
| 189 | * @param string $id option id |
| 190 | * @param string $name descriptive option name |
| 191 | * @param array $options list of options to show array[](option id => descriptive name) |
| 192 | * @param string|string[] $description option description |
| 193 | * @param string $on_change javascript for onchange event (default: empty) |
| 194 | * @param boolean $is_hidden set to true to initially hide the option (default: false) |
| 195 | * @param string $group_name define a class name to access a group of option rows by javascript (default: empty) |
| 196 | * @param boolean $hide_description $hideDescription set to false to show description initially (default: false) |
| 197 | * @param boolean $global set to false if the textarea shows a site-specific option (default: true) |
| 198 | */ |
| 199 | public function show_radio( $id, $name, $options = [], $description = '', $on_change = '', $is_hidden = false, $group_name = '', $hide_description = false, $global = true ) { |
| 200 | $button_list = []; |
| 201 | |
| 202 | $default = $global ? $this->settings->get_global_option( $id ) : $this->settings->get_option( $id ); |
| 203 | if ( is_array( $options ) ) { |
| 204 | foreach ( $options as $key => $info ) { |
| 205 | $label = $info; |
| 206 | $disabled = false; |
| 207 | $tooltip = null; |
| 208 | |
| 209 | if ( is_array( $info ) ) { |
| 210 | $disabled = $info['disabled']; |
| 211 | $tooltip = isset( $info['tooltip'] ) ? $info['tooltip'] : null; |
| 212 | $label = $info['name']; |
| 213 | } |
| 214 | |
| 215 | $radio_id = esc_attr( $id . '_' . $key ); |
| 216 | |
| 217 | $desc = ''; |
| 218 | if ( is_array( $description ) && isset( $description[ $key ] ) ) { |
| 219 | $margin_bottom = array_key_last( $options ) === $key ? '' : 'margin-bottom:1em;'; |
| 220 | $desc = '<p class="description" style="' . $margin_bottom . 'margin-left:1.5em;">' . $description[ $key ] . '</p>'; |
| 221 | } |
| 222 | |
| 223 | $button_list[] = sprintf( |
| 224 | '<span%s><input type="radio" id="%s" name="%s" value="%s" %s %s onchange="%s" />' |
| 225 | . '<label for="%s" style="vertical-align:baseline;font-weight:bold;">%s</label></span>' |
| 226 | . $desc, |
| 227 | ( $tooltip ? ' title="' . esc_attr( $tooltip ) . '"' : '' ), |
| 228 | $radio_id, |
| 229 | esc_attr( TrackingSettings::FORM_NAME ) . '[' . esc_attr( $id ) . ']', |
| 230 | esc_attr( $key ), |
| 231 | // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
| 232 | ( $key == $default ? 'checked="checked"' : '' ), |
| 233 | ( $disabled ? 'disabled="disabled"' : '' ), |
| 234 | esc_attr( $on_change ), |
| 235 | $radio_id, |
| 236 | esc_html( $label ) |
| 237 | ); |
| 238 | } |
| 239 | } |
| 240 | $script_change = ''; |
| 241 | if ( $on_change ) { |
| 242 | // we make sure it will select the right settings by default |
| 243 | $script_change .= '<script type="text/javascript">setTimeout(function () { jQuery("#' . esc_js( $id ) . '").change(); }, 800);</script>'; |
| 244 | } |
| 245 | printf( |
| 246 | '<tr id="' . esc_attr( $id ) . '-field" class="' . esc_attr( $group_name ) . ( $is_hidden ? ' hidden' : '' ) . '">' |
| 247 | . '<th scope="row"><label>%s:%s</label></th>' |
| 248 | . '<td><div id="%s" style="display:inline-block">%s</div> %s</td>' |
| 249 | . '</tr>', |
| 250 | esc_html( $name ), |
| 251 | $script_change, |
| 252 | esc_attr( $id ), |
| 253 | implode( is_array( $description ) ? '' : '<br/>', $button_list ), |
| 254 | is_array( $description ) ? '' : $this->get_description( $id, $description, $hide_description ) |
| 255 | ); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Show an info box |
| 260 | * |
| 261 | * @param string $type box style (e.g., updated, error) |
| 262 | * @param string $icon box icon, see https://developer.wordpress.org/resource/dashicons/ |
| 263 | * @param string $content box message |
| 264 | */ |
| 265 | public function show_box( $type, $icon, $content ) { |
| 266 | 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 ) ); |
| 267 | } |
| 268 | } |
| 269 |