| @@ -9,12 +9,11 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | namespace TUTOR; |
| 12 | 12 | |
| 13 | -defined( 'ABSPATH' ) || exit; | |
| 14 | - | |
| 15 | -use Tutor\Helpers\UrlHelper; | |
| 16 | - | |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 14 | + exit; | |
| 15 | +} | |
| 17 | 16 | /** |
| 18 | 17 | * Dashboard Class |
| 19 | 18 | * |
| 20 | 19 | * @since 1.3.4 |
| @@ -21,225 +20,19 @@ | ||
| 21 | 20 | */ |
| 22 | 21 | class Dashboard { |
| 23 | 22 | |
| 24 | 23 | /** |
| 25 | - * Dashboard page constants | |
| 26 | - * | |
| 27 | - * @since 4.0.0 | |
| 28 | - */ | |
| 29 | - const ACCOUNT_PAGE_SLUG = 'account'; | |
| 30 | - const COURSES_PAGE_SLUG = 'courses'; | |
| 31 | - const QUIZ_ATTEMPTS_PAGE_SLUG = 'quiz-attempts'; | |
| 32 | - const MY_QUIZ_ATTEMPTS_SUBPAGE_SLUG = 'my-quiz-attempts'; | |
| 33 | - const REVIEW_PAGE_SLUG = 'reviews'; | |
| 34 | - const PROFILE_PAGE_SLUG = 'my-profile'; | |
| 35 | - const SETTINGS_PAGE_SLUG = 'settings'; | |
| 36 | - const Q_AND_A_PAGE_SLUG = 'question-answer'; | |
| 37 | - const DISCUSSION_PAGE_SLUG = 'discussions'; | |
| 38 | - const PURCHASE_HISTORY_PAGE_SLUG = 'purchase_history'; | |
| 39 | - const BILLING_PAGE_SLUG = 'billing'; | |
| 40 | - const WISHLIST_PAGE_SLUG = 'wishlist'; | |
| 41 | - const ENROLLED_COURSES_PAGE_SLUG = 'enrolled-courses'; | |
| 42 | - | |
| 43 | - /** | |
| 44 | 24 | * Constructor |
| 45 | 25 | * |
| 46 | 26 | * @since 1.3.4 |
| 47 | - * | |
| 48 | 27 | * @return void |
| 49 | 28 | */ |
| 50 | 29 | public function __construct() { |
| 51 | 30 | add_action( 'tutor_load_template_after', array( $this, 'tutor_load_template_after' ), 10, 2 ); |
| 52 | 31 | add_filter( 'should_tutor_load_template', array( $this, 'should_tutor_load_template' ), 10, 2 ); |
| 53 | - add_action( 'template_redirect', array( $this, 'redirect_old_dashboard_pages' ) ); | |
| 54 | - add_filter( 'tutor_dashboard_back_url', array( $this, 'filter_dashboard_back_url' ) ); | |
| 55 | 32 | } |
| 56 | 33 | |
| 57 | 34 | /** |
| 58 | - * Redirect user old dashboard page slugs to new slugs. | |
| 59 | - * | |
| 60 | - * @since 4.0.0 | |
| 61 | - * | |
| 62 | - * @return void | |
| 63 | - */ | |
| 64 | - public function redirect_old_dashboard_pages() { | |
| 65 | - $current_url = rtrim( UrlHelper::current(), '/' ); | |
| 66 | - | |
| 67 | - if ( ! is_admin() ) { | |
| 68 | - // Redirect logic here. | |
| 69 | - $redirect_mappings = array( | |
| 70 | - tutor_utils()->tutor_dashboard_url( self::PROFILE_PAGE_SLUG ) => self::get_account_page_url( 'profile' ), | |
| 71 | - tutor_utils()->tutor_dashboard_url( self::REVIEW_PAGE_SLUG ) => self::get_account_page_url( self::REVIEW_PAGE_SLUG ), | |
| 72 | - tutor_utils()->tutor_dashboard_url( self::SETTINGS_PAGE_SLUG ) => self::get_account_page_url( self::SETTINGS_PAGE_SLUG ), | |
| 73 | - tutor_utils()->tutor_dashboard_url( self::Q_AND_A_PAGE_SLUG ) => tutor_utils()->tutor_dashboard_url( self::DISCUSSION_PAGE_SLUG ), | |
| 74 | - tutor_utils()->tutor_dashboard_url( self::PURCHASE_HISTORY_PAGE_SLUG ) => self::get_account_page_url( self::BILLING_PAGE_SLUG ), | |
| 75 | - tutor_utils()->tutor_dashboard_url( self::WISHLIST_PAGE_SLUG ) => tutor_utils()->tutor_dashboard_url( 'courses/wishlist' ), | |
| 76 | - tutor_utils()->tutor_dashboard_url( self::MY_QUIZ_ATTEMPTS_SUBPAGE_SLUG ) => tutor_utils()->tutor_dashboard_url( 'courses/my-quiz-attempts' ), | |
| 77 | - tutor_utils()->tutor_dashboard_url( self::ENROLLED_COURSES_PAGE_SLUG ) => tutor_utils()->tutor_dashboard_url( self::COURSES_PAGE_SLUG ), | |
| 78 | - tutor_utils()->get_tutor_dashboard_page_permalink( 'logout' ) => UrlHelper::add_query_params( wp_logout_url(), array( '_wpnonce' => wp_create_nonce( 'log-out' ) ) ), | |
| 79 | - ); | |
| 80 | - | |
| 81 | - if ( ! isset( $redirect_mappings[ $current_url ] ) ) { | |
| 82 | - return; | |
| 83 | - } | |
| 84 | - | |
| 85 | - $redirect_url = $redirect_mappings[ $current_url ]; | |
| 86 | - | |
| 87 | - if ( tutor_utils()->count( $_GET ) ) { | |
| 88 | - $redirect_url = UrlHelper::add_query_params( $redirect_url, $_GET ); | |
| 89 | - } | |
| 90 | - | |
| 91 | - wp_safe_redirect( $redirect_url ); | |
| 92 | - exit; | |
| 93 | - } | |
| 94 | - } | |
| 95 | - | |
| 96 | - | |
| 97 | - /** | |
| 98 | - * Get account page URL. | |
| 99 | - * | |
| 100 | - * @since 4.0.0 | |
| 101 | - * | |
| 102 | - * @param string $page page name. | |
| 103 | - * | |
| 104 | - * @return string | |
| 105 | - */ | |
| 106 | - public static function get_account_page_url( $page = '' ) { | |
| 107 | - $account_page_url = tutor_utils()->tutor_dashboard_url( self::ACCOUNT_PAGE_SLUG ); | |
| 108 | - if ( empty( $page ) ) { | |
| 109 | - return $account_page_url; | |
| 110 | - } | |
| 111 | - | |
| 112 | - return trailingslashit( $account_page_url ) . $page; | |
| 113 | - } | |
| 114 | - | |
| 115 | - /** | |
| 116 | - * Get account pages. | |
| 117 | - * | |
| 118 | - * @since 4.0.0 | |
| 119 | - * | |
| 120 | - * @return array | |
| 121 | - */ | |
| 122 | - public static function get_account_pages() { | |
| 123 | - $pages = array( | |
| 124 | - 'profile' => array( | |
| 125 | - 'title' => esc_html__( 'Profile', 'tutor' ), | |
| 126 | - 'icon' => Icon::USER_CIRCLE, | |
| 127 | - 'icon_active' => Icon::USER_CIRCLE_FILL, | |
| 128 | - 'url' => self::get_account_page_url( 'profile' ), | |
| 129 | - 'template' => tutor_get_template( 'dashboard.account.profile' ), | |
| 130 | - ), | |
| 131 | - 'reviews' => array( | |
| 132 | - 'title' => esc_html__( 'Reviews', 'tutor' ), | |
| 133 | - 'icon' => Icon::RATINGS, | |
| 134 | - 'icon_active' => Icon::RATINGS, | |
| 135 | - 'url' => self::get_account_page_url( 'reviews' ), | |
| 136 | - 'template' => tutor_get_template( 'dashboard.account.reviews' ), | |
| 137 | - ), | |
| 138 | - ); | |
| 139 | - | |
| 140 | - if ( User::is_student_view() ) { | |
| 141 | - $pages['billing'] = array( | |
| 142 | - 'title' => esc_html__( 'Billing', 'tutor' ), | |
| 143 | - 'icon' => Icon::BILLING, | |
| 144 | - 'icon_active' => Icon::BILLING, | |
| 145 | - 'url' => self::get_account_page_url( 'billing' ), | |
| 146 | - 'template' => tutor_get_template( 'dashboard.account.billing' ), | |
| 147 | - ); | |
| 148 | - } | |
| 149 | - | |
| 150 | - if ( User::is_instructor_view() ) { | |
| 151 | - $pages['withdrawals'] = array( | |
| 152 | - 'title' => esc_html__( 'Withdrawals', 'tutor' ), | |
| 153 | - 'icon' => Icon::WALLET, | |
| 154 | - 'icon_active' => Icon::WALLET, | |
| 155 | - 'url' => self::get_account_page_url( 'withdrawals' ), | |
| 156 | - 'template' => tutor_get_template( 'dashboard.account.withdrawals' ), | |
| 157 | - ); | |
| 158 | - } | |
| 159 | - | |
| 160 | - $pages['settings'] = array( | |
| 161 | - 'title' => esc_html__( 'Settings', 'tutor' ), | |
| 162 | - 'icon' => Icon::SETTING, | |
| 163 | - 'icon_active' => Icon::SETTING, | |
| 164 | - 'url' => self::get_account_page_url( 'settings' ), | |
| 165 | - 'template' => tutor_get_template( 'dashboard.account.settings' ), | |
| 166 | - ); | |
| 167 | - | |
| 168 | - return apply_filters( 'tutor_dashboard_account_pages', $pages ); | |
| 169 | - } | |
| 170 | - | |
| 171 | - /** | |
| 172 | - * Get isolated dashboard pages. | |
| 173 | - * | |
| 174 | - * @since 4.0.0 | |
| 175 | - * | |
| 176 | - * @return array | |
| 177 | - */ | |
| 178 | - public static function get_isolated_pages() { | |
| 179 | - $pages = array( | |
| 180 | - self::QUIZ_ATTEMPTS_PAGE_SLUG => array( | |
| 181 | - 'title' => esc_html__( 'Quiz attempts', 'tutor' ), | |
| 182 | - 'template' => tutor_get_template( 'dashboard.quiz-attempts.quiz-reviews' ), | |
| 183 | - 'requires_param' => 'attempt_id', | |
| 184 | - ), | |
| 185 | - self::COURSES_PAGE_SLUG . '/' . self::MY_QUIZ_ATTEMPTS_SUBPAGE_SLUG => array( | |
| 186 | - 'title' => esc_html__( 'Quiz details', 'tutor' ), | |
| 187 | - 'template' => tutor_get_template( 'dashboard.my-quiz-attempts.attempts-details' ), | |
| 188 | - 'requires_param' => 'attempt_id', | |
| 189 | - ), | |
| 190 | - ); | |
| 191 | - | |
| 192 | - return apply_filters( 'tutor_dashboard_isolated_pages', $pages ); | |
| 193 | - } | |
| 194 | - | |
| 195 | - /** | |
| 196 | - * Get isolated dashboard page key. | |
| 197 | - * | |
| 198 | - * @since 4.0.0 | |
| 199 | - * | |
| 200 | - * @param string $dashboard_page Dashboard page slug. | |
| 201 | - * @param string $dashboard_subpage Dashboard subpage slug. | |
| 202 | - * | |
| 203 | - * @return string | |
| 204 | - */ | |
| 205 | - public static function get_isolated_page_key( string $dashboard_page = '', string $dashboard_subpage = '' ): string { | |
| 206 | - if ( self::QUIZ_ATTEMPTS_PAGE_SLUG === $dashboard_page ) { | |
| 207 | - return self::QUIZ_ATTEMPTS_PAGE_SLUG; | |
| 208 | - } | |
| 209 | - | |
| 210 | - if ( self::COURSES_PAGE_SLUG === $dashboard_page && self::MY_QUIZ_ATTEMPTS_SUBPAGE_SLUG === $dashboard_subpage ) { | |
| 211 | - return self::COURSES_PAGE_SLUG . '/' . self::MY_QUIZ_ATTEMPTS_SUBPAGE_SLUG; | |
| 212 | - } | |
| 213 | - | |
| 214 | - return ''; | |
| 215 | - } | |
| 216 | - | |
| 217 | - /** | |
| 218 | - * Check if the current request should use an isolated dashboard page. | |
| 219 | - * | |
| 220 | - * @since 4.0.0 | |
| 221 | - * | |
| 222 | - * @param string $dashboard_page Dashboard page slug. | |
| 223 | - * @param string $dashboard_subpage Dashboard subpage slug. | |
| 224 | - * | |
| 225 | - * @return bool | |
| 226 | - */ | |
| 227 | - public static function is_isolated_page_request( string $dashboard_page = '', string $dashboard_subpage = '' ): bool { | |
| 228 | - $page_key = self::get_isolated_page_key( $dashboard_page, $dashboard_subpage ); | |
| 229 | - $isolated_pages = self::get_isolated_pages(); | |
| 230 | - $page_data = $isolated_pages[ $page_key ] ?? array(); | |
| 231 | - | |
| 232 | - if ( empty( $page_data ) ) { | |
| 233 | - return false; | |
| 234 | - } | |
| 235 | - | |
| 236 | - $required_param = $page_data['requires_param'] ?? ''; | |
| 237 | - | |
| 238 | - return '' === $required_param ? true : Input::has( $required_param ); | |
| 239 | - } | |
| 240 | - | |
| 241 | - /** | |
| 242 | 35 | * Load template after |
| 243 | 36 | * |
| 244 | 37 | * @since 1.3.4 |
| 245 | 38 | * @return void |
| @@ -267,104 +60,6 @@ | ||
| 267 | 60 | if ( 'dashboard.create-course' === $template && ! tutor()->has_pro ) { |
| 268 | 61 | return false; |
| 269 | 62 | } |
| 270 | 63 | return $bool; |
| 271 | - } | |
| 272 | - | |
| 273 | - /** | |
| 274 | - * Filter dashboard back URL. | |
| 275 | - * | |
| 276 | - * @since 4.0.0 | |
| 277 | - * | |
| 278 | - * @param string $url URL. | |
| 279 | - * | |
| 280 | - * @return string | |
| 281 | - */ | |
| 282 | - public function filter_dashboard_back_url( string $url ): string { | |
| 283 | - if ( ! Input::has( 'back_url', Input::GET_REQUEST ) ) { | |
| 284 | - return $url; | |
| 285 | - } | |
| 286 | - | |
| 287 | - $back_url = Input::get( 'back_url', '' ); | |
| 288 | - $decoded = urldecode( $back_url ); | |
| 289 | - if ( ! str_starts_with( $decoded, home_url() ) ) { | |
| 290 | - return $url; | |
| 291 | - } | |
| 292 | - | |
| 293 | - return esc_url_raw( $decoded ); | |
| 294 | - } | |
| 295 | - | |
| 296 | - /** | |
| 297 | - * Build page metadata from page configuration. | |
| 298 | - * | |
| 299 | - * @since 4.0.0 | |
| 300 | - * | |
| 301 | - * @param string $page_slug Page slug. | |
| 302 | - * @param string $page_subslug Page subpage slug. | |
| 303 | - * @param array $pages Page definitions. | |
| 304 | - * | |
| 305 | - * @return array | |
| 306 | - */ | |
| 307 | - public static function get_page_meta_data( string $page_slug = '', string $page_subslug = '', array $pages = array() ): array { | |
| 308 | - $page_data = array(); | |
| 309 | - $page_meta_data = array( | |
| 310 | - 'page_data' => array(), | |
| 311 | - 'meta_title' => '', | |
| 312 | - ); | |
| 313 | - $page_key = trim( $page_slug . '/' . $page_subslug, '/' ); | |
| 314 | - | |
| 315 | - if ( $page_subslug && isset( $pages[ $page_subslug ] ) ) { | |
| 316 | - $page_data = $pages[ $page_subslug ]; | |
| 317 | - } | |
| 318 | - | |
| 319 | - if ( $page_slug && isset( $pages[ $page_slug ] ) ) { | |
| 320 | - $page_data = $pages[ $page_slug ]; | |
| 321 | - } | |
| 322 | - | |
| 323 | - if ( $page_key && isset( $pages[ $page_key ] ) ) { | |
| 324 | - $page_data = $pages[ $page_key ]; | |
| 325 | - } | |
| 326 | - | |
| 327 | - if ( empty( $page_data ) ) { | |
| 328 | - return $page_meta_data; | |
| 329 | - } | |
| 330 | - | |
| 331 | - $page_title = isset( $page_data['title'] ) ? $page_data['title'] : ''; | |
| 332 | - | |
| 333 | - if ( $page_subslug ) { | |
| 334 | - $slug = str_replace( array( '-', '_', '/' ), ' ', (string) $page_subslug ); | |
| 335 | - $page_title = wp_strip_all_tags( ucfirst( $slug ) ); | |
| 336 | - } | |
| 337 | - | |
| 338 | - $site_name = get_bloginfo( 'name' ); | |
| 339 | - $meta_title = ''; | |
| 340 | - | |
| 341 | - if ( '' !== $page_title ) { | |
| 342 | - /* translators: 1: current page title, 2: site name. */ | |
| 343 | - $meta_title = sprintf( __( '%1$s - %2$s', 'tutor' ), $page_title, $site_name ); | |
| 344 | - } | |
| 345 | - | |
| 346 | - $page_meta_data['page_data'] = $page_data; | |
| 347 | - $page_meta_data['meta_title'] = $meta_title; | |
| 348 | - | |
| 349 | - return $page_meta_data; | |
| 350 | - } | |
| 351 | - | |
| 352 | - /** | |
| 353 | - * Set the document title for custom frontend templates. | |
| 354 | - * | |
| 355 | - * @since 4.0.0 | |
| 356 | - * | |
| 357 | - * @param string $title Document title. | |
| 358 | - * | |
| 359 | - * @return void | |
| 360 | - */ | |
| 361 | - public static function set_document_title( string $title ): void { | |
| 362 | - add_filter( | |
| 363 | - 'pre_get_document_title', | |
| 364 | - static function () use ( $title ) { | |
| 365 | - return $title; | |
| 366 | - }, | |
| 367 | - 999 | |
| 368 | - ); | |
| 369 | 64 | } |
| 370 | 65 | } |