| 1 |
<?php |
| 2 |
/** |
| 3 |
* UsersWP Settings Page/Tab |
| 4 |
* |
| 5 |
* @author AyeCode |
| 6 |
* @category Admin |
| 7 |
* @package userswp/admin |
| 8 |
* @version 1.0.24 |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
if ( ! class_exists( 'UsersWP_Settings_Page', false ) ) : |
| 16 |
|
| 17 |
/** |
| 18 |
* UsersWP_Settings_Page. |
| 19 |
*/ |
| 20 |
abstract class UsersWP_Settings_Page { |
| 21 |
|
| 22 |
/** |
| 23 |
* Setting page id. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $id = ''; |
| 28 |
|
| 29 |
/** |
| 30 |
* Setting page label. |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $label = ''; |
| 35 |
|
| 36 |
/** |
| 37 |
* Constructor. |
| 38 |
*/ |
| 39 |
public function __construct() { |
| 40 |
add_filter( 'uwp_settings_tabs_array', array( $this, 'add_settings_page' ), 20 ); |
| 41 |
add_action( 'uwp_sections_' . $this->id, array( $this, 'output_toggle_advanced' ) ); |
| 42 |
add_action( 'uwp_sections_' . $this->id, array( $this, 'output_sections' ) ); |
| 43 |
add_action( 'uwp_settings_' . $this->id, array( $this, 'output' ) ); |
| 44 |
add_action( 'uwp_settings_save_' . $this->id, array( $this, 'save' ) ); |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get settings page ID. |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
public function get_id() { |
| 53 |
return $this->id; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get settings page label. |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
public function get_label() { |
| 61 |
return $this->label; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Add this page to settings. |
| 66 |
*/ |
| 67 |
public function add_settings_page( $pages ) { |
| 68 |
$pages[ $this->id ] = $this->label; |
| 69 |
|
| 70 |
return $pages; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get settings array. |
| 75 |
* |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
public function get_settings($current_section = '') { |
| 79 |
return apply_filters( 'uwp_get_settings_' . $this->id, array() ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get sections. |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public function get_sections() { |
| 88 |
return apply_filters( 'uwp_get_sections_' . $this->id, array() ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Detect if the advanced settings button should be shown or not. |
| 93 |
* |
| 94 |
* @return bool |
| 95 |
*/ |
| 96 |
public function show_advanced(){ |
| 97 |
global $current_section; |
| 98 |
$show = false; |
| 99 |
$settings = $this->get_settings($current_section); |
| 100 |
|
| 101 |
if(!empty($settings)){ |
| 102 |
foreach($settings as $setting){ |
| 103 |
if(isset($setting['advanced']) && $setting['advanced']){ |
| 104 |
$show = true; |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return $show; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Output the toggle show/hide advanced settings. |
| 115 |
*/ |
| 116 |
public function output_toggle_advanced(){ |
| 117 |
global $hide_advanced_toggle; |
| 118 |
|
| 119 |
if($hide_advanced_toggle){ return;} |
| 120 |
|
| 121 |
// check if we need to show advanced or not |
| 122 |
if(!$this->show_advanced()){return;} |
| 123 |
|
| 124 |
$this->toggle_advanced_button('button button-primary ms-auto ml-auto uwp-advanced-toggle'); |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Toggle advanced button. |
| 130 |
* |
| 131 |
*/ |
| 132 |
public static function toggle_advanced_button($btn_class = 'btn btn-sm btn-primary ms-auto ml-auto uwp-advanced-toggle', $init = true){ |
| 133 |
|
| 134 |
$show = uwp_get_option( 'admin_disable_advanced', false ); |
| 135 |
|
| 136 |
if($show){return;} // don't show advanced toggle |
| 137 |
|
| 138 |
$text_show = __("Show Advanced","userswp"); |
| 139 |
$text_hide = __("Hide Advanced","userswp"); |
| 140 |
|
| 141 |
if(!$show){ |
| 142 |
$toggle_CSS = ''; |
| 143 |
}else{ |
| 144 |
$toggle_CSS = 'uwpa-hide'; |
| 145 |
} |
| 146 |
?> |
| 147 |
<style> |
| 148 |
.uwp-advanced-setting{display: none;} |
| 149 |
.uwp-advanced-setting.uwpa-show{display: block;} |
| 150 |
tr.uwp-advanced-setting.uwpa-show{display: table-row;} |
| 151 |
li.uwp-advanced-setting.uwpa-show{display: list-item;} |
| 152 |
/* Show Advanced */ |
| 153 |
.uwp-advanced-toggle .uwpat-text-show {display: block;} |
| 154 |
.uwp-advanced-toggle .uwpat-text-hide {display: none;} |
| 155 |
|
| 156 |
/* Hide Advanced */ |
| 157 |
.uwp-advanced-toggle.uwpa-hide .uwpat-text-show {display: none;} |
| 158 |
.uwp-advanced-toggle.uwpa-hide .uwpat-text-hide {display: block;} |
| 159 |
</style> |
| 160 |
|
| 161 |
<?php |
| 162 |
|
| 163 |
echo "<button class='".esc_attr($btn_class) ." " . esc_attr( $toggle_CSS ) . "' type=\"button\" >"; |
| 164 |
echo "<span class='uwpat-text-show'>" . esc_html( $text_show ) . "</span>"; |
| 165 |
echo "<span class='uwpat-text-hide'>" . esc_html( $text_hide ) . "</span>"; |
| 166 |
echo "</button>"; |
| 167 |
|
| 168 |
if ($init) { |
| 169 |
|
| 170 |
|
| 171 |
?> |
| 172 |
<script> |
| 173 |
uwp_init_advanced_settings(); |
| 174 |
</script> |
| 175 |
<?php |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Output sections. |
| 181 |
*/ |
| 182 |
public function output_sections() { |
| 183 |
global $current_section; |
| 184 |
|
| 185 |
$sections = $this->get_sections(); |
| 186 |
|
| 187 |
if ( empty( $sections ) || 1 === sizeof( $sections ) ) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
echo '<ul class="subsubsub">'; |
| 192 |
|
| 193 |
$array_keys = array_keys( $sections ); |
| 194 |
|
| 195 |
foreach ( $sections as $id => $label ) { |
| 196 |
echo '<li><a href="' . esc_attr( admin_url( 'admin.php?page=userswp&tab=' . $this->id . '§ion=' . sanitize_title( $id ) ) ) . '" class="' . ( $current_section == $id ? 'current' : '' ) . '">' . esc_html( $label ) . '</a> ' . ( end( $array_keys ) == $id ? '' : '|' ) . ' </li>'; |
| 197 |
} |
| 198 |
|
| 199 |
echo '</ul><br class="clear" />'; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Output the settings. |
| 204 |
*/ |
| 205 |
public function output() { |
| 206 |
$settings = $this->get_settings(); |
| 207 |
|
| 208 |
UsersWP_Admin_Settings::output_fields( $settings ); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Save settings. |
| 213 |
*/ |
| 214 |
public function save() { |
| 215 |
global $current_section; |
| 216 |
|
| 217 |
$settings = $this->get_settings(); |
| 218 |
UsersWP_Admin_Settings::save_fields( $settings ); |
| 219 |
|
| 220 |
if ( $current_section ) { |
| 221 |
do_action( 'uwp_update_options_' . $this->id . '_' . $current_section ); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
endif; |
| 227 |
|