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