-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.php
365 lines (309 loc) · 14 KB
/
functions.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/*
* Helper function to log debug messages
*/
if (!function_exists('daiquiri_log')) {
function daiquiri_log($message)
{
if (DAIQUIRI_DEBUG) {
error_log($message);
}
}
}
/*
* Singleton to get and store the layout form daiquiri.
* The layout is seperated in header and footer wordpress style.
*/
class Daiquiri_Layout {
public static function getInstance() {
static $instances = array();
$className = get_called_class();
if (!isset($instances[$className])) {
$instances[$className] = new $className();
}
return $instances[$className];
}
final private function __clone() {
}
private function __construct() {
// get the layout url
$url = rtrim(DAIQUIRI_URL, '/') . '/layout/?request_uri=' . $_SERVER['REQUEST_URI'];
// construct request
require_once('HTTP/Request2.php');
$req = new HTTP_Request2($url);
$req->setMethod('GET');
if (isset($_COOKIE["sessionid"])) {
$req->addCookie("sessionid", $_COOKIE["sessionid"]);
}
$req->setConfig(array(
'ssl_verify_peer' => false, // we trust the certificate here
'connect_timeout' => 2,
'timeout' => 3
));
// get layout from the server
try {
$response = $req->send();
$status = $response->getStatus();
$body = $response->getBody();
daiquiri_log($url . ' returned ' . $status);
if (200 != $status) {
echo '<h1>Error with theme</h1><p>HTTP request status != 200. Please set the correct DAIQUIRI_URL in wp-config.php.</p>';
die(0);
}
} catch (HTTP_Request2_Exception $e) {
echo '<h1>Error with theme</h1><p>Error with HTTP request. Please set the correct DAIQUIRI_URL in wp-config.php.</p>';
throw $e;
}
// split layout in header and footer
$body = explode('<!-- content placeholder -->', $body);
if (count($body) == 2) {
$this->_header = $body[0];
$this->_footer = $body[1];
} else {
echo '<h1>Error with theme</h1><p>Malformatted layout.</p>';
die(0);
}
}
public function get_header() {
// get the closing tag of the html head
$pos = strpos($this->_header,'</head>');
// echo the header and 'inject' the wp_head hook
echo substr($this->_header, 0, $pos);
wp_head();
echo substr($this->_header, $pos);
}
public function get_footer() {
// get the closing tag of the html body
$pos = strpos($this->_footer,'</body>');
// echo the footer and 'inject' the wp_footer hook
echo substr($this->_footer, 0, $pos);
wp_footer();
echo substr($this->_footer, $pos);
}
}
/*
* Initialize dynamic sidebar
*/
register_sidebar(array(
'name' => 'Sidebar',
'id' => 'sidebar',
'before_widget' => '<li>',
'after_widget' => "</li>",
'before_title' => "<h2>",
'after_title' => "</h2>"
));
/*
* Function to render the comment list
*/
function daiquiri_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
extract($args, EXTR_SKIP);
?>
<div <?php comment_class(empty($args['has_children']) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
<div class="comment-header">
<p>
<a href="<?php comment_link() ?>"></a>
<?php if ($comment->comment_approved == '0') : ?>
<em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.') ?></em>
<?php endif; ?>
</p>
</div>
<div class="comment-body">
<?php comment_text() ?>
</div>
<div class="comment-footer">
by <?php comment_author_link(get_comment_ID()) ?>,
<?php comment_date() ?>, <?php comment_time('H:i') ?>
<a href="<?php comment_link() ?>">Permalink</a>
<?php comment_reply_link(array_merge($args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
</div>
</li>
<?php
}
/*
* Modyfy style of admin interface
*/
// add_action('admin_head', 'daiquiri_admin_style');
// function daiquiri_admin_style() {
// echo '<style>
// #wp-auth-check-wrap {
// display: none !important;
// }
// </style>'.PHP_EOL;
// }
/*
* Admin Video/Image Box
* see http://www.smashingmagazine.com/2011/10/04/create-custom-post-meta-boxes-wordpress/
*/
add_action( 'load-post.php', 'daiquiri_post_meta_boxes_setup' );
add_action( 'load-post-new.php', 'daiquiri_post_meta_boxes_setup' );
function daiquiri_post_meta_boxes_setup() {
add_action( 'add_meta_boxes', 'daiquiri_add_post_meta_boxes' );
add_action( 'save_post', 'daiquiri_save_video_meta_box', 10, 2 );
add_action( 'save_post', 'daiquiri_save_image_meta_box', 10, 2 );
}
function daiquiri_add_post_meta_boxes() {
add_meta_box(
'daiquiri-video-meta-box',
esc_html__( 'Video', 'example' ), // Title
'daiquiri_video_meta_box', // Callback function
'post', // Admin page (or post type)
'normal', // Context
'default' // Priority
);
add_meta_box(
'daiquiri-image-meta-box',
esc_html__( 'Image', 'example' ), // Title
'daiquiri_image_meta_box', // Callback function
'post', // Admin page (or post type)
'normal', // Context
'default' // Priority
);
}
function daiquiri_video_meta_box($object, $box) {
wp_nonce_field( basename( __FILE__ ), 'daiquiri_video_meta_box_nonce' );
$post_id = $object->ID;
$poster = get_post_meta($post_id,'video_poster',true);
$mp4 = get_post_meta($post_id,'video_mp4',true);
$avi = get_post_meta($post_id,'video_avi',true);
$ogg = get_post_meta($post_id,'video_ogg',true);
$webm = get_post_meta($post_id,'video_webm',true);
echo '<p>';
echo '<label for="daiquiri-video-meta-box-poster">' . _e("Video poster",'example') . '</label>';
echo '<input class="widefat" type="text" name="daiquiri-video-meta-box-poster" id="daiquiri-video-meta-box-poster" value="' . $poster . '"/>';
echo '</p>';
echo '<p>';
echo '<label for="daiquiri-video-meta-box-mp4">' . _e("Video mp4 file",'example') . '</label>';
echo '<input class="widefat" type="text" name="daiquiri-video-meta-box-mp4" id="daiquiri-video-meta-box-mp4" value="' . $mp4 . '"/>';
echo '</p>';
echo '<p>';
echo '<label for="daiquiri-video-meta-box-avi">' . _e("Video avi file",'example') . '</label>';
echo '<input class="widefat" type="text" name="daiquiri-video-meta-box-avi" id="daiquiri-video-meta-box-avi" value="' . $avi . '"/>';
echo '</p>';
echo '<p>';
echo '<label for="daiquiri-video-meta-box-ogg">' . _e("Video ogg file",'example') . '</label>';
echo '<input class="widefat" type="text" name="daiquiri-video-meta-box-ogg" id="daiquiri-video-meta-box-ogg" value="' . $ogg . '"/>';
echo '</p>';
echo '<p>';
echo '<label for="daiquiri-video-meta-box-webm">' . _e("Video webm file",'example') . '</label>';
echo '<input class="widefat" type="text" name="daiquiri-video-meta-box-webm" id="daiquiri-video-meta-box-webm" value="' . $webm . '"/>';
echo '</p>';
}
function daiquiri_save_video_meta_box($post_id, $post) {
/* Verify the nonce before proceeding. */
if (!isset($_POST['daiquiri_video_meta_box_nonce']) || !wp_verify_nonce( $_POST['daiquiri_video_meta_box_nonce'],basename(__FILE__))) {
return $post_id;
}
/* Get the post type object. */
$post_type = get_post_type_object($post->post_type);
/* Check if the current user has permission to edit the post. */
if (!current_user_can($post_type->cap->edit_post,$post_id)) {
return $post_id;
}
/* Get the posted data and sanitize it */
$new = array();
$new['video_poster'] = (isset($_POST['daiquiri-video-meta-box-poster']) ? sanitize_text_field($_POST['daiquiri-video-meta-box-poster']) : '');
$new['video_mp4'] = (isset($_POST['daiquiri-video-meta-box-mp4']) ? sanitize_text_field($_POST['daiquiri-video-meta-box-mp4']) : '');
$new['video_avi'] = (isset($_POST['daiquiri-video-meta-box-avi']) ? sanitize_text_field($_POST['daiquiri-video-meta-box-avi']) : '');
$new['video_ogg'] = (isset($_POST['daiquiri-video-meta-box-ogg']) ? sanitize_text_field($_POST['daiquiri-video-meta-box-ogg']) : '');
$new['video_webm'] = (isset($_POST['daiquiri-video-meta-box-webm']) ? sanitize_text_field($_POST['daiquiri-video-meta-box-webm']) : '');
/* Get the current data */
$current = array();
$current['video_poster'] = get_post_meta($post_id,'video_poster',true);
$current['video_mp4'] = get_post_meta($post_id,'video_mp4',true);
$current['video_avi'] = get_post_meta($post_id,'video_avi',true);
$current['video_ogg'] = get_post_meta($post_id,'video_ogg',true);
$current['video_webm'] = get_post_meta($post_id,'video_webm',true);
foreach (array('video_poster','video_mp4','video_avi','video_ogg','video_webm') as $key) {
if ($new[$key] && '' == $current[$key]) {
/* If a new meta value was added and there was no previous value, add it. */
add_post_meta($post_id, $key, $new[$key], true);
} elseif ($new[$key] && $new[$key] != $current[$key]) {
/* If the new meta value does not match the old value, update it. */
update_post_meta($post_id, $key, $new[$key]);
} elseif ('' == $new[$key] && $current[$key]) {
/* If there is no new meta value but an old value exists, delete it. */
delete_post_meta($post_id, $key, $new[$key]);
}
}
}
function daiquiri_image_meta_box($object, $box) {
wp_nonce_field( basename( __FILE__ ), 'daiquiri_image_meta_box_nonce' );
$post_id = $object->ID;
$small = get_post_meta($post_id,'image_small',true);
$large = get_post_meta($post_id,'image_large',true);
echo '<p>';
echo '<label for="daiquiri-video-meta-box-small">' . _e("Image source (small, 320px)",'example') . '</label>';
echo '<input class="widefat" type="text" name="daiquiri-image-meta-box-small" id="daiquiri-image-meta-box-small" value="' . $small . '"/>';
echo '</p>';
echo '<p>';
echo '<label for="daiquiri-video-meta-box-large">' . _e("Image source (large)",'example') . '</label>';
echo '<input class="widefat" type="text" name="daiquiri-image-meta-box-large" id="daiquiri-image-meta-box-large" value="' . $large . '"/>';
echo '</p>';
}
function daiquiri_save_image_meta_box($post_id, $post) {
/* Verify the nonce before proceeding. */
if (!isset($_POST['daiquiri_image_meta_box_nonce']) || !wp_verify_nonce( $_POST['daiquiri_image_meta_box_nonce'],basename(__FILE__))) {
return $post_id;
}
/* Get the post type object. */
$post_type = get_post_type_object($post->post_type);
/* Check if the current user has permission to edit the post. */
if (!current_user_can($post_type->cap->edit_post,$post_id)) {
return $post_id;
}
/* Get the posted data and sanitize it */
$new = array();
$new['image_small'] = (isset($_POST['daiquiri-image-meta-box-small']) ? sanitize_text_field($_POST['daiquiri-image-meta-box-small']) : '');
$new['image_large'] = (isset($_POST['daiquiri-image-meta-box-large']) ? sanitize_text_field($_POST['daiquiri-image-meta-box-large']) : '');
/* Get the current data */
$current = array();
$current['image_small'] = get_post_meta($post_id,'image_small',true);
$current['image_large'] = get_post_meta($post_id,'image_large',true);
foreach (array('image_small','image_large') as $key) {
if ($new[$key] && '' == $current[$key]) {
/* If a new meta value was added and there was no previous value, add it. */
add_post_meta($post_id, $key, $new[$key], true);
} elseif ($new[$key] && $new[$key] != $current[$key]) {
/* If the new meta value does not match the old value, update it. */
update_post_meta($post_id, $key, $new[$key]);
} elseif ('' == $new[$key] && $current[$key]) {
/* If there is no new meta value but an old value exists, delete it. */
delete_post_meta($post_id, $key, $new[$key]);
}
}
}
function daiquiri_the_video($width) {
$post_id = get_the_ID();
$poster = get_post_meta($post_id,'video_poster',true);
$mp4 = get_post_meta($post_id,'video_mp4',true);
$avi = get_post_meta($post_id,'video_avi',true);
$ogg = get_post_meta($post_id,'video_ogg',true);
$webm = get_post_meta($post_id,'video_webm',true);
if (!empty($poster) || !empty($mp4) || ! empty($ogg)) {
echo '<div style="width: ' . $width . 'px;">';
echo '<video poster="' . $poster . '" controls="controls" style="width: 100%;">';
if (!empty($mp4)) echo '<source src="' . $mp4 . '" type="video/mp4" />';
if (!empty($avi)) echo '<source src="' . $avi . '" type="video/avi" />';
if (!empty($ogg)) echo '<source src="' . $ogg . '" type="video/ogg" />';
if (!empty($webm)) echo '<source src="' . $webm . '" type="video/webm" />';
echo 'Your browser does not support the video tag.</video></div>';
}
}
function daiquiri_the_image($width) {
$post_id = get_the_ID();
$small = get_post_meta($post_id,'image_small',true);
$large = get_post_meta($post_id,'image_large',true);
$alt = get_the_title($post_id);
if (!empty($small)) {
echo '<div style="width: ' . $width . 'px;">';
if (!empty($large)) echo '<a href="' . $large . '">';
if ($width > 320 && !empty($large)) {
echo '<img src="' . $large . '" alt="' . $alt . '" />';
} else {
echo '<img src="' . $small . '" alt="' . $alt . '" />';
}
if (!empty($large)) echo '</a>';
echo '</div>';
}
}