This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
/
product-template.php
469 lines (392 loc) · 11.1 KB
/
product-template.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<?php
/**
* template functions for products
* @package media-libraries
* @author Christopher Roussel <christopher@impleri.net>
*/
/**
* wrapper to template hack for archive-ml_product
*
* @param string found template
* @return string path to template
*/
function ml_product_archive_template ($template) {
return ml_insert_template ($template, 'ml_product', 'archive');
}
/**
* wrapper to template hack for single-ml_product
*
* @param string found template
* @return string path to template
*/
function ml_product_single_template ($template) {
return ml_insert_template ($template, 'ml_product', 'single');
}
/**
* wrapper to template hack for taxonomy-ml_person
*
* @param string found template
* @return string path to template
*/
function ml_person_taxonomy_template ($template) {
return ml_insert_template ($template, 'ml_person', 'taxonomy');
}
/**
* Retrieve product image
*
* Get the product image URL if it exists
*
* @param int $id Optional. Post ID.
* @return string
*/
function get_the_product_image ($id=0) {
$post = get_post($id);
$id = ($post->post_parent > 0) ? $post->post_parent : $post->ID;
$image = get_post_meta($id, 'ml_image', true);
return apply_filters('the_image', $image, $id);
}
/**
* Display or retrieve the current product image with optional content.
*
* @param string $before Optional. Content to prepend to the image URL.
* @param string $after Optional. Content to append to the image URL.
* @param bool $echo Optional, default to true. Whether to display or return.
* @return null|string Null on no image. String if $echo parameter is false.
*/
function the_product_image ($before='', $after='', $echo=true) {
$image = get_the_product_image();
if (strlen($image)==0) {
return;
}
$image = $before . $image . $after;
if ($echo) {
echo $image;
}
else {
return $image;
}
}
/**
* Retrieve the people for a post.
*
* @uses apply_filters() Calls 'get_the_people' filter on the list of people tags.
*
* @param int $id Post ID.
* @return array
*/
function get_the_people ($id=0) {
$post = get_post($id);
$id = ($post->post_parent > 0) ? $post->post_parent : $post->ID;
return apply_filters( 'get_the_people', get_the_terms($id, 'ml_person'));
}
/**
* Retrieve the people for a product formatted as a string.
*
* @uses apply_filters() Calls 'the_people' filter on string list of people.
*
* @param string $before Optional. Before people.
* @param string $sep Optional. Between people.
* @param string $after Optional. After people.
* @return string
*/
function get_the_people_list ($id=0, $before='', $sep='', $after='') {
$people = get_the_people($id);
if (is_wp_error($people)) {
return $people;
}
if (empty($people)) {
return false;
}
foreach ( $people as $term ) {
$link = get_term_link($term, 'ml_person');
if (is_wp_error($link)) {
return $link;
}
$people_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
}
$people_links = apply_filters('term_links-ml-person', $people_links);
return $before . join($sep, $people_links) . $after;
}
/**
* Retrieve the people for a product.
*
* @param int $id Post ID.
* @param string $before Optional. Before list.
* @param string $sep Optional. Separate items using this.
* @param string $after Optional. After list.
* @return string
*/
function the_people ($id=0, $before=null, $sep=', ',$after = '') {
$people = get_the_people_list($id, $before, $sep, $after);
if (is_wp_error($people)) {
return false;
}
echo apply_filters('the_people', $people, $before, $sep, $after);
}
/**
* Retrieve product type
*
* Get the product type
*
* @param int $id Optional. Post ID.
* @return string
*/
function get_the_product_type ($id=0) {
$post = get_post($id);
$id = ($post->post_parent > 0) ? $post->post_parent : $post->ID;
$type = get_post_meta($id, 'ml_type', true);
$type = isset($type) ? $type : 'b';
$types = ml_product_categories();
return apply_filters('the_product_type', $types[$type]);
}
/**
* Display or retrieve the current product type with optional content.
*
* @param string $before Optional. Content to prepend to the type.
* @param string $after Optional. Content to append to the type.
* @param bool $echo Optional, default to true. Whether to display or return.
* @return null|string Null on no type. String if $echo parameter is false.
*/
function the_product_type ($before='', $after='', $echo=true) {
$type = get_the_product_type();
if (strlen($type)==0) {
return;
}
$type = $before . $type . $after;
if ($echo) {
echo $type;
}
else {
return $type;
}
}
/**
* Retrieve product type
*
* Get the product type
*
* @param int $id Optional. Post ID.
* @return string
*/
function get_the_product_title ($id=0) {
$post = get_post($id);
if ($post->post_parent > 0) {
$parent = get_post($post->post_parent);
$title = $parent->post_title;
}
else {
$title = $post->post_title;
}
return apply_filters('the_title', $title);
}
/**
* Display or retrieve the current product type with optional content.
*
* @param string $before Optional. Content to prepend to the type.
* @param string $after Optional. Content to append to the type.
* @param bool $echo Optional, default to true. Whether to display or return.
* @return null|string Null on no type. String if $echo parameter is false.
*/
function the_product_title ($before='', $after='', $echo=true) {
$type = get_the_product_title();
if (strlen($type)==0) {
return;
}
$type = $before . $type . $after;
if ($echo) {
echo $type;
}
else {
return $type;
}
}
/**
* Retrieve product link
*
* Get the product source URL if it exists
*
* @param int $id Optional. Post ID.
* @return string
*/
function get_the_product_link ($id=0) {
$post = get_post($id);
$id = ($post->post_parent > 0) ? $post->post_parent : $post->ID;
$link = get_post_meta($id, 'ml_link', true);
return apply_filters('the_product_link', $link, $id);
}
/**
* Display or retrieve the current product link with optional content.
*
* @param string $before Optional. Content to prepend to the source URL.
* @param string $after Optional. Content to append to the source URL.
* @param bool $echo Optional, default to true. Whether to display or return.
* @return null|string Null on no image. String if $echo parameter is false.
*/
function the_product_link ($before='', $after='', $echo=true) {
$link = get_the_product_link();
if (strlen($link)==0) {
return;
}
$link = $before . $link . $after;
if ($echo) {
echo $link;
}
else {
return $link;
}
}
/**
* Retrieve the amount of reviews a post has.
*
* @param int $product_id The Product ID
* @return int The number of reviews a post has
*/
function get_reviews_number ($product_id=0, $type='all') {
$product_id = absint($product_id);
if (!$product_id) {
$id = get_the_ID();
}
$post = get_post($id);
$product_id = ($post->post_parent > 0) ? $post->post_parent : $post->ID;
$args = array('post_type' => 'ml_review', 'post_parent' => $product_id);
if ($type != 'all') {
$official = get_post_meta($product_id, 'ml_official_review');
if ($type == 'official') {
return count($official);
}
$args['exclude'] = $official;
}
$reviews = get_posts($args);
$count = count($reviews);
return apply_filters('get_reviews_number', $count, $product_id);
}
/**
* Display the language string for the number of reviews the current post has.
*
* @param string $zero Text for no reviews
* @param string $one Text for one review
* @param string $more Text for more than one review
*/
function reviews_number ($zero=false, $one=false, $more=false) {
$number = get_reviews_number();
if ($number > 1) {
$output = str_replace('%', number_format_i18n($number), ( false === $more ) ? __('% Reviews', 'media-libraries') : $more);
}
elseif ($number == 0) {
$output = ( false === $zero ) ? __('No Reviews', 'media-libraries') : $zero;
}
else {
$output = ( false === $one ) ? __('1 Review', 'media-libraries') : $one;
}
echo apply_filters('reviews_number', $output, $number);
}
/**
* Quickly tell if a product has reviews
*
* @param int product ID (defaults to get_the_ID())
* @return bool true if reviews exist for the product
*/
function has_reviews ($product_ID=0, $type='all') {
return (get_reviews_number($product_ID, $type) > 0);
}
/**
* Retrieve page numbers links.
*
* @param int $pagenum Optional. Page number.
* @return string
*/
function get_reviews_pagenum_link ($pagenum=1, $max_page=0) {
global $post, $wp_rewrite;
$pagenum = (int) $pagenum;
$result = get_permalink($post->ID);
if ('newest' == get_option('default_comments_page')) {
if ($pagenum != $max_page) {
if ($wp_rewrite->using_permalinks()) {
$result = user_trailingslashit( trailingslashit($result) . 'review-page-' . $pagenum, 'reviewpaged');
}
else {
$result = add_query_arg('cpage', $pagenum, $result);
}
}
}
elseif ($pagenum > 1) {
if ($wp_rewrite->using_permalinks()) {
$result = user_trailingslashit( trailingslashit($result) . 'review-page-' . $pagenum, 'reviewpaged');
}
else {
$result = add_query_arg( 'cpage', $pagenum, $result );
}
}
$result .= '#reviews';
$result = apply_filters('get_reviews_pagenum_link', $result);
return $result;
}
/**
* Return the link to next reviews pages.
*
* @param string $label Optional. Label for link text.
* @param int $max_page Optional. Max page.
* @return string|null
*/
function get_next_reviews_link ($label='', $max_page=0) {
global $wp_query;
if (!is_singular()) {
return;
}
$page = get_query_var('cpage');
$nextpage = intval($page) + 1;
if (empty($max_page)) {
$max_page = get_review_pages_count();
}
if ($nextpage >= $max_page) {
return;
}
if (empty($label)) {
$label = __('Newer Reviews »');
}
return '<a href="' . esc_url(get_reviews_pagenum_link($nextpage, $max_page)) . '" ' . apply_filters( 'next_reviews_link_attributes', '' ) . '>'. preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) .'</a>';
}
/**
* Display the link to next reviews pages.
*
* @since 2.7.0
*
* @param string $label Optional. Label for link text.
* @param int $max_page Optional. Max page.
*/
function next_reviews_link( $label = '', $max_page = 0 ) {
echo get_next_reviews_link( $label, $max_page );
}
/**
* Return the previous reviews page link.
*
* @since 2.7.1
*
* @param string $label Optional. Label for reviews link text.
* @return string|null
*/
function get_previous_reviews_link( $label = '' ) {
if ( !is_singular() || !get_option('page_reviews') )
return;
$page = get_query_var('cpage');
if ( intval($page) <= 1 )
return;
$prevpage = intval($page) - 1;
if ( empty($label) )
$label = __('« Older Comments');
return '<a href="' . esc_url( get_reviews_pagenum_link( $prevpage ) ) . '" ' . apply_filters( 'previous_reviews_link_attributes', '' ) . '>' . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) .'</a>';
}
/**
* Display the previous reviews page link.
*
* @since 2.7.0
*
* @param string $label Optional. Label for reviews link text.
*/
function previous_reviews_link( $label = '' ) {
echo get_previous_reviews_link( $label );
}
// everything below is unstable
function product_shelf_listing() {
}
function the_product_usage () {}