class-activation.php
3 years ago
class-admin-interface.php
3 years ago
class-content-management.php
3 years ago
class-deactivation.php
3 years ago
class-disable-components.php
3 years ago
class-security.php
3 years ago
class-settings-fields-render.php
3 years ago
class-settings-sanitization.php
3 years ago
class-settings-sections-fields.php
3 years ago
class-utilities.php
3 years ago
class-settings-fields-render.php
352 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | /** |
| 6 | * Class related to rendering of settings fields on the admin page |
| 7 | * |
| 8 | * @since 2.2.0 |
| 9 | */ |
| 10 | class Settings_Fields_Render { |
| 11 | |
| 12 | /** |
| 13 | * Render checkbox field as a toggle/switcher |
| 14 | * |
| 15 | * @since 1.0.0 |
| 16 | */ |
| 17 | function render_checkbox_toggle( $args ) { |
| 18 | |
| 19 | $options = get_option( ASENHA_SLUG_U ); |
| 20 | |
| 21 | $field_name = $args['field_name']; |
| 22 | $field_description = $args['field_description']; |
| 23 | $field_option_value = ( array_key_exists( $args['field_id'], $options ) ) ? $options[$args['field_id']] : false; |
| 24 | |
| 25 | echo '<input type="checkbox" id="' . esc_attr( $field_name ) . '" class="asenha-field-checkbox" name="' . esc_attr( $field_name ) . '" ' . checked( $field_option_value, true, false ) . '>'; |
| 26 | echo '<label for="' . esc_attr( $field_name ) . '"></label>'; |
| 27 | |
| 28 | // For field with additional options / sub-fields, we add a wrapper to enclose field descriptions |
| 29 | if ( array_key_exists( 'field_options_wrapper', $args ) && $args['field_options_wrapper'] ) { |
| 30 | // For when the options / sub-fields occupy lengthy vertical space, we add show all / less toggler |
| 31 | if ( array_key_exists( 'field_options_moreless', $args ) && $args['field_options_moreless'] ) { |
| 32 | echo '<div class="asenha-field-with-options field-show-more">'; |
| 33 | echo '<a id="' . $args['field_slug'] . '-show-moreless" class="show-more-less show-more" href="#">Expand ▼</a>'; |
| 34 | echo '<div class="asenha-field-options-wrapper wrapper-show-more">'; |
| 35 | } else { |
| 36 | echo '<div class="asenha-field-with-options">'; |
| 37 | echo '<div class="asenha-field-options-wrapper">'; |
| 38 | } |
| 39 | |
| 40 | } |
| 41 | |
| 42 | echo '<div class="asenha-field-description">' . wp_kses_post( $field_description ) . '</div>'; |
| 43 | |
| 44 | // For field with additional options / sub-fields, we add wrapper for them |
| 45 | if ( array_key_exists( 'field_options_wrapper', $args ) && $args['field_options_wrapper'] ) { |
| 46 | echo '<div class="asenha-subfields" style="display:none"></div>'; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | // For field with additional options / sub-fields, we add a wrapper to enclose field descriptions |
| 51 | if ( array_key_exists( 'field_options_wrapper', $args ) && $args['field_options_wrapper'] ) { |
| 52 | echo '</div>'; |
| 53 | echo '</div>'; |
| 54 | } |
| 55 | |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Render checkbox field as sub-field of a toggle/switcher checkbox |
| 60 | * |
| 61 | * @since 1.9.0 |
| 62 | */ |
| 63 | function render_checkbox_plain( $args ) { |
| 64 | |
| 65 | $options = get_option( ASENHA_SLUG_U ); |
| 66 | |
| 67 | $field_name = $args['field_name']; |
| 68 | $field_label = $args['field_label']; |
| 69 | $field_option_value = ( isset( $options[$args['field_id']] ) ) ? $options[$args['field_id']] : false; |
| 70 | |
| 71 | echo '<input type="checkbox" id="' . esc_attr( $field_name ) . '" class="asenha-subfield-checkbox" name="' . esc_attr( $field_name ) . '" ' . checked( $field_option_value, true, false ) . '>'; |
| 72 | echo '<label for="' . esc_attr( $field_name ) . '" class="asenha-subfield-checkbox-label">' . wp_kses_post( $field_label ) . '</label>'; |
| 73 | |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Render checkbox field as sub-field of a toggle/switcher checkbox |
| 78 | * |
| 79 | * @since 1.3.0 |
| 80 | */ |
| 81 | function render_checkbox_subfield( $args ) { |
| 82 | |
| 83 | $options = get_option( ASENHA_SLUG_U ); |
| 84 | |
| 85 | $field_name = $args['field_name']; |
| 86 | $field_label = $args['field_label']; |
| 87 | $field_option_value = ( isset( $options[$args['parent_field_id']][$args['field_id']] ) ) ? $options[$args['parent_field_id']][$args['field_id']] : false; |
| 88 | |
| 89 | echo '<input type="checkbox" id="' . esc_attr( $field_name ) . '" class="asenha-subfield-checkbox" name="' . esc_attr( $field_name ) . '" ' . checked( $field_option_value, true, false ) . '>'; |
| 90 | echo '<label for="' . esc_attr( $field_name ) . '" class="asenha-subfield-checkbox-label">' . wp_kses_post( $field_label ) . '</label>'; |
| 91 | |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Render text field as sub-field of a toggle/switcher checkbox |
| 96 | * |
| 97 | * @since 1.4.0 |
| 98 | */ |
| 99 | function render_text_subfield( $args ) { |
| 100 | |
| 101 | $options = get_option( ASENHA_SLUG_U ); |
| 102 | |
| 103 | $field_id = $args['field_id']; |
| 104 | $field_name = $args['field_name']; |
| 105 | $field_type = $args['field_type']; |
| 106 | $field_prefix = $args['field_prefix']; |
| 107 | $field_suffix = $args['field_suffix']; |
| 108 | $field_description = $args['field_description']; |
| 109 | $field_option_value = ( isset( $options[$args['field_id']] ) ) ? $options[$args['field_id']] : ''; |
| 110 | |
| 111 | if ( $field_id == 'custom_login_slug' ) { |
| 112 | $placeholder = 'e.g. backend'; |
| 113 | } elseif ( $field_id == 'redirect_after_login_to_slug' ) { |
| 114 | $placeholder = 'e.g. my-account'; |
| 115 | } elseif ( $field_id == 'redirect_after_logout_to_slug' ) { |
| 116 | $placeholder = 'e.g. come-visit-again'; |
| 117 | } else {} |
| 118 | |
| 119 | echo $field_prefix . '<input type="text" id="' . esc_attr( $field_name ) . '" class="asenha-subfield-text" name="' . esc_attr( $field_name ) . '" placeholder="' . esc_attr( $placeholder ) . '" value="' . esc_attr( $field_option_value ) . '">' . $field_suffix; |
| 120 | echo '<label for="' . esc_attr( $field_name ) . '" class="asenha-subfield-checkbox-label">' . esc_html( $field_description ) . '</label>'; |
| 121 | |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Render textarea field as sub-field of a toggle/switcher checkbox |
| 126 | * |
| 127 | * @since 2.3.0 |
| 128 | */ |
| 129 | function render_textarea_subfield( $args ) { |
| 130 | |
| 131 | $options = get_option( ASENHA_SLUG_U ); |
| 132 | |
| 133 | $field_id = $args['field_id']; |
| 134 | $field_slug = $args['field_slug']; |
| 135 | $field_name = $args['field_name']; |
| 136 | $field_type = $args['field_type']; |
| 137 | $field_prefix = $args['field_prefix']; |
| 138 | $field_suffix = $args['field_suffix']; |
| 139 | $field_description = $args['field_description']; |
| 140 | $field_option_value = ( isset( $options[$args['field_id']] ) ) ? $options[$args['field_id']] : ''; |
| 141 | |
| 142 | echo '<textarea rows="30" class="asenha-subfield-textarea" id="' . esc_attr( $field_name ) . '" name="' . esc_attr( $field_name ) . '">' . esc_textarea( $field_option_value ) . '</textarea>'; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Render sortable menu field |
| 147 | * |
| 148 | * @since 2.0.0 |
| 149 | */ |
| 150 | function render_sortable_menu( $args ) { |
| 151 | |
| 152 | ?> |
| 153 | <div class="subfield-description">Drag and drop menu items to the desired position and optionally hide them until "Show All" at the bottom of the admin menu is clicked.</div> |
| 154 | <ul id="custom-admin-menu" class="menu ui-sortable"> |
| 155 | <?php |
| 156 | |
| 157 | global $menu; |
| 158 | global $submenu; |
| 159 | $options = get_option( ASENHA_SLUG_U ); |
| 160 | |
| 161 | // Get hidden menu items |
| 162 | if ( array_key_exists( 'custom_menu_hidden', $options ) ) { |
| 163 | $hidden_menu = $options['custom_menu_hidden']; |
| 164 | $hidden_menu = explode( ',', $hidden_menu ); |
| 165 | } else { |
| 166 | $hidden_menu = array(); |
| 167 | } |
| 168 | |
| 169 | $i = 1; |
| 170 | |
| 171 | // Check if there's an existing custom menu order data stored in options |
| 172 | |
| 173 | if ( array_key_exists( 'custom_menu_order', $options ) ) { |
| 174 | |
| 175 | $custom_menu = $options['custom_menu_order']; |
| 176 | $custom_menu = explode( ',', $custom_menu ); |
| 177 | |
| 178 | // Render sortables with data in custom menu order |
| 179 | |
| 180 | foreach ( $custom_menu as $custom_menu_item ) { |
| 181 | |
| 182 | foreach ( $menu as $menu_key => $menu_info ) { |
| 183 | |
| 184 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 185 | $menu_item_id = $menu_info[2]; |
| 186 | } else { |
| 187 | $menu_item_id = $menu_info[5]; |
| 188 | } |
| 189 | |
| 190 | if ( $custom_menu_item == $menu_item_id ) { |
| 191 | |
| 192 | ?> |
| 193 | <li id="<?php echo wp_kses_post( $menu_item_id ); ?>" class="menu-item menu-item-depth-0"> |
| 194 | <div class="menu-item-bar"> |
| 195 | <div class="menu-item-handle ui-sortable-handle"> |
| 196 | <div class="item-title"> |
| 197 | <span class="menu-item-title"> |
| 198 | <?php |
| 199 | |
| 200 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 201 | $separator_name = $menu_info[2]; |
| 202 | $separator_name = str_replace( 'separator', 'Separator-', $separator_name ); |
| 203 | $separator_name = str_replace( '--last', '-Last', $separator_name ); |
| 204 | echo '~~ ' . wp_kses_post( $separator_name ) . ' ~~'; |
| 205 | } else { |
| 206 | echo wp_kses_post( $menu_info[0] ); |
| 207 | } |
| 208 | |
| 209 | ?> |
| 210 | </span> |
| 211 | <label class="menu-item-checkbox-label"> |
| 212 | <?php |
| 213 | if ( in_array( $custom_menu_item, $hidden_menu ) ) { |
| 214 | ?> |
| 215 | <input type="checkbox" class="menu-item-checkbox" data-menu-item-id="<?php echo wp_kses_post( $menu_item_id ); ?>" checked> |
| 216 | <span>Hide</span> |
| 217 | <?php |
| 218 | } else { |
| 219 | ?> |
| 220 | <input type="checkbox" class="menu-item-checkbox" data-menu-item-id="<?php echo wp_kses_post( $menu_item_id ); ?>"> |
| 221 | <span>Hide</span> |
| 222 | <?php |
| 223 | } |
| 224 | ?> |
| 225 | </label> |
| 226 | </div> |
| 227 | </div> |
| 228 | </div> |
| 229 | <?php |
| 230 | |
| 231 | $i = 1; |
| 232 | |
| 233 | if ( array_key_exists( $menu_info[2], $submenu ) && @is_countable( $submenu[$menu_info[2]] ) && @sizeof( $submenu[$menu_info[2]] ) > 0 ) { |
| 234 | ?> |
| 235 | <div class="menu-item-settings wp-clearfix" style="display:none;"> |
| 236 | <?php |
| 237 | |
| 238 | foreach ( $submenu[$menu_info[2]] as $submenu_item ) { |
| 239 | |
| 240 | $i++; |
| 241 | |
| 242 | // echo $submenu_item[0]; |
| 243 | |
| 244 | } |
| 245 | ?> |
| 246 | </div> |
| 247 | <?php |
| 248 | |
| 249 | } |
| 250 | ?> |
| 251 | </li> |
| 252 | |
| 253 | <?php |
| 254 | |
| 255 | } |
| 256 | |
| 257 | } |
| 258 | |
| 259 | } |
| 260 | |
| 261 | } else { |
| 262 | |
| 263 | // Render sortables with existing items in the admin menu |
| 264 | |
| 265 | foreach ( $menu as $menu_key => $menu_info ) { |
| 266 | |
| 267 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 268 | $menu_item_id = $menu_info[2]; |
| 269 | } else { |
| 270 | $menu_item_id = $menu_info[5]; |
| 271 | } |
| 272 | |
| 273 | ?> |
| 274 | <li id="<?php echo wp_kses_post( $menu_item_id ); ?>" class="menu-item menu-item-depth-0"> |
| 275 | <div class="menu-item-bar"> |
| 276 | <div class="menu-item-handle ui-sortable-handle"> |
| 277 | <div class="item-title"> |
| 278 | <span class="menu-item-title"> |
| 279 | <?php |
| 280 | |
| 281 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 282 | $separator_name = $menu_info[2]; |
| 283 | $separator_name = str_replace( 'separator', 'Separator-', $separator_name ); |
| 284 | $separator_name = str_replace( '--last', '-Last', $separator_name ); |
| 285 | echo '~~ ' . wp_kses_post( $separator_name ) . ' ~~'; |
| 286 | } else { |
| 287 | echo wp_kses_post( $menu_info[0] ); |
| 288 | } |
| 289 | |
| 290 | ?> |
| 291 | </span> |
| 292 | <label class="menu-item-checkbox-label"> |
| 293 | <input type="checkbox" class="menu-item-checkbox" data-menu-item-id="<?php echo wp_kses_post( $menu_item_id ); ?>"> |
| 294 | <span>Hide</span> |
| 295 | </label> |
| 296 | </div> |
| 297 | </div> |
| 298 | </div> |
| 299 | <?php |
| 300 | |
| 301 | $i = 1; |
| 302 | |
| 303 | if ( array_key_exists( $menu_info[2], $submenu ) && @is_countable( $submenu[$menu_info[2]] ) && @sizeof( $submenu[$menu_info[2]] ) > 0 ) { |
| 304 | ?> |
| 305 | <div class="menu-item-settings wp-clearfix" style="display:none;"> |
| 306 | <?php |
| 307 | |
| 308 | foreach ( $submenu[$menu_info[2]] as $submenu_item ) { |
| 309 | |
| 310 | $i++; |
| 311 | |
| 312 | // echo $submenu_item[0]; |
| 313 | |
| 314 | } |
| 315 | ?> |
| 316 | </div> |
| 317 | <?php |
| 318 | |
| 319 | } |
| 320 | ?> |
| 321 | </li> |
| 322 | |
| 323 | <?php |
| 324 | |
| 325 | } |
| 326 | |
| 327 | |
| 328 | } |
| 329 | |
| 330 | |
| 331 | ?> |
| 332 | </ul> |
| 333 | <?php |
| 334 | |
| 335 | $field_id = $args['field_id']; |
| 336 | $field_name = $args['field_name']; |
| 337 | $field_description = $args['field_description']; |
| 338 | $field_option_value = ( isset( $options[$args['field_id']] ) ) ? $options[$args['field_id']] : ''; |
| 339 | |
| 340 | // Hidden input field to store custom menu order (from options as is, or sortupdate) upon clicking Save Changes. |
| 341 | echo '<input type="hidden" id="' . esc_attr( $field_name ) . '" class="asenha-subfield-text" name="' . esc_attr( $field_name ) . '" value="' . esc_attr( $field_option_value ) . '">'; |
| 342 | |
| 343 | $field_id = 'custom_menu_hidden'; |
| 344 | $field_name = ASENHA_SLUG_U . '['. $field_id .']'; |
| 345 | $field_option_value = ( isset( $options[$field_id] ) ) ? $options[$field_id] : ''; |
| 346 | |
| 347 | // Hidden input field to store hidden menu itmes (from options as is, or 'Hide' checkbox clicks) upon clicking Save Changes. |
| 348 | echo '<input type="hidden" id="' . esc_attr( $field_name ) . '" class="asenha-subfield-text" name="' . esc_attr( $field_name ) . '" value="' . esc_attr( $field_option_value ) . '">'; |
| 349 | |
| 350 | } |
| 351 | |
| 352 | } |