'Invalid nonce' ] ); } $user_id = get_current_user_id(); if ( ! $user_id ) { wp_send_json_error( [ 'message' => 'Not logged in' ] ); } $chart_type = sanitize_text_field( $_POST['chart_type'] ?? 'sales' ); $period = sanitize_text_field( $_POST['period'] ?? 'this_month' ); $is_admin = user_can( $user_id, 'administrator' ); $instructor_id = $is_admin ? 0 : $user_id; $filter = $this->get_period_filter( $period ); if ( empty( $filter ) ) { wp_send_json_error( [ 'message' => 'Invalid period' ] ); } $lp_statistic_db = LP_Statistics_DB::getInstance(); if ( $chart_type === 'sales' ) { $raw_data = $lp_statistic_db->get_net_sales_data_scoped( $filter['filter_type'], $filter['time'], $instructor_id ); } else { $raw_data = $lp_statistic_db->get_enrollment_chart_data( $filter['filter_type'], $filter['time'], $instructor_id ); } require_once LP_PLUGIN_PATH . 'inc/rest-api/v1/admin/class-lp-admin-rest-statistics-controller.php'; $stats_controller = new LP_REST_Admin_Statistics_Controller(); $chart_data = $stats_controller->process_chart_data( $filter, $raw_data ); wp_send_json_success( [ 'labels' => $chart_data['labels'] ?? [], 'data' => $chart_data['data'] ?? [], ] ); } catch ( Throwable $e ) { error_log( __METHOD__ . ': ' . $e->getMessage() ); wp_send_json_error( [ 'message' => 'Server error' ] ); } } /** * Convert period string to filter params. * * @param string $period * @return array */ private function get_period_filter( string $period ): array { switch ( $period ) { case 'this_week': return [ 'filter_type' => 'previous_days', 'time' => 6, ]; case 'this_month': return [ 'filter_type' => 'month', 'time' => current_time( 'Y-m-d' ), ]; case 'this_year': return [ 'filter_type' => 'year', 'time' => current_time( 'Y-m-d' ), ]; default: return []; } } }