-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathacf-2way-pr-v4.php
191 lines (151 loc) · 4.02 KB
/
acf-2way-pr-v4.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
180
181
182
183
184
185
186
187
188
189
190
191
<?php
class acf_field_prbd extends acf_field_relationship {
/*
* __construct
*
* Set name / label needed for actions / filters
*
* @since 3.6
* @date 23/01/13
*/
function __construct()
{
// vars
$this->name = 'bidirectional-post-relation';
$this->label = __( 'Bidirectional Post Relation', 'acf' );
$this->category = __( 'Relational', 'acf' ); // Basic, Content, Choice, etc
$this->defaults = array(
'post_type' => array('all'),
'max' => '',
'taxonomy' => array('all'),
'filters' => array('search'),
'result_elements' => array('post_title', 'post_type'),
'return_format' => 'object'
);
$this->l10n = array(
'max' => __("Maximum values reached ( {max} values )",'acf'),
'tmpl_li' => '
<li>
<a href="#" data-post_id="<%= post_id %>"><%= title %><span class="acf-button-remove"></span></a>
<input type="hidden" name="<%= name %>[]" value="<%= post_id %>" />
</li>
'
);
// do not delete!
acf_field::__construct();
// extra
add_action('wp_ajax_acf/fields/relationship/query_posts', array($this, 'query_posts'));
add_action('wp_ajax_nopriv_acf/fields/relationship/query_posts', array($this, 'query_posts'));
}
function clean_post_value( $value ) {
// validate
if( empty($value) )
{
return $value;
}
if( is_string($value) )
{
// string
$value = explode(',', $value);
}
elseif( is_object($value) && isset($value->ID) )
{
// object
$value = array( $value->ID );
}
elseif( is_array($value) )
{
// array
foreach( $value as $k => $v ){
// object?
if( is_object($v) && isset($v->ID) )
{
$value[ $k ] = $v->ID;
}
}
}
return array_map( 'strval', $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'], false, false ) );
$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, false );
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, false );
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;
}
if( is_string($value) )
{
// string
$value = explode(',', $value);
}
elseif( is_object($value) && isset($value->ID) )
{
// object
$value = array( $value->ID );
}
elseif( is_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 $value;
}
}
// create field
new acf_field_prbd();