forked from scribu/wp-lib-posts-to-posts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.php
125 lines (97 loc) · 2.24 KB
/
util.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
<?php
/** @internal */
function p2p_list_cluster( $items, $callback ) {
return scb_list_group_by( $items, $callback );
}
/** @internal */
function _p2p_expand_direction( $direction ) {
if ( !$direction )
return array();
if ( 'any' == $direction )
return array( 'from', 'to' );
else
return array( $direction );
}
/** @internal */
function _p2p_compress_direction( $directions ) {
if ( empty( $directions ) )
return false;
if ( count( $directions ) > 1 )
return 'any';
return reset( $directions );
}
/** @internal */
function _p2p_flip_direction( $direction ) {
$map = array(
'from' => 'to',
'to' => 'from',
'any' => 'any',
);
return $map[ $direction ];
}
/** @internal */
function _p2p_normalize( $items ) {
if ( !is_array( $items ) )
$items = array( $items );
foreach ( $items as &$item ) {
if ( is_a( $item, 'P2P_Item' ) )
$item = $item->get_id();
elseif ( is_object( $item ) )
$item = $item->ID;
}
return $items;
}
/** @internal */
function _p2p_wrap( $items, $class ) {
foreach ( $items as &$item ) {
$item = new $class( $item );
}
return $items;
}
/** @internal */
function _p2p_extract_post_types( $sides ) {
$ptypes = array();
foreach ( $sides as $side ) {
if ( 'post' == $side->get_object_type() )
_p2p_append( $ptypes, $side->query_vars['post_type'] );
}
return array_unique( $ptypes );
}
/** @internal */
function _p2p_meta_sql_helper( $query ) {
global $wpdb;
if ( isset( $query[0] ) ) {
$meta_query = $query;
}
else {
$meta_query = array();
foreach ( $query as $key => $value ) {
$meta_query[] = compact( 'key', 'value' );
}
}
return get_meta_sql( $meta_query, 'p2p', $wpdb->p2p, 'p2p_id' );
}
/** @internal */
function _p2p_pluck( &$arr, $key ) {
$value = $arr[ $key ];
unset( $arr[ $key ] );
return $value;
}
/** @internal */
function _p2p_append( &$arr, $values ) {
$arr = array_merge( $arr, $values );
}
/** @internal */
function _p2p_first( $args ) {
if ( empty( $args ) )
return false;
return reset( $args );
}
/** @internal */
function _p2p_get_other_id( $item ) {
if ( $item->ID == $item->p2p_from )
return $item->p2p_to;
if ( $item->ID == $item->p2p_to )
return $item->p2p_from;
trigger_error( "Corrupted data for item $inner_item->ID", E_USER_WARNING );
}