forked from setola/Wordpress-Theme-Utils-Classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EscapeRoute.class.php
125 lines (113 loc) · 2.86 KB
/
EscapeRoute.class.php
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* stores the EscapeRoute class definition
*/
/**
* Manages the navigation links on a 404 page
* @author etessore
* @version 1.0.1
* @package classes
*/
/*
* Changelog
* 1.0.1
* moved from ThemeHelpers to HtmlHelper class
*/
class EscapeRoute{
/**
* @var array the list of link to be printed in case of 404
*/
public $links = array();
/**
* @var string the substitution template
*/
public $tpl;
/**
* @var SubstitutionTemplate the substitution engine
*/
public $templates;
/**
* @var string the list separator
*/
public $list_separator = '';
/**
* Initializes the object to the default values
*/
public function __construct(){
$tpl = <<<EOF
<h1>%title%</h1>
<h2>%subtitle%</h2>
<p>%desc%</p>
%list%
EOF;
$this->templates = new SubstitutionTemplate();
$this->templates
->set_tpl($tpl)
->set_markup('title', __('The page was not found', 'theme'))
->set_markup('subtitle', __('I\'m sorry but the page you were looking for is not available', 'theme'))
->set_markup('desc', __('Maybe one of these links will be usefull', 'theme'));
$this
->set_template($tpl)
->set_separator(' - ')
->add_link('javascript:history.back();', __('Back', 'theme'))
->add_link(home_url(), __('Homepage', 'theme'))
->add_link('javascript.void();', __('Book Now', 'theme'), array('class'=>'book-action2'));
}
/**
* Adds a link
* @param string $href the url to be linked
* @param string $label the inner html for the <a> tag
* @param string|array $params additional parameters for the <a>
* @return EscapeRoute $this for chainability
*/
public function add_link($href, $label, $params=''){
$tmp = new stdClass();
$tmp->href = $href;
$tmp->label = $label;
$tmp->params = $params;
$this->links[$href] = $tmp;
return $this;
}
/**
* Set the html template
* @param string $tpl the template
* @return EscapeRoute $this for chainability
*/
public function set_template($tpl){
$this->tpl = $tpl;
return $this;
}
/**
* Set the separator for the list of anchors
* @param string $separator html between every <a>
*/
public function set_separator($separator){
$this->list_separator = $separator;
return $this;
}
/**
* Renders the list of anchors
* @return the rendered the list of anchors
*/
protected function render_list(){
$toret = '';
$first = true;
foreach($this->links as $link){
if(!$first){
$toret .= $this->list_separator;
}
$toret .= HtmlHelper::anchor($link->href, $link->label, $link->params);
$first = false;
}
return $toret;
}
/**
* Retrieves the markup
* @returns the markup
*/
public function get_markup(){
return $this->templates
->set_markup('list', $this->render_list())
->replace_markup();
}
}