1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 | <?php
#Breadcrumb List Generator
class Breadcrumb
{
public static function c()
{
static $instance;
if(!is_object($instance))
{
$instance= new Breadcrumb();
}
return $instance;
}
private function rglob($no,$root)
{
#find root firts elements
$objects = $this->sanitize($no,array_merge(glob("$root*.php",GLOB_NOSORT),glob("$root*",GLOB_ONLYDIR)));
#find each root first element sub
foreach ($objects as $object)
if(is_dir($object) && !is_link($object)) $objects= $this->sanitize($no,array_merge($objects,glob("$object/*",GLOB_NOSORT|GLOB_ONLYDIR)));
return $objects;
}
private function getItems()
{
#Exclusions. to exclude unwanted file or dir. add more yourself
$no=array("error","cgi-",".xml",".txt","_");
#Translations. add more yourself
$ts=array("index.php"=>"Portail","index.html"=>"Portail");
#for breadcrumb items store
$_barr=array();
#operating root, script path
$root=preg_replace('/\b(\w+.php)\b/',"",$_SERVER["SCRIPT_FILENAME"]);
#uribase
$uribase="https://".$_SERVER["HTTP_HOST"];//URL
#docRoot
$docRoot=$_SERVER["DOCUMENT_ROOT"];
#only dir aloowed
if(is_dir($root)){
#get all dirs, subdirs and files
$objects = $this->rglob($no,$root);
foreach($objects as $i=>$object){
#when $object has valid name
if($object != "." && $object != "..")
{
#create an array for item real positions from PATH
$tmp1=array_filter(array_merge(array(0),preg_split('@/@',str_replace($docRoot,"",$object),NULL,PREG_SPLIT_NO_EMPTY)));
#name
$n=basename($object);
#index
$index=array_search($n,$tmp1);
#url
$q=$uribase.str_replace($docRoot,"",$object);
#translate name
$n=array_key_exists($n,$ts)?$ts[$n]:$n;
#breadcrumb item
$_barr[]="\r\n{\"@type\": \"ListItem\",\r\n\"position\": $index,\r\n\"name\": \"$n\",\r\n\"item\": \"$q\"\r\n}\r\n";
}
}
}
#implode items with comma and return
$_barr=implode(",", $_barr);
return $_barr;
}
public function dataLayer()
{
#for Google Tag Manager dataLayer
$g=$this->getItems();
return "\r\n<script>\r\n(dataLayer= window.dataLayer || []).push({\r\n'SiteBreadcrumb':'allowed',\r\n'Breadcrumb': [$g]\r\n});\r\n</script>\r\n";
}
public function List()
{
#for Google ld+json directly
$g=$this->getItems();
return "\r\n<script type=\"application/ld+json\">\r\n{\r\n\"@context\": \"https://schema.org\"\r\n,\"@type\": \"BreadcrumbList\"\r\n,\"itemListElement\": [$g]}\r\n</script>\r\n";
}
private function sanitize($no,$aa)
{
#remove unwanted items from glob results
$f=$aa;
foreach ($no as $a => $x) {
foreach ($aa as $b => $y) {
# code...
if(strpos($y, $x)!==false)
unset($f[$b]);
}
}
return array_values($f);
}
}
?>
|