birgir/geo-query

修改 WP_Query 以支持 geo_query 参数。使用 Ollie Jones 的 Haversine SQL 优化。

安装量: 2,904

依赖项: 0

建议者: 0

安全: 0

星级: 67

关注者: 6

分支: 11

开放问题: 5

类型:wordpress-plugin

0.2.1 2023-07-26 16:30 UTC

This package is auto-updated.

Last update: 2024-09-05 18:18:14 UTC


README

Build Status GitHub license Packagist

描述

此插件增加了对 WP_QueryWP_User_Querygeo_query 部分的支持。

支持存储在帖子/用户元或自定义表中的地理数据。

它使用 Ollie Jones 的 Haversine SQL 实现(见这里)。

插件适用于 PHP 5.3+。

它支持 GitHub Updater。

激活插件后,您可以在所有 WP_QueryWP_User_Query 查询中使用 geo_query 参数。

以下是一些示例,例如用于 Rest API。

安装

将插件上传到插件文件夹并激活它。

使用 Composer 安装依赖项(不是必需的)

composer install

php composer.phar install

在我们的文件夹内。有关如何安装 Composer 的更多信息,请参阅此处。

然后尝试下面的示例,在你的主题或插件中。

祝您玩得开心 ;-)

示例 - 基本用法 WP_Query

以下是 geo_query 部分的默认输入参数示例

$args = [
    'post_type'           => 'post',    
    'posts_per_page'      => 10,
    'ignore_sticky_posts' => true,
    'orderby'             => [ 'title' => 'DESC' ],
    'geo_query'           => [
        'lat'                =>  64,                                // Latitude point
        'lng'                =>  -22,                               // Longitude point
        'lat_meta_key'       =>  'geo_lat',                         // Meta-key for the latitude data
        'lng_meta_key'       =>  'geo_lng',                         // Meta-key for the longitude data 
        'radius'             =>  150,                               // Find locations within a given radius (km)
        'order'              =>  'DESC',                            // Order by distance
        'distance_unit'      =>  111.045,                           // Default distance unit (km per degree). Use 69.0 for statute miles per degree.
        'context'            => '\\Birgir\\Geo\\GeoQueryHaversine', // Default implementation, you can use your own here instead.
    ],
];
$query = new WP_Query( $args );

示例 - Rest API 使用

以下是 @florianweich 的修改后的示例

add_filter( 'rest_query_vars', function ( $valid_vars ) {
	return array_merge( $valid_vars, [ 'geo_location' ] );
} );

add_filter( 'rest_post_query', function( $args, $request ) {
	$geo = json_decode( $request->get_param( 'geo_location' ) );
	if ( isset( $geo->lat, $geo->lng ) ) {
		$args['geo_query'] = [
			'lat'                =>  (float) $geo->lat,
			'lng'                =>  (float) $geo->lng,
			'lat_meta_key'       =>  'geo_lat',
			'lng_meta_key'       =>  'geo_lng',
			'radius'             =>  ($geo->radius) ? (float) $geo->radius : 50,
		];
	}
	return $args;
}, 10, 2 );

使用例如测试它

https://example.com/wp-json/wp/v2/posts?geo_location={"lat":"64.128288","lng":"-21.827774","radius":"50"}

可以使用 rest_{custom-post-type-slug}_query 过滤器为自定义帖子类型。

示例 - 基本用法 WP_User_Query

以下是 @acobster 的示例

$args = [
  'role'      => 'subscriber',
  'geo_query' => [
    'lat'          => 47.236,
    'lng'          => -122.435,
    'lat_meta_key' => 'geo_lat',
    'lng_meta_key' => 'geo_lng',
    'radius'       => 1,
    'context'      => '\\Birgir\\Geo\\GeoQueryUserHaversine',
  ],
];

$query = new WP_User_Query( $args );

示例 - 基本用法 WP_Query,用于从具有 Haversine 公式的自定义表中获取经纬度数据

