su_is_valid_template_name

The shortcodes-ultimate su is valid template name function.

Published Date - February 15, 2021

Description

Helper function to check that the given path is related to the current theme or to the plugin directory.

su_is_valid_template_name( $path );

Parameters

  1. $path


Usage

The following example is for adding a hook callback.

if ( !function_exists( 'su_is_valid_template_name' ) ) {
    require_once ABSPATH . PLUGINDIR . 'shortcodes-ultimateshortcodes-ultimate/includes/functions-helpers.php';
}

// The input(s). 
$path = null;

// NOTICE! Understand what this does before running.
$result = su_is_valid_template_name( $path );
            

Defined

The function is defined in the following location(s).

shortcodes-ultimate/includes/functions-helpers.php

function su_is_valid_template_name( $path ) {

	$path = su_set_file_extension( $path, 'php' );

	$allowed = apply_filters(
		'su/allowed_template_paths',
		array(
			get_stylesheet_directory(),
			get_template_directory(),
			plugin_dir_path( dirname( __FILE__ ) ),
		)
	);

	foreach ( $allowed as $dir ) {

		$dir  = untrailingslashit( $dir );
		$real = realpath( path_join( $dir, $path ) );

		$dir  = str_replace( '\\', '/', $dir );
		$real = str_replace( '\\', '/', $real );

		if ( strpos( $real, $dir ) === 0 ) {
			return true;
		}

	}

	return false;

}