-
Notifications
You must be signed in to change notification settings - Fork 9
/
acf-2way-pr-v5.php
176 lines (152 loc) · 3.79 KB
/
acf-2way-pr-v5.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
<?php
class acf_field_prbd extends acf_field_relationship {
/*
* __construct
*
* This function will setup the field type data
*
* @type function
* @date 5/03/2014
* @since 5.0.0
*
* @param n/a
* @return n/a
*/
function __construct() {
// vars
$this->name = 'bidirectional-post-relation';
$this->label = __( 'Bidirectional Post Relation', 'acf' );
$this->category = 'relational';
$this->defaults = array(
'post_type' => array(),
'taxonomy' => array(),
'max' => 0,
'filters' => array('search', 'post_type', 'taxonomy'),
'elements' => array(),
'return_format' => 'object'
);
$this->l10n = array(
'max' => __("Maximum values reached ( {max} values )",'acf'),
'loading' => __('Loading','acf'),
'empty' => __('No matches found','acf'),
);
// extra
add_action('wp_ajax_acf/fields/relationship/query', array($this, 'ajax_query'));
add_action('wp_ajax_nopriv_acf/fields/relationship/query', array($this, 'ajax_query'));
// do not delete!
acf_field::__construct();
}
function clean_post_value( $value ) {
// validate
if( empty($value) ) {
return $value;
}
// force value to array
$value = acf_force_type_array( $value );
// array
foreach( $value as $k => $v ){
// object?
if( is_object($v) && isset($v->ID) )
{
$value[ $k ] = $v->ID;
}
}
// save value as strings, so we can clearly search for them in SQL LIKE statements
$value = array_map('strval', $value);
// return
return $value;
}
/*
* update_value()
*
* This filter is applied to the $value before it is updated in the db
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param $value - the value which will be saved in the database
* @param $post_id - the $post_id of which the value will be saved
* @param $field - the field array holding all the field options
*
* @return $value - the modified value
*/
//
// function update_value( $value, $post_id, $field ) {
//
// if ( $post_id === $GLOBALS['post_id'] ) {
// $new_value = $this->clean_post_value( $value );
// $old_value = $this->clean_post_value( get_field( $field['key'] ) );
//
// $new_values = array();
// $missing_values = array();
//
// if ( ( ! empty( $new_value ) ) && ( ! empty( $old_value ) ) ) {
// $new_values = array_diff( $new_value, $old_value );
// $missing_values = array_diff( $old_value, $new_value );
// } else {
// if ( ! empty( $new_value ) ) {
// $new_values = array_map( 'strval', $new_value );
// }
// if ( ! empty( $old_value ) ) {
// $missing_values = array_map( 'strval', $old_value );
// }
// }
//
// foreach( $new_values as $value_add ) {
// $existing_value = get_field( $field['key'], $value_add );
//
// if ( ! empty( $existing_value ) ) {
// $existing_value[] = $post_id;
// } else {
// $existing_value = array( $post_id );
// }
//
// update_field( $field['key'], $existing_value, $value_add );
// }
//
// foreach( $missing_values as $value_remove ) {
// $existing_value = get_field( $field['key'], $value_remove );
//
// if ( ! empty( $existing_value ) ) {
// $existing_value = array_diff( $existing_value, array( $post_id ) );
// update_field( $field['key'], $existing_value, $value_remove );
// }
//
// }
// }
//
// // validate
// if( empty($value) ) {
//
// return $value;
//
// }
//
//
// // force value to array
// $value = acf_force_type_array( $value );
//
//
// // array
// foreach( $value as $k => $v ){
//
// // object?
// if( is_object($v) && isset($v->ID) )
// {
// $value[ $k ] = $v->ID;
// }
// }
//
//
// // save value as strings, so we can clearly search for them in SQL LIKE statements
// $value = array_map('strval', $value);
//
//
// // return
// return $value;
//
// }
}
// create field
//new acf_field_prbd();