Filter uncanny-toolkit-pro

uo_generate_preview_certificate_content

Filters the content for a generated certificate before it's displayed or saved.

add_filter( 'uo_generate_preview_certificate_content', $callback, 10, 3 );

Description

Filters the content of a certificate preview before it's displayed. Developers can modify the `$cert_content`, `$user` data, or `$parameters` to customize the preview output. This hook fires after initial content generation, allowing for dynamic adjustments based on user, course, or parameter specifics.


Usage

add_filter( 'uo_generate_preview_certificate_content', 'your_function_name', 10, 3 );

Parameters

$cert_content (mixed)
This parameter contains the HTML content of the certificate that is being generated.
$user (mixed)
This parameter contains the raw HTML or string content of the certificate that is being generated or previewed.
$parameters (mixed)
This parameter holds the WordPress user object associated with the certificate.

Return Value

The filtered value.


Examples

<?php
/**
 * Example filter callback for the uo_generate_preview_certificate_content hook.
 *
 * This example demonstrates how to modify the certificate content for preview.
 * It adds a custom message indicating it's a preview and potentially modifies
 * the completion date format if a specific shortcode attribute is found.
 *
 * @param mixed $cert_content   The current certificate content.
 * @param mixed $user           The user object for whom the certificate is being generated.
 * @param mixed $parameters     An array of parameters, including 'course-id'.
 *
 * @return mixed The modified certificate content.
 */
add_filter( 'uo_generate_preview_certificate_content', 'my_custom_preview_certificate_content', 10, 3 );

function my_custom_preview_certificate_content( $cert_content, $user, $parameters ) {
    // Check if we are in a preview context and if user and parameters are available.
    if ( ! is_user_logged_in() || ! $user || ! isset( $parameters['course-id'] ) ) {
        return $cert_content;
    }

    // Add a clear indicator that this is a preview.
    $preview_indicator = '<p style="text-align:center; color:#888; font-style:italic;"><strong>Preview:</strong> This is a sample certificate and not official.</p>';
    $cert_content = $preview_indicator . $cert_content;

    // Example: If a specific shortcode attribute is present, change the date format.
    $completion_time = current_time( 'timestamp' );
    $default_format  = 'F d, Y';
    $specific_format = $default_format; // Initialize with default

    // Look for a specific shortcode that might define a custom date format.
    // This mimics the logic found in the source context.
    preg_match( '/[courseinfo(.*?)(completed_on)(.*?)]/', $cert_content, $courseinfo_match );
    if ( $courseinfo_match && is_array( $courseinfo_match ) ) {
        $shortcode_text = $courseinfo_match[0];
        // Assume a helper function or direct parsing to get attributes.
        // For this example, we'll simulate finding a 'format' attribute.
        if ( strpos( $shortcode_text, 'format="' ) !== false ) {
            preg_match( '/format="(.*?)"/', $shortcode_text, $format_match );
            if ( ! empty( $format_match[1] ) ) {
                $specific_format = $format_match[1];
            }
        }
    }

    // Replace placeholders with preview data.
    // This part is simplified to focus on the filter's role. The source context
    // has more extensive replacement logic for various shortcodes.
    $cert_content = preg_replace( '/[courseinfo(.*?)(course_title)(.*?)]/', 'Certificate for Course Preview', $cert_content );
    $cert_content = preg_replace( '/[courseinfo(.*?)(completed_on)(.*?)]/', date_i18n( $specific_format, $completion_time ), $cert_content );
    $cert_content = preg_replace( '/[groupinfo(.*?)(group_title)(.*?)]/', 'Group Preview', $cert_content );
    $cert_content = preg_replace( '/([usermeta)/', '[usermeta user_id="' . $user->ID . '" ', $cert_content );

    // Replace generic placeholders if they exist after specific replacements.
    $cert_content = preg_replace( '/[courseinfo(.*?)]/', 'Course Name Placeholder', $cert_content );
    $cert_content = preg_replace( '/[quizinfo(.*?)]/', 'Quiz Placeholder', $cert_content );
    $cert_content = preg_replace( '/[groupinfo(.*?)]/', 'Group Name Placeholder', $cert_content );

    return $cert_content;
}

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/includes/tcpdf-certificate-code.php:900

