| 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 |
add_filter( "give_default_setting_tab_section_{$this->id}", array( $this, 'set_default_setting_tab' ), 10 ); |
| 74 |
add_filter( "{$this->current_setting_page}_tabs_array", array( $this, 'add_settings_page' ), 20 ); |
| 75 |
add_action( "{$this->current_setting_page}_settings_{$this->id}_page", array( $this, 'output' ) ); |
| 76 |
|
| 77 |
// Output section only if exist. |
| 78 |
$sections = $this->get_sections(); |
| 79 |
if ( ! empty( $sections ) ) { |
| 80 |
add_action( "{$this->current_setting_page}_sections_{$this->id}_page", array( |
| 81 |
$this, |
| 82 |
'output_sections', |
| 83 |
) ); |
| 84 |
} |
| 85 |
|
| 86 |
// Save hide button by default. |
| 87 |
$GLOBALS['give_hide_save_button'] = true; |
| 88 |
|
| 89 |
// Enable saving feature. |
| 90 |
if ( $this->enable_save ) { |
| 91 |
add_action( "{$this->current_setting_page}_save_{$this->id}", array( $this, 'save' ) ); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
/** |
| 97 |
* Get setting id |
| 98 |
* |
| 99 |
* @since 1.8.17 |
| 100 |
* @access public |
| 101 |
* @return string |
| 102 |
*/ |
| 103 |
public function get_id() { |
| 104 |
return $this->id; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Default setting tab. |
| 109 |
* |
| 110 |
* @since 1.8 |
| 111 |
* |
| 112 |
* @param $setting_tab |
| 113 |
* |
| 114 |
* @return string |
| 115 |
*/ |
| 116 |
function set_default_setting_tab( $setting_tab ) { |
| 117 |
return $this->default_tab; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Add this page to settings. |
| 122 |
* |
| 123 |
* @since 1.8 |
| 124 |
* |
| 125 |
* @param array $pages Lst of pages. |
| 126 |
* |
| 127 |
* @return array |
| 128 |
*/ |
| 129 |
public function add_settings_page( $pages ) { |
| 130 |
$pages[ $this->id ] = $this->label; |
| 131 |
|
| 132 |
return $pages; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get settings array. |
| 137 |
* |
| 138 |
* @since 1.8 |
| 139 |
* @return array |
| 140 |
*/ |
| 141 |
public function get_settings() { |
| 142 |
/** |
| 143 |
* Filter the settings. |
| 144 |
* |
| 145 |
* @since 1.8 |
| 146 |
* |
| 147 |
* @param array $settings |
| 148 |
*/ |
| 149 |
$settings = apply_filters( 'give_get_settings_' . $this->id, array() ); |
| 150 |
|
| 151 |
// Output. |
| 152 |
return $settings; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Get sections. |
| 157 |
* |
| 158 |
* @since 1.8 |
| 159 |
* @return array |
| 160 |
*/ |
| 161 |
public function get_sections() { |
| 162 |
return apply_filters( 'give_get_sections_' . $this->id, array() ); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Output sections. |
| 167 |
* |
| 168 |
* @since 1.8 |
| 169 |
* @return void |
| 170 |
*/ |
| 171 |
public function output_sections() { |
| 172 |
// Get current section. |
| 173 |
$current_section = give_get_current_setting_section(); |
| 174 |
|
| 175 |
// Get all sections. |
| 176 |
$sections = $this->get_sections(); |
| 177 |
|
| 178 |
// Bailout. |
| 179 |
if ( empty( $sections ) ) { |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
// Show section settings only if setting section exist. |
| 184 |
if ( $current_section && ! in_array( $current_section, array_keys( $sections ), true ) ) { |
| 185 |
echo wp_kses_post( '<div class="error"><p>' . __( 'Oops, this settings page does not exist.', 'give' ) . '</p></div>' ); |
| 186 |
$GLOBALS['give_hide_save_button'] = true; |
| 187 |
|
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
if ( is_null( $this->current_setting_page ) ) { |
| 192 |
$this->current_setting_page = give_get_current_setting_page(); |
| 193 |
} |
| 194 |
|
| 195 |
$section_list = array(); |
| 196 |
foreach ( $sections as $id => $label ) { |
| 197 |
/** |
| 198 |
* Fire the filter to hide particular section on tab. |
| 199 |
* |
| 200 |
* @since 2.0 |
| 201 |
*/ |
| 202 |
if ( apply_filters( "give_hide_section_{$id}_on_{$this->id}_page", false, $sections, $this->id ) ) { |
| 203 |
continue; |
| 204 |
} |
| 205 |
|
| 206 |
$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>'; |
| 207 |
} |
| 208 |
|
| 209 |
echo wp_kses_post( sprintf( |
| 210 |
'<ul class="give-subsubsub">%s</ul><br class="clear" /><hr>', |
| 211 |
implode( ' | </li>', $section_list ) |
| 212 |
) ); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Output the settings. |
| 217 |
* |
| 218 |
* Note: if you want to overwrite this function then manage show/hide save button in your class. |
| 219 |
* |
| 220 |
* @since 1.8 |
| 221 |
* @return void |
| 222 |
*/ |
| 223 |
public function output() { |
| 224 |
if ( $this->enable_save ) { |
| 225 |
$GLOBALS['give_hide_save_button'] = false; |
| 226 |
} |
| 227 |
|
| 228 |
$settings = $this->get_settings(); |
| 229 |
|
| 230 |
Give_Admin_Settings::output_fields( $settings, 'give_settings' ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Save settings. |
| 235 |
* |
| 236 |
* @since 1.8 |
| 237 |
* @return void |
| 238 |
*/ |
| 239 |
public function save() { |
| 240 |
$settings = $this->get_settings(); |
| 241 |
$current_section = give_get_current_setting_section(); |
| 242 |
|
| 243 |
/** |
| 244 |
* Use this filter if you want to implement your custom save logic. |
| 245 |
* |
| 246 |
* @since 2.1 |
| 247 |
*/ |
| 248 |
if ( apply_filters( "give_save_options_{$this->id}_{$current_section}", true ) ) { |
| 249 |
Give_Admin_Settings::save_fields( $settings, 'give_settings' ); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Trigger Action |
| 254 |
* |
| 255 |
* @since 1.8 |
| 256 |
*/ |
| 257 |
do_action( 'give_update_options_' . $this->id . '_' . $current_section ); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Get heading labels |
| 262 |
* |
| 263 |
* @since 1.8.7 |
| 264 |
* @access private |
| 265 |
* |
| 266 |
* @return array |
| 267 |
*/ |
| 268 |
private function get_heading() { |
| 269 |
$heading[] = give_get_admin_page_menu_title(); |
| 270 |
$heading[] = $this->label; |
| 271 |
$section = $this->get_sections(); |
| 272 |
$current_section = give_get_current_setting_section(); |
| 273 |
|
| 274 |
if ( array_key_exists( $current_section, $section ) ) { |
| 275 |
$heading[] = $section[ $current_section ]; |
| 276 |
} |
| 277 |
|
| 278 |
return array_unique( $heading ); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Get heading html |
| 283 |
* |
| 284 |
* @since 1.8.7 |
| 285 |
* @access private |
| 286 |
* |
| 287 |
* @return string |
| 288 |
*/ |
| 289 |
public function get_heading_html() { |
| 290 |
return sprintf( |
| 291 |
'<h1 class="wp-heading-inline">%s</h1><hr class="wp-header-end">', |
| 292 |
implode( ' <span class="give-settings-heading-sep dashicons dashicons-arrow-right-alt2"></span> ', $this->get_heading() ) |
| 293 |
); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
endif; |
| 298 |
|