Forms.php
163 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 WpMatomo\Admin\TrackingSettings; |
| 17 | use WpMatomo\Settings; |
| 18 | |
| 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | exit; // if accessed directly |
| 21 | } |
| 22 | |
| 23 | class Forms { |
| 24 | /** |
| 25 | * @var Settings |
| 26 | */ |
| 27 | private $settings; |
| 28 | |
| 29 | public function __construct( $settings ) { |
| 30 | $this->settings = $settings; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Show an option's description |
| 35 | * |
| 36 | * @param string $id option id |
| 37 | * @param string $description option description |
| 38 | * @param boolean $hide_description set to false to show description initially (default: true) |
| 39 | * |
| 40 | * @return string full description HTML |
| 41 | */ |
| 42 | public function get_description( $id, $description, $hide_description = true ) { |
| 43 | $title = esc_html__( 'Click to read help', 'matomo' ); |
| 44 | |
| 45 | return sprintf( '<span class="dashicons dashicons-editor-help" title="%1$s" style="cursor: pointer;" onclick="jQuery(\'#%2$s-desc\').toggleClass(\'hidden\');"></span> <p class="description' . ( $hide_description ? ' hidden' : '' ) . '" id="%2$s-desc">%3$s</p>', $title, esc_attr( $id ), $description ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Show a checkbox option |
| 50 | * |
| 51 | * @param string $id option id |
| 52 | * @param string $name descriptive option name |
| 53 | * @param string $description option description |
| 54 | * @param boolean $is_hidden set to true to initially hide the option (default: false) |
| 55 | * @param string $group_name define a class name to access a group of option rows by javascript (default: empty) |
| 56 | * @param boolean $hide_description $hideDescription set to false to show description initially (default: true) |
| 57 | * @param string $on_change javascript for onchange event (default: empty) |
| 58 | */ |
| 59 | public function show_checkbox( $id, $name, $description, $is_hidden = false, $group_name = '', $hide_description = true, $on_change = '' ) { |
| 60 | printf( '<tr class="' . esc_attr( $group_name ) . ( $is_hidden ? ' hidden' : '' ) . '"><th scope="row"><label for="%2$s">%s</label>:</th><td><input type="checkbox" value="1"' . ( $this->settings->get_global_option( $id ) ? ' checked="checked"' : '' ) . ' onchange="jQuery(\'#%s\').val(this.checked?1:0);%s" /><input id="%2$s" type="hidden" name="' . esc_attr( TrackingSettings::FORM_NAME ) . '[%2$s]" value="' . (int) $this->settings->get_global_option( $id ) . '" /> %s</td></tr>', esc_html( $name ), $id, $on_change, $this->get_description( $id, $description, $hide_description ) ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Show a textarea option |
| 65 | * |
| 66 | * @param string $id option id |
| 67 | * @param string $name descriptive option name |
| 68 | * @param int $rows number of rows to show |
| 69 | * @param string $description option description |
| 70 | * @param boolean $is_hidden set to true to initially hide the option (default: false) |
| 71 | * @param string $group_name define a class name to access a group of option rows by javascript (default: empty) |
| 72 | * @param boolean $hide_description $hideDescription set to false to show description initially (default: true) |
| 73 | * @param string $on_change javascript for onchange event (default: empty) |
| 74 | * @param boolean $is_readonly set textarea to read only (default: false) |
| 75 | * @param boolean $global set to false if the textarea shows a site-specific option (default: true) |
| 76 | */ |
| 77 | public function show_textarea( $id, $name, $rows, $description, $is_hidden, $group_name, $hide_description = true, $on_change = '', $is_readonly = false, $global = true ) { |
| 78 | printf( |
| 79 | '<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" name="' . esc_attr( TrackingSettings::FORM_NAME ) . '[%2$s]" onchange="%s"' . ( $is_readonly ? ' readonly="readonly"' : '' ) . '>%s</textarea> %s</td></tr>', |
| 80 | esc_html( $name ), |
| 81 | esc_attr( $id ), |
| 82 | $on_change, |
| 83 | ( $global ? $this->settings->get_global_option( $id ) : $this->settings->get_option( $id ) ), |
| 84 | $this->get_description( $id, $description, $hide_description ) |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Show a simple text |
| 90 | * |
| 91 | * @param string $text Text to show |
| 92 | */ |
| 93 | public function show_text( $text , $group_name = '' ) { |
| 94 | printf( '<tr class="%s"><td colspan="2"><p>%s</p></td></tr>', $group_name, esc_html( $text ) ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Show a simple text |
| 99 | * |
| 100 | * @param string $text Text to show |
| 101 | */ |
| 102 | public function show_headline( $text , $group_name = '') { |
| 103 | printf( '<tr class="%s"><td colspan="2"><h3>%s</h3></td></tr>', $group_name, esc_html( $text ) ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Show an input option |
| 108 | * |
| 109 | * @param string $id option id |
| 110 | * @param string $name descriptive option name |
| 111 | * @param string $description option description |
| 112 | * @param boolean $is_hidden set to true to initially hide the option (default: false) |
| 113 | * @param string $group_name define a class name to access a group of option rows by javascript (default: empty) |
| 114 | * @param string $row_name define a class name to access the specific option row by javascript (default: empty) |
| 115 | * @param boolean $hide_description $hideDescription set to false to show description initially (default: true) |
| 116 | * @param boolean $wide Create a wide box (default: false) |
| 117 | */ |
| 118 | public function show_input( $id, $name, $description, $is_hidden = false, $group_name = '', $row_name = false, $hide_description = true, $wide = false ) { |
| 119 | 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 ) : '' ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Show a select box option |
| 124 | * |
| 125 | * @param string $id option id |
| 126 | * @param string $name descriptive option name |
| 127 | * @param array $options list of options to show array[](option id => descriptive name) |
| 128 | * @param string $description option description |
| 129 | * @param string $on_change javascript for onchange event (default: empty) |
| 130 | * @param boolean $is_hidden set to true to initially hide the option (default: false) |
| 131 | * @param string $group_name define a class name to access a group of option rows by javascript (default: empty) |
| 132 | * @param boolean $hide_description $hideDescription set to false to show description initially (default: true) |
| 133 | * @param boolean $global set to false if the textarea shows a site-specific option (default: true) |
| 134 | */ |
| 135 | public function show_select( $id, $name, $options = array(), $description = '', $on_change = '', $is_hidden = false, $group_name = '', $hide_description = true, $global = true ) { |
| 136 | $options_list = ''; |
| 137 | $default = $global ? $this->settings->get_global_option( $id ) : $this->settings->get_option( $id ); |
| 138 | if ( is_array( $options ) ) { |
| 139 | foreach ( $options as $key => $value ) { |
| 140 | $options_list .= sprintf( '<option value="%s"' . ( $key == $default ? ' selected="selected"' : '' ) . '>%s</option>', esc_attr( $key ), esc_html( $value ) ); |
| 141 | } |
| 142 | } |
| 143 | $script_change = ''; |
| 144 | if ( $on_change ) { |
| 145 | // we make sure it will select the right settings by default |
| 146 | $script_change .= '<script type="text/javascript">setTimeout(function () { jQuery("#' . esc_js( $id ) . '").change(); }, 800);</script>'; |
| 147 | } |
| 148 | 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 ) ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Show an info box |
| 153 | * |
| 154 | * @param string $type box style (e.g., updated, error) |
| 155 | * @param string $icon box icon, see https://developer.wordpress.org/resource/dashicons/ |
| 156 | * @param string $content box message |
| 157 | */ |
| 158 | public function show_box( $type, $icon, $content ) { |
| 159 | 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 ) ); |
| 160 | } |
| 161 | |
| 162 | } |
| 163 |