-
Notifications
You must be signed in to change notification settings - Fork 2
/
class-mb-facetwp-integrator.php
184 lines (173 loc) · 5.25 KB
/
class-mb-facetwp-integrator.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
<?php
/**
* Integrates Meta Box custom fields with FacetWP
*
* @package Meta Box
* @subpackage MB FacetWP Integrator
*/
/**
* Integration class.
*/
class MB_FacetWP_Integrator {
/**
* Indexer from FacetWP.
*
* @var FacetWP_Indexer
*/
protected $indexer;
public function __construct() {
add_filter( 'facetwp_facet_sources', [ $this, 'add_source' ] );
add_filter( 'facetwp_indexer_post_facet', [ $this, 'index' ], 1, 2 );
}
/**
* Add Meta Box fields to the Data Sources dropdown
*
* @param array $sources Array of sources.
*
* @return array
*/
public function add_source( $sources ) {
$sources['meta-box'] = [
'label' => 'Meta Box',
'choices' => [],
'weight' => 5,
];
$fields = $this->get_fields();
foreach ( $fields as $post_type => $list ) {
$post_type_object = get_post_type_object( $post_type );
if ( ! $post_type_object ) {
continue;
}
$post_type_label = $post_type_object->labels->singular_name;
foreach ( $list as $field ) {
if ( in_array( $field['type'], [ 'heading', 'divider', 'custom_html', 'button' ], true ) ) {
continue;
}
$field_label = $field['name'] ? $field['name'] : $field['id'];
$sources['meta-box']['choices'][ "meta-box/{$field['id']}" ] = "[{$post_type_label}] {$field_label}";
}
}
return $sources;
}
/**
* Index Meta Box field data
*
* @param bool $bypass Bypass default indexing.
* @param array $params Extra helper data.
*
* @return bool
*/
public function index( $bypass, $params ) {
$this->indexer = FWP()->indexer;
$defaults = $params['defaults'];
$facet = $params['facet'];
if ( ! isset( $facet['source'] ) || 'meta-box/' !== substr( $facet['source'], 0, 9 ) ) {
return $bypass;
}
$field_id = substr( $facet['source'], 9 );
$field = rwmb_get_field_settings( $field_id, [], $defaults['post_id'] );
if ( ! $field ) {
return;
}
$value = rwmb_get_value( $field_id, [], $defaults['post_id'] );
if ( $field['clone'] ) {
foreach ( $value as $clone_value ) {
if ( $field['multiple'] ) {
foreach ( $clone_value as $sub_value ) {
$this->index_field_value( $sub_value, $field, $defaults );
}
} else {
$this->index_field_value( $clone_value, $field, $defaults );
}
}
} else {
if ( $field['multiple'] ) {
foreach ( $value as $sub_value ) {
$this->index_field_value( $sub_value, $field, $defaults );
}
} else {
$this->index_field_value( $value, $field, $defaults );
}
}
return $bypass;
}
/**
* Get all fields, grouped by post type.
*
* @return array
*/
protected function get_fields() {
return rwmb_get_registry( 'field' )->get_by_object_type( 'post' );
}
/**
* Index field value.
*
* @param mixed $value Field value.
* @param array $field Field settings.
* @param array $params Extra parameters.
*/
protected function index_field_value( $value, $field, $params ) {
// Choices.
if ( in_array( $field['type'], [ 'checkbox_list', 'radio', 'select', 'select_advanced' ], true ) && isset( $field['options'][ $value ] ) ) {
$params['facet_value'] = $value;
$params['facet_display_value'] = $field['options'][ $value ];
$this->indexer->index_row( $params );
} // Post
elseif ( 'post' === $field['type'] ) {
$post = get_post( $value );
$params['facet_value'] = $value;
$params['facet_display_value'] = $post->post_title;
$this->indexer->index_row( $params );
} // User.
elseif ( 'user' === $field['type'] ) {
$user = get_userdata( $value );
if ( false !== $user ) {
$params['facet_value'] = $value;
$params['facet_display_value'] = $user->display_name;
$this->indexer->index_row( $params );
}
} // Taxonomy
elseif ( in_array( $field['type'], [ 'taxonomy', 'taxonomy_advanced' ], true ) ) {
if ( null !== $value ) {
$params['facet_value'] = $value->slug;
$params['facet_display_value'] = $value->name;
$params['term_id'] = $value->term_id;
$this->indexer->index_row( $params );
}
} // Checkbox.
elseif ( 'checkbox' === $field['type'] ) {
$display_value = ( 0 < (int) $value ) ? __( 'Yes', 'meta-box-facetwp-integrator' ) : __( 'No', 'meta-box-facetwp-integrator' );
$params['facet_value'] = $value;
$params['facet_display_value'] = $display_value;
$this->indexer->index_row( $params );
} // Google Maps.
elseif ( 'map' === $field['type'] || 'osm' === $field['type'] ) {
$lat = $value['latitude'] ?? '';
$lng = $value['longitude'] ?? '';
if ( $lat && $lng ) {
$params['facet_value'] = "$lat,$lng";
$params['facet_display_value'] = "$lat,$lng";
$this->indexer->index_row( $params );
}
} // File, image
elseif ( in_array( $field['type'], [
'file',
'file_advanced',
'file_upload',
'image',
'image_advanced',
'image_upload',
'plupload_image',
'thickbox_image',
], true ) ) {
$params['facet_value'] = $value['ID'];
$params['facet_display_value'] = $value['title'];
$this->indexer->index_row( $params );
} // Others.
else {
$params['facet_value'] = $value;
$params['facet_display_value'] = apply_filters( 'facetwp_meta_box_display_value', $value, $params );
$this->indexer->index_row( $params );
}
}
}