| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\TemplateHooks\Admin; |
| 4 |
|
| 5 |
use LearnPress\Helpers\Singleton; |
| 6 |
use LP_Admin_Notice; |
| 7 |
use LP_Cache; |
| 8 |
use LP_Helper; |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit(); |
| 11 |
|
| 12 |
/** |
| 13 |
* Admin Tools page renderer. |
| 14 |
* |
| 15 |
* @since 4.2.8 |
| 16 |
*/ |
| 17 |
class AdminToolsPage { |
| 18 |
use Singleton; |
| 19 |
|
| 20 |
/** |
| 21 |
* Singleton initialization hook. |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public function init(): void {} |
| 26 |
|
| 27 |
/** |
| 28 |
* Tools tabs with their labels. |
| 29 |
* |
| 30 |
* @return array |
| 31 |
*/ |
| 32 |
private static function get_tabs(): array { |
| 33 |
return apply_filters( |
| 34 |
'learn-press/admin/tools-tabs', |
| 35 |
array( |
| 36 |
'course' => __( 'Course Data', 'learnpress' ), |
| 37 |
'assign_course' => __( 'Assign/Unassigned Course', 'learnpress' ), |
| 38 |
'database' => __( 'Database', 'learnpress' ), |
| 39 |
'template' => __( 'Templates', 'learnpress' ), |
| 40 |
'lp_beta_version' => __( 'LearnPress Beta Version', 'learnpress' ), |
| 41 |
'cache' => __( 'Cache', 'learnpress' ), |
| 42 |
) |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Map of tool tabs to view paths. |
| 48 |
* |
| 49 |
* @return array |
| 50 |
*/ |
| 51 |
private static function get_tab_views(): array { |
| 52 |
return apply_filters( |
| 53 |
'learn-press/admin/tools-tab-views', |
| 54 |
array( |
| 55 |
'course' => 'tools/html-course', |
| 56 |
'database' => 'tools/html-database', |
| 57 |
'template' => 'tools/html-template', |
| 58 |
) |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Render the LearnPress Tools page. |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
public function html_page() { |
| 68 |
$tabs = self::get_tabs(); |
| 69 |
$tab_views = self::get_tab_views(); |
| 70 |
$tab = isset( $_REQUEST['tab'] ) ? LP_Helper::sanitize_params_submitted( $_REQUEST['tab'] ) : ''; |
| 71 |
$tab = array_key_exists( $tab, $tabs ) ? $tab : ( array_key_first( $tabs ) ?? '' ); |
| 72 |
|
| 73 |
ob_start(); |
| 74 |
?> |
| 75 |
<div class="lp-admin-tab-content"> |
| 76 |
<?php |
| 77 |
if ( isset( $tab_views[ $tab ] ) ) { |
| 78 |
learn_press_admin_view( $tab_views[ $tab ] ); |
| 79 |
} else { |
| 80 |
$this->render_custom_tab( $tab ); |
| 81 |
} |
| 82 |
?> |
| 83 |
</div> |
| 84 |
<?php |
| 85 |
$content = ob_get_clean(); |
| 86 |
|
| 87 |
echo AdminTemplate::html_on_wp_admin_screen( |
| 88 |
array( |
| 89 |
'tabs' => $tabs, |
| 90 |
'content' => $content, |
| 91 |
'title' => __( 'LearnPress Tools', 'learnpress' ), |
| 92 |
'id' => 'learn-press-tools', |
| 93 |
) |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Render tabs that do not have a dedicated view file. |
| 99 |
* |
| 100 |
* @param string $tab Active tab slug. |
| 101 |
* |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
private function render_custom_tab( string $tab ) { |
| 105 |
switch ( $tab ) { |
| 106 |
case 'assign_course': |
| 107 |
learn_press_admin_view( 'tools/course/html-assign-course' ); |
| 108 |
learn_press_admin_view( 'tools/course/html-unassign-course' ); |
| 109 |
break; |
| 110 |
case 'cache': |
| 111 |
$lp_cache = new LP_Cache( true ); |
| 112 |
$lp_cache->clear_all(); |
| 113 |
|
| 114 |
echo sprintf( |
| 115 |
'<form action="" method="post"><button class="button button-primary" type="submit">%s</button></form>', |
| 116 |
esc_html__( 'Clear all cache', 'learnpress' ) |
| 117 |
); |
| 118 |
break; |
| 119 |
case 'lp_beta_version': |
| 120 |
$lp_beta_version_info = LP_Admin_Notice::check_lp_beta_version(); |
| 121 |
learn_press_admin_view( |
| 122 |
'admin-notices/beta-version', |
| 123 |
array( |
| 124 |
'data' => array( |
| 125 |
'check' => 1, |
| 126 |
'info' => $lp_beta_version_info, |
| 127 |
), |
| 128 |
) |
| 129 |
); |
| 130 |
break; |
| 131 |
default: |
| 132 |
do_action( 'learn-press/admin/tools-tab-content', $tab ); |
| 133 |
break; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|