/
page-data-video.php
109 lines (88 loc) · 2.53 KB
/
page-data-video.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
<?php
/*
Template Name: Data - Video
*/
include('lib/stathat.php');
stathat_ez_count('patrickbest@patrickbest.com', 'novaramedia.com: api-data-tv', 1);
if (!empty($_GET['page'])) {
$page_raw = $_GET['page'];
if (is_numeric($page_raw)) {
$page = filter_var($page_raw, FILTER_SANITIZE_NUMBER_INT);
} else {
header('content-type: application/json; charset=utf-8');
$json = json_encode(array('error' => 'badly formed request'));
echo isset($_GET['callback'])
? "{$_GET['callback']}($json)"
: $json;
}
}
if (!empty($page)) {
$offset = ($page-1)*10;
} else {
$offset = 0;
$page = 1;
}
$args = array(
'posts_per_page' => 10,
'category_name' => 'video',
'post_status' => 'publish',
'offset' => $offset
);
$the_query = new WP_Query($args);
if ($the_query->post_count === 0) {
$output = array(
'site_url' => site_url(),
'channel' => 'video',
'page' => $page,
'error' => true,
'posts' => 'no posts'
);
header('content-type: application/json; charset=utf-8');
$json = json_encode($output);
echo isset($_GET['callback'])
? "{$_GET['callback']}($json)"
: $json;
} else {
$posts = array();
while ( $the_query->have_posts() ) :
$the_query->the_post();
$id = $the_query->post->ID;
$meta = get_post_meta($id);
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'col24-16to9' );
$thumbmedium = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'col12-16to9' );
$thumbsmall = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'col6-16to9' );
$tags = wp_get_post_tags( $id, array( 'fields' => 'names' ) );
$short_desc = '';
if (!empty($meta['_cmb_short_desc'])) {
$short_desc = $meta['_cmb_short_desc'][0];
}
$youtube_id = '';
if (!empty($meta['_cmb_utube'])) {
$youtube_id = $meta['_cmb_utube'][0];
}
array_push($posts, array(
'id' => $id,
'title' => $the_query->post->post_title,
'permalink' => get_permalink($id),
'thumb_large' => $thumb[0],
'thumb_medium' => $thumbmedium[0],
'thumb_small' => $thumbsmall[0],
'short_desc' => $short_desc,
'youtube_id' => $youtube_id,
'tags' => $tags
));
endwhile;
wp_reset_postdata();
$output = array(
'site_url' => site_url(),
'channel' => 'video',
'page' => $page,
'posts' => $posts
);
header('content-type: application/json; charset=utf-8');
$json = json_encode($output);
echo isset($_GET['callback'])
? "{$_GET['callback']}($json)"
: $json;
}
?>