public static function generate_preview_content( $cert_content, $args ) {
		$user            = $args['user'];
		$parameters      = $args['parameters'];
		$completion_time = current_time( 'timestamp' );
		$format          = 'F d, Y';
		preg_match( '/[courseinfo(.*?)(completed_on)(.*?)]/', $cert_content, $courseinfo_match );
		if ( $courseinfo_match && is_array( $courseinfo_match ) ) {
			$text        = $courseinfo_match[0];
			$date_format = self::maybe_extract_shorcode_attributes( 'courseinfo', $text );
			if ( $date_format ) {
				$format = key_exists( 'format', $date_format ) ? $date_format['format'] : $format;
			}
		}
		$cert_content = preg_replace( '/[courseinfo(.*?)(course_title)(.*?)]/', esc_attr__( 'Certificate Preview', 'uncanny-pro-toolkit' ), $cert_content );
		$cert_content = preg_replace( '/[courseinfo(.*?)(completed_on)(.*?)]/', date_i18n( $format, $completion_time ), $cert_content );
		$cert_content = preg_replace( '/[groupinfo(.*?)(group_title)(.*?)]/', esc_attr__( 'Certificate Preview', 'uncanny-pro-toolkit' ), $cert_content );
		$cert_content = preg_replace( '/([usermeta)/', '[usermeta user_id="' . $user->ID . '" ', $cert_content );

		preg_match_all( '/[quizinfo(.+?)]/', $cert_content, $matches );

		if ( $matches ) {
			foreach ( $matches[0] as $quizinfo ) {
				if ( strpos( $quizinfo, 'timestamp' ) ) {
					$qinfo = str_replace( 'show="timestamp"', '', $quizinfo );
					preg_match( '/"(.*)"/', $qinfo, $date_format );
					if ( $date_format ) {
						$date = date_i18n( $date_format[1], $completion_time );
					} else {
						$date = date_i18n( 'F d, Y', $completion_time );
					}
					$cert_content = str_ireplace( $quizinfo, $date, $cert_content );
				}
				if ( strpos( $quizinfo, 'timespent' ) ) {
					$cert_content = str_ireplace( $quizinfo, '88.9', $cert_content );
				}
				if ( strpos( $quizinfo, 'percentage' ) ) {
					$cert_content = str_ireplace( $quizinfo, '85', $cert_content );
				}
				if ( strpos( $quizinfo, 'points' ) ) {
					$cert_content = str_ireplace( $quizinfo, '8', $cert_content );
				}
				if ( strpos( $quizinfo, 'total_points' ) ) {
					$cert_content = str_ireplace( $quizinfo, '10', $cert_content );
				}
				if ( strpos( $quizinfo, 'pass' ) ) {
					$cert_content = str_ireplace( $quizinfo, 'Yes', $cert_content );
				}
				if ( strpos( $quizinfo, 'count' ) ) {
					$cert_content = str_ireplace( $quizinfo, '8', $cert_content );
				}
				if ( strpos( $quizinfo, 'score' ) ) {
					$cert_content = str_ireplace( $quizinfo, '85', $cert_content );
				}
				if ( strpos( $quizinfo, 'field' ) ) {
					$cert_content = str_ireplace( $quizinfo, 'Custom field', $cert_content );
				}
			}
		}

		$cert_content = preg_replace( '/[courseinfo(.*?)]/', _x( 'Course shortcode placeholder', 'Preview certificate', 'uncanny-toolkit-pro' ), $cert_content );
		$cert_content = preg_replace( '/[quizinfo(.*?)]/', _x( 'Quiz shortcode placeholder', 'Preview certificate', 'uncanny-toolkit-pro' ), $cert_content );
		$cert_content = preg_replace( '/[groupinfo(.*?)]/', _x( 'Groups shortcode placeholder', 'Preview certificate', 'uncanny-toolkit-pro' ), $cert_content );

		$cert_content = apply_filters( 'uo_generate_preview_certificate_content', $cert_content, $user->ID, $parameters['course-id'] );

		return $cert_content;
	}


Scroll to Top