'$', 'EUR' => '€', 'GBP' => '£', 'AUD' => 'A$', 'CAD' => 'C$', 'INR' => '₹', 'JPY' => '¥', 'CNY' => '¥', ); return $symbols[ $currency ] ?? '$'; } /** * Format a price amount using currency, decimal, and separator settings. * * @param float|int|string|null $price Raw price amount. * @param array $args { * Optional. Formatting arguments. * * @type string $currency Currency code. Defaults to the configured currency option. * @type string $position Symbol position, 'left' or 'right'. Defaults to the configured option. * @type int $decimals Number of decimal places. Defaults to the configured option. * @type string $decimal_sep Decimal separator. Defaults to the configured option. * @type string $thousand_sep Thousands separator. Defaults to the configured option. * @type string $gateway Payment gateway slug (e.g. 'razorpay'). * @type bool $is_minor_unit Whether $price is in the currency's minor unit (e.g. cents). * } * @return string Formatted price string, or an empty string if $price is empty/null. */ function aimslms_format_price( $price, $args = array() ) { if ( '' === $price || null === $price ) { return ''; } $currency = $args['currency'] ?? get_option( 'aimslms_currency', 'USD' ); $position = $args['position'] ?? get_option( 'aimslms_currency_position', 'left' ); $decimals = (int) ( $args['decimals'] ?? get_option( 'aimslms_decimal_count', 2 ) ); $decimal_sep = $args['decimal_sep'] ?? get_option( 'aimslms_decimal_separator', '.' ); $thousand_sep = $args['thousand_sep'] ?? get_option( 'aimslms_thousand_separator', ',' ); $gateway = $args['gateway'] ?? ''; $is_minor_unit = $args['is_minor_unit'] ?? false; if ( 'razorpay' === $gateway || true === $is_minor_unit ) { $price = $price / 100; $currency = $args['currency'] ?? 'INR'; } $symbol = aimslms_get_currency_symbol( $currency ); $formatted = number_format( (float) $price, $decimals, $decimal_sep, $thousand_sep ); return ( 'right' === $position ) ? $formatted . $symbol : $symbol . $formatted; } /** * Get the configured store currency code. * * @return string Uppercase currency code (e.g. 'USD'). */ function aimslms_get_currency_code() { return strtoupper( get_option( 'aimslms_currency', 'USD' ) ); } /** * Get the configured store currency code in lowercase, as required by Stripe. * * @return string Lowercase currency code (e.g. 'usd'). */ function aimslms_get_stripe_currency_code() { return strtolower( aimslms_get_currency_code() ); } /** * Get the default course image shown whenever a course has no featured image set. * * @param string $title Unused. Kept for backward compatibility with existing call sites. * @return string URL of the plugin's default course image. */ function aimslms_generate_course_placeholder( $title = '' ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- kept for backward compatibility with existing call sites. return aimslms_get_default_course_image(); } /** * Get the URL of the plugin's default course image, used whenever a course * has no featured image selected. * * @return string URL of the default course image. */ function aimslms_get_default_course_image() { return AIMSLMS_PLUGIN_URL . 'assets/img/defult-course.jpg'; } // Admin + Frontend. add_action( 'init', function () { add_rewrite_rule( '^courses/([^/]+)/lessons/([^/]+)/?$', 'index.php?aimslms_course=$matches[1]&aimslms_lesson=$matches[2]', 'top' ); add_rewrite_tag( '%aimslms_lesson%', '([^&]+)' ); } ); add_filter( 'template_include', function ( $template ) { if ( get_query_var( 'aimslms_lesson' ) ) { return AIMSLMS_PLUGIN_DIR . 'public/templates/lesson-single.php'; } if ( is_singular( 'aimslms_course' ) ) { return AIMSLMS_PLUGIN_DIR . 'public/templates/course-single.php'; } return $template; } ); /** * Get Total count of section and lesson * * @param int $course_id Course post ID. * @return array { * Section and lesson counts. * * @type int $sections Total number of sections. * @type int $lessons Total number of lessons. * } */ function aimslms_get_course_section_lesson_counts( $course_id ) { global $wpdb; $sections_table = $wpdb->prefix . 'aimslms_course_sections'; $lessons_table = $wpdb->prefix . 'aimslms_course_lessons'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin tables, no WP API alternative; $sections_table/$lessons_table are derived from $wpdb->prefix, not user input. $result = $wpdb->get_row( $wpdb->prepare( " SELECT COUNT(DISTINCT s.id) AS total_sections, COUNT(l.id) AS total_lessons FROM {$sections_table} s LEFT JOIN {$lessons_table} l ON l.section_id = s.id WHERE s.course_id = %d ", $course_id ), ARRAY_A ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return array( 'sections' => (int) $result['total_sections'], 'lessons' => (int) $result['total_lessons'], ); } /** * Get all sections for a course * * @param int $course_id Course post ID. * @return array Array of section rows (associative arrays), ordered by sort_order. */ function aimslms_get_course_sections( $course_id ) { global $wpdb; $table = $wpdb->prefix . 'aimslms_course_sections'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin table, no WP API alternative; $table is derived from $wpdb->prefix, not user input. return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} WHERE course_id = %d ORDER BY sort_order ASC", $course_id ), ARRAY_A ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching } /** * Get single section * * @param int $section_id Section ID. * @return array|null Section row as an associative array, or null if not found. */ function aimslms_get_section( $section_id ) { global $wpdb; $table = $wpdb->prefix . 'aimslms_course_sections'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin table, no WP API alternative; $table is derived from $wpdb->prefix, not user input. return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $section_id ), ARRAY_A ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching } /** * Get lessons for a section * * @param int $section_id Section ID. * @return array Array of lesson rows (associative arrays), ordered by sort_order. */ function aimslms_get_section_lessons( $section_id ) { global $wpdb; $table = $wpdb->prefix . 'aimslms_course_lessons'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin table, no WP API alternative; $table is derived from $wpdb->prefix, not user input. return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} WHERE section_id = %d ORDER BY sort_order ASC", $section_id ), ARRAY_A ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching } /** * Get single lesson * * @param int $lesson_id Lesson ID. * @return array|null Lesson row as an associative array, or null if not found. */ function aimslms_get_lesson( $lesson_id ) { global $wpdb; $table = $wpdb->prefix . 'aimslms_course_lessons'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin table, no WP API alternative; $table is derived from $wpdb->prefix, not user input. return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $lesson_id ), ARRAY_A ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching } /** * Get a lesson's numeric ID by its slug within a course. * * @param int $course_id Course post ID. * @param string $slug Lesson slug. * @return int Lesson ID, or 0 if not found. */ function aimslms_get_lesson_id_by_slug( $course_id, $slug ) { global $wpdb; $table = $wpdb->prefix . 'aimslms_course_lessons'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin table, no WP API alternative; $table is derived from $wpdb->prefix, not user input. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$table} WHERE slug = %s AND course_id = %s", $slug, $course_id, ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching } /** * Render the course/lesson breadcrumb navigation. * * @param array $args { * Optional. Breadcrumb arguments. * * @type int $course_id Course post ID. Default 0 (no course crumb). * @type array $lesson Lesson array from the DB (must include a 'title' key). Default null (no lesson crumb). * } */ function aimslms_render_breadcrumb( $args = array() ) { $defaults = array( 'course_id' => 0, 'lesson' => null, // lesson array from DB. ); $args = wp_parse_args( $args, $defaults ); echo '
'; } /** * Get uploaded file type in lesson * * @param string $file_url URL of the uploaded lesson file. * @return string|false One of 'video', 'text', 'document', 'other', or false if $file_url is empty. */ function aimslms_get_lesson_file_type( $file_url ) { if ( empty( $file_url ) ) { return false; } // Get file extension. $extension = strtolower( pathinfo( wp_parse_url( $file_url, PHP_URL_PATH ), PATHINFO_EXTENSION ) ); // Common video formats. $video_types = array( 'mp4', 'webm', 'ogg', 'mov', 'm4v' ); // Text-based formats. $text_types = array( 'txt', 'md', 'csv', 'json', 'xml' ); // Document formats. $doc_types = array( 'pdf', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx' ); if ( in_array( $extension, $video_types, true ) ) { return 'video'; } if ( in_array( $extension, $text_types, true ) ) { return 'text'; } if ( in_array( $extension, $doc_types, true ) ) { return 'document'; } return 'other'; } /** * File icons * * @param string $file_url URL of the uploaded lesson file. * @return string Emoji icon representing the file's type. */ function aimslms_get_lesson_file_icon( $file_url ) { $type = aimslms_get_lesson_file_type( $file_url ); switch ( $type ) { case 'video': return '🎥'; // or icon class. case 'document': return '📖'; case 'text': return '📝'; default: return '📁'; } } /** * Get Level Label * * @param string $level Course level slug ('beginner', 'intermediate', 'expert'). * @return string Human-readable level label. */ function aimslms_get_level_label( $level ) { switch ( $level ) { case 'beginner': return 'Beginner Level'; case 'intermediate': return 'Intermediate Level'; case 'expert': return 'Expert Level'; default: return 'All Level'; } } /** * Get lesson Url * * @param int $course_id Course post ID. * @param int $lesson_id Lesson ID. * @return string Front-end lesson URL, or '#' if the course slug or lesson cannot be found. */ function aimslms_get_lesson_url( $course_id, $lesson_id ) { // Course slug (WordPress post). $course_slug = get_post_field( 'post_name', $course_id ); // Lesson data (custom table). $lesson = aimslms_get_lesson( $lesson_id ); if ( ! $course_slug || ! $lesson ) { return '#'; } // Lesson slug fallback. $lesson_slug = ! empty( $lesson['slug'] ) ? $lesson['slug'] : $lesson_id; return home_url( "/courses/{$course_slug}/lesson/{$lesson_slug}/" ); } /** * TEMP ENROLLMENT (for testing) * * @param int $user_id User ID. Currently unused; reserved for the real enrollment check. * @param int $lesson_id Lesson ID. Currently unused; reserved for the real enrollment check. * @return bool Always true until replaced with real enrollment logic. */ function aimslms_user_enrolled_in_lesson( $user_id, $lesson_id ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Stub pending real enrollment logic; params reserved for the future implementation. return true; // replace later with real enrollment logic. } /** * SECURE FILE URL * * @param int $lesson_id Lesson ID the file belongs to. * @param string $file_url Original file URL to protect. * @return string Secure, token-based download URL, or an empty string if no user is logged in. */ function aimslms_secure_upload_url( $lesson_id, $file_url ) { // Get current user ID. $user_id = get_current_user_id(); if ( ! $user_id ) { return ''; // User not logged in. } // Prepare payload. $payload = array( 'user' => $user_id, 'lesson' => (int) $lesson_id, 'file' => $file_url, 'time' => time(), // timestamp to expire links if needed. ); // Encode payload as JSON. $payload_json = wp_json_encode( $payload ); // Encrypt payload using AES-128-ECB. // Note: ECB is not the most secure, consider AES-256-CBC with IV for higher security. $encrypted = openssl_encrypt( $payload_json, 'AES-128-ECB', AUTH_KEY, OPENSSL_RAW_DATA ); // Base64 encode the encrypted payload to make it URL safe. // Uses the shared aimslms_base64url_encode()/aimslms_base64url_decode() pair (see // includes/aimslms-secure-file.php) so encoding and decoding stay symmetric. $token = aimslms_base64url_encode( $encrypted ); // Return full secure URL. return home_url( '/aimslms-file/' . $token ); } /** * Get the bundled list of countries, loaded from the local countries.json data file. * * @return array Associative array of country code => country name, or an empty array on failure. */ function aimslms_get_countries() { static $countries = null; if ( null !== $countries ) { return $countries; } $file = AIMSLMS_PLUGIN_DIR . 'assets/data/countries.json'; if ( ! file_exists( $file ) ) { $countries = array(); return $countries; } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local bundled data file, not a remote request. $countries = json_decode( file_get_contents( $file ), true ); return is_array( $countries ) ? $countries : array(); } /** * Get the bundled list of states/provinces, loaded from the local states.json data file. * * @return array Associative array of country code => (state code => state name), or an empty array on failure. */ function aimslms_get_states() { $file = AIMSLMS_PLUGIN_DIR . 'assets/data/states.json'; if ( ! file_exists( $file ) ) { return array(); } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local bundled data file, not a remote request. $states = json_decode( file_get_contents( $file ), true ); return is_array( $states ) ? $states : array(); } /** * Get the display name for a country code. * * @param string $code Country code. * @return string Country name, the code itself if not found, or an em dash if $code is empty. */ function aimslms_get_country_name( $code ) { if ( ! $code ) { return '-'; } $countries = aimslms_get_countries(); return $countries[ $code ] ?? $code; } /** * Get the display name for a state/province code within a country. * * @param string $country_code Country code. * @param string $state_code State/province code. * @return string State name, the code itself if not found, or an em dash if either argument is empty. */ function aimslms_get_state_name( $country_code, $state_code ) { if ( ! $country_code || ! $state_code ) { return '-'; } $states = aimslms_get_states(); return $states[ $country_code ][ $state_code ] ?? $state_code; } /** * Get all active login sessions for a user. * * @param int $user_id User ID. * @return array Array of session data, or an empty array if the user has none. */ function aimslms_get_user_sessions( $user_id ) { $manager = WP_Session_Tokens::get_instance( $user_id ); $sessions = $manager->get_all(); return $sessions ? $sessions : array(); } /** * Parse a User-Agent string into a human-readable browser and device summary. * * @param string $ua User-Agent string. * @return string Browser and device summary (e.g. 'Google Chrome - Windows PC'). */ function aimslms_parse_user_agent( $ua ) { $browser = 'Unknown Browser'; $device = 'Desktop'; if ( strpos( $ua, 'Edg/' ) !== false ) { $browser = 'Microsoft Edge'; } elseif ( strpos( $ua, 'Chrome/' ) !== false ) { $browser = 'Google Chrome'; } elseif ( strpos( $ua, 'Firefox/' ) !== false ) { $browser = 'Mozilla Firefox'; } elseif ( strpos( $ua, 'Safari/' ) !== false && strpos( $ua, 'Chrome' ) === false ) { $browser = 'Safari'; } if ( strpos( $ua, 'Windows' ) !== false ) { $device = 'Windows PC'; } elseif ( strpos( $ua, 'Mac OS' ) !== false ) { $device = 'Mac'; } elseif ( strpos( $ua, 'Linux' ) !== false ) { $device = 'Linux'; } if ( strpos( $ua, 'Mobile' ) !== false ) { $device = 'Mobile Device'; } return $browser . ' - ' . $device; } /** * Get the payment gateway logo URLs. * * @return array Associative array of gateway slug => logo image URL. */ function aimslms_get_gateway_logos() { return array( 'paypal' => AIMSLMS_PLUGIN_URL . 'assets/img/PaypalPayment.png', 'stripe' => AIMSLMS_PLUGIN_URL . 'assets/img/StripePayment.png', 'razorpay' => AIMSLMS_PLUGIN_URL . 'assets/img/RazorpayPayment.webp', ); } /** * Get order and payment details joined for a single order. * * @param int $order_id Order ID. * @return array|null Associative array of order + payment fields, or null if $order_id is invalid. */ function aimslms_get_order_details( $order_id ) { if ( empty( $order_id ) || ! is_numeric( $order_id ) ) { return null; } global $wpdb; $orders_table = $wpdb->prefix . 'aimslms_course_orders'; $payment_table = $wpdb->prefix . 'aimslms_course_payment'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin tables, no WP API alternative; $orders_table/$payment_table are derived from $wpdb->prefix, not user input. return $wpdb->get_row( $wpdb->prepare( "SELECT o.id AS order_id, o.user_id, o.order_total, o.order_status, o.created_at, p.payment_method, p.payment_gateway, p.transaction_id, p.amount, p.status AS payment_status, p.raw_response FROM {$orders_table} o INNER JOIN {$payment_table} p ON p.order_id = o.id WHERE o.id = %d", $order_id ), ARRAY_A ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching } /** * Get a payment transaction by its transaction ID. * * @param string $transaction_id Payment gateway transaction ID. * @return array|null Payment row as an associative array, or null if $transaction_id is empty or not found. */ function aimslms_get_payment_transaction( $transaction_id ) { if ( empty( $transaction_id ) ) { return null; } global $wpdb; $table = $wpdb->prefix . 'aimslms_course_payment'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin table, no WP API alternative; $table is derived from $wpdb->prefix, not user input. return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE transaction_id = %s", $transaction_id ), ARRAY_A ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching } /** * Check whether a user is enrolled in (has completed payment for) a course. * * @param int $course_id Course post ID. * @param int $user_id User ID. * @return bool True if the user has a completed order and a matching enrollment record, false otherwise. */ function aimslms_get_course_enrollment( $course_id, $user_id ) { if ( ! $course_id || ! $user_id ) { return false; } global $wpdb; $orders_table = $wpdb->prefix . 'aimslms_course_orders'; $enrollments_table = $wpdb->prefix . 'aimslms_course_enrollments'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin tables, no WP API alternative; $orders_table/$enrollments_table are derived from $wpdb->prefix, not user input. $order = $wpdb->get_row( $wpdb->prepare( "SELECT transaction_id FROM $orders_table WHERE user_id = %d AND course_id = %d AND order_status = %s ORDER BY created_at DESC LIMIT 1", $user_id, $course_id, 'completed' ) ); if ( ! $order || empty( $order->transaction_id ) ) { return false; } $enrollment_exists = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $enrollments_table WHERE user_id = %d AND course_id = %d AND transaction_id = %s", $user_id, $course_id, $order->transaction_id ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return ( $enrollment_exists > 0 ); } /** * Get the URL of the first lesson (by sort order) of a course. * * @param int $course_id Course post ID. * @return string|false Front-end lesson URL, or false if no section/lesson could be found. */ function aimslms_get_first_lesson_url( $course_id ) { global $wpdb; if ( ! $course_id ) { return false; } $sections_table = $wpdb->prefix . 'aimslms_course_sections'; $lessons_table = $wpdb->prefix . 'aimslms_course_lessons'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin tables, no WP API alternative; $sections_table/$lessons_table are derived from $wpdb->prefix, not user input. $section_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $sections_table WHERE course_id = %d ORDER BY sort_order ASC, id ASC LIMIT 1", $course_id ) ); if ( ! $section_id ) { return false; } $lesson_slug = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $lessons_table WHERE section_id = %d ORDER BY sort_order ASC, id ASC LIMIT 1", $section_id ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching if ( ! $lesson_slug ) { return false; } return site_url( '/courses/' . get_post_field( 'post_name', $course_id ) . '/lesson/' . $lesson_slug . '/' ); } /** * Check whether the current session has a course pending in the checkout cart. * * @return bool True if a valid, published course is present in the session checkout data. */ function aimslms_has_course_in_cart() { return ( isset( $_SESSION['aimslms_checkout'] ) && ! empty( $_SESSION['aimslms_checkout']['course_id'] ) && get_post_status( (int) $_SESSION['aimslms_checkout']['course_id'] ) === 'publish' ); } /** * Get the payment status for a user's latest order on a course. * * @param int $user_id User ID. * @param int $course_id Course post ID. * @return string|null 'completed' if the order is completed, the latest payment status string, or null if no order/status is found. */ function aimslms_get_course_payment_status( $user_id, $course_id ) { if ( ! $user_id || ! $course_id ) { return null; } global $wpdb; $orders_table = $wpdb->prefix . 'aimslms_course_orders'; $payment_table = $wpdb->prefix . 'aimslms_course_payment'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom plugin tables, no WP API alternative; $orders_table/$payment_table are derived from $wpdb->prefix, not user input. // Get latest order for this user + course. $order = $wpdb->get_row( $wpdb->prepare( "SELECT o.id, o.order_status FROM {$orders_table} o WHERE o.user_id = %d AND o.course_id = %d ORDER BY o.created_at DESC LIMIT 1", $user_id, $course_id ), ARRAY_A ); if ( ! $order ) { return null; } // If order already completed → treat as completed. if ( 'completed' === $order['order_status'] ) { return 'completed'; } // Get payment status. $payment_status = $wpdb->get_var( $wpdb->prepare( "SELECT status FROM {$payment_table} WHERE order_id = %d ORDER BY id DESC LIMIT 1", $order['id'] ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return $payment_status ? $payment_status : null; } /** * Build a signed invoice download URL for an order. * * @param int $order_id Order ID. * @return string URL to the invoice download endpoint, with a nonce for verification. */ function aimslms_get_invoice_url( $order_id ) { return add_query_arg( array( 'aimslms_invoice' => 1, 'order_id' => $order_id, '_nonce' => wp_create_nonce( 'aimslms_invoice_' . $order_id ), ), home_url( '/' ) ); } /** * Convert a USD amount to another currency using a live exchange rate, with a fallback rate. * * @param float $usd_amount Amount in USD to convert. * @param string $from Source currency code for the exchange rate lookup. * @param string $to Target currency code for the exchange rate lookup. * @return float Converted amount, rounded to 2 decimal places. */ function aimslms_convert_usd_to_inr( $usd_amount, $from, $to ) { $transient_key = 'aimslms_usd_to_inr_rate_' . $from . '_' . $to; $rate = get_transient( $transient_key ); if ( ! $rate ) { $response = wp_remote_get( "https://api.frankfurter.app/latest?from={$from}&to={$to}", array( 'timeout' => 10 ) ); if ( is_wp_error( $response ) ) { $rate = 90; } else { $body = json_decode( wp_remote_retrieve_body( $response ), true ); $rate = $body['rates']['INR'] ?? 90; } set_transient( $transient_key, $rate, HOUR_IN_SECONDS ); } return round( $usd_amount * $rate, 2 ); } /** * Get the IDs of all published courses. * * @return int[] Array of published course post IDs. */ function aimslms_get_all_courses() { return get_posts( array( 'post_type' => 'aimslms_course', 'post_status' => 'publish', 'posts_per_page' => -1, 'fields' => 'ids', ) ); } /** * Get the display-formatted payment amount for a gateway, converting minor units where needed. * * @param float|int $amount Raw payment amount. * @param string $gateway Payment gateway slug (e.g. 'razorpay'). * @param string $currency Currency code. * @return string Formatted price string. */ function aimslms_get_payment_amount( $amount, $gateway, $currency ) { if ( 'razorpay' === $gateway ) { $amount = $amount / 100; $currency = 'INR'; } return aimslms_format_price( $amount, array( 'currency' => $currency, ) ); } if ( ! function_exists( 'aimslms_admin_user_meta' ) ) { /** * Get a raw user meta value for display on admin screens (student/order views). * Callers are responsible for escaping the returned value for their output context * (e.g. esc_html(), esc_attr()). * * @param int $user_id User ID. * @param string $key Meta key to look up. * @param string $fallback Value to return when the meta is empty. * @return string Meta value, or $fallback when unset. */ function aimslms_admin_user_meta( $user_id, $key, $fallback = '' ) { $val = get_user_meta( $user_id, $key, true ); return $val ? $val : $fallback; } } if ( ! function_exists( 'aimslms_meta' ) ) { /** * Get an escaped meta value for the current user, for display on front-end account templates. * * @param string $key Meta key to look up. * @return string Escaped meta value, or 'No Info' when unset. */ function aimslms_meta( $key ) { $val = get_user_meta( get_current_user_id(), $key, true ); return $val ? esc_html( $val ) : 'No Info'; } } if ( ! function_exists( 'aimslms_meta_edit' ) ) { /** * Get an escaped meta value for the current user, for pre-filling front-end form fields. * * @param string $key Meta key to look up. * @return string Escaped meta value, or an empty string when unset. */ function aimslms_meta_edit( $key ) { $val = get_user_meta( get_current_user_id(), $key, true ); return $val ? esc_html( $val ) : ''; } }