-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtootpress_menu.php
296 lines (242 loc) · 6.12 KB
/
tootpress_menu.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/**
* Menu
*
* @package TootPress
* @since 0.1
*/
// Security: Stops code execution if WordPress is not loaded
if (!defined('ABSPATH')) { exit; }
/**
* Create TootPress Menu
*
* @since 0.1
*
* @param int Current Page
* @return html Menu HTML
*/
function tootpress_create_menu($current_page) {
$menu_html='';
$amount_of_pages=tootpress_amount_of_pages();
// No Bottom Navigation required
if($amount_of_pages==1) {
$menu_html='<!-- No Bottom Navigation required -->';
return $menu_html;
}
$menu_html.='<div class="tootpress_nav">';
// Type of Menu
$tootpress_menu_type=get_option('tootpress_navigation');
if ($tootpress_menu_type=='standard') {
$menu_html.=tootpress_generate_nav_standard($current_page);
} elseif ($tootpress_menu_type=='numbers') {
$menu_html.=tootpress_generate_nav_numbers($current_page);
}
$menu_html.='</div>';
return $menu_html;
}
/**
* Generate TootPress Standard Navigation
*
* @since 0.1
*
* @param int Current Page
* @return html Menu HTML
*/
function tootpress_generate_nav_standard($current_page) {
$menu_html='';
$is_perma_enabled=tootpress_is_pretty_permalink_enabled();
$amount_of_pages=tootpress_amount_of_pages();
// Newer Toots Link
if($current_page>1) {
$menu_html.='<a href="';
$menu_html.=tootpress_blog_url();
if($current_page==2) {
// Special Case: Page 2
$menu_html.='"';
} else {
// Page 3 to n
if($is_perma_enabled) {
// Perma
$menu_html.=($current_page-1);
$menu_html.='/"';
} else {
// Simple
$menu_html.='&tootpress=';
$menu_html.=($current_page-1);
$menu_html.='"';
}
}
$menu_html.='class="tootpress_nav_standard_newer_toots"';
$menu_html.='>';
$menu_html.=tootpress_label_newer_toots();
$menu_html.='</a>';
}
// Older Toots Link
if($current_page<$amount_of_pages) {
$menu_html.='<a href="';
$menu_html.=tootpress_blog_url();
if($is_perma_enabled) {
// Perma
$menu_html.=($current_page+1);
$menu_html.='/" ';
} else {
// Simple
$menu_html.='&tootpress=';
$menu_html.=($current_page+1);
$menu_html.='" ';
}
$menu_html.='class="tootpress_nav_standard_older_toots"';
$menu_html.='>';
$menu_html.=tootpress_label_older_toots();
$menu_html.='</a>';
}
return $menu_html;
}
/**
* Generate TootPress Number Navigation
*
* @since 0.1
*
* @param int Current Page
* @return html Menu HTML
*/
function tootpress_generate_nav_numbers($current_page) {
$menu_html='';
$is_perma_enabled=tootpress_is_pretty_permalink_enabled();
$amount_of_pages=tootpress_amount_of_pages();
for($i=0; $i<$amount_of_pages; $i++) {
$current_number=$i+1;
if(tootpress_nav_numbers_condition_number($current_number, $current_page, $amount_of_pages)) {
$menu_html.='<a href="';
$menu_html.=tootpress_blog_url();
if($i==0) {
$menu_html.='" '; // Special Case Number 1 (Perma & Simple)
}else{
if($is_perma_enabled){
// Perma
$menu_html.=($current_number);
$menu_html.='/" ';
} else {
// Simple
$menu_html.='&tootpress=';
$menu_html.=($current_number).'" ';
}
}
$menu_html.='class="tootpress_nav_number ';
if($current_page==($current_number)) {$menu_html.=' active'; }
$menu_html.='">';
$menu_html.=$current_number;
$menu_html.='</a>';
}
// Display Dots
if(tootpress_nav_numbers_condition_dots($current_number, $current_page, $amount_of_pages)) {
$menu_html.='<span class="tootpress_nav_dots">...</span>';
}
}
return $menu_html;
}
/**
* Rules to Display Numbers
*
* @since 0.1
*
* @return bool
*/
function tootpress_nav_numbers_condition_number($current_number, $current_page, $amount_of_pages) {
// 1
if($current_number==1) {
return true;
}
// Highest Number
// Always display the last number: $current_number==($amount_of_pages)
if ($current_number==($amount_of_pages)) {
return true;
}
// First 5 Numbers
// Display the first 5 Pages: $current_number <= 5
// Stop process with page 5: $current_page < 5
elseif ($current_number <= 5 && $current_page < 5 ) {
return true;
}
// Last 5 Numbers
// Display 5 Pages before Last Page: $current_number > ($amount_of_pages-5)
// Start with the fifth last page: $current_page > ($amount_of_pages-4)
elseif ($current_number > ($amount_of_pages-5) AND $current_page > ($amount_of_pages-4)) {
return true;
}
// 5 Inner Numbers
// Start = 2 Pages before Current Page: $current_number>($current_page-3)
// End = 2 Pages after Current Page: $current_number<($current_page+3)
elseif ($current_number>($current_page-3) && $current_number<($current_page+3)) {
return true;
}
return false;
}
/**
* Rules to Display Dots
*
* @since 0.1
*
* @return bool
*/
function tootpress_nav_numbers_condition_dots($current_number, $current_page, $amount_of_pages) {
// Forward Dots
// Display starting with Page 5: current page>5
// Display at iteration 1: current number==1
if($current_page>4 && $current_number==1) {
return true;
}
// Backward Dots
// Activate with more than 6 pages: $amount_of_pages>6
// Deactivate at fourth last iteration: $current_page<($amount_of_pages-3)
// Display at second last iteration: current_number==($amount_of_pages-1)
if ($amount_of_pages>6
&& $current_page<($amount_of_pages-3)
&& $current_number==($amount_of_pages-1)) {
return true;
}
return false;
}
/**
* Calculates the amount of pages
*
* @since 0.1
*
* @return int Amount of Pages
*/
function tootpress_amount_of_pages() {
$amount_of_toots=tootpress_get_amount_of_toots();
$amount_of_toots_on_page=get_option('tootpress_amount_toots_page');
$amount_of_pages=$amount_of_toots/$amount_of_toots_on_page;
$amount_of_pages=ceil($amount_of_pages);
return $amount_of_pages;
}
/**
* Creates Older Toots Label
*
* @since 0.1
*
* @return string Label
*/
function tootpress_label_older_toots() {
if(tootpress_is_language_german()) {
return 'Ältere Toots';
} else {
return 'Older Toots';
}
}
/**
* Creates Newer Toots Label
*
* @since 0.1
*
* @return string Label
*/
function tootpress_label_newer_toots() {
if(tootpress_is_language_german()) {
return 'Neuere Toots';
} else {
return 'Newer Toots';
}
}
?>