class-activation.php
3 years ago
class-admin-interface.php
3 years ago
class-common-methods.php
3 years ago
class-content-management.php
3 years ago
class-custom-code.php
3 years ago
class-deactivation.php
3 years ago
class-disable-components.php
3 years ago
class-login-logout.php
3 years ago
class-optimizations.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
909 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 | $option_name = $args['option_name']; |
| 20 | |
| 21 | if ( !empty($option_name) ) { |
| 22 | $options = get_option( $option_name, array() ); |
| 23 | } else { |
| 24 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 25 | } |
| 26 | |
| 27 | $field_name = $args['field_name']; |
| 28 | $field_description = $args['field_description']; |
| 29 | $field_option_value = ( array_key_exists( $args['field_id'], $options ) ? $options[$args['field_id']] : false ); |
| 30 | echo '<input type="checkbox" id="' . esc_attr( $field_name ) . '" class="asenha-field-checkbox" name="' . esc_attr( $field_name ) . '" ' . checked( $field_option_value, true, false ) . '>' ; |
| 31 | echo '<label for="' . esc_attr( $field_name ) . '"></label>' ; |
| 32 | // For field with additional options / sub-fields, we add a wrapper to enclose field descriptions |
| 33 | if ( array_key_exists( 'field_options_wrapper', $args ) && $args['field_options_wrapper'] ) { |
| 34 | // For when the options / sub-fields occupy lengthy vertical space, we add show all / less toggler |
| 35 | |
| 36 | if ( array_key_exists( 'field_options_moreless', $args ) && $args['field_options_moreless'] ) { |
| 37 | echo '<div class="asenha-field-with-options field-show-more">' ; |
| 38 | echo '<a id="' . $args['field_slug'] . '-show-moreless" class="show-more-less show-more" href="#">Expand ▼</a>' ; |
| 39 | echo '<div class="asenha-field-options-wrapper wrapper-show-more">' ; |
| 40 | } else { |
| 41 | echo '<div class="asenha-field-with-options">' ; |
| 42 | echo '<div class="asenha-field-options-wrapper">' ; |
| 43 | } |
| 44 | |
| 45 | } |
| 46 | echo '<div class="asenha-field-description">' . wp_kses_post( $field_description ) . '</div>' ; |
| 47 | // For field with additional options / sub-fields, we add wrapper for them |
| 48 | if ( array_key_exists( 'field_options_wrapper', $args ) && $args['field_options_wrapper'] ) { |
| 49 | echo '<div class="asenha-subfields" style="display:none"></div>' ; |
| 50 | } |
| 51 | // For field with additional options / sub-fields, we add a wrapper to enclose field descriptions |
| 52 | |
| 53 | if ( array_key_exists( 'field_options_wrapper', $args ) && $args['field_options_wrapper'] ) { |
| 54 | echo '</div>' ; |
| 55 | echo '</div>' ; |
| 56 | } |
| 57 | |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Render checkbox field as sub-field of a toggle/switcher checkbox |
| 62 | * |
| 63 | * @since 1.9.0 |
| 64 | */ |
| 65 | function render_checkbox_plain( $args ) |
| 66 | { |
| 67 | $option_name = $args['option_name']; |
| 68 | |
| 69 | if ( !empty($option_name) ) { |
| 70 | $options = get_option( $option_name, array() ); |
| 71 | } else { |
| 72 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 73 | } |
| 74 | |
| 75 | $field_name = $args['field_name']; |
| 76 | $field_label = $args['field_label']; |
| 77 | $field_option_value = ( isset( $options[$args['field_id']] ) ? $options[$args['field_id']] : false ); |
| 78 | echo '<input type="checkbox" id="' . esc_attr( $field_name ) . '" class="asenha-subfield-checkbox" name="' . esc_attr( $field_name ) . '" ' . checked( $field_option_value, true, false ) . '>' ; |
| 79 | echo '<label for="' . esc_attr( $field_name ) . '" class="asenha-subfield-checkbox-label">' . wp_kses_post( $field_label ) . '</label>' ; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Render checkbox field as sub-field of a toggle/switcher checkbox |
| 84 | * |
| 85 | * @since 1.3.0 |
| 86 | */ |
| 87 | function render_checkbox_subfield( $args ) |
| 88 | { |
| 89 | $option_name = $args['option_name']; |
| 90 | |
| 91 | if ( !empty($option_name) ) { |
| 92 | $options = get_option( $option_name, array() ); |
| 93 | } else { |
| 94 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 95 | } |
| 96 | |
| 97 | $field_name = $args['field_name']; |
| 98 | $field_label = $args['field_label']; |
| 99 | $field_option_value = ( isset( $options[$args['parent_field_id']][$args['field_id']] ) ? $options[$args['parent_field_id']][$args['field_id']] : false ); |
| 100 | echo '<input type="checkbox" id="' . esc_attr( $field_name ) . '" class="asenha-subfield-checkbox" name="' . esc_attr( $field_name ) . '" ' . checked( $field_option_value, true, false ) . '>' ; |
| 101 | echo '<label for="' . esc_attr( $field_name ) . '" class="asenha-subfield-checkbox-label">' . wp_kses_post( $field_label ) . '</label>' ; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Render radio buttons field as sub-field of a toggle/switcher checkbox |
| 106 | * |
| 107 | * @since 1.3.0 |
| 108 | */ |
| 109 | function render_radio_buttons_subfield( $args ) |
| 110 | { |
| 111 | $option_name = $args['option_name']; |
| 112 | |
| 113 | if ( !empty($option_name) ) { |
| 114 | $options = get_option( $option_name, array() ); |
| 115 | } else { |
| 116 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 117 | } |
| 118 | |
| 119 | $field_id = $args['field_id']; |
| 120 | $field_name = $args['field_name']; |
| 121 | // $field_label = $args['field_label']; |
| 122 | $field_radios = $args['field_radios']; |
| 123 | |
| 124 | if ( !empty($args['field_default']) ) { |
| 125 | $default_value = $args['field_default']; |
| 126 | } else { |
| 127 | $default_value = false; |
| 128 | } |
| 129 | |
| 130 | $field_option_value = ( isset( $options[$field_id] ) ? $options[$field_id] : $default_value ); |
| 131 | foreach ( $field_radios as $radio_label => $radio_value ) { |
| 132 | echo '<input type="radio" id="' . esc_attr( $field_id . '_' . $radio_value ) . '" class="asenha-subfield-radio-button" name="' . esc_attr( $field_name ) . '" value="' . $radio_value . '" ' . checked( $radio_value, $field_option_value, false ) . '>' ; |
| 133 | echo '<label for="' . esc_attr( $field_id . '_' . $radio_value ) . '" class="asenha-subfield-radio-button-label">' . wp_kses_post( $radio_label ) . '</label>' ; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Render text field as sub-field of a toggle/switcher checkbox |
| 139 | * |
| 140 | * @since 1.4.0 |
| 141 | */ |
| 142 | function render_text_subfield( $args ) |
| 143 | { |
| 144 | $option_name = $args['option_name']; |
| 145 | |
| 146 | if ( !empty($option_name) ) { |
| 147 | $options = get_option( $option_name, array() ); |
| 148 | } else { |
| 149 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 150 | } |
| 151 | |
| 152 | $field_id = $args['field_id']; |
| 153 | $field_name = $args['field_name']; |
| 154 | $field_type = $args['field_type']; |
| 155 | $field_prefix = $args['field_prefix']; |
| 156 | $field_suffix = $args['field_suffix']; |
| 157 | $field_description = $args['field_description']; |
| 158 | $field_placeholder = ( isset( $args['field_placeholder'] ) ? $args['field_placeholder'] : '' ); |
| 159 | $field_option_value = ( isset( $options[$args['field_id']] ) ? $options[$args['field_id']] : '' ); |
| 160 | |
| 161 | if ( !empty($field_prefix) && !empty($field_suffix) ) { |
| 162 | $field_classname = ' with-prefix with-suffix'; |
| 163 | } elseif ( !empty($field_prefix) && empty($field_suffix) ) { |
| 164 | $field_classname = ' with-prefix'; |
| 165 | } elseif ( empty($field_prefix) && !empty($field_suffix) ) { |
| 166 | $field_classname = ' with-suffix'; |
| 167 | } else { |
| 168 | $field_classname = ''; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | if ( $field_id == 'custom_login_slug' ) { |
| 173 | $field_placeholder = 'e.g. backend'; |
| 174 | } elseif ( $field_id == 'redirect_after_login_to_slug' ) { |
| 175 | $field_placeholder = 'e.g. my-account'; |
| 176 | } elseif ( $field_id == 'redirect_after_logout_to_slug' ) { |
| 177 | $field_placeholder = 'e.g. come-visit-again'; |
| 178 | } elseif ( $field_id == 'login_fails_allowed' ) { |
| 179 | $field_placeholder = '3'; |
| 180 | } elseif ( $field_id == 'login_lockout_maxcount' ) { |
| 181 | $field_placeholder = '3'; |
| 182 | } else { |
| 183 | } |
| 184 | |
| 185 | echo $field_prefix . '<input type="text" id="' . esc_attr( $field_name ) . '" class="asenha-subfield-text' . esc_attr( $field_classname ) . '" name="' . esc_attr( $field_name ) . '" placeholder="' . esc_attr( $field_placeholder ) . '" value="' . esc_attr( $field_option_value ) . '">' . $field_suffix ; |
| 186 | echo '<label for="' . esc_attr( $field_name ) . '" class="asenha-subfield-checkbox-label">' . esc_html( $field_description ) . '</label>' ; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Render description field as sub-field of a toggle/switcher checkbox |
| 191 | * |
| 192 | * @since 4.6.0 |
| 193 | */ |
| 194 | function render_description_subfield( $args ) |
| 195 | { |
| 196 | $field_description = $args['field_description']; |
| 197 | echo '<div class="asenha-subfield-description">' . $field_description . '</div>' ; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Render heading for sub-fields of a toggle/switcher checkbox |
| 202 | * |
| 203 | * @since 5.0.0 |
| 204 | */ |
| 205 | function render_subfields_heading( $args ) |
| 206 | { |
| 207 | $subfields_heading = $args['subfields_heading']; |
| 208 | echo '<div class="asenha-subfields-heading">' . $subfields_heading . '</div>' ; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Render password field as sub-field of a toggle/switcher checkbox |
| 213 | * |
| 214 | * @since 4.1.0 |
| 215 | */ |
| 216 | function render_password_subfield( $args ) |
| 217 | { |
| 218 | $option_name = $args['option_name']; |
| 219 | |
| 220 | if ( !empty($option_name) ) { |
| 221 | $options = get_option( $option_name, array() ); |
| 222 | } else { |
| 223 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 224 | } |
| 225 | |
| 226 | $field_id = $args['field_id']; |
| 227 | $field_name = $args['field_name']; |
| 228 | $field_type = $args['field_type']; |
| 229 | $field_prefix = $args['field_prefix']; |
| 230 | $field_suffix = $args['field_suffix']; |
| 231 | $field_description = $args['field_description']; |
| 232 | $field_option_value = ( isset( $options[$args['field_id']] ) ? $options[$args['field_id']] : '' ); |
| 233 | |
| 234 | if ( !empty($field_prefix) && !empty($field_suffix) ) { |
| 235 | $field_classname = ' with-prefix with-suffix'; |
| 236 | } elseif ( !empty($field_prefix) && empty($field_suffix) ) { |
| 237 | $field_classname = ' with-prefix'; |
| 238 | } elseif ( empty($field_prefix) && !empty($field_suffix) ) { |
| 239 | $field_classname = ' with-suffix'; |
| 240 | } else { |
| 241 | $field_classname = ''; |
| 242 | } |
| 243 | |
| 244 | $placeholder = ''; |
| 245 | echo $field_prefix . '<input type="password" id="' . esc_attr( $field_name ) . '" class="asenha-subfield-password' . esc_attr( $field_classname ) . '" name="' . esc_attr( $field_name ) . '" placeholder="' . esc_attr( $placeholder ) . '" size="24" autocomplete="off" value="' . $field_option_value . '">' . $field_suffix ; |
| 246 | echo '<label for="' . esc_attr( $field_name ) . '" class="asenha-subfield-checkbox-label">' . esc_html( $field_description ) . '</label>' ; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Render number field as sub-field of a toggle/switcher checkbox |
| 251 | * |
| 252 | * @since 1.4.0 |
| 253 | */ |
| 254 | function render_number_subfield( $args ) |
| 255 | { |
| 256 | $option_name = $args['option_name']; |
| 257 | |
| 258 | if ( !empty($option_name) ) { |
| 259 | $options = get_option( $option_name, array() ); |
| 260 | } else { |
| 261 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 262 | } |
| 263 | |
| 264 | $field_id = $args['field_id']; |
| 265 | $field_name = $args['field_name']; |
| 266 | $field_type = $args['field_type']; |
| 267 | $field_prefix = $args['field_prefix']; |
| 268 | $field_suffix = $args['field_suffix']; |
| 269 | $field_intro = $args['field_intro']; |
| 270 | $field_description = $args['field_description']; |
| 271 | $field_option_value = ( isset( $options[$args['field_id']] ) ? $options[$args['field_id']] : '' ); |
| 272 | |
| 273 | if ( !empty($field_prefix) && !empty($field_suffix) ) { |
| 274 | $field_classname = ' with-prefix with-suffix'; |
| 275 | } elseif ( !empty($field_prefix) && empty($field_suffix) ) { |
| 276 | $field_classname = ' with-prefix'; |
| 277 | } elseif ( empty($field_prefix) && !empty($field_suffix) ) { |
| 278 | $field_classname = ' with-suffix'; |
| 279 | } else { |
| 280 | $field_classname = ''; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | if ( $field_id == 'revisions_max_number' || $field_id == 'custom_frontend_css_priority' || $field_id == 'head_code_priority' || $field_id == 'body_code_priority' || $field_id == 'footer_code_priority' ) { |
| 285 | $placeholder = '10'; |
| 286 | } else { |
| 287 | $placeholder = ''; |
| 288 | } |
| 289 | |
| 290 | echo '<div class="asenha-subfield-number-wrapper">' ; |
| 291 | if ( !empty($field_intro) ) { |
| 292 | echo '<div class="asenha-subfield-number-intro">' . wp_kses_post( $field_intro ) . '</div>' ; |
| 293 | } |
| 294 | echo '<div>' . $field_prefix . '<input type="number" id="' . esc_attr( $field_name ) . '" class="asenha-subfield-number' . esc_attr( $field_classname ) . '" name="' . esc_attr( $field_name ) . '" placeholder="' . esc_attr( $placeholder ) . '" value="' . esc_attr( $field_option_value ) . '">' . $field_suffix . '</div>' ; |
| 295 | if ( !empty($field_description) ) { |
| 296 | echo '<div class="asenha-subfield-number-description">' . wp_kses_post( $field_description ) . '</div>' ; |
| 297 | } |
| 298 | echo '</div>' ; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Render select field as sub-field of a toggle/switcher checkbox |
| 303 | * |
| 304 | * @since 1.4.0 |
| 305 | */ |
| 306 | function render_select_subfield( $args ) |
| 307 | { |
| 308 | $option_name = $args['option_name']; |
| 309 | |
| 310 | if ( !empty($option_name) ) { |
| 311 | $options = get_option( $option_name, array() ); |
| 312 | } else { |
| 313 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 314 | } |
| 315 | |
| 316 | $field_id = $args['field_id']; |
| 317 | $field_name = $args['field_name']; |
| 318 | $field_type = $args['field_type']; |
| 319 | $field_prefix = $args['field_prefix']; |
| 320 | $field_suffix = $args['field_suffix']; |
| 321 | $field_select_options = $args['field_select_options']; |
| 322 | $field_select_default = $args['field_select_default']; |
| 323 | $field_intro = $args['field_intro']; |
| 324 | $field_description = $args['field_description']; |
| 325 | |
| 326 | if ( !empty($args['field_select_default']) ) { |
| 327 | $default_value = $args['field_select_default']; |
| 328 | } else { |
| 329 | $default_value = false; |
| 330 | } |
| 331 | |
| 332 | $field_option_value = ( isset( $options[$field_id] ) ? $options[$field_id] : $default_value ); |
| 333 | |
| 334 | if ( !empty($field_prefix) && !empty($field_suffix) ) { |
| 335 | $field_classname = ' with-prefix with-suffix'; |
| 336 | } elseif ( !empty($field_prefix) && empty($field_suffix) ) { |
| 337 | $field_classname = ' with-prefix'; |
| 338 | } elseif ( empty($field_prefix) && !empty($field_suffix) ) { |
| 339 | $field_classname = ' with-suffix'; |
| 340 | } else { |
| 341 | $field_classname = ''; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | if ( $args['display_none_on_load'] ) { |
| 346 | $inline_style = ' style="display:none;"'; |
| 347 | } else { |
| 348 | $inline_style = ''; |
| 349 | } |
| 350 | |
| 351 | echo '<div class="asenha-subfield-select-wrapper">' ; |
| 352 | if ( !empty($field_intro) ) { |
| 353 | echo '<div class="asenha-subfield-select-intro">' . wp_kses_post( $field_intro ) . '</div>' ; |
| 354 | } |
| 355 | echo '<div' . $inline_style . ' class="asenha-subfield-select-inner">' . $field_prefix ; |
| 356 | echo '<select name="' . $field_name . '" class="asenha-subfield-select' . esc_attr( $field_classname ) . '">' ; |
| 357 | foreach ( $field_select_options as $label => $value ) { |
| 358 | echo '<option value="' . $value . '" ' . selected( $value, $field_option_value, false ) . '>' . $label . '</option>' ; |
| 359 | } |
| 360 | echo '</select>' ; |
| 361 | echo $field_suffix . '</div>' ; |
| 362 | if ( !empty($field_description) ) { |
| 363 | echo '<div class="asenha-subfield-select-description">' . wp_kses_post( $field_description ) . '</div>' ; |
| 364 | } |
| 365 | echo '</div>' ; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Render textarea field as sub-field of a toggle/switcher checkbox |
| 370 | * |
| 371 | * @since 2.3.0 |
| 372 | */ |
| 373 | function render_textarea_subfield( $args ) |
| 374 | { |
| 375 | $option_name = $args['option_name']; |
| 376 | |
| 377 | if ( !empty($option_name) ) { |
| 378 | $options = get_option( $option_name, array() ); |
| 379 | } else { |
| 380 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 381 | } |
| 382 | |
| 383 | $field_id = $args['field_id']; |
| 384 | $field_slug = $args['field_slug']; |
| 385 | $field_name = $args['field_name']; |
| 386 | $field_type = $args['field_type']; |
| 387 | $field_rows = $args['field_rows']; |
| 388 | $field_intro = $args['field_intro']; |
| 389 | $field_description = $args['field_description']; |
| 390 | $field_placeholder = ( isset( $args['field_placeholder'] ) ? $args['field_placeholder'] : '' ); |
| 391 | // Always load textarea content from robots.txt URL, whether it's a real, custom-made robots.txt file or a virtual one generated by WordPress |
| 392 | |
| 393 | if ( 'robots_txt_content' == $field_id ) { |
| 394 | |
| 395 | if ( array_key_exists( 'manage_robots_txt', $options ) ) { |
| 396 | |
| 397 | if ( !$options['manage_robots_txt'] ) { |
| 398 | // Manage robots.txt feature is NOT enabled |
| 399 | |
| 400 | if ( array_key_exists( 'robots_txt_content', $options ) && $options['robots_txt_content'] ) { |
| 401 | $field_option_value = $options['robots_txt_content']; |
| 402 | } else { |
| 403 | $robots_txt_content = wp_remote_get( get_site_url() . '/robots.txt' ); |
| 404 | $robots_txt_content = esc_textarea( trim( wp_remote_retrieve_body( $robots_txt_content ) ) ); |
| 405 | $field_option_value = $robots_txt_content; |
| 406 | } |
| 407 | |
| 408 | } else { |
| 409 | // Manage robots.txt feature is enabled |
| 410 | |
| 411 | if ( array_key_exists( 'robots_txt_content', $options ) && $options['robots_txt_content'] ) { |
| 412 | $field_option_value = $options['robots_txt_content']; |
| 413 | } else { |
| 414 | $robots_txt_content = wp_remote_get( get_site_url() . '/robots.txt' ); |
| 415 | $robots_txt_content = esc_textarea( trim( wp_remote_retrieve_body( $robots_txt_content ) ) ); |
| 416 | $field_option_value = $robots_txt_content; |
| 417 | } |
| 418 | |
| 419 | } |
| 420 | |
| 421 | } else { |
| 422 | // Manage robots.txt feature has never been enabled yet |
| 423 | $robots_txt_content = wp_remote_get( get_site_url() . '/robots.txt' ); |
| 424 | $robots_txt_content = esc_textarea( trim( wp_remote_retrieve_body( $robots_txt_content ) ) ); |
| 425 | $field_option_value = $robots_txt_content; |
| 426 | } |
| 427 | |
| 428 | } else { |
| 429 | $field_option_value = ( isset( $options[$args['field_id']] ) ? $options[$args['field_id']] : '' ); |
| 430 | } |
| 431 | |
| 432 | echo '<div class="asenha-subfield-textarea-wrapper">' ; |
| 433 | if ( !empty($field_intro) ) { |
| 434 | echo '<div class="asenha-subfield-textarea-intro">' . wp_kses_post( $field_intro ) . '</div>' ; |
| 435 | } |
| 436 | echo '<textarea rows="' . $field_rows . '" class="asenha-subfield-textarea" id="' . esc_attr( $field_name ) . '" name="' . esc_attr( $field_name ) . '" placeholder="' . esc_attr( $field_placeholder ) . '">' . esc_textarea( $field_option_value ) . '</textarea>' ; |
| 437 | if ( !empty($field_description) ) { |
| 438 | echo '<div class="asenha-subfield-textarea-description">' . wp_kses_post( $field_description ) . '</div>' ; |
| 439 | } |
| 440 | echo '</div>' ; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Render sortable menu field |
| 445 | * |
| 446 | * @since 2.0.0 |
| 447 | */ |
| 448 | function render_sortable_menu( $args ) |
| 449 | { |
| 450 | ?> |
| 451 | <div class="subfield-description">Drag and drop menu items to the desired position. Optionally change 3rd party plugin/theme's menu item titles or hide some items until toggled by clicking "Show All" at the bottom of the admin menu.</div> |
| 452 | <?php |
| 453 | ?> |
| 454 | <ul id="custom-admin-menu" class="menu ui-sortable"> |
| 455 | <?php |
| 456 | global $menu, $submenu ; |
| 457 | $common_methods = new Common_Methods(); |
| 458 | $option_name = $args['option_name']; |
| 459 | |
| 460 | if ( !empty($option_name) ) { |
| 461 | $options = get_option( $option_name, array() ); |
| 462 | } else { |
| 463 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 464 | } |
| 465 | |
| 466 | // Set menu items to be excluded from title renaming. These are from WordPress core. |
| 467 | $renaming_not_allowed = array( |
| 468 | 'menu-dashboard', |
| 469 | 'menu-pages', |
| 470 | 'menu-posts', |
| 471 | 'menu-media', |
| 472 | 'menu-comments', |
| 473 | 'menu-appearance', |
| 474 | 'menu-plugins', |
| 475 | 'menu-users', |
| 476 | 'menu-tools', |
| 477 | 'menu-settings' |
| 478 | ); |
| 479 | // Get custom menu item titles |
| 480 | |
| 481 | if ( array_key_exists( 'custom_menu_titles', $options ) ) { |
| 482 | $custom_menu_titles = $options['custom_menu_titles']; |
| 483 | $custom_menu_titles = explode( ',', $custom_menu_titles ); |
| 484 | } else { |
| 485 | $custom_menu_titles = array(); |
| 486 | } |
| 487 | |
| 488 | // Get menu items hidden by toggle |
| 489 | $menu_hidden_by_toggle = $common_methods->get_menu_hidden_by_toggle(); |
| 490 | $i = 1; |
| 491 | // Check if there's an existing custom menu order data stored in options |
| 492 | |
| 493 | if ( array_key_exists( 'custom_menu_order', $options ) ) { |
| 494 | $custom_menu = $options['custom_menu_order']; |
| 495 | $custom_menu = explode( ',', $custom_menu ); |
| 496 | $menu_key_in_use = array(); |
| 497 | // Render sortables with data in custom menu order |
| 498 | foreach ( $custom_menu as $custom_menu_item ) { |
| 499 | foreach ( $menu as $menu_key => $menu_info ) { |
| 500 | |
| 501 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 502 | $menu_item_title = $menu_info[2]; |
| 503 | $menu_item_id = $menu_info[2]; |
| 504 | } else { |
| 505 | $menu_item_title = $menu_info[0]; |
| 506 | $menu_item_id = $menu_info[5]; |
| 507 | } |
| 508 | |
| 509 | $menu_url_fragment = ''; |
| 510 | |
| 511 | if ( $custom_menu_item == $menu_item_id ) { |
| 512 | $menu_item_id_transformed = $common_methods->transform_menu_item_id( $menu_item_id ); |
| 513 | ?> |
| 514 | <li id="<?php |
| 515 | echo esc_attr( $menu_item_id ) ; |
| 516 | ?>" class="menu-item parent-menu-item menu-item-depth-0"> |
| 517 | <div class="menu-item-bar"> |
| 518 | <div class="menu-item-handle"> |
| 519 | <span class="dashicons dashicons-menu"></span> |
| 520 | <div class="item-title"> |
| 521 | <div class="title-wrapper"> |
| 522 | <span class="menu-item-title"> |
| 523 | <?php |
| 524 | |
| 525 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 526 | $separator_name_ori = $menu_info[2]; |
| 527 | $separator_name = str_replace( 'separator', 'Separator-', $separator_name_ori ); |
| 528 | $separator_name = str_replace( '--last', '-Last', $separator_name ); |
| 529 | echo '~~ ' . esc_html( $separator_name ) . ' ~~' ; |
| 530 | } else { |
| 531 | |
| 532 | if ( in_array( $menu_item_id, $renaming_not_allowed ) ) { |
| 533 | $menu_item_title = $menu_info[0]; |
| 534 | echo wp_kses_post( $common_methods->strip_html_tags_and_content( $menu_item_title ) ) ; |
| 535 | } else { |
| 536 | // Get defaul/custom menu item title |
| 537 | foreach ( $custom_menu_titles as $custom_menu_title ) { |
| 538 | // At this point, $custom_menu_title value looks like toplevel_page_snippets__Code Snippets |
| 539 | $custom_menu_title = explode( '__', $custom_menu_title ); |
| 540 | |
| 541 | if ( $custom_menu_title[0] == $menu_item_id ) { |
| 542 | $menu_item_title = $common_methods->strip_html_tags_and_content( $custom_menu_title[1] ); |
| 543 | // e.g. Code Snippets |
| 544 | break; |
| 545 | // stop foreach loop so $menu_item_title is not overwritten in the next iteration |
| 546 | } else { |
| 547 | $menu_item_title = $common_methods->strip_html_tags_and_content( $menu_info[0] ); |
| 548 | } |
| 549 | |
| 550 | } |
| 551 | ?> |
| 552 | <input type="text" value="<?php |
| 553 | echo wp_kses_post( $menu_item_title ) ; |
| 554 | ?>" class="menu-item-custom-title" data-menu-item-id="<?php |
| 555 | echo esc_attr( $menu_item_id ) ; |
| 556 | ?>"> |
| 557 | <?php |
| 558 | } |
| 559 | |
| 560 | } |
| 561 | |
| 562 | ?> |
| 563 | </span><!-- end of .menu-item-title --> |
| 564 | <?php |
| 565 | ?> |
| 566 | </div><!-- end of .title-wrapper --> |
| 567 | <div class="options-for-hiding"> |
| 568 | <?php |
| 569 | $hide_text = 'Hide until toggled'; |
| 570 | $checkbox_class = 'parent-menu-hide-checkbox'; |
| 571 | ?> |
| 572 | <label class="menu-item-checkbox-label"> |
| 573 | <?php |
| 574 | |
| 575 | if ( in_array( $custom_menu_item, $menu_hidden_by_toggle ) ) { |
| 576 | ?> |
| 577 | <input type="checkbox" id="hide-status-for-<?php |
| 578 | echo esc_attr( $menu_item_id_transformed ) ; |
| 579 | ?>" class="<?php |
| 580 | echo esc_attr( $checkbox_class ) ; |
| 581 | ?>" data-menu-item-title="<?php |
| 582 | echo esc_attr( $common_methods->strip_html_tags_and_content( $menu_item_title ) ) ; |
| 583 | ?>" data-menu-item-id="<?php |
| 584 | echo esc_attr( $menu_item_id_transformed ) ; |
| 585 | ?>" data-menu-item-id-ori="<?php |
| 586 | echo esc_attr( $menu_item_id ) ; |
| 587 | ?>" data-menu-url-fragment="<?php |
| 588 | echo esc_attr( $menu_url_fragment ) ; |
| 589 | ?>" checked> |
| 590 | <span><?php |
| 591 | echo esc_html( $hide_text ) ; |
| 592 | ?></span> |
| 593 | <?php |
| 594 | } else { |
| 595 | ?> |
| 596 | <input type="checkbox" id="hide-status-for-<?php |
| 597 | echo esc_attr( $menu_item_id_transformed ) ; |
| 598 | ?>" class="<?php |
| 599 | echo esc_attr( $checkbox_class ) ; |
| 600 | ?>" data-menu-item-title="<?php |
| 601 | echo esc_attr( $common_methods->strip_html_tags_and_content( $menu_item_title ) ) ; |
| 602 | ?>" data-menu-item-id="<?php |
| 603 | echo esc_attr( $menu_item_id_transformed ) ; |
| 604 | ?>" data-menu-item-id-ori="<?php |
| 605 | echo esc_attr( $menu_item_id ) ; |
| 606 | ?>" data-menu-url-fragment="<?php |
| 607 | echo esc_attr( $menu_url_fragment ) ; |
| 608 | ?>"> |
| 609 | <span><?php |
| 610 | echo esc_html( $hide_text ) ; |
| 611 | ?></span> |
| 612 | <?php |
| 613 | } |
| 614 | |
| 615 | ?> |
| 616 | </label> |
| 617 | <?php |
| 618 | ?> |
| 619 | </div><!-- end of .options-for-hiding --> |
| 620 | </div><!-- end of .item-title --> |
| 621 | </div><!-- end of .menu-item-handle --> |
| 622 | </div><!-- end of .menu-item-bar --> |
| 623 | <?php |
| 624 | ?> |
| 625 | <?php |
| 626 | $i = 1; |
| 627 | if ( array_key_exists( $menu_info[2], $submenu ) && @is_countable( $submenu[$menu_info[2]] ) && @sizeof( $submenu[$menu_info[2]] ) > 0 ) { |
| 628 | } |
| 629 | ?> |
| 630 | </li> |
| 631 | <?php |
| 632 | $menu_key_in_use[] = $menu_key; |
| 633 | } |
| 634 | |
| 635 | } |
| 636 | } |
| 637 | // Render the rest of the current menu towards the end of the sortables |
| 638 | foreach ( $menu as $menu_key => $menu_info ) { |
| 639 | |
| 640 | if ( !in_array( $menu_key, $menu_key_in_use ) ) { |
| 641 | |
| 642 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 643 | $menu_item_id = $menu_info[2]; |
| 644 | } else { |
| 645 | $menu_item_id = $menu_info[5]; |
| 646 | } |
| 647 | |
| 648 | $menu_item_title = $menu_info[0]; |
| 649 | $menu_url_fragment = ''; |
| 650 | // Exclude Show All/Less toggles |
| 651 | |
| 652 | if ( false === strpos( $menu_item_id, 'toplevel_page_asenha_' ) ) { |
| 653 | $menu_item_id_transformed = $common_methods->transform_menu_item_id( $menu_item_id ); |
| 654 | ?> |
| 655 | <li id="<?php |
| 656 | echo esc_attr( $menu_item_id ) ; |
| 657 | ?>" class="menu-item parent-menu-item menu-item-depth-0"> |
| 658 | <div class="menu-item-bar"> |
| 659 | <div class="menu-item-handle"> |
| 660 | <span class="dashicons dashicons-menu"></span> |
| 661 | <div class="item-title"> |
| 662 | <div class="title-wrapper"> |
| 663 | <span class="menu-item-title"> |
| 664 | <?php |
| 665 | |
| 666 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 667 | $separator_name_ori = $menu_info[2]; |
| 668 | $separator_name = str_replace( 'separator', 'Separator-', $separator_name_ori ); |
| 669 | $separator_name = str_replace( '--last', '-Last', $separator_name ); |
| 670 | echo '~~ ' . esc_html( $separator_name ) . ' ~~' ; |
| 671 | } else { |
| 672 | ?> |
| 673 | <input type="text" value="<?php |
| 674 | echo wp_kses_post( $menu_item_title ) ; |
| 675 | ?>" class="menu-item-custom-title" data-menu-item-id="<?php |
| 676 | echo esc_attr( $menu_item_id ) ; |
| 677 | ?>"> |
| 678 | <?php |
| 679 | } |
| 680 | |
| 681 | ?> |
| 682 | </span> |
| 683 | <?php |
| 684 | ?> |
| 685 | </div> |
| 686 | <div class="options-for-hiding"> |
| 687 | <?php |
| 688 | $hide_text = 'Hide until toggled'; |
| 689 | $checkbox_class = 'parent-menu-hide-checkbox'; |
| 690 | ?> |
| 691 | <label class="menu-item-checkbox-label"> |
| 692 | <input type="checkbox" id="hide-status-for-<?php |
| 693 | echo esc_attr( $menu_item_id_transformed ) ; |
| 694 | ?>" class="<?php |
| 695 | echo esc_attr( $checkbox_class ) ; |
| 696 | ?>" data-menu-item-id="<?php |
| 697 | echo esc_attr( $menu_item_id ) ; |
| 698 | ?>"> |
| 699 | <span><?php |
| 700 | echo esc_html( $hide_text ) ; |
| 701 | ?></span> |
| 702 | </label> |
| 703 | <?php |
| 704 | ?> |
| 705 | </div><!-- end of .options-for-hiding --> |
| 706 | </div><!-- end of .item-title --> |
| 707 | </div><!-- end of .menu-item-handle --> |
| 708 | </div><!-- end of .menu-item-bar --> |
| 709 | <div class="menu-item-settings parent-menu-item-settings wp-clearfix" style="display:none;"> |
| 710 | </div><!-- end of .menu-item-settings --> |
| 711 | <?php |
| 712 | $i = 1; |
| 713 | if ( array_key_exists( $menu_info[2], $submenu ) && @is_countable( $submenu[$menu_info[2]] ) && @sizeof( $submenu[$menu_info[2]] ) > 0 ) { |
| 714 | ?> |
| 715 | <?php |
| 716 | } |
| 717 | ?> |
| 718 | </li><!-- end of .menu-item --> |
| 719 | <?php |
| 720 | } |
| 721 | |
| 722 | } |
| 723 | |
| 724 | } |
| 725 | } else { |
| 726 | // No custom menu order has been saved yet |
| 727 | // Render sortables with existing items in the admin menu |
| 728 | foreach ( $menu as $menu_key => $menu_info ) { |
| 729 | |
| 730 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 731 | $menu_item_id = $menu_info[2]; |
| 732 | } else { |
| 733 | $menu_item_id = $menu_info[5]; |
| 734 | } |
| 735 | |
| 736 | $menu_url_fragment = ''; |
| 737 | $menu_item_id_transformed = $common_methods->transform_menu_item_id( $menu_item_id ); |
| 738 | ?> |
| 739 | <li id="<?php |
| 740 | echo esc_attr( $menu_item_id ) ; |
| 741 | ?>" class="menu-item parent-menu-item menu-item-depth-0"> |
| 742 | <div class="menu-item-bar"> |
| 743 | <div class="menu-item-handle"> |
| 744 | <span class="dashicons dashicons-menu"></span> |
| 745 | <div class="item-title"> |
| 746 | <div class="title-wrapper"> |
| 747 | <span class="menu-item-title"> |
| 748 | <?php |
| 749 | |
| 750 | if ( false !== strpos( $menu_info[4], 'wp-menu-separator' ) ) { |
| 751 | $separator_name_ori = $menu_info[2]; |
| 752 | $separator_name = str_replace( 'separator', 'Separator-', $separator_name_ori ); |
| 753 | $separator_name = str_replace( '--last', '-Last', $separator_name ); |
| 754 | echo '~~ ' . esc_html( $separator_name ) . ' ~~' ; |
| 755 | } else { |
| 756 | |
| 757 | if ( in_array( $menu_item_id, $renaming_not_allowed ) ) { |
| 758 | $menu_item_title = $menu_info[0]; |
| 759 | echo wp_kses_post( $common_methods->strip_html_tags_and_content( $menu_item_title ) ) ; |
| 760 | } else { |
| 761 | ?> |
| 762 | <input type="text" value="<?php |
| 763 | echo wp_kses_post( $common_methods->strip_html_tags_and_content( $menu_info[0] ) ) ; |
| 764 | ?>" class="menu-item-custom-title" data-menu-item-id="<?php |
| 765 | echo esc_attr( $menu_item_id ) ; |
| 766 | ?>"> |
| 767 | <?php |
| 768 | } |
| 769 | |
| 770 | } |
| 771 | |
| 772 | ?> |
| 773 | </span> |
| 774 | <?php |
| 775 | ?> |
| 776 | </div><!-- end of .title-wrapper --> |
| 777 | <div class="options-for-hiding"> |
| 778 | <?php |
| 779 | $hide_text = 'Hide until toggled'; |
| 780 | $checkbox_class = 'parent-menu-hide-checkbox'; |
| 781 | ?> |
| 782 | <label class="menu-item-checkbox-label"> |
| 783 | <input type="checkbox" id="hide-status-for-<?php |
| 784 | echo esc_attr( $menu_item_id_transformed ) ; |
| 785 | ?>" class="<?php |
| 786 | echo esc_attr( $checkbox_class ) ; |
| 787 | ?>" data-menu-item-id="<?php |
| 788 | echo esc_attr( $menu_item_id ) ; |
| 789 | ?>"> |
| 790 | <span><?php |
| 791 | echo esc_html( $hide_text ) ; |
| 792 | ?></span> |
| 793 | </label> |
| 794 | <?php |
| 795 | ?> |
| 796 | </div><!-- end of .options-for-hiding --> |
| 797 | </div><!-- end of .item-title --> |
| 798 | </div><!-- end of .menu-item-handle --> |
| 799 | </div><!-- end of .menu-item-bar --> |
| 800 | <?php |
| 801 | $i = 1; |
| 802 | if ( array_key_exists( $menu_info[2], $submenu ) && @is_countable( $submenu[$menu_info[2]] ) && @sizeof( $submenu[$menu_info[2]] ) > 0 ) { |
| 803 | ?> |
| 804 | <?php |
| 805 | } |
| 806 | ?> |
| 807 | </li> |
| 808 | <?php |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | ?> |
| 813 | </ul> |
| 814 | <?php |
| 815 | // Hidden input field to store custom menu order (from options as is, or sortupdate) upon clicking Save Changes. |
| 816 | $field_id = $args['field_id']; |
| 817 | $field_name = $args['field_name']; |
| 818 | $field_description = $args['field_description']; |
| 819 | $field_option_value = ( isset( $options[$args['field_id']] ) ? $options[$args['field_id']] : '' ); |
| 820 | echo '<input type="hidden" id="' . esc_attr( $field_name ) . '" class="asenha-subfield-text" name="' . esc_attr( $field_name ) . '" value="' . esc_attr( $field_option_value ) . '">' ; |
| 821 | // Hidden input field to store custom menu titles (from options as is, or custom values entered on each non-WP-default menu items. |
| 822 | $field_id = 'custom_menu_titles'; |
| 823 | $field_name = ASENHA_SLUG_U . '[' . $field_id . ']'; |
| 824 | $field_option_value = ( isset( $options[$field_id] ) ? $options[$field_id] : '' ); |
| 825 | echo '<input type="hidden" id="' . esc_attr( $field_name ) . '" class="asenha-subfield-text" name="' . esc_attr( $field_name ) . '" value="' . esc_attr( $field_option_value ) . '">' ; |
| 826 | // Hidden input field to store hidden menu items (from options as is, or 'Hide' checkbox clicks) upon clicking Save Changes. |
| 827 | $field_id = 'custom_menu_hidden'; |
| 828 | $field_name = ASENHA_SLUG_U . '[' . $field_id . ']'; |
| 829 | $field_option_value = ( isset( $options[$field_id] ) ? $options[$field_id] : '' ); |
| 830 | echo '<input type="hidden" id="' . esc_attr( $field_name ) . '" class="asenha-subfield-text" name="' . esc_attr( $field_name ) . '" value="' . esc_attr( $field_option_value ) . '">' ; |
| 831 | } |
| 832 | |
| 833 | /** |
| 834 | * Render textarea field as sub-field of a toggle/switcher checkbox |
| 835 | * |
| 836 | * @since 2.3.0 |
| 837 | */ |
| 838 | function render_datatable( $args ) |
| 839 | { |
| 840 | global $wpdb ; |
| 841 | $option_name = $args['option_name']; |
| 842 | |
| 843 | if ( !empty($option_name) ) { |
| 844 | $options = get_option( $option_name, array() ); |
| 845 | } else { |
| 846 | $options = get_option( ASENHA_SLUG_U, array() ); |
| 847 | } |
| 848 | |
| 849 | $field_id = $args['field_id']; |
| 850 | $field_slug = $args['field_slug']; |
| 851 | $field_name = $args['field_name']; |
| 852 | $field_type = $args['field_type']; |
| 853 | $field_description = $args['field_description']; |
| 854 | $table_title = $args['table_title']; |
| 855 | $table_name = $args['table_name']; |
| 856 | $field_option_value = ( isset( $options[$args['field_id']] ) ? $options[$args['field_id']] : '' ); |
| 857 | ?> |
| 858 | <table id="login-attempts-log" class="wp-list-table widefat striped datatable"> |
| 859 | <thead> |
| 860 | <tr class="datatable-tr"> |
| 861 | <th class="datatable-th">IP Address<br />Last Username</th> |
| 862 | <th class="datatable-th">Attempts<br />Lockouts</th> |
| 863 | <th class="datatable-th">Last Attempt On</th> |
| 864 | </tr> |
| 865 | </thead> |
| 866 | <tbody> |
| 867 | <?php |
| 868 | $limit = 1000; |
| 869 | $sql = $wpdb->prepare( "SELECT * FROM {$table_name} ORDER BY unixtime DESC LIMIT %d", array( $limit ) ); |
| 870 | $entries = $wpdb->get_results( $sql, ARRAY_A ); |
| 871 | foreach ( $entries as $entry ) { |
| 872 | $unixtime = $entry['unixtime']; |
| 873 | |
| 874 | if ( function_exists( 'wp_date' ) ) { |
| 875 | $date = wp_date( 'F j, Y', $unixtime ); |
| 876 | $time = wp_date( 'H:i:s', $unixtime ); |
| 877 | } else { |
| 878 | $date = date_i18n( 'F j, Y', $unixtime ); |
| 879 | $time = date_i18n( 'H:i:s', $unixtime ); |
| 880 | } |
| 881 | |
| 882 | ?> |
| 883 | <tr class="datatable-tr"> |
| 884 | <td class="datatable-td"><?php |
| 885 | echo esc_html( $entry['ip_address'] ) ; |
| 886 | ?><br /><?php |
| 887 | echo esc_html( $entry['username'] ) ; |
| 888 | ?></td> |
| 889 | <td class="datatable-td"><?php |
| 890 | echo esc_html( $entry['fail_count'] ) ; |
| 891 | ?><br /><?php |
| 892 | echo esc_html( $entry['lockout_count'] ) ; |
| 893 | ?></td> |
| 894 | <td class="datatable-td"><?php |
| 895 | echo esc_html( $date ) ; |
| 896 | ?><br /><?php |
| 897 | echo esc_html( $time ) ; |
| 898 | ?></td> |
| 899 | </tr> |
| 900 | <?php |
| 901 | } |
| 902 | ?> |
| 903 | </tbody> |
| 904 | </table> |
| 905 | <?php |
| 906 | echo '<input type="hidden" id="' . esc_attr( $field_name ) . '" class="asenha-subfield-datatable" name="' . esc_attr( $field_name ) . '" value="' . esc_attr( $field_option_value ) . '">' ; |
| 907 | } |
| 908 | |
| 909 | } |