forked from markng/wordpress-mtv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortcuts.php
273 lines (248 loc) · 7.63 KB
/
shortcuts.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
<?php
/**
* @package MTV
* @version 1.0
*/
namespace mtv\shortcuts;
use mtv\http\Http404;
use mtv\http\Http500;
use mtv\http\AjaxHttp500;
use mtv\models\wp\PostCollection;
use Exception;
/**
* Render and display a template
**/
function display_template( $template_name, $context=array() ) {
global $twig;
if ( !is_object($twig) ) throw new Exception('Twig template engine not available');
$template = $twig->loadTemplate( $template_name );
$template->display( $context );
}
/**
* Render and return a template
**/
function render( $template_name, $context=array() ) {
global $twig;
if ( !is_object($twig) ) throw new Exception('Twig template engine not available');
$template = $twig->loadTemplate( $template_name );
return $template->render( $context );
}
/**
* Encode and display JSON
**/
function display_json($data) {
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
$json_encoded = @json_encode( $data );
if ( json_last_error() == JSON_ERROR_NONE ) {
print( $json_encoded );
} else {
switch (json_last_error()) {
case JSON_ERROR_NONE:
$json_error = ' - No errors';
break;
case JSON_ERROR_DEPTH:
$json_error = ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$json_error = ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$json_error = ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$json_error = ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$json_error = ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$json_error = ' - Unknown error';
break;
}
//error_log( $json_error );
//error_log( print_r($data, true) );
}
}
/**
* Check current user for a capability, throw an exception if not allowed
**/
function require_capability($cap, $kwargs=null) {
if ( ! empty( $kwargs['blogid'] ) )
$answer = current_user_can_for_blog( $kwargs['blogid'], $cap );
else
$answer = current_user_can( $cap );
if ( ! $answer ) {
if ( ! empty($kwargs['ajax']) ) throw new AjaxHttp500("You can't do that");
else throw new Exception("You can't do that");
}
return true;
}
/**
* Configure the flags in wp_query
*
* List of available WP query flags
* $wp_query->is_single
* $wp_query->is_page
* $wp_query->is_archive
* $wp_query->is_date
* $wp_query->is_year
* $wp_query->is_month
* $wp_query->is_day
* $wp_query->is_time
* $wp_query->is_author
* $wp_query->is_category
* $wp_query->is_tag
* $wp_query->is_tax
* $wp_query->is_search
* $wp_query->is_home
* $wp_query->is_paged
* $wp_query->is_admin
* $wp_query->is_attachment
* $wp_query->is_singular
* $wp_query->is_404
*
* No case implemented for these yet:
* $wp_query->is_feed
* $wp_query->is_comment_feed
* $wp_query->is_trackback
* $wp_query->is_comments_popup
* $wp_query->is_robots
* $wp_query->is_posts_page
* $wp_query->is_post_type_archive
* $wp_query->is_preview
*
**/
function set_query_flags($views=null) {
global $wp_query;
if ($wp_query->max_num_pages > 1)
$wp_query->is_paged = true;
if (!is_array($views))
$views = array($views);
if ($wp_query->query_vars['preview'] == true)
$wp_query->is_preview = true;
foreach ($views as $view) {
switch ($view) {
case '404':
$wp_query->is_404 = true;
$wp_query->is_singular = false;
break;
case 'home':
$wp_query->is_home = true;
break;
case 'search':
$wp_query->is_search = true;
break;
case 'date':
$wp_query->is_date = true;
$wp_query->is_archive = true;
break;
case 'year':
$wp_query->is_year = true;
$wp_query->is_archive = true;
break;
case 'month':
$wp_query->is_month = true;
$wp_query->is_archive = true;
break;
case 'day':
$wp_query->is_day = true;
$wp_query->is_archive = true;
break;
case 'time':
$wp_query->is_time = true;
$wp_query->is_archive = true;
break;
case 'author':
$wp_query->is_author = true;
$wp_query->is_archive = true;
break;
case 'category':
$wp_query->is_category = true;
$wp_query->is_archive = true;
break;
case 'tag':
$wp_query->is_tag = true;
$wp_query->is_archive = true;
break;
case 'tax':
$wp_query->is_tax = true;
$wp_query->is_archive = true;
break;
case 'archive':
$wp_query->is_archive = true;
break;
case 'single':
$wp_query->is_single = true;
$wp_query->is_singular = true;
break;
case 'attachment':
$wp_query->is_attachment = true;
$wp_query->is_singular = true;
break;
case 'page':
$wp_query->is_page = true;
$wp_query->is_singular = true;
break;
default:
// stuff like our directory and pitch
// page will end up here.
$wp_query->is_page = true;
}
}
// Fill in the missing variables
$wp_query->query_vars = $wp_query->fill_query_vars( $wp_query->query_vars );
$wp_query->query_vars_changed = true;
$wp_query->parse_tax_query( $wp_query->query_vars );
}
/**
* Reset all of the $wp_query->is_blah flags and clear $wp_query->posts. Preps for
* our url resolver
**/
function reset_wp_query() {
global $wp_query;
// Reset query flags
$wp_query->is_single = false;
$wp_query->is_preview = false;
$wp_query->is_page = false;
$wp_query->is_archive = false;
$wp_query->is_date = false;
$wp_query->is_year = false;
$wp_query->is_month = false;
$wp_query->is_day = false;
$wp_query->is_time = false;
$wp_query->is_author = false;
$wp_query->is_category = false;
$wp_query->is_tag = false;
$wp_query->is_tax = false;
$wp_query->is_search = false;
$wp_query->is_feed = false;
$wp_query->is_comment_feed = false;
$wp_query->is_trackback = false;
$wp_query->is_home = false;
$wp_query->is_404 = false;
$wp_query->is_comments_popup = false;
$wp_query->is_paged = false;
$wp_query->is_admin = false;
$wp_query->is_attachment = false;
$wp_query->is_singular = false;
$wp_query->is_robots = false;
$wp_query->is_posts_page = false;
$wp_query->is_post_type_archive = false;
if (!empty($wp_query->posts))
unset($wp_query->posts);
}
/**
* Return a Mysql date string or unix timestamp for the current local time
**/
function current_time( $type, $gmt = 0 ) {
$t = ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * 3600 ) ) );
switch ( $type ) {
case 'mysql':
return $t;
break;
case 'timestamp':
return strtotime($t);
break;
}
}