add-ons
6 years ago
donors
6 years ago
emails
6 years ago
forms
6 years ago
payments
6 years ago
reports
6 years ago
settings
6 years ago
shortcodes
6 years ago
tools
6 years ago
upgrades
5 years ago
views
6 years ago
abstract-admin-settings-page.php
6 years ago
admin-actions.php
6 years ago
admin-filters.php
6 years ago
admin-footer.php
6 years ago
admin-pages.php
6 years ago
class-addon-activation-banner.php
6 years ago
class-admin-settings.php
6 years ago
class-api-keys-table.php
6 years ago
class-blank-slate.php
6 years ago
class-give-admin.php
6 years ago
class-give-html-elements.php
6 years ago
class-give-welcome.php
6 years ago
class-i18n-module.php
6 years ago
dashboard-widgets.php
6 years ago
give-metabox-functions.php
6 years ago
import-functions.php
6 years ago
misc-functions.php
6 years ago
plugins.php
6 years ago
setting-page-functions.php
6 years ago
abstract-admin-settings-page.php
309 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Give Settings Page/Tab |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Classes/Give_Settings_Page |
| 7 | * @copyright Copyright (c) 2016, GiveWP |
| 8 | * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 | * @since 1.8 |
| 10 | */ |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; // Exit if accessed directly |
| 14 | } |
| 15 | |
| 16 | if ( ! class_exists( 'Give_Settings_Page' ) ) : |
| 17 | |
| 18 | /** |
| 19 | * Give_Settings_Page. |
| 20 | * |
| 21 | * @sine 1.8 |
| 22 | */ |
| 23 | class Give_Settings_Page { |
| 24 | |
| 25 | /** |
| 26 | * Setting page id. |
| 27 | * |
| 28 | * @since 1.8 |
| 29 | * @var string |
| 30 | */ |
| 31 | protected $id = ''; |
| 32 | |
| 33 | /** |
| 34 | * Setting page label. |
| 35 | * |
| 36 | * @since 1.8 |
| 37 | * @var string |
| 38 | */ |
| 39 | protected $label = ''; |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * Default tab. |
| 44 | * |
| 45 | * @since 1.8 |
| 46 | * @var string |
| 47 | */ |
| 48 | protected $default_tab = ''; |
| 49 | |
| 50 | /** |
| 51 | * Current setting page. |
| 52 | * |
| 53 | * @since 1.8 |
| 54 | * @var string|null |
| 55 | */ |
| 56 | private $current_setting_page = null; |
| 57 | |
| 58 | /** |
| 59 | * Flag to check if enable saving option for setting page or not |
| 60 | * |
| 61 | * @since 1.8.17 |
| 62 | * @var bool |
| 63 | */ |
| 64 | protected $enable_save = true; |
| 65 | |
| 66 | /** |
| 67 | * Constructor. |
| 68 | */ |
| 69 | public function __construct() { |
| 70 | // Get current setting page. |
| 71 | $this->current_setting_page = give_get_current_setting_page(); |
| 72 | |
| 73 | // Get current section. |
| 74 | $this->current_section = give_get_current_setting_section(); |
| 75 | |
| 76 | add_filter( "give_default_setting_tab_section_{$this->id}", array( $this, 'set_default_setting_tab' ), 10 ); |
| 77 | add_filter( "{$this->current_setting_page}_tabs_array", array( $this, 'add_settings_page' ), 20 ); |
| 78 | add_action( "{$this->current_setting_page}_settings_{$this->id}_page", array( $this, 'output' ) ); |
| 79 | |
| 80 | // Output sections. |
| 81 | add_action( |
| 82 | "{$this->current_setting_page}_sections_{$this->id}_page", |
| 83 | array( |
| 84 | $this, |
| 85 | 'output_sections', |
| 86 | ) |
| 87 | ); |
| 88 | |
| 89 | // Save hide button by default. |
| 90 | $GLOBALS['give_hide_save_button'] = true; |
| 91 | |
| 92 | // Enable saving feature. |
| 93 | if ( $this->enable_save ) { |
| 94 | add_action( "{$this->current_setting_page}_save_{$this->id}", array( $this, 'save' ) ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * Get setting id |
| 101 | * |
| 102 | * @since 1.8.17 |
| 103 | * @access public |
| 104 | * @return string |
| 105 | */ |
| 106 | public function get_id() { |
| 107 | return $this->id; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Default setting tab. |
| 112 | * |
| 113 | * @since 1.8 |
| 114 | * |
| 115 | * @param $setting_tab |
| 116 | * |
| 117 | * @return string |
| 118 | */ |
| 119 | function set_default_setting_tab( $setting_tab ) { |
| 120 | return $this->default_tab; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Add this page to settings. |
| 125 | * |
| 126 | * @since 1.8 |
| 127 | * |
| 128 | * @param array $pages Lst of pages. |
| 129 | * |
| 130 | * @return array |
| 131 | */ |
| 132 | public function add_settings_page( $pages ) { |
| 133 | $pages[ $this->id ] = $this->label; |
| 134 | |
| 135 | return $pages; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Get settings array. |
| 140 | * |
| 141 | * @since 1.8 |
| 142 | * @return array |
| 143 | */ |
| 144 | public function get_settings() { |
| 145 | /** |
| 146 | * Filter the settings. |
| 147 | * |
| 148 | * @since 1.8 |
| 149 | * |
| 150 | * @param array $settings |
| 151 | */ |
| 152 | $settings = apply_filters( 'give_get_settings_' . $this->id, array() ); |
| 153 | |
| 154 | // Output. |
| 155 | return $settings; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Get sections. |
| 160 | * |
| 161 | * @since 1.8 |
| 162 | * @return array |
| 163 | */ |
| 164 | public function get_sections() { |
| 165 | return apply_filters( 'give_get_sections_' . $this->id, array() ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Output sections. |
| 170 | * |
| 171 | * @since 1.8 |
| 172 | * @return void |
| 173 | */ |
| 174 | public function output_sections() { |
| 175 | // Get current section. |
| 176 | $current_section = give_get_current_setting_section(); |
| 177 | |
| 178 | // Get all sections. |
| 179 | $sections = $this->get_sections(); |
| 180 | |
| 181 | // Bailout. |
| 182 | if ( empty( $sections ) ) { |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | // Show section settings only if setting section exist. |
| 187 | if ( $current_section && ! in_array( $current_section, array_keys( $sections ), true ) ) { |
| 188 | echo wp_kses_post( '<div class="error"><p>' . __( 'Oops, this settings page does not exist.', 'give' ) . '</p></div>' ); |
| 189 | $GLOBALS['give_hide_save_button'] = true; |
| 190 | |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | if ( is_null( $this->current_setting_page ) ) { |
| 195 | $this->current_setting_page = give_get_current_setting_page(); |
| 196 | } |
| 197 | |
| 198 | $section_list = array(); |
| 199 | foreach ( $sections as $id => $label ) { |
| 200 | |
| 201 | // If the `$label` return array then get title from the array as a label. |
| 202 | if ( is_array( $label ) && ! empty( $label['title'] ) ) { |
| 203 | $label = $label['title']; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Fire the filter to hide particular section on tab. |
| 208 | * |
| 209 | * @since 2.0 |
| 210 | */ |
| 211 | if ( apply_filters( "give_hide_section_{$id}_on_{$this->id}_page", false, $sections, $this->id ) ) { |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | $section_list[] = '<li><a href="' . admin_url( 'edit.php?post_type=give_forms&page=' . $this->current_setting_page . '&tab=' . $this->id . '§ion=' . sanitize_title( $id ) ) . '" class="' . ( $current_section === $id ? 'current' : '' ) . '">' . $label . '</a>'; |
| 216 | } |
| 217 | |
| 218 | echo wp_kses_post( |
| 219 | sprintf( |
| 220 | '<ul class="give-subsubsub">%s</ul><br class="clear" /><hr>', |
| 221 | implode( ' | </li>', $section_list ) |
| 222 | ) |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Output the settings. |
| 228 | * |
| 229 | * Note: if you want to overwrite this function then manage show/hide save button in your class. |
| 230 | * |
| 231 | * @since 1.8 |
| 232 | * @return void |
| 233 | */ |
| 234 | public function output() { |
| 235 | if ( $this->enable_save ) { |
| 236 | $GLOBALS['give_hide_save_button'] = false; |
| 237 | } |
| 238 | |
| 239 | $settings = $this->get_settings(); |
| 240 | |
| 241 | Give_Admin_Settings::output_fields( $settings, 'give_settings' ); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Save settings. |
| 246 | * |
| 247 | * @since 1.8 |
| 248 | * @return void |
| 249 | */ |
| 250 | public function save() { |
| 251 | $settings = $this->get_settings(); |
| 252 | $current_section = give_get_current_setting_section(); |
| 253 | |
| 254 | /** |
| 255 | * Use this filter if you want to implement your custom save logic. |
| 256 | * |
| 257 | * @since 2.1 |
| 258 | */ |
| 259 | if ( apply_filters( "give_save_options_{$this->id}_{$current_section}", true ) ) { |
| 260 | Give_Admin_Settings::save_fields( $settings, 'give_settings' ); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Trigger Action |
| 265 | * |
| 266 | * @since 1.8 |
| 267 | */ |
| 268 | do_action( 'give_update_options_' . $this->id . '_' . $current_section ); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Get heading labels |
| 273 | * |
| 274 | * @since 1.8.7 |
| 275 | * @access private |
| 276 | * |
| 277 | * @return array |
| 278 | */ |
| 279 | private function get_heading() { |
| 280 | $heading[] = give_get_admin_page_menu_title(); |
| 281 | $heading[] = $this->label; |
| 282 | $section = $this->get_sections(); |
| 283 | $current_section = give_get_current_setting_section(); |
| 284 | |
| 285 | if ( array_key_exists( $current_section, $section ) ) { |
| 286 | $heading[] = $section[ $current_section ]; |
| 287 | } |
| 288 | |
| 289 | return array_unique( $heading ); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Get heading html |
| 294 | * |
| 295 | * @since 1.8.7 |
| 296 | * @access private |
| 297 | * |
| 298 | * @return string |
| 299 | */ |
| 300 | public function get_heading_html() { |
| 301 | return sprintf( |
| 302 | '<h1 class="wp-heading-inline">%s</h1>', |
| 303 | implode( ' <span class="give-settings-heading-sep dashicons dashicons-arrow-right-alt2"></span> ', $this->get_heading() ) |
| 304 | ); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | endif; |
| 309 |