| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\TemplateHooks\Admin; |
| 4 |
|
| 5 |
use LearnPress\Helpers\Singleton; |
| 6 |
use LearnPress\Helpers\Template; |
| 7 |
use LearnPress\Models\UserModel; |
| 8 |
use LearnPress\Services\UserService; |
| 9 |
use LP_Helper; |
| 10 |
use LP_Request; |
| 11 |
use LP_Settings_Addons; |
| 12 |
use LP_Settings_Advanced; |
| 13 |
use LP_Settings_Cache; |
| 14 |
use LP_Settings_Courses; |
| 15 |
|
| 16 |
defined( 'ABSPATH' ) || exit(); |
| 17 |
|
| 18 |
/** |
| 19 |
* Renders the LearnPress Settings admin page (tabs + sections), |
| 20 |
* replacing the former LP_Submenu_Settings/LP_Abstract_Submenu classes. |
| 21 |
* |
| 22 |
* @since 4.2.8 |
| 23 |
*/ |
| 24 |
class AdminSettingsPage { |
| 25 |
use Singleton; |
| 26 |
|
| 27 |
const PAGE_ID = 'learn-press-settings'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Cached tabs for the current request. |
| 31 |
* |
| 32 |
* @var array|null |
| 33 |
*/ |
| 34 |
private $tabs = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* Singleton initialization hook. |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function init(): void { |
| 42 |
add_action( 'learn-press/admin/page-content-sections', array( $this, 'output_section_nav' ) ); |
| 43 |
add_filter( 'admin_body_class', array( $this, 'body_class' ) ); |
| 44 |
add_action( 'learn-press/admin/page-content-settings', array( $this, 'page_contents' ) ); |
| 45 |
add_action( 'learn-press/admin/page-settings/section-content', array( $this, 'section_content' ) ); |
| 46 |
add_action( 'admin_init', array( $this, 'save_settings' ) ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get settings tabs (cached for the current request). |
| 51 |
* |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
public function get_tabs(): array { |
| 55 |
if ( null !== $this->tabs ) { |
| 56 |
return $this->tabs; |
| 57 |
} |
| 58 |
|
| 59 |
$tabs = apply_filters( |
| 60 |
'learn-press/admin/settings-tabs-array', |
| 61 |
array( |
| 62 |
'general' => include_once LP_PLUGIN_PATH . 'inc/admin/settings/class-lp-settings-general.php', |
| 63 |
'courses' => new LP_Settings_Courses(), |
| 64 |
'profile' => include_once LP_PLUGIN_PATH . 'inc/admin/settings/class-lp-settings-profile.php', |
| 65 |
'payments' => include_once LP_PLUGIN_PATH . 'inc/admin/settings/class-lp-settings-payments.php', |
| 66 |
'emails' => include_once LP_PLUGIN_PATH . 'inc/admin/settings/class-lp-settings-emails.php', |
| 67 |
'permalink' => include_once LP_PLUGIN_PATH . 'inc/admin/settings/class-lp-settings-permalink.php', |
| 68 |
'advanced' => include_once LP_PLUGIN_PATH . 'inc/admin/settings/class-lp-settings-advanced.php', |
| 69 |
'open-ai' => include_once LP_PLUGIN_PATH . 'inc/admin/settings/class-lp-settings-open-ai.php', |
| 70 |
'addons' => include_once LP_PLUGIN_PATH . 'inc/admin/settings/class-lp-settings-addons.php', |
| 71 |
) |
| 72 |
); |
| 73 |
|
| 74 |
// Check if no addon config on tab addons then remove it. |
| 75 |
if ( isset( $tabs['addons'] ) |
| 76 |
&& $tabs['addons'] instanceof LP_Settings_Addons |
| 77 |
&& ! $tabs['addons']->has_sections() ) { |
| 78 |
unset( $tabs['addons'] ); |
| 79 |
} |
| 80 |
|
| 81 |
$this->tabs = apply_filters( 'learn-press/submenu-' . self::PAGE_ID . '-heading-tabs', $tabs ); |
| 82 |
|
| 83 |
return $this->tabs; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get active tab by checking ?tab=tab-name. |
| 88 |
* |
| 89 |
* @return bool|string |
| 90 |
*/ |
| 91 |
public function get_active_tab() { |
| 92 |
$tabs = $this->get_tabs(); |
| 93 |
|
| 94 |
if ( ! $tabs ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
$tab = LP_Helper::sanitize_params_submitted( $_REQUEST['tab'] ?? '' ); |
| 99 |
|
| 100 |
if ( empty( $tab ) || empty( $tabs[ $tab ] ) ) { |
| 101 |
$tab_keys = array_keys( $tabs ); |
| 102 |
$tab = reset( $tab_keys ); |
| 103 |
} |
| 104 |
|
| 105 |
return $tab; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Get sections of the active tab. |
| 110 |
* |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
public function get_sections(): array { |
| 114 |
$tabs = $this->get_tabs(); |
| 115 |
$active_tab = $this->get_active_tab(); |
| 116 |
$sections = array(); |
| 117 |
|
| 118 |
if ( ! empty( $tabs[ $active_tab ] ) && is_callable( array( $tabs[ $active_tab ], 'get_sections' ) ) ) { |
| 119 |
$sections = call_user_func( array( $tabs[ $active_tab ], 'get_sections' ) ); |
| 120 |
} |
| 121 |
|
| 122 |
return apply_filters( 'learn-press/submenu-sections', $sections ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Get active section by checking ?section=section-name. |
| 127 |
* |
| 128 |
* @return bool|string |
| 129 |
*/ |
| 130 |
public function get_active_section() { |
| 131 |
$sections = $this->get_sections(); |
| 132 |
|
| 133 |
if ( ! $sections ) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
$section = LP_Helper::sanitize_params_submitted( $_REQUEST['section'] ?? '' ); |
| 138 |
|
| 139 |
if ( empty( $section ) || empty( $sections[ $section ] ) ) { |
| 140 |
$section_keys = array_keys( $sections ); |
| 141 |
$section = reset( $section_keys ); |
| 142 |
} |
| 143 |
|
| 144 |
return $section; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Output section navigation. Hooked to 'learn-press/admin/page-content-sections'. |
| 149 |
* |
| 150 |
* @return void |
| 151 |
*/ |
| 152 |
public function output_section_nav() { |
| 153 |
if ( self::PAGE_ID !== LP_Request::get_param( 'page' ) ) { |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
$active_section = $this->get_active_section(); |
| 158 |
$sections = $this->get_sections(); |
| 159 |
|
| 160 |
if ( ! $sections ) { |
| 161 |
return; |
| 162 |
} |
| 163 |
?> |
| 164 |
|
| 165 |
<ul class="lp-admin-tab-navs"> |
| 166 |
<?php foreach ( $sections as $slug => $section ) : ?> |
| 167 |
<?php |
| 168 |
$active_class = ( $slug === $active_section ) ? ' nav-section-active' : ''; |
| 169 |
$section_title = apply_filters( 'learn-press/admin/submenu-section-title', $section, $slug ); |
| 170 |
?> |
| 171 |
|
| 172 |
<li class="nav-section<?php echo esc_attr( $active_class ); ?>"> |
| 173 |
<?php if ( $active_class ) : ?> |
| 174 |
<span><?php echo wp_kses_post( $section_title ); ?></span> |
| 175 |
<?php else : ?> |
| 176 |
<a href="<?php echo esc_url_raw( remove_query_arg( 'sub-section', add_query_arg( 'section', $slug ) ) ); ?>"><?php echo wp_kses_post( $section_title ); ?></a> |
| 177 |
<?php endif; ?> |
| 178 |
</li> |
| 179 |
<?php endforeach; ?> |
| 180 |
</ul> |
| 181 |
|
| 182 |
<?php |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Append body classes on the Settings page. |
| 187 |
* |
| 188 |
* @param string $classes |
| 189 |
* |
| 190 |
* @return string |
| 191 |
*/ |
| 192 |
public function body_class( $classes ) { |
| 193 |
if ( self::PAGE_ID !== LP_Request::get_param( 'page' ) ) { |
| 194 |
return $classes; |
| 195 |
} |
| 196 |
|
| 197 |
$classes = $classes ? explode( ' ', $classes ) : array(); |
| 198 |
$classes[] = 'learnpress'; |
| 199 |
$classes[] = 'lp-submenu-settings'; |
| 200 |
$classes = array_unique( array_filter( $classes ) ); |
| 201 |
|
| 202 |
return implode( ' ', $classes ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Render the content of the active tab, hooked to 'learn-press/admin/page-content-settings'. |
| 207 |
* |
| 208 |
* @return void |
| 209 |
*/ |
| 210 |
public function page_contents() { |
| 211 |
$tabs = $this->get_tabs(); |
| 212 |
$active_tab = $this->get_active_tab(); |
| 213 |
$section = $this->get_active_section(); |
| 214 |
|
| 215 |
if ( 'permalink' === $active_tab && isset( $_GET['lp-user-slug-generated'] ) ) { |
| 216 |
$processed = absint( $_GET['lp-user-slug-processed'] ?? 0 ); |
| 217 |
$generated = absint( $_GET['lp-user-slug-generated'] ?? 0 ); |
| 218 |
$skipped = absint( $_GET['lp-user-slug-skipped'] ?? 0 ); |
| 219 |
$failed = absint( $_GET['lp-user-slug-failed'] ?? 0 ); |
| 220 |
?> |
| 221 |
<div class="notice notice-success"> |
| 222 |
<p> |
| 223 |
<?php |
| 224 |
echo esc_html( |
| 225 |
sprintf( |
| 226 |
/* translators: 1: processed users, 2: generated slugs, 3: skipped users, 4: failed users */ |
| 227 |
__( 'User slug generation finished. Processed: %1$d, Generated: %2$d, Skipped: %3$d, Failed: %4$d.', 'learnpress' ), |
| 228 |
$processed, |
| 229 |
$generated, |
| 230 |
$skipped, |
| 231 |
$failed |
| 232 |
) |
| 233 |
); |
| 234 |
?> |
| 235 |
</p> |
| 236 |
</div> |
| 237 |
<?php |
| 238 |
} |
| 239 |
|
| 240 |
$tabs[ $active_tab ]->admin_page_settings( $section, $this->get_sections() ); |
| 241 |
|
| 242 |
$hide_save_button = false; |
| 243 |
if ( 'advanced' === $active_tab && 'mcp' === $section && class_exists( 'LP_Settings_Advanced' ) ) { |
| 244 |
$hide_save_button = ! learn_press_is_mcp_available(); |
| 245 |
} |
| 246 |
?> |
| 247 |
|
| 248 |
<?php if ( ! $hide_save_button ) : ?> |
| 249 |
<input type="hidden" name="lp-settings-nonce" value="<?php echo wp_create_nonce( 'lp-settings' ); ?>"> |
| 250 |
<p class="lp-admin-settings-buttons"> |
| 251 |
<button class="button button-primary"><?php esc_html_e( 'Save settings', 'learnpress' ); ?></button> |
| 252 |
</p> |
| 253 |
<?php endif; ?> |
| 254 |
<?php |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Section content, hooked to 'learn-press/admin/page-settings/section-content'. |
| 259 |
* |
| 260 |
* @param string $section |
| 261 |
* |
| 262 |
* @return void |
| 263 |
*/ |
| 264 |
public function section_content( $section ) { |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Render tab content wrapper: sections nav + active tab content dispatch. |
| 269 |
* |
| 270 |
* @return void |
| 271 |
*/ |
| 272 |
private function page_content() { |
| 273 |
do_action( 'learn-press/admin/page-content-sections' ); |
| 274 |
|
| 275 |
echo '<div class="lp-admin-tab-content">'; |
| 276 |
|
| 277 |
if ( $this->get_tabs() ) { |
| 278 |
$tab = $this->get_active_tab(); |
| 279 |
|
| 280 |
do_action( 'learn-press/admin/before-page-content-sections', 'settings', $tab ); |
| 281 |
do_action( 'learn-press/admin/page-content-settings', $tab ); |
| 282 |
do_action( 'learn-press/admin/page-content-settings/' . $tab ); |
| 283 |
do_action( 'learn-press/admin/after-page-content-sections', 'settings', $tab ); |
| 284 |
} |
| 285 |
|
| 286 |
echo '</div>'; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Main callback for the Settings admin page. Used as 'callback' in |
| 291 |
* config/wp-menus.php, same pattern as the other menus. |
| 292 |
* |
| 293 |
* @return void |
| 294 |
*/ |
| 295 |
public function html_page() { |
| 296 |
$tabs = $this->get_tabs(); |
| 297 |
$active_tab = $this->get_active_tab(); |
| 298 |
|
| 299 |
$flat_tabs = array(); |
| 300 |
foreach ( $tabs as $tab_slug => $tab_obj ) { |
| 301 |
$tab_id = $tab_obj->id ?? $tab_slug; |
| 302 |
$tab_title = $tab_obj->text ?? $tab_slug; |
| 303 |
$flat_tabs[ $tab_id ] = apply_filters( 'learn-press/admin/submenu-heading-tab-title', $tab_title, $tab_id ); |
| 304 |
} |
| 305 |
|
| 306 |
ob_start(); |
| 307 |
do_action( 'learn-press/admin/heading-icon', $active_tab ); |
| 308 |
echo esc_html__( 'Settings', 'learnpress' ); |
| 309 |
do_action( 'learn-press/admin/heading-title', $active_tab ); |
| 310 |
$title = ob_get_clean(); |
| 311 |
|
| 312 |
$classes = array( 'lp-admin-tabs' ); |
| 313 |
$sections = $this->get_sections(); |
| 314 |
|
| 315 |
$has_sections = $sections && sizeof( $sections ) > 1; |
| 316 |
$has_sections = apply_filters( 'learn-press/admin/submenu-has-sections', $has_sections, $sections, $active_tab ); |
| 317 |
|
| 318 |
if ( $has_sections ) { |
| 319 |
$classes[] = 'has-sections'; |
| 320 |
} |
| 321 |
|
| 322 |
ob_start(); |
| 323 |
$this->page_content(); |
| 324 |
$page_content = ob_get_clean(); |
| 325 |
|
| 326 |
$wrapper = array( |
| 327 |
sprintf( |
| 328 |
'<form class="%s" method="post" enctype="multipart/form-data">', |
| 329 |
esc_attr( implode( ' ', $classes ) ) |
| 330 |
) => '</form>', |
| 331 |
); |
| 332 |
|
| 333 |
echo AdminTemplate::html_on_wp_admin_screen( |
| 334 |
array( |
| 335 |
'tabs' => $flat_tabs, |
| 336 |
'content' => Template::instance()->nest_elements( $wrapper, $page_content ), |
| 337 |
'title' => $title, |
| 338 |
'id' => self::PAGE_ID, |
| 339 |
) |
| 340 |
); |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Save settings of the active tab. Hooked to 'admin_init'. |
| 345 |
* |
| 346 |
* @return void |
| 347 |
*/ |
| 348 |
public function save_settings() { |
| 349 |
if ( ! current_user_can( UserModel::ROLE_ADMINISTRATOR ) |
| 350 |
|| ! is_admin() || ! isset( $_GET['page'] ) |
| 351 |
|| self::PAGE_ID !== $_GET['page'] ) { |
| 352 |
return; |
| 353 |
} |
| 354 |
|
| 355 |
$nonce = learn_press_get_request( 'lp-settings-nonce' ); |
| 356 |
|
| 357 |
if ( ! wp_verify_nonce( $nonce, 'lp-settings' ) ) { |
| 358 |
return; |
| 359 |
} |
| 360 |
|
| 361 |
$tabs = $this->get_tabs(); |
| 362 |
$active_tab = $this->get_active_tab(); |
| 363 |
|
| 364 |
$tabs[ $active_tab ]->save_settings( $this->get_active_section(), $this->get_sections() ); |
| 365 |
|
| 366 |
$redirect_args = array(); |
| 367 |
if ( 'permalink' === $active_tab && |
| 368 |
'yes' === LP_Request::get_param( 'lp_generate_user_slug' ) ) { |
| 369 |
$result = UserService::instance()->generate_users_pretty_slug(); |
| 370 |
$redirect_args = array( |
| 371 |
'lp-user-slug-generated' => $result['generated'], |
| 372 |
'lp-user-slug-processed' => $result['processed'], |
| 373 |
'lp-user-slug-skipped' => $result['skipped'], |
| 374 |
'lp-user-slug-failed' => $result['failed'], |
| 375 |
); |
| 376 |
} |
| 377 |
|
| 378 |
do_action( 'learn-press/update-settings/updated' ); |
| 379 |
|
| 380 |
// Clear cache settings. |
| 381 |
$lp_settings_cache = new LP_Settings_Cache( true ); |
| 382 |
$lp_settings_cache->clean_lp_settings(); |
| 383 |
|
| 384 |
// Flush rewrite rules after save settings. |
| 385 |
if ( isset( $_REQUEST['tab'] ) && 'permalink' === $_REQUEST['tab'] ) { |
| 386 |
flush_rewrite_rules(); |
| 387 |
} |
| 388 |
|
| 389 |
// Filter redirect. |
| 390 |
$redirect = apply_filters( |
| 391 |
'learn-press/update-settings/redirect', |
| 392 |
esc_url_raw( |
| 393 |
add_query_arg( |
| 394 |
array_merge( |
| 395 |
array( 'settings-updated' => 'yes' ), |
| 396 |
$redirect_args |
| 397 |
) |
| 398 |
) |
| 399 |
) |
| 400 |
); |
| 401 |
|
| 402 |
if ( $redirect ) { |
| 403 |
wp_safe_redirect( $redirect ); |
| 404 |
exit(); |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|