AdminPage.Menu.class.php
4 years ago
AdminPage.Submenu.class.php
4 years ago
AdminPage.Themes.class.php
4 years ago
AdminPage.class.php
4 years ago
AdminPageSection.class.php
4 years ago
AdminPageSetting.Address.class.php
4 years ago
AdminPageSetting.Checkbox.class.php
4 years ago
AdminPageSetting.ColorPicker.class.php
4 years ago
AdminPageSetting.Count.class.php
4 years ago
AdminPageSetting.Editor.class.php
4 years ago
AdminPageSetting.FileUpload.class.php
4 years ago
AdminPageSetting.HTML.class.php
4 years ago
AdminPageSetting.Image.class.php
4 years ago
AdminPageSetting.InfiniteTable.class.php
4 years ago
AdminPageSetting.McApiKey.class.php
4 years ago
AdminPageSetting.McListMerge.class.php
4 years ago
AdminPageSetting.Number.class.php
4 years ago
AdminPageSetting.OpeningHours.class.php
4 years ago
AdminPageSetting.Ordering.class.php
4 years ago
AdminPageSetting.Radio.class.php
4 years ago
AdminPageSetting.Scheduler.class.php
4 years ago
AdminPageSetting.Select.class.php
4 years ago
AdminPageSetting.SelectMenu.class.php
4 years ago
AdminPageSetting.SelectPost.class.php
4 years ago
AdminPageSetting.SelectTaxonomy.class.php
4 years ago
AdminPageSetting.Text.class.php
4 years ago
AdminPageSetting.Textarea.class.php
4 years ago
AdminPageSetting.Time.class.php
4 years ago
AdminPageSetting.Toggle.class.php
4 years ago
AdminPageSetting.WarningTip.class.php
4 years ago
AdminPageSetting.class.php
4 years ago
Library.class.php
4 years ago
AdminPage.class.php
294 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Register, display and save a settings page in the WordPress admin menu. |
| 5 | * |
| 6 | * @since 1.0 |
| 7 | * @package Simple Admin Pages |
| 8 | */ |
| 9 | |
| 10 | class sapAdminPage_2_6_1 { |
| 11 | |
| 12 | public $title; |
| 13 | public $menu_title; |
| 14 | public $description; // optional description for this page |
| 15 | public $capability; // user permissions needed to edit this panel |
| 16 | public $id; // id of this page |
| 17 | public $sections = array(); // array of sections to display on this page |
| 18 | public $show_button = true; // whether or not to show the Save Changes button |
| 19 | |
| 20 | public $setup_function = 'add_options_page'; // WP function to register the page |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * Initialize the page |
| 25 | * @since 1.0 |
| 26 | */ |
| 27 | public function __construct( $args ) { |
| 28 | |
| 29 | // Parse the values passed |
| 30 | $this->parse_args( $args ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Parse the arguments passed in the construction and assign them to |
| 35 | * internal variables. |
| 36 | * @since 1.1 |
| 37 | */ |
| 38 | private function parse_args( $args ) { |
| 39 | foreach ( $args as $key => $val ) { |
| 40 | switch ( $key ) { |
| 41 | |
| 42 | case 'id' : |
| 43 | $this->{$key} = esc_attr( $val ); |
| 44 | |
| 45 | default : |
| 46 | $this->{$key} = $val; |
| 47 | |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Modify the capability required to save settings on this page |
| 54 | * @since 2.0 |
| 55 | */ |
| 56 | public function modify_required_capability( $cap ) { |
| 57 | return $this->capability; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Add the page to the appropriate menu slot. |
| 62 | * @note The default will be to post to the options page, but other classes |
| 63 | * should override this function. |
| 64 | * @since 1.0 |
| 65 | */ |
| 66 | public function add_admin_menu() { |
| 67 | call_user_func( $this->setup_function, $this->title, $this->menu_title, $this->capability, $this->id, array( $this, 'display_admin_menu' ) ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Add a section to the page |
| 72 | * @since 1.0 |
| 73 | */ |
| 74 | public function add_section( $section ) { |
| 75 | |
| 76 | if ( !$section ) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | $this->sections[ $section->id ] = $section; |
| 81 | |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Register the settings and sanitization callbacks for each setting |
| 86 | * @since 1.0 |
| 87 | */ |
| 88 | public function register_admin_menu() { |
| 89 | |
| 90 | foreach ( $this->sections as $section ) { |
| 91 | $section->add_settings_section(); |
| 92 | |
| 93 | foreach ( $section->settings as $setting ) { |
| 94 | $setting->add_settings_field( $section->id ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | register_setting( $this->id, $this->id, array( $this, 'sanitize_callback' ) ); |
| 99 | |
| 100 | // Modify capability required to save the settings if it's not |
| 101 | // the default `manage_options` |
| 102 | if ( !empty( $this->capability ) && $this->capability !== 'manage_options') { |
| 103 | add_filter( 'option_page_capability_' . $this->id, array( $this, 'modify_required_capability' ) ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Loop through the settings and sanitize the data |
| 109 | * @since 2.0 |
| 110 | */ |
| 111 | public function sanitize_callback( $value ) { |
| 112 | |
| 113 | if ( empty( $_POST['_wp_http_referer'] ) ) { |
| 114 | return $value; |
| 115 | } |
| 116 | |
| 117 | // Get the current page/tab so we only update those settings |
| 118 | parse_str( $_POST['_wp_http_referer'], $referrer ); |
| 119 | $current_page = $this->get_current_page( $referrer ); |
| 120 | |
| 121 | // Use a new empty value so only values for settings that were added are |
| 122 | // passed to the db. |
| 123 | $new_value = array(); |
| 124 | |
| 125 | foreach ( $this->sections as $section ) { |
| 126 | foreach ( $section->settings as $setting ) { |
| 127 | if ( $setting->tab == $current_page ) { |
| 128 | $setting_value = isset( $value[$setting->id] ) ? $value[$setting->id] : ''; |
| 129 | $new_value[$setting->id] = $setting->sanitize_callback_wrapper( $setting_value ); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Pull in the existing values so we never overwrite values that were |
| 135 | // on a different tab |
| 136 | $old_value = get_option( $this->id ); |
| 137 | |
| 138 | if ( is_array( $old_value ) ) { |
| 139 | return array_merge( $old_value, $new_value ); |
| 140 | } else { |
| 141 | return $new_value; |
| 142 | } |
| 143 | |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Get the current page/tab being viewed |
| 148 | * @since 2.0 |
| 149 | */ |
| 150 | public function get_current_page( $request ) { |
| 151 | |
| 152 | if ( !empty( $request['tab'] ) ) { |
| 153 | return $request['tab']; |
| 154 | } elseif ( !empty( $this->default_tab ) ) { |
| 155 | return $this->default_tab; |
| 156 | } else { |
| 157 | return $this->id; |
| 158 | } |
| 159 | |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Output the settings passed to this page |
| 164 | * @since 1.0 |
| 165 | */ |
| 166 | public function display_admin_menu() { |
| 167 | |
| 168 | if ( !$this->title && !count( $this->settings ) ) { |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | if ( !current_user_can( $this->capability ) ) { |
| 173 | wp_die( __('You do not have sufficient permissions to access this page.') ); |
| 174 | } |
| 175 | |
| 176 | $current_page = $this->get_current_page( $_GET ); |
| 177 | |
| 178 | $this->order_tabs_by_rank(); |
| 179 | $this->order_sections_by_rank(); |
| 180 | |
| 181 | ?> |
| 182 | |
| 183 | <div class="wrap sap-settings-page"> |
| 184 | |
| 185 | <?php $this->display_page_title(); ?> |
| 186 | |
| 187 | <?php if ( isset( $this->default_tab ) ) : ?> |
| 188 | <h2 class="nav-tab-wrapper"> |
| 189 | <?php |
| 190 | foreach( $this->sections as $section ) { |
| 191 | |
| 192 | if ( isset( $section->is_tab ) && $section->is_tab === true ) { |
| 193 | |
| 194 | $tab_url = add_query_arg( |
| 195 | array( |
| 196 | 'settings-updated' => false, |
| 197 | 'tab' => $section->id |
| 198 | ) |
| 199 | ); |
| 200 | |
| 201 | $active = $current_page == $section->id ? ' nav-tab-active' : ''; |
| 202 | echo '<a href="' . esc_url( $tab_url ) . '" title="' . esc_attr( $section->title ) . '" class="nav-tab' . $active . '">'; |
| 203 | echo esc_html( $section->title ); |
| 204 | echo '</a>'; |
| 205 | } |
| 206 | } |
| 207 | ?> |
| 208 | </h2> |
| 209 | <?php endif; ?> |
| 210 | |
| 211 | <form method="post" action="options.php" class="sap-parent-form"> |
| 212 | <?php settings_fields( $this->id ); ?> |
| 213 | <?php do_settings_sections( $current_page ); ?> |
| 214 | <?php if ( $this->show_button ) { submit_button(); } ?> |
| 215 | </form> |
| 216 | </div> |
| 217 | |
| 218 | <?php |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Output the title of the page |
| 223 | * @since 1.0 |
| 224 | */ |
| 225 | public function display_page_title() { |
| 226 | |
| 227 | if ( empty( $this->title ) ) { |
| 228 | return; |
| 229 | } |
| 230 | ?> |
| 231 | <h1><?php echo $this->title; ?></h1> |
| 232 | <?php |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Order the tabs as per the value of rank |
| 237 | * @since 2.6 |
| 238 | */ |
| 239 | public function order_tabs_by_rank() |
| 240 | { |
| 241 | // Fetch and Order sections/tabs |
| 242 | $tab_list = []; |
| 243 | $section_list = []; |
| 244 | foreach( $this->sections as $id => $section ) { |
| 245 | if ( isset( $section->is_tab ) && $section->is_tab === true ) { |
| 246 | if( property_exists( $section, 'rank' ) && count( $tab_list ) > $section->rank ) { |
| 247 | // array start from 0, rank start from 1 |
| 248 | array_splice($tab_list, ($section->rank - 1), 0, [$id => $section]); |
| 249 | } |
| 250 | else { |
| 251 | $tab_list[$id] = $section; |
| 252 | } |
| 253 | } |
| 254 | else { |
| 255 | $section_list[$id] = $section; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | $this->sections = array_merge($tab_list, $section_list); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Order the sections in tabs as per the value of rank |
| 264 | * @since 2.6 |
| 265 | */ |
| 266 | public function order_sections_by_rank() |
| 267 | { |
| 268 | // Fetch and Order sections/tabs |
| 269 | $tab_list = []; |
| 270 | $non_tab_list = []; |
| 271 | foreach( $this->sections as $id => $section ) { |
| 272 | if ( isset( $section->is_tab ) && $section->is_tab == true ) { |
| 273 | $tab_list[$id] = $section; |
| 274 | } |
| 275 | else { |
| 276 | $non_tab_list[$id] = $section; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | foreach( $this->sections as $id => $section ) { |
| 281 | if ( empty( $section->is_tab ) ) { |
| 282 | if( property_exists( $section, 'rank' ) && count( $non_tab_list ) > $section->rank ) { |
| 283 | // array start from 0, rank start from 1 |
| 284 | unset( $non_tab_list[$id]); |
| 285 | array_splice($non_tab_list, ($section->rank - 1), 0, [$id => $section]); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | $this->sections = array_merge($tab_list, $non_tab_list); |
| 291 | } |
| 292 | |
| 293 | } |
| 294 |