SimpleHistory::api PHP Method

api() public method

Examples: http://playground-root.ep/wp-admin/admin-ajax.php?action=simple_history_api&posts_per_page=5&paged=1&format=html
public api ( )
    public function api()
    {
        global $wpdb;
        // Fake slow answers
        //sleep(2);
        //sleep(rand(0,3));
        $args = $_GET;
        unset($args["action"]);
        // Type = overview | ...
        $type = isset($_GET["type"]) ? $_GET["type"] : null;
        if (empty($args) || !$type) {
            wp_send_json_error(array(_x("Not enough args specified", "API: not enought arguments passed", "simple-history")));
        }
        // User must have capability to view the history page
        if (!current_user_can($this->get_view_history_capability())) {
            wp_send_json_error(array("error" => "CAPABILITY_ERROR"));
        }
        if (isset($args["id"])) {
            $args["post__in"] = array($args["id"]);
        }
        $data = array();
        switch ($type) {
            case "overview":
            case "occasions":
            case "single":
                // API use SimpleHistoryLogQuery, so simply pass args on to that
                $logQuery = new SimpleHistoryLogQuery();
                $data = $logQuery->query($args);
                $data["api_args"] = $args;
                // Output can be array or HMTL
                if (isset($args["format"]) && "html" === $args["format"]) {
                    $data["log_rows_raw"] = array();
                    foreach ($data["log_rows"] as $key => $oneLogRow) {
                        $args = array();
                        if ($type == "single") {
                            $args["type"] = "single";
                        }
                        $data["log_rows"][$key] = $this->getLogRowHTMLOutput($oneLogRow, $args);
                        $data["num_queries"] = get_num_queries();
                    }
                } else {
                    // $data["logRows"] = $logRows;
                }
                break;
            default:
                $data[] = "Nah.";
        }
        wp_send_json_success($data);
    }
SimpleHistory