Filter tin-canny-learndash-reporting

uo_enable_H5P_admin_ajax

Filters whether H5P admin AJAX requests are enabled, allowing customization of this core integration.

add_filter( 'uo_enable_H5P_admin_ajax', $callback, 10, 1 );

Description

Filters whether H5P admin AJAX requests should be enabled. Developers can use this filter to conditionally disable H5P's admin AJAX functionality, for example, if `doing_wp_cron` is set, preventing potential conflicts or unnecessary script enqueuing.


Usage

add_filter( 'uo_enable_H5P_admin_ajax', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

/**
 * Filters the ability to use WordPress admin AJAX for H5P xAPI statements.
 *
 * This function allows administrators to decide whether H5P xAPI statements
 * should be processed via the WordPress admin AJAX endpoint or a separate PHP file.
 * By default, it's enabled (true).
 *
 * @param bool $enable_admin_ajax Whether to enable WordPress admin AJAX for H5P xAPI.
 * @return bool The filtered value for enabling or disabling admin AJAX.
 */
function my_custom_h5p_admin_ajax_filter( $enable_admin_ajax ) {
	// Example: Only allow admin AJAX if a specific custom user meta key is set.
	if ( is_user_logged_in() ) {
		$user_id = get_current_user_id();
		$allow_admin_ajax_meta = get_user_meta( $user_id, 'my_h5p_force_admin_ajax', true );

		// If the meta key is set to 'true' (or a truthy value), force admin AJAX.
		if ( $allow_admin_jsx_meta === 'true' || (bool) $allow_admin_ajax_meta === true ) {
			return true;
		} else {
			// Otherwise, disable it.
			return false;
		}
	}

	// For logged-out users or if the meta key is not found, return the original value.
	// In this specific example, the default is true, so we can just return the passed parameter.
	return $enable_admin_ajax;
}
add_filter( 'uo_enable_H5P_admin_ajax', 'my_custom_h5p_admin_ajax_filter', 10, 1 );

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/h5p-xapi/wp-h5p-xapi.php:53

function h5pxapi_enqueue_scripts() {
	if ( ! isset( $_REQUEST['doing_wp_cron'] ) ){
		
		$permalink = get_permalink();
		if ( $permalink ) {
			// Check if TinCanny Setting is enabled frontend only.
			$tincanny_settings   = TINCANNYSNCAdminOptions::get_options();
			$capture_tincan_data = isset( $tincanny_settings['tinCanActivation'] ) && $tincanny_settings['tinCanActivation'] == 1 ? true : false;
			// Bail early if we don't have to capture the data.
			if ( ! $capture_tincan_data ) {
				return;
			}
		}
		
		// Get the data the script needs
		$settings = h5pxapi_get_auth_settings();
		// Get H5P_XAPI_STATEMENT_URL
		$h5p_xapi_statement_url = null;
		if ( $settings && $settings[ 'endpoint_url' ] ){
			// Check if we have to enable the admin AJAX
			$uo_enable_H5P_admin_ajax = apply_filters( 'uo_enable_H5P_admin_ajax', true );

			if ( $uo_enable_H5P_admin_ajax === true ){
				$h5p_xapi_statement_url = admin_url( 'admin-ajax.php?action=process-xapi-statement' );
			}
			else {
				$h5p_xapi_statement_url = UCWPH5PxAPI_PLUGIN_URL . '/process-xapi-statement.php?security=' . wp_create_nonce( "process-xapi-statement" );
			}
		}
		// Get HP5_XAPI_CONTEXTACTIVITY
		$h5p_xapi_contextactivity = null;
		if ( $permalink ){
			$h5p_xapi_contextactivity = [
				'id'           => $permalink,
				'definition'   => [
					'name'     => [
						'en'   => wp_title( '|', false ),
					],
					'moreInfo' => $permalink
				]
			];
		} 

		// Register and enqueue style
		wp_enqueue_style( 'wp-h5p-xapi', UCWPH5PxAPI_PLUGIN_URL . '/wp-h5p-xapi.css', array(), UNCANNY_REPORTING_VERSION );

		// Register script
		wp_register_script( 'wp-h5p-xapi', UCWPH5PxAPI_PLUGIN_URL . '/wp-h5p-xapi.js', array( "jquery" ), UNCANNY_REPORTING_VERSION );

		// Add inline script
		$h5p_inline_script  = "WP_H5P_XAPI_STATEMENT_URL = '" . $h5p_xapi_statement_url . "';";
		$h5p_inline_script .= "WP_H5P_XAPI_CONTEXTACTIVITY = JSON.parse( '" . json_encode( $h5p_xapi_contextactivity ) . "' );";
		wp_add_inline_script( 'wp-h5p-xapi', $h5p_inline_script, 'before' );

		// Enqueue script
		wp_enqueue_script( "wp-h5p-xapi" );
	}
}

Scroll to Top