10, 'redirection' => 0, 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $settings['owner_mobile'] . ':' . $settings['api_token'] ), 'Accept' => 'application/json', ), ) ); if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { return new WP_Error( 'videm_app_courses_failed', __( 'دریافت فهرست دورهها از پنل ویدم ناموفق بود.', 'videm-app' ) ); } $data = json_decode( wp_remote_retrieve_body( $response ), true ); $courses = is_array( $data['data'] ?? null ) ? $data['data'] : array(); $participants = null; foreach ( $courses as $course ) { $count = videm_app_course_participant_count( $course ); if ( null !== $count ) { $participants = (int) $participants + $count; } } $stats = array( 'balance' => isset( $owner['balance'] ) && is_numeric( $owner['balance'] ) ? (float) $owner['balance'] : null, 'courses' => count( $courses ), 'participants' => $participants, ); set_transient( 'videm_app_dashboard_stats', $stats, VIDEM_APP_DASHBOARD_TTL ); return $stats; } /** * Pull a participant count out of a single course object. * * The exact field the panel uses is not fixed, so the common names are tried in turn. * A site can point at the right field with the videm_app_course_participant_count filter. * * @return int|null The count, or null when the course carries no recognizable count. */ function videm_app_course_participant_count( $course ) { $count = apply_filters( 'videm_app_course_participant_count', null, $course ); if ( null !== $count && is_numeric( $count ) ) { return (int) $count; } if ( ! is_array( $course ) ) { return null; } foreach ( array( 'customers_count', 'participants_count', 'students_count', 'members_count', 'customers', 'participants', 'students' ) as $key ) { if ( isset( $course[ $key ] ) ) { $value = $course[ $key ]; if ( is_array( $value ) ) { return count( $value ); } if ( is_numeric( $value ) ) { return (int) $value; } } } return null; } /** Register the dashboard widget for administrators only. */ function videm_app_add_dashboard_widget() { if ( ! current_user_can( 'manage_options' ) ) { return; } wp_add_dashboard_widget( 'videm_app_dashboard', __( 'آمار ویدم', 'videm-app' ), 'videm_app_render_dashboard_widget' ); videm_app_pin_dashboard_widget_first(); } add_action( 'wp_dashboard_setup', 'videm_app_add_dashboard_widget' ); /** * Move the Videm widget to the top of the main dashboard column. * * wp_add_dashboard_widget appends to the "normal" column; this lifts our box to the * front of that column so it always renders first, above the core widgets. A user's * own drag-to-reorder is stored per user and is re-applied by core after this, so this * only sets the default position without overriding a deliberate rearrangement. */ function videm_app_pin_dashboard_widget_first() { global $wp_meta_boxes; if ( empty( $wp_meta_boxes['dashboard']['normal'] ) ) { return; } foreach ( $wp_meta_boxes['dashboard']['normal'] as $priority => $widgets ) { if ( isset( $widgets['videm_app_dashboard'] ) ) { $widget = $widgets['videm_app_dashboard']; unset( $wp_meta_boxes['dashboard']['normal'][ $priority ]['videm_app_dashboard'] ); $existing = isset( $wp_meta_boxes['dashboard']['normal']['high'] ) ? $wp_meta_boxes['dashboard']['normal']['high'] : array(); $wp_meta_boxes['dashboard']['normal']['high'] = array_merge( array( 'videm_app_dashboard' => $widget ), $existing ); break; } } } /** Render the three Videm stats, or a reason the panel data is unavailable. */ function videm_app_render_dashboard_widget() { if ( ! current_user_can( 'manage_options' ) ) { return; } $stats = videm_app_dashboard_stats(); if ( is_wp_error( $stats ) ) { $settings_url = admin_url( 'admin.php?page=videm-app-settings' ); printf( '
%s
', esc_html( $stats->get_error_message() ), esc_url( $settings_url ), esc_html__( 'رفتن به تنظیمات اتصال ویدم', 'videm-app' ) ); return; } $balance = null === $stats['balance'] ? __( 'نامشخص', 'videm-app' ) : number_format_i18n( $stats['balance'], 0 ) . ' ' . __( 'تومان', 'videm-app' ); $participants = null === $stats['participants'] ? __( 'نامشخص', 'videm-app' ) : number_format_i18n( $stats['participants'] ); $cards = array( array( 'label' => __( 'شارژ ویدم', 'videm-app' ), 'value' => $balance, ), array( 'label' => __( 'تعداد دورهها', 'videm-app' ), 'value' => number_format_i18n( $stats['courses'] ), ), array( 'label' => __( 'تعداد شرکتکنندهها', 'videm-app' ), 'value' => $participants, ), ); echo '' . esc_html__( 'تعداد شرکتکنندهها از پاسخ پنل قابلخواندن نبود.', 'videm-app' ) . '
'; } videm_app_render_dashboard_sales(); } /** * Second row: WooCommerce sales of the last 30 days and the count of paid orders * whose Videm enrollment is still incomplete. Shown only when WooCommerce is active. */ function videm_app_render_dashboard_sales() { if ( ! function_exists( 'videm_app_reports_dashboard_snapshot' ) ) { return; } $snapshot = videm_app_reports_dashboard_snapshot(); if ( null === $snapshot ) { return; } $reports_url = function_exists( 'videm_app_reports_url' ) ? videm_app_reports_url() : admin_url( 'admin.php?page=videm-app-settings&tab=reports' ); $revenue = wp_strip_all_tags( wc_price( $snapshot['revenue_30d'] ) ); $incomplete = (int) $snapshot['incomplete']; $incomplete_bg = $incomplete > 0 ? '#fcf0e6' : '#f6f7f7'; $incomplete_bc = $incomplete > 0 ? '#e0a561' : '#e0e0e0'; echo '