Wordpress进程API请求在后台

时间:2018-03-14 12:50:37

标签: wordpress api

我的WordPress页面上有一些API端点。问题是,响应时间大约需要(6 - 30秒 - 取决于要做什么)。

当客户端向我的API发送请求时,他必须等待6 - 30秒才能得到响应,这就是问题所在。

我读到有关背景调度的信息 - 这如何与WordPress一起使用? API唯一做的就是插入,删除或更新帖子。

目前,API执行以下操作:

  1. 验证数据
  2. 检查client_id是否存在且client_secret是否匹配
  3. 检查帖子是否已存在 - >如果是更新 - >如果没有创建
  4. 更新/创建帖子
  5. (可选)使用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();
        }
    ?>
    

0 个答案:

没有答案