Skip to content

Commit c6bfd2b

Browse files
committed
API 接口统一新增 sql 查询数;优化 Wnd_Filter_Ajax 查询参数
1 parent ab88970 commit c6bfd2b

8 files changed

+46
-25
lines changed

includes/controller/wnd-controller.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public static function handle_module(WP_REST_Request $request): array{
197197

198198
try {
199199
$module = new $class($request->get_query_params());
200-
return ['status' => 1, 'data' => $module->get_structure(), 'time' => timer_stop()];
200+
return ['status' => 1, 'data' => $module->get_structure(), 'time' => timer_stop(), 'queries' => get_num_queries()];
201201
} catch (Exception $e) {
202202
return ['status' => $e->getCode(), 'msg' => $e->getMessage()];
203203
}
@@ -222,7 +222,7 @@ public static function handle_query(WP_REST_Request $request): array{
222222
}
223223

224224
try {
225-
return ['status' => 1, 'msg' => '', 'data' => $class::get($request->get_query_params()), 'time' => timer_stop()];
225+
return ['status' => 1, 'msg' => '', 'data' => $class::get($request->get_query_params()), 'time' => timer_stop(), 'queries' => get_num_queries()];
226226
} catch (Exception $e) {
227227
return ['status' => $e->getCode(), 'msg' => $e->getMessage()];
228228
}
@@ -250,9 +250,10 @@ public static function handle_action(WP_REST_Request $request): array{
250250
}
251251

252252
try {
253-
$action = new $class($request);
254-
$res = $action->do_action();
255-
$res['time'] = timer_stop();
253+
$action = new $class($request);
254+
$res = $action->do_action();
255+
$res['time'] = timer_stop();
256+
$res['queries'] = get_num_queries();
256257
return $res;
257258
} catch (Exception $e) {
258259
return ['status' => $e->getCode(), 'msg' => $e->getMessage()];

includes/module/admin/wnd-admin-posts-panel.php

+3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ protected function structure(): array{
1919
$filter->add_post_type_filter(wnd_get_user_panel_post_types(), true);
2020
$filter->add_post_status_filter([__('待审', 'wnd') => 'pending']);
2121
$filter->set_posts_per_page($this->args['posts_per_page']);
22+
$filter->add_query_vars(['update_post_term_cache' => false, 'update_post_meta_cache' => false]);
2223
$filter->query();
24+
2325
return $filter->get_filter();
2426
}
2527

@@ -28,4 +30,5 @@ protected static function check($args) {
2830
throw new Exception(__('权限不足', 'wnd'));
2931
}
3032
}
33+
3134
}

includes/module/admin/wnd-finance-list.php

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ protected function structure(): array{
2828
]
2929
);
3030
$filter->set_posts_per_page($this->args['posts_per_page']);
31+
$filter->add_query_vars(['update_post_term_cache' => false, 'update_post_meta_cache' => false]);
3132
$filter->query();
3233
return $filter->get_filter();
3334
}
@@ -37,4 +38,5 @@ protected static function check($args) {
3738
throw new Exception(__('权限不足', 'wnd'));
3839
}
3940
}
41+
4042
}

includes/module/user/wnd-mail-box.php

+3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ protected function structure(): array{
1818
$filter->add_post_status_filter([__('全部', 'wnd') => 'any', __('未读', 'wnd') => 'wnd-unread', __('已读', 'wnd') => 'wnd-read']);
1919
$filter->add_query_vars(['author' => get_current_user_id()]);
2020
$filter->set_posts_per_page($this->args['posts_per_page']);
21+
$filter->add_query_vars(['update_post_term_cache' => false, 'update_post_meta_cache' => false]);
2122
$filter->query();
23+
2224
return $filter->get_filter();
2325
}
26+
2427
}

includes/module/user/wnd-user-finance-panel.php

+3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ protected function structure(): array{
5757
$filter->add_post_status_filter(['any']);
5858
$filter->add_query_vars(['author' => get_current_user_id()]);
5959
$filter->set_posts_per_page($posts_per_page);
60+
$filter->add_query_vars(['update_post_term_cache' => false, 'update_post_meta_cache' => false]);
6061
$filter->query();
62+
6163
return $filter->get_filter();
6264
}
65+
6366
}

