autoloader( string $class_name )

Description #

Callback function for auto loading class on demand.

Parameters #

  • $class_name
    string (Required) Name of class.

Changelog #

VersionDescription
4.1.8Introduced.

Source #

File: loader.php

function autoloader( $class_name ) {
	if ( false === strpos( $class_name, 'AnsPress\\' ) ) {
		return;
	}

	// Replace AnsPress\Pro\ and change to lowercase to fix WPCS warning.
	$class_name = strtolower( str_replace( 'AnsPress\\', '', $class_name ) );
	$filename   = ANSPRESS_DIR . str_replace( '_', '-', str_replace( '\\', '/', $class_name ) ) . '.php';

	// Check if file exists before including.
	if ( file_exists( $filename ) ) {
		require_once $filename;

		// Check class exists.
		if ( class_exists( $class_name ) ) {
			return true;
		}
	}

	return false;
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment