-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunctions.php
179 lines (164 loc) · 3.54 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
<?php
/**
* Utility functions
*
* @package tscf
*/
/**
* Get plugin version
*
* @since 1.0.0
* @return string
*/
function tscf_version() {
static $info = null;
if ( is_null( $info ) ) {
$info = get_file_data( __DIR__ . '/tscf.php', [
'version' => 'Version',
] );
}
return $info['version'];
}
function tscf( $key, $object ) {
}
/**
* Get meta for post
*
* @param string $key
* @param null|int|WP_Post $post
*
* @return mixed
*/
function tscfp( $key, $post = null ) {
$post = get_post( $post );
return $post ? get_post_meta( $post->ID, $key, true ) : '';
}
function tscft( $key, $term = null ) {
}
function tscfc( $key, $comment = null ) {
}
/**
* Get post list
*
* @param string $key
* @param null|int|WP_post $post
*
* @return array
*/
function tscf_post_list( $key, $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return [];
}
$post_ids = array_filter( explode( ',', get_post_meta( $post->ID, $key, true ) ) );
if ( empty( $post_ids ) ) {
return [];
}
$args = [
'post_type' => 'any',
'post__in' => $post_ids,
'post_status' => 'publish',
'suppress_filters' => false,
'orderby' => 'post__in',
];
/**
* tscf_post_list_args
*
* @package tscf
* @since 1.0.0
* @param array $args
* @param string $key
* @param WP_Post $post
*/
$args = apply_filters( 'tscf_post_list_args', $args, $key, $post );
return get_posts( $args );
}
/**
* Return group as array
*
* @param string $group
* @param null|int|WP_Post $post
*
* @return array
*/
function tscf_repeat_field( $group, $post = null ) {
$post = get_post( $post );
$atts = [];
foreach ( get_post_custom( $post->ID ) as $key => $values ) {
if ( preg_match( "#{$group}_(.*)_([0-9]+)#u", $key, $matches ) ) {
if ( ! isset( $atts[ $matches[2] ] ) ) {
$atts[ $matches[2] ] = [];
}
$atts[ $matches[2] ][ $matches[1] ] = current( $values );
}
}
ksort( $atts );
return $atts;
}
/**
* Get images
*
* @package tscf
* @param string $key Post meta key.
* @param null|int|WP_Post $post Post object.
*
* @return array
*/
function tscf_images( $key, $post = null ) {
$post = get_post( $post );
$image_ids = array_filter( array_map( 'trim', explode( ',', get_post_meta( $post->ID, $key, true ) ) ) );
if ( ! $image_ids ) {
return [];
}
return get_posts( [
'post_type' => 'attachment',
'post__in' => $image_ids,
'orderby' => 'post__in',
'posts_per_page' => - 1,
] );
}
/**
* Get post status label.
*
* @package tscf
* @since 1.0.3
* @param null|int|WP_Post $post Post object.
* @return string
*/
function tscf_post_status( $post = null ) {
$status = get_post_status( $post );
if ( ! $status ) {
return '';
}
$status_obj = get_post_status_object( $status );
return $status_obj ? $status_obj->label : '';
}
/**
* Get referencing posts
*
* @param string $key
* @param array $args
* @param null|int|WP_Post $post
* @return array
*/
function tscf_referencing_posts( $key, $args = [], $post = null ) {
global $wpdb;
$post = get_post( $post );
$query = <<<SQL
SELECT DISTINCT post_id FROM {$wpdb->postmeta}
WHERE meta_key = %s
AND FIND_IN_SET(%s, meta_value)
SQL;
$post_ids = $wpdb->get_col( $wpdb->prepare( $query, $key, $post->ID ) );
if ( ! $post_ids ) {
return [];
}
$args = array_merge( [
'post_type' => 'any',
'post__in' => $post_ids,
'post_status' => 'publish',
'posts_per_page' => -1,
'suppress_filters' => false,
], $args );
return get_posts( $args );
}