label. The key matches the * template file certificate-template-{key}.php. */ public static function get_designs() { return array( 'modern' => __( 'Modern', 'aims-infosoft-lms' ), 'elegant' => __( 'Elegant', 'aims-infosoft-lms' ), 'classic' => __( 'Classic', 'aims-infosoft-lms' ), ); } /** * Hook the download/verification/preview handlers. */ public static function init() { add_action( 'init', array( __CLASS__, 'handle_certificate_download' ) ); add_action( 'init', array( __CLASS__, 'handle_certificate_verification' ) ); add_action( 'admin_init', array( __CLASS__, 'handle_certificate_preview' ) ); } /** * Get an existing certificate record for a user/course, or issue a new one. * * @param int $user_id User ID. * @param int $course_id Course post ID. * @param int $timestamp Completion timestamp, used only when a new record is issued. * @return object Row with certificate_id and issue_date. */ private static function get_or_create_certificate_record( $user_id, $course_id, $timestamp ) { global $wpdb; $table = $wpdb->prefix . 'aimslms_course_certificates'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $row = $wpdb->get_row( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $table is derived from $wpdb->prefix, not user input. "SELECT * FROM {$table} WHERE user_id = %d AND course_id = %d", $user_id, $course_id ) ); if ( $row ) { return $row; } $certificate_id = 'AIMSLMS-' . strtoupper( substr( wp_hash( $user_id . '-' . $course_id . '-' . $timestamp, 'nonce' ), 0, 10 ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Custom table, no WP API alternative. $wpdb->insert( $table, array( 'user_id' => $user_id, 'course_id' => $course_id, 'certificate_id' => $certificate_id, 'issue_date' => date_i18n( 'Y-m-d H:i:s', $timestamp ), 'file_path' => '', ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. return $wpdb->get_row( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $table is derived from $wpdb->prefix, not user input. "SELECT * FROM {$table} WHERE user_id = %d AND course_id = %d", $user_id, $course_id ) ); } /** * Build the settings array the certificate template files expect. */ private static function build_settings() { $logo_id = get_theme_mod( 'custom_logo' ); $logo_url = $logo_id ? wp_get_attachment_image_url( $logo_id, 'full' ) : ''; $designs = self::get_designs(); $design = get_option( 'aimslms_cert_design', 'modern' ); if ( ! isset( $designs[ $design ] ) ) { $design = 'modern'; } return array( 'design' => $design, 'bg_type' => get_option( 'aimslms_cert_bg_type', 'color' ), 'bg_color' => get_option( 'aimslms_cert_bg_color', '#eef6f1' ), 'bg_image' => get_option( 'aimslms_cert_bg_image' ), 'bg_opacity' => (float) get_option( 'aimslms_cert_bg_image_opacity', 1 ), 'primary_color' => get_option( 'aimslms_cert_primary_color', '#1f4d3a' ), 'secondary_color' => get_option( 'aimslms_cert_secondary_color', '#555555' ), 'heading_text' => get_option( 'aimslms_cert_heading_text', 'CERTIFICATE' ), 'grant_text' => get_option( 'aimslms_cert_grant_text', 'This certificate is proudly presented to' ), 'participation' => get_option( 'aimslms_cert_participation_text', '' ), 'header_type' => get_option( 'aimslms_cert_header_type', 'title' ), 'logo_width' => (int) get_option( 'aimslms_cert_logo_width', 100 ), 'logo_url' => $logo_url, 'sign_img' => get_option( 'aimslms_cert_signature_image' ), 'sign_enabled' => get_option( 'aimslms_cert_enable_signature' ), 'sign_name' => get_option( 'aimslms_cert_signature_name' ), 'sign_desc' => get_option( 'aimslms_cert_signature_desc' ), ); } /** * Render + stream a certificate PDF for a given recipient/course. * * @param string $filename Download filename (without extension). * @param object $user WP_User-like object with first_name/last_name. * @param object $course WP_Post-like object with post_title. * @param string $certificate_id Certificate ID to display. * @param string $issued_date_str Already-formatted issue date string. */ private static function render_pdf( $filename, $user, $course, $certificate_id, $issued_date_str ) { require_once AIMSLMS_PLUGIN_DIR . 'vendor/autoload.php'; $settings = self::build_settings(); $settings['participation'] = str_replace( '(aimslms-cs-course-name)', '' . $course->post_title . '', $settings['participation'] ); $issued_date = $issued_date_str; $logo_url = $settings['logo_url']; $template_file = AIMSLMS_PLUGIN_DIR . 'public/templates/certificate/certificate-template-' . $settings['design'] . '.php'; if ( ! file_exists( $template_file ) ) { $template_file = AIMSLMS_PLUGIN_DIR . 'public/templates/certificate/certificate-template-modern.php'; } ob_start(); include $template_file; $html = ob_get_clean(); $dompdf = new \Dompdf\Dompdf( array( 'isRemoteEnabled' => true, ) ); $dompdf->loadHtml( $html ); $dompdf->setPaper( 'A4', 'landscape' ); $dompdf->render(); $dompdf->stream( 'Certificate-' . sanitize_title( $filename ) . '.pdf', array( 'Attachment' => false ) ); exit; } /** * Verify the request nonce and completion status, then stream the earned certificate PDF for download. */ public static function handle_certificate_download() { if ( empty( $_GET['aimslms_certificate'] ) || empty( $_GET['course_id'] ) ) { return; } if ( ! is_user_logged_in() ) { wp_die( esc_html__( 'Unauthorized.', 'aims-infosoft-lms' ), '', array( 'response' => 401 ) ); } $course_id = absint( $_GET['course_id'] ); $user_id = get_current_user_id(); if ( empty( $_GET['_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_nonce'] ) ), 'aimslms_certificate_' . $course_id ) ) { wp_die( esc_html__( 'Invalid certificate request.', 'aims-infosoft-lms' ), '', array( 'response' => 403 ) ); } // ✅ Verify user completed course $progress = aimslms_get_course_progress_data( $course_id, $user_id ); $completed_timestamp = ! empty( $progress['completed_at'] ) ? (int) $progress['completed_at'] : time(); if ( empty( $progress['percentage'] ) || (int) $progress['percentage'] < 100 ) { wp_die( esc_html__( 'Course not completed.', 'aims-infosoft-lms' ), '', array( 'response' => 403 ) ); } $user = wp_get_current_user(); $course = get_post( $course_id ); if ( ! $course ) { wp_die( esc_html__( 'Invalid course.', 'aims-infosoft-lms' ), '', array( 'response' => 404 ) ); } $certificate = self::get_or_create_certificate_record( $user_id, $course_id, $completed_timestamp ); $issued_date = date_i18n( 'd M Y', strtotime( $certificate->issue_date ) ); self::render_pdf( $course->post_title, $user, $course, $certificate->certificate_id, $issued_date ); } /** * Admin-only "see the real PDF" preview, using the currently saved * settings - hit from the Certificate settings screen so an admin can * check their changes without needing a completed course/real student. * URL: admin.php?page=aimslms-certificate&aimslms_preview_certificate=1&_wpnonce=... */ public static function handle_certificate_preview() { if ( empty( $_GET['aimslms_preview_certificate'] ) ) { return; } if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Unauthorized', 'aims-infosoft-lms' ) ); } check_admin_referer( 'aimslms_preview_certificate' ); $user = wp_get_current_user(); $user->first_name = $user->first_name ? $user->first_name : 'John'; $user->last_name = $user->last_name ? $user->last_name : 'Doe'; $course = new stdClass(); $course->post_title = __( 'Sample Course Title', 'aims-infosoft-lms' ); self::render_pdf( 'preview', $user, $course, 'AIMSLMS-PREVIEW01', date_i18n( 'd M Y' ) ); } /** * Public certificate verification page: /?aimslms_verify_certificate=1&certificate_id=AIMSLMS-XXXXXXXXXX * No login required - this is meant to let anyone verify a certificate's authenticity. */ public static function handle_certificate_verification() { if ( empty( $_GET['aimslms_verify_certificate'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- public read-only certificate lookup, no state change. return; } global $wpdb; $certificate_id = isset( $_GET['certificate_id'] ) ? sanitize_text_field( wp_unslash( $_GET['certificate_id'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- public read-only certificate lookup, no state change. $table = $wpdb->prefix . 'aimslms_course_certificates'; $record = null; if ( $certificate_id ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no WP API alternative. $record = $wpdb->get_row( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $table is derived from $wpdb->prefix, not user input. "SELECT * FROM {$table} WHERE certificate_id = %s", $certificate_id ) ); } nocache_headers(); header( 'Content-Type: text/html; charset=utf-8' ); // Standalone document (no wp_head()) - register/enqueue the stylesheet and // print just that handle instead of hardcoding a tag. wp_register_style( 'aimslms-certificate-verify', AIMSLMS_PLUGIN_URL . 'assets/css/certificate-verify.css', array(), AIMSLMS_VERSION ); wp_enqueue_style( 'aimslms-certificate-verify' ); echo '
' . esc_html__( 'Certificate ID:', 'aims-infosoft-lms' ) . ' ' . esc_html( $record->certificate_id ) . '
'; echo '' . esc_html__( 'Recipient:', 'aims-infosoft-lms' ) . ' ' . esc_html( $user ? $user->display_name : __( 'Unknown', 'aims-infosoft-lms' ) ) . '
'; echo '' . esc_html__( 'Course:', 'aims-infosoft-lms' ) . ' ' . esc_html( $course ? $course->post_title : __( 'Unknown', 'aims-infosoft-lms' ) ) . '
'; echo '' . esc_html__( 'Issued:', 'aims-infosoft-lms' ) . ' ' . esc_html( date_i18n( 'd M Y', strtotime( $record->issue_date ) ) ) . '
'; } elseif ( $certificate_id ) { echo '' . esc_html__( 'No certificate matches that ID. Please double-check and try again.', 'aims-infosoft-lms' ) . '
'; } else { echo '' . esc_html__( 'Enter a certificate ID to check its authenticity.', 'aims-infosoft-lms' ) . '
'; } echo ''; echo '