-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
75 lines (61 loc) · 1.78 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
<?php
$wpvc_num_views_key = "wpvc_num_views";
function wpvc_get_num_views_meta($post) {
global $wpvc_num_views_key;
return get_post_meta($post->ID, $wpvc_num_views_key, true);
}
function wpvc_get_num_views($post) {
return wpvc_coerce_valid_num_views(wpvc_get_num_views_meta($post));
}
function wpvc_set_num_views($post, $num_views) {
global $wpvc_num_views_key;
update_post_meta($post->ID, $wpvc_num_views_key, $num_views);
}
function wpvc_num_views_is_valid($num_views) {
return isset($num_views) and !empty($num_views) or is_int($num_views);
}
function wpvc_coerce_valid_num_views(&$num_views) {
if (!wpvc_num_views_is_valid($num_views)) {
$num_views = 0;
}
return $num_views;
}
function wpvc_increment_num_views($post) {
wpvc_set_num_views($post, wpvc_get_num_views($post) + 1);
}
function wpvc_get_all_num_views() {
global $wpdb, $wpvc_num_views_key;
$type = "page";
$status = "publish";
return $wpdb->get_results($wpdb->prepare(
"
select * from (
select ID, post_title, meta_value as $wpvc_num_views_key
from $wpdb->posts post
left join $wpdb->postmeta postmeta on postmeta.post_id = post.ID
where postmeta.meta_key = 'wpvc_num_views'
and post.post_type = %s
and post.post_status = %s
) as a
union all
select * from (
select ID, post_title, '0'
from $wpdb->posts post
where post.post_type = %s
and post.post_status = %s
and not exists
(
select 1
from $wpdb->postmeta postmeta
where post.ID = postmeta.post_id
and postmeta.meta_key = 'wpvc_num_views'
)
) as b
order by $wpvc_num_views_key desc, post_title asc;
",
$type,
$status,
$type,
$status
));
}