PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.2
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / Ajax / BuilderDashboardAjax.php

BuilderDashboardAjax.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.2, at inc/Ajax/BuilderDashboardAjax.php

109 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * AJAX handler for Dashboard chart data in Course Builder.
4 *
5 * @since 4.3.0
6 * @version 1.0.0
7 */
8
9 namespace LearnPress\Ajax;
10
11 use LearnPress\Helpers\Singleton;
12 use LP_REST_Admin_Statistics_Controller;
13 use LP_Statistics_DB;
14 use Throwable;
15
16 class BuilderDashboardAjax {
17 use Singleton;
18
19 public function init() {
20 add_action( 'wp_ajax_lp_cb_dashboard_chart_data', [ $this, 'handle_chart_data' ] );
21 }
22
23 /**
24 * Handle AJAX request for chart data.
25 *
26 * @return void
27 */
28 public function handle_chart_data() {
29 try {
30 if ( ! wp_verify_nonce( $_POST['nonce'] ?? '', 'lp_cb_dashboard_nonce' ) ) {
31 wp_send_json_error( [ 'message' => 'Invalid nonce' ] );
32 }
33
34 $user_id = get_current_user_id();
35 if ( ! $user_id ) {
36 wp_send_json_error( [ 'message' => 'Not logged in' ] );
37 }
38
39 $chart_type = sanitize_text_field( $_POST['chart_type'] ?? 'sales' );
40 $period = sanitize_text_field( $_POST['period'] ?? 'this_month' );
41 $is_admin = user_can( $user_id, 'administrator' );
42
43 $instructor_id = $is_admin ? 0 : $user_id;
44 $filter = $this->get_period_filter( $period );
45
46 if ( empty( $filter ) ) {
47 wp_send_json_error( [ 'message' => 'Invalid period' ] );
48 }
49
50 $lp_statistic_db = LP_Statistics_DB::getInstance();
51 if ( $chart_type === 'sales' ) {
52 $raw_data = $lp_statistic_db->get_net_sales_data_scoped(
53 $filter['filter_type'],
54 $filter['time'],
55 $instructor_id
56 );
57 } else {
58 $raw_data = $lp_statistic_db->get_enrollment_chart_data(
59 $filter['filter_type'],
60 $filter['time'],
61 $instructor_id
62 );
63 }
64
65 require_once LP_PLUGIN_PATH . 'inc/rest-api/v1/admin/class-lp-admin-rest-statistics-controller.php';
66 $stats_controller = new LP_REST_Admin_Statistics_Controller();
67 $chart_data = $stats_controller->process_chart_data( $filter, $raw_data );
68
69 wp_send_json_success(
70 [
71 'labels' => $chart_data['labels'] ?? [],
72 'data' => $chart_data['data'] ?? [],
73 ]
74 );
75 } catch ( Throwable $e ) {
76 error_log( __METHOD__ . ': ' . $e->getMessage() );
77 wp_send_json_error( [ 'message' => 'Server error' ] );
78 }
79 }
80
81 /**
82 * Convert period string to filter params.
83 *
84 * @param string $period
85 * @return array
86 */
87 private function get_period_filter( string $period ): array {
88 switch ( $period ) {
89 case 'this_week':
90 return [
91 'filter_type' => 'previous_days',
92 'time' => 6,
93 ];
94 case 'this_month':
95 return [
96 'filter_type' => 'month',
97 'time' => current_time( 'Y-m-d' ),
98 ];
99 case 'this_year':
100 return [
101 'filter_type' => 'year',
102 'time' => current_time( 'Y-m-d' ),
103 ];
104 default:
105 return [];
106 }
107 }
108 }
109