$args = array(
   	'post_type'           => 'post',    
   	'posts_per_page'      => 10,
   	'ignore_sticky_posts' => true,
   	'orderby'             => array( 'title' => 'DESC' ),		    
	'geo_query' => array(
		'table'         => 'custom_table', // Table name for the geo custom table.
		'pid_col'       => 'pid',          // Column name for the post ID data
		'lat_col'       => 'lat',          // Column name for the latitude data
		'lng_col'       => 'lng',          // Column name for the longitude data 
		'lat'           => 64.0,           // Latitude point
		'lng'           => -22.0,          // Longitude point
		'radius'        => 1,              // Find locations within a given radius (km)
		'order'         => 'DESC',         // Order by distance
		'distance_unit' => 111.045,        // Default distance unit (km per degree). Use 69.0 for statute miles per degree.
		'context'       => '\\Birgir\\Geo\\GeoQueryPostCustomTableHaversine', // Custom table implementation, you can use your own here instead.
	),
);

$query = new WP_Query( $args );

请查看单元测试方法 test_custom_table_in_wp_query() 作为更详细的示例。

关于参数的说明

  • 插件假设我们以自定义字段(帖子元数据)的形式存储纬度和经度,因此我们需要通过 'lat_meta_key''lng_meta_key' 参数告诉查询关于元键。

  • 省略 'radius' 参数意味着不会进行距离过滤。

  • 如果我们在 'geo_query' 中使用 'order' 参数,则它将被添加到本地的 'orderby' 参数之前。

  • 'distance_unit' 参数应为 69.0(英里),否则为 111.045(千米)。

  • 如果我们想使用 Ollie Jones 优化的 Haversine 版本,我们使用

      'context' => '\\Birgir\\Geo\\GeoQueryHaversineOptimized'
    

    请注意,在我们的当前插件设置中(即从 LONGTEXT 帖子元字段获取数据),这并不比默认的 GeoQueryHaversine 类更高效。

    未来的工作可以是使用带索引的自定义表来优化。

  • 如果我们创建了 Haversine 公式的自己的实现,例如 GeoQueryCustom 类,我们只需确保它实现了 GeoQueryInterface 接口。

      'context' => 'GeoQueryCustom'
    

反馈

欢迎任何建议。

变更日志

0.2.2 (2024-01-05)

  • Bump composer/installers to ^2.0.0. Props @wujekbogdan

0.2.1 (2023-07-26)

  • 修复 PHP 8.1 中的弃用通知 #24

0.2.0 (2020-04-25)

  • 支持从自定义表获取点并执行 WP_Query 中的 Haversine 公式。

0.1.1 (2019-01-23)

  • 修复 #14. 修复了用户查询排序。Props @baden03

0.1.0 (2018-08-06)

  • 添加了对用户查询的支持。Props @acobster
  • 修复了 Travis 的问题。通过 Composer 安装时,Travis 可成功运行 PHP 7.2、7.1、7、5.6,未通过 Composer 安装时也能成功运行 5.4。暂时跳过对 5.4 的 Composer 检查。

0.0.7 (2018-06-27)

  • 修复了 #10。使用 ^1.0.0 作为 Composer 安装器。感谢 @wujekbogdan。

0.0.6 (2017-11-16)

  • 修复了 #6。支持浮点半径。感谢 @wujekbogdan。
  • 添加了集成测试。

0.0.5 (2017-02-26)

  • 为不使用 Composer 的用户添加了回退机制。
  • 移除了 vendor 目录。

0.0.4 (2017-02-26)

  • 修复了 #4。感谢 @billzhong。

0.0.3 (2015-04-29)

  • 修复了 #2。修复了一个错别字。感谢 @Ben764 和 @con322。

0.0.2 (2015-03-10)

  • 添加:支持 GitHub 更新器。
  • 更新:README.md
  • 修改:在 SQL 中使用 distance_value 代替 distance_in_km,因为我们可以通过更改 distance_unit 参数来使用英里。

0.0.1 - 初始化