我的WordPress页面上有一些API端点。问题是,响应时间大约需要(6 - 30秒 - 取决于要做什么)。
当客户端向我的API发送请求时,他必须等待6 - 30秒才能得到响应,这就是问题所在。
我读到有关背景调度的信息 - 这如何与WordPress一起使用? API唯一做的就是插入,删除或更新帖子。
目前,API执行以下操作:
(可选)使用media_sideload_image()上传图片 5/6。更新自定义字段 - > update_field()
if (have_rows("api_users", "options")) {
while (have_rows("api_users", "options")) {
the_row();
if (get_sub_field("api_user_client_id") == $client_id) {
$flag = true;
}
}
}
if (!$flag) {
wp_send_json_error(array("message" => "Wrong ClientID"));
}
return false;
}
public function rest_create_xxx($request) {
// check if clientID is correct
$this->checkIfClientIsAllowed($request->get_param("client_id"));
// validate params
$params = $request->get_params();
$validator = $this->validation->make($params, array(
// my validation rules
));
if ($validator->fails()) {
wp_send_json_error(array("message" => "validation failed");
}
// check if parent post exist
$result = self::findPostByInternId("yyy", $request->get_param("client_id"), $request->get_param("parent"));
$parent_post = 0;
if ($result->have_posts() && $result->post_count == 1) {
$parent_post = $result->posts[0];
} else {
wp_send_json_error(array("message" => "parent post not found"));
}
// check if xxx with that id already exists (if yes update, if no insert)
$result = self::findPostByInternId("xxx", $request->get_param("client_id"), $request->get_param("intern_id"));
$post = null;
if ($result->have_posts() && $result->post_count == 1) {
// post already exist - so update it
$post = wp_update_post(array(
"ID" => $result->posts[0]->ID,
"post_title" => $request->get_param("title"),
"post_type" => "xxx",
"post_parent" => $parent_post->ID,
"post_status" => "publish"
));
} else {
// post not found - so insert it
$post = wp_insert_post(array(
"post_title" => $request->get_param("title"),
"post_type" => "xxx",
"post_parent" => $parent_post->ID,
"post_status" => "publish"
));
}
// update some acf fields
update_field("a", $request->get_param("a"), $post);
update_field("b", $request->get_param("b"), $post);
update_field("c", $request->get_param("c"), $post);
wp_send_json_success();
}
?>