-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathElasticPressGeoFeature.php
225 lines (188 loc) · 6.63 KB
/
ElasticPressGeoFeature.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/**
* ElasticPressGeoFeature class
*/
class ElasticPressGeoFeature extends \ElasticPress\Feature {
/**
* Initialize feature settings.
*/
public function __construct() {
$this->slug = 'ep_geo';
$this->title = esc_html__( 'Geo', 'ep-geo' );
$this->requires_install_reindex = true;
parent::__construct();
}
/**
* Output feature box summary.
*/
public function output_feature_box_summary() {
echo '<p>' . esc_html_e( 'Integrate geo location data with ElasticSearch, and enable geo queries.', 'ep-geo' ) . '</p>';
}
/**
* Output feature box long
*/
public function output_feature_box_long() {
echo '<p>' . esc_html_e( 'By default, this plugin looks in post meta fields named "latitude" and "longitude". They should be plain text fields with lat/lon represented as floats.', 'ep-geo' ) . '</p>';
echo '<p>' . esc_html_e( 'If your latitude and longitude data is stored somewhere else, or if you need to calculate or preprocess the geo_point location, it\'s configurable with a WordPress hook.', 'ep-geo' ) . '</p>';
}
/**
* Setup all feature filters
*/
public function setup() {
add_filter( 'ep_config_mapping', [ $this, 'ep_geo_config_mapping' ], 10, 2 );
add_filter( 'ep_post_sync_args', [ $this, 'ep_geo_post_sync_args' ], 10, 2 );
add_filter( 'ep_formatted_args', [ $this, 'ep_geo_formatted_args' ], 10, 2 );
}
/**
* Alter ES index to add location property.
*
* @param array $mapping
*
* @return array
*/
public function ep_geo_config_mapping( $mapping ) {
if ( !class_exists('ElasticPress\\Elasticsearch') || version_compare( \ElasticPress\Elasticsearch::factory()->get_elasticsearch_version(), '7.0', '<' ) ) {
// Index geo_point:
$mapping[ 'mappings' ]['post']['properties']['geo_point'] = array(
'properties' => array(
'location' => array(
'type' => 'geo_point',
'ignore_malformed' => true,
),
),
);
// Index geo_shape:
$mapping[ 'mappings' ]['post']['properties']['geo_shape'] = array(
'properties' => array(
'location' => array(
'type' => 'geo_shape',
'ignore_malformed' => true,
),
),
);
} else {
// Index geo_point:
$mapping[ 'mappings' ]['properties']['geo_point'] = array(
'properties' => array(
'location' => array(
'type' => 'geo_point',
'ignore_malformed' => true,
),
),
);
// Index geo_shape:
$mapping[ 'mappings' ]['properties']['geo_shape'] = array(
'properties' => array(
'location' => array(
'type' => 'geo_shape',
'ignore_malformed' => true,
),
),
);
}
return $mapping;
}
/**
* Alter ES sync data to post geo_points.
*
* @param array $post_args
* @param int $post_id
*
* @return array
*/
public function ep_geo_post_sync_args( $post_args, $post_id ) {
// Sync geo_point:
$geo_point = [
'location' => [],
];
if ( isset( $post_args['meta'] ) && isset( $post_args['meta']['latitude'] ) ) {
$meta = $post_args['meta'];
if ( ! empty( $meta['latitude'][0]['double'] ) ) {
$geo_point['location']['lat'] = (float) number_format( $meta['latitude'][0]['double'], 6, '.', '' );
}
if ( ! empty( $meta['longitude'][0]['double'] ) ) {
$geo_point['location']['lon'] = (float) number_format( $meta['longitude'][0]['double'], 6, '.', '' );
}
} elseif ( isset( $post_args['post_meta'] ) && isset( $post_args['post_meta']['latitude'] ) ) {
// Handle legacy post_meta property, for older versions of elasticpress.
$post_meta = $post_args['post_meta'];
if ( isset( $post_meta['latitude'][0] ) ) {
$geo_point['location']['lat'] = (float) number_format( $post_meta['latitude'][0], 6, '.', '' );
}
if ( isset( $post_meta['longitude'][0] ) ) {
$geo_point['location']['lon'] = (float) number_format( $post_meta['longitude'][0], 6, '.', '' );
}
} elseif ( !empty( get_post_meta( $post_id, 'latitude', true ) ) ) {
$lat = (float) number_format( get_post_meta( $post_id, 'latitude', true ), 6, '.', '' );
if ( ! empty( $lat ) ) {
$geo_point['location']['lat'] = $lat;
$post_args['meta']['latitude'][0]['double'] = $lat;
}
$lon = (float) number_format( get_post_meta( $post_id, 'longitude', true ), 6, '.', '' );
if ( ! empty( $lon ) ) {
$geo_point['location']['lon'] = $lon;
$post_args['meta']['longitude'][0]['double'] = $lon;
}
}
$post_args['geo_point'] = apply_filters( 'ep_geo_post_sync_geo_point', $geo_point, $post_args, $post_id );
// Sync geo_shape:
$geo_shape = [
'location' => [],
];
$post_args['geo_shape'] = apply_filters( 'ep_geo_post_sync_geo_shape', $geo_shape, $post_args, $post_id );
return $post_args;
}
/**
* Alter formatted WP query args for geo filter.
*
* @param array $formatted_args
* @param array $args
*
* @return array
*/
public function ep_geo_formatted_args( $formatted_args, $args ) {
if ( isset( $args['geo_shape'] ) ) {
$formatted_args['post_filter']['bool']['filter']['geo_shape'] = $args['geo_shape'];
} elseif ( isset( $args['geo_bounding_box'] ) ) {
$formatted_args['post_filter']['bool']['filter']['geo_bounding_box'] = $args['geo_bounding_box'];
} elseif ( isset( $args['geo_polygon'] ) ) {
$formatted_args['post_filter']['bool']['filter']['geo_polygon'] = $args['geo_polygon'];
} elseif ( isset( $args['geo_distance'] ) ) {
$formatted_args['post_filter']['bool']['filter']['geo_distance'] = $args['geo_distance'];
}
if ( ! empty( $formatted_args['sort'] ) ) {
foreach ( $formatted_args['sort'] as $key => &$sort ) {
if ( isset( $sort['geo_distance'] ) ) {
$sort['_geo_distance'] = $sort['geo_distance'];
if ( isset( $args['geo_distance']['geo_point.location'] ) ) {
$sort['_geo_distance']['geo_point.location'] = $args['geo_distance']['geo_point.location'];
}
unset( $sort['geo_distance'] );
}
}
}
// Legacy "geo_query" filter (deprecated):
if ( isset( $args['geo_query'] ) ) {
$geo_distance = array();
if ( isset( $args['geo_query']['distance'] ) ) {
$geo_distance['distance'] = $args['geo_query']['distance'];
}
if ( isset( $args['geo_query']['lat'] ) ) {
$geo_distance['geo_point.location']['lat'] = $args['geo_query']['lat'];
}
if ( isset( $args['geo_query']['lon'] ) ) {
$geo_distance['geo_point.location']['lon'] = $args['geo_query']['lon'];
}
$formatted_args['post_filter']['bool']['filter']['geo_distance'] = $geo_distance;
if ( isset( $args['geo_query']['order'] ) ) {
array_unshift( $formatted_args['sort'], array(
'_geo_distance' => array(
'geo_point.location' => $geo_distance['geo_point.location'],
'order' => $args['geo_query']['order'],
),
) );
}
}
return $formatted_args;
}
}