LLMS_Admin_Settings
Admin settings and fields class
Contents
Source Source
File: includes/admin/class.llms.admin.settings.php
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 | class LLMS_Admin_Settings { /** * Settings array * * @var array */ private static $settings = array (); /** * Errors array * * @var array */ private static $errors = array (); /** * Messages array * * @var array */ private static $messages = array (); /** * Instantiates setting page objects, if not already done, and returns them. * * @since 6.0.0 Removed loading of class files that don't instantiate their class in favor of autoloading. * * @return LLMS_Settings_Page[] self::$settings */ public static function get_settings_tabs() { if ( empty ( self:: $settings ) ) { $settings = array (); $settings [] = include 'settings/class.llms.settings.general.php' ; $settings [] = include 'settings/class.llms.settings.courses.php' ; $settings [] = include 'settings/class.llms.settings.memberships.php' ; $settings [] = include 'settings/class.llms.settings.accounts.php' ; $settings [] = include 'settings/class.llms.settings.checkout.php' ; $settings [] = include 'settings/class.llms.settings.engagements.php' ; $settings [] = include 'settings/class.llms.settings.notifications.php' ; $settings [] = include 'settings/class.llms.settings.integrations.php' ; /** * Allow 3rd parties to add or remove setting pages. * * @since 1.0.0 * * @param LLMS_Settings_Page[] $settings Setting page objects. */ self:: $settings = apply_filters( 'lifterlms_get_settings_pages' , $settings ); } return self:: $settings ; } /** * Save method. Saves all fields on current tab * * @return void */ public static function save() { global $current_tab ; if ( isset( $_POST [ '_wpnonce' ] ) && ! llms_verify_nonce( '_wpnonce' , 'lifterlms-settings' ) ) { die ( __( 'Whoa! something went wrong there!. Please refresh the page and retry.' , 'lifterlms' ) ); } do_action( 'lifterlms_settings_save_' . $current_tab ); do_action( 'lifterlms_update_options_' . $current_tab ); do_action( 'lifterlms_update_options' ); self::set_message( __( 'Your settings have been saved.' , 'lifterlms' ) ); do_action( 'lifterlms_settings_saved' ); do_action( 'lifterlms_settings_saved_' . $current_tab ); } /** * set message to messages array * * @param string $message * @return void */ public static function set_message( $message ) { self:: $messages [] = $message ; } /** * set message to messages array * * @param string $message * @return void */ public static function set_error( $message ) { self:: $errors [] = $message ; } /** * display messages in settings * * @return void */ public static function display_messages_html() { if ( count ( self:: $errors ) > 0 ) { foreach ( self:: $errors as $error ) { echo '<div class="error"><p><strong>' . $error . '</strong></p></div>' ; } } elseif ( count ( self:: $messages ) > 0 ) { foreach ( self:: $messages as $message ) { echo '<div class="updated"><p><strong>' . $message . '</strong></p></div>' ; } } } /** * Settings Page output tabs * * @since 1.0.0 * @since 3.29.0 Unknown. * @since 3.35.0 Sanitize `$_GET` data. * @since 3.35.1 Fix issue causing data to be saved on every page load. * @since 5.9.0 Stop using deprecated `FILTER_SANITIZE_STRING`. * * @return void */ public static function output() { global $current_tab ; do_action( 'lifterlms_settings_start' ); self::get_settings_tabs(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonce is checked in self::save(). $current_tab = empty ( $_GET [ 'tab' ] ) ? 'general' : llms_filter_input_sanitize_string( INPUT_GET, 'tab' ); // phpcs:disable WordPress.Security.NonceVerification.Missing -- nonce is checked in self::save(). if ( ! empty ( $_POST ) ) { self::save(); } // phpcs:enable WordPress.Security.NonceVerification.Missing. $err = llms_filter_input_sanitize_string( INPUT_GET, 'llms_error' ); if ( $err ) { self::set_error( $err ); } $msg = llms_filter_input_sanitize_string( INPUT_GET, 'llms_message' ); if ( $msg ) { self::set_message( $msg ); } self::display_messages_html(); $tabs = apply_filters( 'lifterlms_settings_tabs_array' , array () ); include 'views/settings.php' ; } /** * Output fields for settings tabs. Dynamically generates fields. * * Needs to be refactored! Sets up all of the fields..gross... * * @return void */ public static function output_fields( $settings ) { foreach ( $settings as $field ) { // Skip item if no field type is set. if ( ! isset( $field [ 'type' ] ) ) { continue ; } // Output the field. self::output_field( $field ); } } /** * Output fields * * @since Unknown. * @since 3.29.0 Unknown. * @since 3.34.4 Add "keyval" field for displaying custom html next to a setting key. * @since 3.37.9 Add option for fields to show an asterisk for required fields. * @since 5.0.2 Pass any option value sanitized as a "slug" through `urldecode()` prior to displaying it. * @since 7.0.0 Add `$after_html` to all field types. * * @param array $field { * Array of field settings. * * @type string $id The setting ID. Used as the from element's `name` and `id` attributes and * automatically correspond to an option key using the WP options API. * @type string $type The field type. Accepts: 'title', 'table', 'subtitle', 'desc', 'custom-html', * 'custom-html-no-wrap', 'sectionstart', 'sectionend', 'button', 'hidden', 'keyval', * 'text', 'email', 'number', 'password', 'textarea', 'wpeditor', 'select', 'multiselect', * 'radio', 'checkbox', 'image', 'single_select_page', 'single_select_membership'. * @type string $title The title / name of the option as displayed to the user. * @type string $name For "button" fields only: used as HTML `name` attribute. If not supplied the default * value "save" will be used. For other field types used as a fallback for `$title` if * no value is supplied. * @type string $class A space-separated list of CSS class names to apply the setting form element (the * `<input>`, `<select>` etc...). * @type string $css An inline CSS style string. * @type string $default The default value of the setting. * @type string $desc The setting's description. * @type bool $desc_tooltip If `true`, displays `$desc` in a hoverable tooltip. * @type string $value The value of the setting. If supplied this will override the automatic setting retrieval * using `get_option( $id, $default )`. * @type array $custom_attributes An associative array of custom HTML attributes to be added to the form element (the * `<input>`, `<select>` etc...). * @type bool $disabled If `true` adds the `llms-disabled-field` class to the settings field wrapper. * @type bool $required If `true`, text, email, number, and password fields will require user input. * @type string $secure_option The name of settings secure option equivalent. If specified, the fields value will be * automatically removed from the database and the value will be masked when displayed on * on screen. See {@see llms_get_secure_option()} for more information. * @type string $sanitize Automatically apply the specified sanitization to the value before storing and outputting * the stored value. Supported filters: * + "slug": Uses `sanitize_title()` on the value when storing and `urldecode()` when displaying. * @type string $after_html Additional HTML to add after the field's form element. * @type array $editor_settings Used with "wpeditor" field type only. An array of options to pass to `wp_editor()` as the `$settings` argument. * @type array $options For "select", "multiselect", and "radio" fields, an array of key/value pairs where the * key is the setting value stored in database and the value is the setting label displayed * on screen. * } * @return void */ public static function output_field( $field ) { // Set missing values with defaults. $field = self::set_field_defaults( $field ); // Setup custom attributes. $custom_attributes = self::format_field_custom_attributes( $field [ 'custom_attributes' ] ?? array () ); // Setup field description and tooltip. extract( self::set_field_descriptions( $field ) ); $description .= ' ' . $field [ 'after_html' ]; // Get the field value. $option_value = isset( $field [ 'value' ] ) ? $field [ 'value' ] : self::get_option( $field [ 'id' ], $field [ 'default' ] ); // Setup the disabled CSS class. $disabled_class = ( isset( $field [ 'disabled' ] ) && true === $field [ 'disabled' ] ) ? 'llms-disabled-field' : '' ; // Switch based on type. switch ( $field [ 'type' ] ) { // Section Titles. case 'title' : if ( ! empty ( $field [ 'title' ] ) ) { echo '<p class="llms-label">' . esc_html( $field [ 'title' ] ) . '</p>' ; } if ( ! empty ( $field [ 'desc' ] ) ) { echo '<p class="llms-description">' . wpautop( wptexturize( wp_kses_post( $field [ 'desc' ] ) ) ) . '</p>' ; } echo '<table class="form-table">' . "\n\n" ; if ( ! empty ( $field [ 'id' ] ) ) { do_action( 'lifterlms_settings_' . sanitize_title( $field [ 'id' ] ) ); } break ; case 'table' : echo '<tr valign="top" class="' . $disabled_class . '"><td>' ; $field [ 'table' ]->get_results(); echo $field [ 'table' ]->get_table_html(); echo '</td></tr>' ; break ; case 'subtitle' : if ( ! empty ( $field [ 'title' ] ) ) { echo '<tr valign="top" class="' . $disabled_class . '"><td colspan= "2" > <h3 class = "llms-subtitle" > ' . $field[' title '] . ' </h3>'; if ( ! empty ( $field [ 'desc' ] ) ) { echo '<p>' . $field [ 'desc' ] . '</p>' ; } echo '</tr></td>' ; } break ; case 'desc' : if ( ! empty ( $field [ 'desc' ] ) ) { echo '<th colspan="2" style="font-weight: normal;">' . wpautop( wptexturize( wp_kses_post( $field [ 'desc' ] ) ) ) . '</th>' ; } break ; case 'custom-html' : if ( ! empty ( $field [ 'value' ] ) ) { echo '<tr valign="top" class="' . $disabled_class . '"><td colspan="2">' . $field [ 'value' ] . '</tr></td>' ; } break ; case 'custom-html-no-wrap' : if ( ! empty ( $field [ 'value' ] ) ) { echo $field [ 'value' ]; } break ; case 'sectionstart' : if ( ! empty ( $field [ 'id' ] ) ) { do_action( 'lifterlms_settings_' . sanitize_title( $field [ 'id' ] ) . '_before' ); echo '<div class="llms-setting-group ' . $field [ 'class' ] . '">' ; do_action( 'lifterlms_settings_' . sanitize_title( $field [ 'id' ] ) . '_start' ); } break ; case 'sectionend' : if ( ! empty ( $field [ 'id' ] ) ) { do_action( 'lifterlms_settings_' . sanitize_title( $field [ 'id' ] ) . '_end' ); } echo '</table>' ; echo '</div>' ; if ( ! empty ( $field [ 'id' ] ) ) { do_action( 'lifterlms_settings_' . sanitize_title( $field [ 'id' ] ) . '_after' ); } break ; case 'button' : $name = isset( $field [ 'name' ] ) ? $field [ 'name' ] : 'save' ; echo '<tr valign="top" class="' . $disabled_class . '"><th> <label for = "' . esc_attr( $field['id'] ) . '" > ' . esc_html( $field[' title '] ) . ' </label> ' . $tooltip . ' </th>'; echo '<td class="forminp forminp-' . sanitize_title( $field [ 'type' ] ) . '">' ; echo '<div id="llms-form-wrapper">' ; echo $description . '<br><br>' ; echo '<input name="' . $name . '" class="llms-button-primary" type="submit" value="' . esc_attr( $field [ 'value' ] ) . '" />' ; echo '</div>' ; echo '</td></tr>' ; // phpcs:ignore -- commented out code // get_submit_button( 'Filter Results', 'primary', 'llms_search', true, array( 'id' => 'llms_analytics_search' ) ); break ; case 'hidden' : echo '<th></th>' ; echo '<td><input type= "hidden" name= "' . esc_attr( $field['id'] ) . '" id= "' . esc_attr( $field['id'] ) . '" value= "' . esc_attr( $field['value'] ) . '" >'; break ; case 'keyval' : ?><tr valign= "top" > <th> <label for = "<?php echo esc_attr( $field['id'] ); ?>" ><?php echo esc_html( $field [ 'title' ] ); ?></label> <?php echo $tooltip ; ?> </th> <td class = "forminp forminp-<?php echo sanitize_title( $field['type'] ); ?>" > <div id= "<?php echo esc_attr( $field['id'] ); ?>" ><?php echo $field [ 'value' ]; ?></div> </td> </tr> <?php break ; case 'text' : case 'email' : case 'number' : case 'password' : $type = $field [ 'type' ]; $class = '' ; $required = ! empty ( $field [ 'required' ] ); $secure_val = isset( $field [ 'secure_option' ] ) ? llms_get_secure_option( $field [ 'secure_option' ], false ) : false; $option_value = ( false !== $secure_val ) ? str_repeat ( '*' , strlen ( $secure_val ) ) : $option_value ; // Ensure slugs with non-latin characters are not displayed as urlencoded strings. if ( ! empty ( $field [ 'sanitize' ] ) && 'slug' === $field [ 'sanitize' ] ) { $option_value = urldecode( $option_value ); } ?> <tr valign= "top" class = "<?php echo $disabled_class; ?>" > <th> <label for = "<?php echo esc_attr( $field['id'] ); ?>" > <?php echo esc_html( $field [ 'title' ] ); ?> <?php echo $required ? '<span class="llms-required">*</span>' : '' ; ?> </label> <?php echo $tooltip ; ?> </th> <td class = "forminp forminp-<?php echo sanitize_title( $field['type'] ); ?>" > <input name= "<?php echo esc_attr( $field['id'] ); ?>" id= "<?php echo esc_attr( $field['id'] ); ?>" type= "<?php echo esc_attr( $type ); ?>" style= "<?php echo esc_attr( $field['css'] ); ?>" value= "<?php echo esc_attr( $option_value ); ?>" class = "<?php echo esc_attr( $field['class'] ); ?>" <?php echo $secure_val ? 'disabled="disabled"' : '' ; ?> <?php echo implode( ' ' , $custom_attributes ); ?> <?php echo $required ? 'required="required"' : '' ; ?> /> <?php echo $description ; ?> </td> </tr> <?php break ; // Textarea. case 'textarea' : ?> <tr valign= "top" class = "<?php echo $disabled_class; ?>" > <th> <label for = "<?php echo esc_attr( $field['id'] ); ?>" ><?php echo esc_html( $field [ 'title' ] ); ?></label> <?php echo $tooltip ; ?> </th> <td class = "forminp forminp-<?php echo sanitize_title( $field['type'] ); ?>" > <textarea name= "<?php echo esc_attr( $field['id'] ); ?>" id= "<?php echo esc_attr( $field['id'] ); ?>" style= "<?php echo esc_attr( $field['css'] ); ?>" class = "<?php echo esc_attr( $field['class'] ); ?>" <?php echo implode( ' ' , $custom_attributes ); ?> ><?php echo esc_textarea( $option_value ); ?></textarea> <?php echo $description ; ?> </td> </tr> <?php break ; case 'wpeditor' : $editor_settings = isset( $field [ 'editor_settings' ] ) ? $field [ 'editor_settings' ] : array (); ?> <tr valign= "top" class = "<?php echo $disabled_class; ?>" > <th> <label for = "<?php echo esc_attr( $field['id'] ); ?>" ><?php echo esc_html( $field [ 'title' ] ); ?></label> <?php echo $tooltip ; ?> </th> <td class = "forminp forminp-<?php echo sanitize_title( $field['type'] ); ?>" > <?php wp_editor( $option_value , $field [ 'id' ], $editor_settings ); ?> <?php echo $description ; ?> </td> </tr> <?php break ; // Select boxes. case 'select' : case 'multiselect' : $field_name = esc_attr( $field [ 'id' ] ); if ( 'multiselect' === $field [ 'type' ] ) { $field_name .= '[]' ; } ?> <tr valign= "top" class = "<?php echo $disabled_class; ?>" > <th> <label for = "<?php echo esc_attr( $field['id'] ); ?>" ><?php echo esc_html( $field [ 'title' ] ); ?></label> <?php echo $tooltip ; ?> </th> <td class = "forminp forminp-<?php echo sanitize_title( $field['type'] ); ?>" > <select name= "<?php echo $field_name; ?>" id= "<?php echo esc_attr( $field['id'] ); ?>" style= "<?php echo esc_attr( $field['css'] ); ?>" class = "<?php echo esc_attr( $field['class'] ); ?>" <?php echo implode( ' ' , $custom_attributes ); ?> <?php if ( 'multiselect' === $field [ 'type' ] ) { echo 'multiple="multiple"' ; } ?> > <?php foreach ( $field [ 'options' ] as $key => $val ) { // Convert an array from llms_make_select2_post_array(). if ( is_array ( $val ) ) { $key = $val [ 'key' ]; $val = $val [ 'title' ]; } ?> <option value= "<?php echo esc_attr( $key ); ?>" <?php if ( is_array ( $option_value ) ) { selected( in_array( $key , $option_value ), true ); } else { selected( $option_value , $key ); } ?> ><?php echo $val ; ?></option> <?php } ?> </select> <?php echo $description ; ?> </td> </tr> <?php break ; // Radio inputs. case 'radio' : ?> <tr valign= "top" class = "<?php echo $disabled_class; ?>" > <th> <label for = "<?php echo esc_attr( $field['id'] ); ?>" ><?php echo esc_html( $field [ 'title' ] ); ?></label> <?php echo $tooltip ; ?> </th> <td class = "forminp forminp-<?php echo sanitize_title( $field['type'] ); ?>" > <fieldset> <?php echo $description ; ?> <ul> <?php foreach ( $field [ 'options' ] as $key => $val ) { ?> <li> <label><input name= "<?php echo esc_attr( $field['id'] ); ?>" value= "<?php echo $key; ?>" type= "radio" style= "<?php echo esc_attr( $field['css'] ); ?>" class = "<?php echo esc_attr( $field['class'] ); ?>" <?php echo implode( ' ' , $custom_attributes ); ?> <?php checked( $key , $option_value ); ?> /> <?php echo $val ; ?></label> </li> <?php } ?> </ul> </fieldset> </td> </tr> <?php break ; // Checkbox input. case 'checkbox' : $visbility_class = array (); if ( ! isset( $field [ 'hide_if_checked' ] ) ) { $field [ 'hide_if_checked' ] = false; } if ( ! isset( $field [ 'show_if_checked' ] ) ) { $field [ 'show_if_checked' ] = false; } if ( 'yes' === $field [ 'hide_if_checked' ] || 'yes' === $field [ 'show_if_checked' ] ) { $visbility_class [] = 'hidden_option' ; } if ( 'option' === $field [ 'hide_if_checked' ] ) { $visbility_class [] = 'hide_options_if_checked' ; } if ( 'option' === $field [ 'show_if_checked' ] ) { $visbility_class [] = 'show_options_if_checked' ; } if ( ! isset( $field [ 'checkboxgroup' ] ) || 'start' === $field [ 'checkboxgroup' ] ) { ?> <tr valign= "top" class = "<?php echo esc_attr( implode( ' ', $visbility_class ) ); ?> <?php echo $disabled_class; ?>" > <th><?php echo esc_html( $field [ 'title' ] ); ?></th> <td class = "forminp forminp-checkbox" > <fieldset> <?php } else { ?> <fieldset class = "<?php echo esc_attr( implode( ' ', $visbility_class ) ); ?>" > <?php } if ( ! empty ( $field [ 'title' ] ) ) { ?> <legend class = "screen-reader-text" ><span><?php echo esc_html( $field [ 'title' ] ); ?></span></legend> <?php } ?> <label for = "<?php echo $field['id']; ?>" > <input name= "<?php echo esc_attr( $field['id'] ); ?>" id= "<?php echo esc_attr( $field['id'] ); ?>" type= "checkbox" value= "1" <?php checked( $option_value , 'yes' ); ?> <?php echo implode( ' ' , $custom_attributes ); ?> /> <?php echo $description ; ?> </label> <?php echo $tooltip ; ?> <?php if ( ! isset( $field [ 'checkboxgroup' ] ) || 'end' === $field [ 'checkboxgroup' ] ) { ?> </fieldset> </td> </tr> <?php } else { ?> </fieldset> <?php } break ; case 'image' : $type = $field [ 'type' ]; $class = '' ; if ( $option_value ) { // Media lib object ID. if ( is_numeric ( $option_value ) ) { $size = isset( $field [ 'image_size' ] ) ? $field [ 'image_size' ] : 'medium' ; $attachment = wp_get_attachment_image_src( $option_value , $size ); $src = $attachment [0]; } else { // Raw img src. $src = $option_value ; } } else { $src = '' ; } ?> <tr valign= "top" class = "<?php echo $disabled_class; ?>" > <th> <label for = "<?php echo esc_attr( $field['id'] ); ?>" ><?php echo esc_html( $field [ 'title' ] ); ?></label> <?php echo $tooltip ; ?> </th> <td class = "forminp forminp-<?php echo sanitize_title( $field['type'] ); ?>" > <img class = "llms-image-field-preview" src= "<?php echo $src; ?>" > <button class = "llms-button-secondary llms-image-field-upload" data-id= "<?php echo esc_attr( $field['id'] ); ?>" type= "button" > <span class = "dashicons dashicons-admin-media" ></span> <?php _e( 'Upload' , 'lifterlms' ); ?> </button> <button class = "llms-button-danger llms-image-field-remove<?php echo ( ! $src ) ? ' hidden' : ''; ?>" data-id= "<?php echo esc_attr( $field['id'] ); ?>" type= "button" > <span class = "dashicons dashicons-no" ></span> </button> <input name= "<?php echo esc_attr( $field['id'] ); ?>" id= "<?php echo esc_attr( $field['id'] ); ?>" type= "hidden" style= "<?php echo esc_attr( $field['css'] ); ?>" value= "<?php echo esc_attr( $option_value ); ?>" class = "<?php echo esc_attr( $field['class'] ); ?>" <?php echo implode( ' ' , $custom_attributes ); ?> /> <?php echo $description ; ?> </td> </tr> <?php break ; // Single page selects. case 'single_select_page' : $args = array ( 'name' => $field [ 'id' ], 'id' => $field [ 'id' ], 'sort_column' => 'menu_order' , 'sort_order' => 'ASC' , 'show_option_none' => ' ' , 'class' => $field [ 'class' ], 'echo' => false, 'selected' => absint( self::get_option( $field [ 'id' ] ) ), ); if ( isset( $field [ 'args' ] ) ) { $args = wp_parse_args( $field [ 'args' ], $args ); } ?> <tr valign= "top" class = "single_select_page" > <th><?php echo esc_html( $field [ 'title' ] ); ?> <?php echo $tooltip ; ?></th> <td class = "forminp" > <?php echo str_replace ( ' id=' , " data-placeholder='" . __( 'Select a page… ', ' lifterlms ' ) . "' style= '" . $field[' css '] . "' class = '" . $field[' class '] . "' id=", wp_dropdown_pages( $args ) ); ?> <?php echo $description ; ?> </td> </tr> <?php break ; // Single page selects. case 'single_select_membership' : $args = array ( 'posts_per_page' => -1, 'post_type' => 'llms_membership' , 'nopaging' => true, 'post_status' => 'publish' , 'class' => $field [ 'class' ], 'selected' => absint( self::get_option( $field [ 'id' ] ) ), ); $posts = get_posts( $args ); if ( isset( $field [ 'args' ] ) ) { $args = wp_parse_args( $field [ 'args' ], $args ); } ?> <tr valign= "top" class = "single_select_membership" > <th><?php echo esc_html( $field [ 'title' ] ); ?> <?php echo $tooltip ; ?></th> <td class = "forminp" > <select class = "<?php echo $args['class']; ?>" style= "<?php echo $field['css']; ?>" name= "lifterlms_membership_required" id= "lifterlms_membership_required" > <option value= "" > <?php _e( 'None' , 'lifterlms' ); ?></option> <?php foreach ( $posts as $post ) : setup_postdata( $post ); if ( $args [ 'selected' ] === $post ->ID ) { $selected = 'selected' ; } else { $selected = '' ; } ?> <option value= "<?php echo $post->ID; ?>" <?php echo $selected ; ?> ><?php echo $post ->post_title; ?></option> <?php endforeach ; ?> </select> </td> </tr> <?php break ; // Default: run an action. default : do_action( 'lifterlms_admin_field_' . $field [ 'type' ], $field , $option_value , $description , $tooltip , $custom_attributes ); break ; } } /** * Add and set default values for a field when looping * * @since 1.4.5 * @since 7.0.0 Use `wp_parse_args()` to simplify method logic & add `after_html` default. * * @param array $field Associative array of field data, {@see LLMS_Admin_Settings::output_field()} for a full description. * @return array */ public static function set_field_defaults( $field = array () ) { $field = wp_parse_args( $field , array ( 'id' => '' , 'title' => $field [ 'name' ] ?? '' , 'class' => '' , 'css' => '' , 'default' => '' , 'desc' => '' , 'desc_tooltip' => '' , 'after_html' => '' , ) ); return $field ; } /** * Setup a field's tooltip and description based on supplied values * * @since 1.4.5 * @since 3.24.0 Unknown. * @since 4.2.0 Use a dashicon in place of image for tooltip icon. * * @param array $field Associative array of field data. * @return array { * Associative array containing field description and tooltip HTML. * * @type string $description Description element HTML. * @type string $tooltip Tooltip element HTML. * } */ public static function set_field_descriptions( $field = array () ) { $description = '' ; $tooltip = '' ; if ( true === $field [ 'desc_tooltip' ] ) { $description = '' ; $tooltip = $field [ 'desc' ]; } elseif ( ! empty ( $field [ 'desc_tooltip' ] ) ) { $description = $field [ 'desc' ]; $tooltip = $field [ 'desc_tooltip' ]; } elseif ( ! empty ( $field [ 'desc' ] ) ) { $description = $field [ 'desc' ]; $tooltip = '' ; } if ( $description && in_array( $field [ 'type' ], array ( 'radio' ), true ) ) { $description = '<p style="margin-top:0">' . wp_kses_post( $description ) . '</p>' ; } elseif ( $description && in_array( $field [ 'type' ], array ( 'checkbox' ), true ) ) { $description = wp_kses_post( $description ); } elseif ( $description ) { $description = '<p class="description">' . wp_kses_post( $description ) . '</p>' ; } if ( $tooltip && in_array( $field [ 'type' ], array ( 'checkbox' ), true ) ) { $tooltip = '<p class="description">' . $tooltip . '</p>' ; } elseif ( $tooltip ) { $position = isset( $field [ 'tooltip_position' ] ) ? $field [ 'tooltip_position' ] : 'top-right' ; $tooltip = '<span class="llms-help-tooltip tip--' . esc_attr( $position ) . '" data-tip="' . esc_attr( $tooltip ) . '"><span class="dashicons dashicons-editor-help"></span></span>' ; } return compact( 'description' , 'tooltip' ); } /** * Formats an associative array of custom field attributes as an array of HTML strings * * @param array $attributes associative array of attributes * @return array * * @since 1.4.5 */ public static function format_field_custom_attributes( $attributes = array () ) { // Custom attribute handling. $custom_attributes = array (); foreach ( $attributes as $attribute => $attribute_value ) { $custom_attributes [] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"' ; } return $custom_attributes ; } /** * Get a setting from the settings API. * * @since Unknown * @since 3.7.5 Unknown * * @param string $option_name Option name. * @param mixed $default Optional default value. * @return string */ public static function get_option( $option_name , $default = '' ) { // Array value. if ( strstr ( $option_name , '[' ) ) { parse_str ( $option_name , $option_array ); // Option name is first key. $option_name = current( array_keys ( $option_array ) ); // Get value. $option_values = get_option( $option_name , '' ); $key = key( $option_array [ $option_name ] ); if ( isset( $option_values [ $key ] ) ) { $option_value = $option_values [ $key ]; } else { $option_value = null; } } else { $option_value = get_option( $option_name , null ); } if ( is_array ( $option_value ) ) { $option_value = array_map ( 'stripslashes' , $option_value ); } elseif ( ! is_null ( $option_value ) ) { $option_value = stripslashes ( $option_value ); } return null === $option_value ? $default : $option_value ; } /** * Save admin fields. * * Loops through a LifterLMS settings field options array and saves the values via `update_option()`. * * @since 1.0.0 * @since 3.29.0 Unknown. * @since 3.35.0 Sanitize `$_POST` data. * @since 3.35.2 Don't strip tags on editor and textarea fields that allow HTML. * @since 5.9.0 Stop using deprecated `FILTER_SANITIZE_STRING`. * @since 7.0.0 Add handling for array fields for standard input types. * Account for the `maxlength` input text and textarea attribute. * * @param array $settings Opens array to output * @return boolean */ public static function save_fields( $settings ) { // phpcs:disable WordPress.Security.NonceVerification.Missing -- nonce is checked in self::save(). if ( empty ( $_POST ) ) { return false; } // Options to update will be stored here. $update_options = array (); // Loop options and get values to save. foreach ( $settings as $field ) { if ( ! isset( $field [ 'id' ] ) ) { continue ; } $type = isset( $field [ 'type' ] ) ? sanitize_title( $field [ 'type' ] ) : '' ; // Remove secure options from the database. if ( isset( $field [ 'secure_option' ] ) && llms_get_secure_option( $field [ 'secure_option' ] ) ) { delete_option( $field [ 'id' ] ); continue ; } // Get the option name. $option_value = null; // Determines if the option value is an array. $is_array_option = false !== strpos ( $field [ 'id' ], '[' ); switch ( $type ) { case 'checkbox' : if ( $is_array_option ) { $option_value = self::get_array_field_posted_value( $field [ 'id' ] ) ? 'yes' : 'no' ; } elseif ( isset( $_POST [ $field [ 'id' ] ] ) ) { $option_value = 'yes' ; } else { $option_value = 'no' ; } break ; case 'textarea' : case 'wpeditor' : if ( isset( $_POST [ $field [ 'id' ] ] ) ) { $option_value = wp_kses_post( trim( llms_filter_input( INPUT_POST, $field [ 'id' ], FILTER_DEFAULT ) ) ); } else { $option_value = '' ; } break ; case 'password' : case 'text' : case 'email' : case 'number' : case 'select' : case 'single_select_page' : case 'single_select_membership' : case 'radio' : case 'hidden' : case 'image' : if ( $is_array_option ) { $option_value = self::get_array_field_posted_value( $field [ 'id' ] ); } elseif ( isset( $_POST [ $field [ 'id' ] ] ) ) { $option_value = llms_filter_input_sanitize_string( INPUT_POST, $field [ 'id' ] ); } else { $option_value = '' ; } if ( isset( $field [ 'sanitize' ] ) && 'slug' === $field [ 'sanitize' ] ) { $option_value = sanitize_title( $option_value ); } break ; case 'multiselect' : if ( isset( $_POST [ $field [ 'id' ] ] ) ) { $option_value = llms_filter_input_sanitize_string( INPUT_POST, $field [ 'id' ], array ( FILTER_REQUIRE_ARRAY ) ); } else { $option_value = '' ; } break ; default : /** * Action run for external field types. * * @since Unknown * @deprecated 7.0.0 Use `llms_update_option_{$type}` filter hook instead. * * @param type $arg Description. */ do_action_deprecated( "lifterlms_update_option_{$type}" , array ( $field ), '7.0.0' ); } // Special treatment for the 'maxlength' attribute. if ( in_array( $type , array ( 'text' , 'textarea' ), true ) && isset( $field [ 'custom_attributes' ][ 'maxlength' ] ) ) { $option_value = llms_trim_string( $option_value , (int) $field [ 'custom_attributes' ][ 'maxlength' ], '' ); } /** * Filters the value of a settings field after it has been parsed and sanitized * and before it is saved to the database. * * The dynamic portion of this hook, `{$type}` refers to the setting field type: * email, text, checkbox, etc... * * @since 7.0.0 * * @param string|null $option_value The sanitized option value or `null`. * @param array $field The settings field array. */ $option_value = apply_filters( "llms_update_option_{$type}" , $option_value , $field ); if ( ! is_null ( $option_value ) ) { if ( $is_array_option ) { parse_str ( $field [ 'id' ], $option_array ); // Option name is first key. $option_name = current( array_keys ( $option_array ) ); // Get old option value. if ( ! isset( $update_options [ $option_name ] ) ) { $update_options [ $option_name ] = get_option( $option_name , array () ); } if ( ! is_array ( $update_options [ $option_name ] ) ) { $update_options [ $option_name ] = array (); } // Set keys and value. $key = key( $option_array [ $option_name ] ); $update_options [ $option_name ][ $key ] = $option_value ; } else { $update_options [ $field [ 'id' ] ] = $option_value ; } } /** * Action run prior to the update of a LifterLMS setting field option. * * An update isn't guaranteed after this action if the method's logic can't * find a valid posted valued to persist to the database. * * @since Unknown * * @param array $field The admin setting field array to be updated. */ do_action( 'lifterlms_update_option' , $field ); } // Now save the options. foreach ( $update_options as $name => $value ) { update_option( $name , $value ); } // phpcs:enable WordPress.Security.NonceVerification.Missing return true; } /** * Retrieves the posted value for an array type setting field. * * @since 7.0.0 * * @param string $id The field ID, eg: "my_setting[field_one]". * @return string Returns the (sanitized) posted value or an empty string if it wasn't posted. */ private static function get_array_field_posted_value( $id ) { parse_str ( $id , $parsed_id ); $opt_id = key( $parsed_id ); $opt_key = key( $parsed_id [ $opt_id ] ); $posted = llms_filter_input_sanitize_string( INPUT_POST, $opt_id , array ( FILTER_REQUIRE_ARRAY ) ); return $posted [ $opt_key ] ?? '' ; } } |
Expand full source code Collapse full source code View on GitHub
Methods Methods
- display_messages_html — display messages in settings
- format_field_custom_attributes — Formats an associative array of custom field attributes as an array of HTML strings
- get_array_field_posted_value — Retrieves the posted value for an array type setting field.
- get_option — Get a setting from the settings API.
- get_settings_tabs — Instantiates setting page objects, if not already done, and returns them.
- output — Settings Page output tabs
- output_field — Output fields
- output_fields — Output fields for settings tabs. Dynamically generates fields.
- save — Save method. Saves all fields on current tab
- save_fields — Save admin fields.
- set_error — set message to messages array
- set_field_defaults — Add and set default values for a field when looping
- set_field_descriptions — Setup a field's tooltip and description based on supplied values
- set_message — set message to messages array
Changelog Changelog
Version | Description |
---|---|
4.2.0 | Use dashicons for tooltip icon display. |
3.37.9 | Add option for fields to show an asterisk for required fields. |
3.35.2 | Don't strip tags on editor and textarea fields that allow HTML. |
3.35.1 | Fix saving issue. |
3.35.0 | Sanitize input data. |
3.34.4 | Add "keyval" field for displaying custom html next to a setting key. |
3.29.0 | Unknown. |
1.0.0 | Introduced. |