| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | Plugin Name: Settings API Test |
|---|
| 4 | Description: Plugin to test the settings api |
|---|
| 5 | Author: prettyboymp |
|---|
| 6 | License: GPLv2 or later |
|---|
| 7 | */ |
|---|
| 8 | class Test_Settings_1 { |
|---|
| 9 | |
|---|
| 10 | private $settings_page_key; |
|---|
| 11 | |
|---|
| 12 | public function __construct() { |
|---|
| 13 | $this->settings_page_key = 'test-settings-page'; |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | public function initialize() { |
|---|
| 17 | add_action('admin_menu', array($this, '_admin_menu')); |
|---|
| 18 | add_action('admin_init', array($this, '_admin_init')); |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | public function _admin_init() { |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | public function _admin_menu() { |
|---|
| 25 | $hook = add_menu_page('Test Settings', 'Test', 'manage_options', $this->settings_page_key, array($this, '_print_test_settings_page')); |
|---|
| 26 | |
|---|
| 27 | add_action('load-'.$hook, array($this, '_on_load_settings_page_1')); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | public function _on_load_settings_page_1() { |
|---|
| 31 | global $current_screen; |
|---|
| 32 | |
|---|
| 33 | add_contextual_help( $current_screen, '<p>' . __( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.' ) . '</p>' ); |
|---|
| 34 | |
|---|
| 35 | add_settings_section( 'section-a', 'Section A', '', $this->settings_page_key ); |
|---|
| 36 | add_settings_field( 'foo', 'Foo', array( $this, 'display_generic_select_field' ), $this->settings_page_key, 'section-a', array( 'options' => array( 1 => 'Foo', 2 => 'Bar', 3 => 'Quasi' ) ), array( $this, 'sanitize_generic_select_field' ), 'foobar' ); |
|---|
| 37 | add_settings_field( 'bar', 'Bar', array( $this, 'display_generic_text_field' ), $this->settings_page_key, 'section-a', array( ), array( $this, 'sanitize_generic_text_field' ), 'foobar' ); |
|---|
| 38 | add_settings_field( 'someint', 'Some Int', array( $this, 'display_generic_text_field' ), $this->settings_page_key, 'section-a', array( ), array( $this, 'sanitize_generic_int_field' ), 'foobar' ); |
|---|
| 39 | |
|---|
| 40 | add_settings_section( 'section-b', 'Section B', '', $this->settings_page_key ); |
|---|
| 41 | add_settings_field( 'cheese', 'Cheese', array( $this, 'display_generic_text_field' ), $this->settings_page_key, 'section-b', array( ), array( $this, 'sanitize_generic_text_field' ) ); |
|---|
| 42 | add_settings_field( 'wine', 'Wine', array( $this, 'display_generic_text_field' ), $this->settings_page_key, 'section-b', array( ), array( $this, 'sanitize_generic_text_field' ) ); |
|---|
| 43 | |
|---|
| 44 | add_settings_section( 'section-c', 'Section C', '', $this->settings_page_key ); |
|---|
| 45 | add_settings_field( 'mayo', 'mayo', array( $this, 'display_generic_text_field' ), $this->settings_page_key, 'section-c', array( ), array( $this, 'sanitize_generic_text_field' ) ); |
|---|
| 46 | add_settings_field( 'mustard', 'Mustard', array( $this, 'display_generic_text_field' ), $this->settings_page_key, 'section-c', array( ), array( $this, 'sanitize_generic_text_field' ) ); |
|---|
| 47 | |
|---|
| 48 | if( isset( $_REQUEST[ 'action' ] ) ) { |
|---|
| 49 | check_admin_referer( $this->settings_page_key . '-options' ); |
|---|
| 50 | save_settings_page( $this->settings_page_key ); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | public function display_generic_text_field( $args, $current_value, $field, $section_id, $page_id ) { |
|---|
| 56 | ?> |
|---|
| 57 | <input type="text" name="<?php echo esc_attr( $field[ 'id' ] );?>" value="<?php echo esc_attr( $current_value );?>" /> |
|---|
| 58 | <?php |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | public function display_generic_integer_field( $args, $current_value, $field, $section_id, $page_id ) { |
|---|
| 62 | ?> |
|---|
| 63 | <input type="text" id="<?php echo esc_attr( $field[ 'id' ] )?>" name="<?php echo esc_attr( $field[ 'id' ] );?>" value="<?php echo esc_attr( $current_value );?>" /> |
|---|
| 64 | <?php |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | public function display_generic_select_field( $args, $current_value, $field, $section_id, $page_id ) { |
|---|
| 68 | if( isset( $args[ 'options' ] ) && is_array( $args[ 'options' ] ) ) { |
|---|
| 69 | ?> |
|---|
| 70 | <select id="<?php echo esc_attr( $field[ 'id' ] )?>" name="<?php echo esc_attr( $field[ 'id' ] );?>"> |
|---|
| 71 | <?php foreach( $args[ 'options' ] as $key => $name ) :?> |
|---|
| 72 | <option value="<?php echo esc_attr( $key )?>"<?php selected( $key, $current_value )?>><?php echo esc_html( $name )?></option> |
|---|
| 73 | <?php endforeach;?> |
|---|
| 74 | </select> |
|---|
| 75 | <?php |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | public function sanitize_generic_text_field( $new_value, $old_value ) { |
|---|
| 80 | return strip_tags($new_value); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | public function sanitize_generic_select_field( $new_value, $old_value, $field ) { |
|---|
| 84 | if( isset( $field[ 'args' ][ 'options' ] ) && in_array( $new_value, array_keys( $field[ 'args' ][ 'options' ] ) ) ) |
|---|
| 85 | return $new_value; |
|---|
| 86 | |
|---|
| 87 | add_settings_error( $field[ 'id' ], 'invalid-option', sprintf( 'An invalid option was selected for %s', $field[ 'title' ] ) ); |
|---|
| 88 | return $old_value; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | public function sanitize_generic_int_field( $new_value ) { |
|---|
| 92 | return intval( $new_value ); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | public function _print_test_settings_page() { |
|---|
| 97 | $title = __( 'Test Settings' ); |
|---|
| 98 | $page_key = $this->settings_page_key; |
|---|
| 99 | $description = __( 'Some description text' ); |
|---|
| 100 | ?> |
|---|
| 101 | <div class="wrap"> |
|---|
| 102 | <?php screen_icon();?> |
|---|
| 103 | <h2><?php echo $title?></h2> |
|---|
| 104 | <?php settings_errors( $page_key );?> |
|---|
| 105 | <?php if( $description ) |
|---|
| 106 | echo "<p>{$description}</p>";?> |
|---|
| 107 | <form action="" method="post"> |
|---|
| 108 | <?php settings_fields( $this->settings_page_key ); //would like to rename this function ?> |
|---|
| 109 | <?php do_settings_sections( $this->settings_page_key );?> |
|---|
| 110 | <p class="submit"> |
|---|
| 111 | <input type="submit" value="Save Changes" class="button-primary" name="Submit"> |
|---|
| 112 | </p> |
|---|
| 113 | </form> |
|---|
| 114 | </div> |
|---|
| 115 | <?php |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | add_action( 'init', array( new Test_Settings_1(), 'initialize' ) ); |
|---|
| 119 | add_action( 'init', array( new Test_Settings_1(), 'initialize' ) ); //kicking off two instances to be sure that multiple registrations don't interfer |
|---|
| 120 | |
|---|