You could filter the include paths returned by get_included_files
by removing any files from the list that are not in your theme (and/or child theme) directory:
function get_theme_includes() {
$includedfiles = get_included_files();
// normalize theme paths for matching
$styledir = str_replace("\\","https://wordpress.stackexchange.com/",get_stylesheet_directory());
$templatedir = str_replace("\\","https://wordpress.stackexchange.com/",get_template_directory());
$i = 0; // loop included files
foreach ($includedfiles as $includedfile) {
// normalize include path for match
$includedfile = str_replace("\\","https://wordpress.stackexchange.com/",$includedfile);
// check if included file is in stylesheet directory
if (substr($includedfile,0,strlen($styledir)) != $styledir) {
// if stylesheet is same as template, not a child theme
if ($styledir == $templatedir) {unset($includedfiles[$vi]);}
else {
// check if included file is in template directory
if (substr($includedfile,0,strlen($templatedir)) != $templatedir) {unset($includedfiles[$i]);}
else {
// strip template directory from include path
$pathinfo = pathinfo(str_replace(dirname($templatedir),'',$includedfile));
// add filename.php => pathinfo array to the template array
$themeincludes[$pathinfo['basename']] = $pathinfo;
}
}
} else {
// strip stylesheet dir from include path
$pathinfo = pathinfo(str_replace(dirname($styledir),'',$includedfile));
// add filename.php => pathinfo array to the template array
$themeincludes[$pathinfo['basename']] = $pathinfo;
}
$i++;
}
return $themeincludes;
}
Depending on when this function is run however you would get different results, you can use this to your advantage to further remove early theme includes since those are likely not a page templates.
add_action('wp_loaded','check_theme_includes');
add_action('wp_footer','check_theme_templates');
function check_theme_includes() {
global $themeincludes; $themeincludes = get_theme_includes();
}
function check_theme_templates() {
global $themeincludes, $templateincludes;
$templateincludes = get_theme_includes();
// strip out already included theme files from template list
foreach ($templateincludes as $template => $pathinfo) {
if (array_key_exists($template,$themeincludes)) {
if ($pathinfo['dirname'] == $themeincludes[$template]['dirname']) {
unset($templateincludes[$template]);
}
}
}
// debug point
// echo "<!-- INCLUDED TEMPLATES: "; print_r($templateincludes); echo "-->";
// output template array for use by jquery/ajax
echo "<script>var templatenames = new Array(); var templatepaths = new Array(); ";
$i = 0;
foreach ($templateincludes as $template => $pathinfo) {
// optionally strip the .php extension
$template = str_replace('.php','',$template);
// output the template array key/value
echo "templatenames[".$i."] = '".$pathinfo['filename']."'; ";
echo "templatepaths[".$i."] = '".$pathinfo['dirname']."'; ";
$i++;
}
echo "</script>";
}
Which for example would give something like this in your page footer:
<script>var templatenames = new Array(); var templatepaths = new Array(); templatenames[0] = 'front'; templatepaths[0] = '/bioship/sidebar'; templatenames[1] = 'subfront'; templatepaths[1] = '/bioship/sidebar'; templatenames[2] = 'index'; templatepaths[2] = '/bioship'; templatenames[3] = 'header'; templatepaths[3] = '/bioship/sidebar'; templatenames[4] = 'loop-hybrid'; templatepaths[4] = '/bioship'; templatenames[5] = 'content'; templatepaths[5] = '/bioship/content'; templatenames[6] = 'loop-nav'; templatepaths[6] = '/bioship/content'; templatenames[7] = 'footer'; templatepaths[7] = '/bioship/sidebar'; </script>
Which would allow you to compare these arrays and add to them with jQuery/javascript callbacks.