includes/module/user/wnd-user-posts-panel.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ protected function structure(): array{
1919
$filter->add_post_status_filter([__('发布', 'wnd') => 'publish', __('待审', 'wnd') => 'pending', __('关闭', 'wnd') => 'wnd-closed', __('草稿', 'wnd') => 'draft']);
2020
$filter->add_category_filter();
2121
// $filter->add_tags_filter(10);
22-
$filter->add_query_vars(['author' => get_current_user_id()]);
22+
$filter->add_query_vars(['author' => get_current_user_id(), 'update_post_term_cache' => false, 'update_post_meta_cache' => false]);
2323
$filter->set_posts_per_page($this->args['posts_per_page']);
2424
$filter->query();
2525

2626
return $filter->get_filter();
2727
}
28+
2829
}

includes/view/wnd-filter-ajax.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ protected function get_posts(): array{
136136
return __('未执行WP_Query', 'wnd');
137137
}
138138

139-
foreach ($this->wp_query->get_posts() as $post) {
139+
$posts = $this->wp_query->get_posts();
140+
foreach ($posts as $post) {
140141
/**
141142
* - 请求指定移除内容
142143
* - 财务类 Post Content 为金额,需格式化
@@ -151,6 +152,7 @@ protected function get_posts(): array{
151152
}
152153

153154
// 用户信息
155+
update_post_author_caches($posts);
154156
$author = get_userdata($post->post_author);
155157
$author_name = $author ? ($author->display_name ?? $author->user_login) : 'anonymous';
156158
$author_link = $author ? get_author_posts_url($post->post_author) : '';
@@ -224,6 +226,7 @@ public function get_filter(): array{
224226

225227
'add_query_vars' => $this->get_add_query_vars(),
226228
'query_vars' => $this->wp_query->query_vars,
229+
'querys' => get_num_queries(),
227230
];
228231
}
229232

@@ -237,4 +240,5 @@ public function get_results(): array{
237240
unset($structure['after_html']);
238241
return $structure;
239242
}
243+
240244
}

includes/view/wnd-filter-query.php

+22-18
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,20 @@ class Wnd_Filter_Query {
3131

3232
// 初始化查询参数
3333
public static $defaults = [
34-
'orderby' => 'date',
35-
'order' => 'DESC',
36-
'meta_query' => [],
37-
'tax_query' => [],
38-
'date_query' => [],
39-
'meta_key' => '',
40-
'meta_value' => '',
41-
'post_type' => '',
42-
'post_status' => '',
43-
'no_found_rows' => true,
44-
'paged' => 1,
34+
'orderby' => 'date',
35+
'order' => 'DESC',
36+
'meta_query' => [],
37+
'tax_query' => [],
38+
'date_query' => [],
39+
'meta_key' => '',
40+
'meta_value' => '',
41+
'post_type' => '',
42+
'post_status' => '',
43+
'author' => 0,
44+
'no_found_rows' => true,
45+
'paged' => 1,
46+
'update_post_term_cache' => true,
47+
'update_post_meta_cache' => true,
4548
];
4649

4750
/**
@@ -90,6 +93,13 @@ public static function parse_query_vars(): array{
9093
*/
9194
$allowed_keys = array_keys(static::$defaults);
9295
foreach ($_GET as $key => $value) {
96+
// 将【字符串布尔值】转为实体布尔值
97+
if ('false' == $value) {
98+
$value = false;
99+
} elseif ('true' == $value) {
100+
$value = true;
101+
}
102+
93103
/**
94104
* post type tabs生成的GET参数为:type={$post_type}
95105
* 直接用 post_type 作为参数会触发WordPress原生请求导致错误
@@ -183,16 +193,10 @@ public static function parse_query_vars(): array{
183193
} else {
184194
$query_vars[$key] = $value;
185195
}
186-
continue;
187196
}
188197
unset($key, $value);
189198

190-
/**
191-
* 定义如何过滤HTTP请求
192-
* 此处定义:过滤空值,但保留0
193-
* @since 2019.10.26
194-
*/
195-
return wnd_array_filter($query_vars);
199+
return $query_vars;
196200
}
197201

198202
/**

0 commit comments

Comments
 (0)