From 8d4b6efe345b5e39287efa716cacd0a002d778de Mon Sep 17 00:00:00 2001 From: Fred Date: Mon, 7 Dec 2020 06:37:19 +0800 Subject: [PATCH] added examples shortcode --- classes/APIgoatDoc.php | 39 ++++++++++++++++++++++++++++++++ classes/APIgoatFetchAPI.php | 25 ++++++++++++++++++++ classes/APIgoatListExamples.php | 25 ++++++++++++++++++++ classes/APIgoatListModifiers.php | 2 +- includes/class-apigoat_doc.php | 1 + 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 classes/APIgoatListExamples.php diff --git a/classes/APIgoatDoc.php b/classes/APIgoatDoc.php index e56eae3..ddefab2 100644 --- a/classes/APIgoatDoc.php +++ b/classes/APIgoatDoc.php @@ -3,6 +3,45 @@ class APIgoatDoc extends APIgoatTemplate { + static function getExamples(array $data, array $headers) + { + $content = ''; + $menu = ''; + + foreach ($data as $row) { + if (!empty($row['name'])) { + $menu .= li(href($row['name'], '#' . $row['name']), "class='page_item wd-state-closed'"); + $content .= div( + anchor($row['name']) + . h2($row['name'], "class='entry-title'") + . p(trim($row['text'])) + . ((!empty($row['json'])) ? div(span("Example:") . $row['json']) : ''), + "", + "class='doc-item'" + ); + } + } + + foreach ($headers as $name => $header) { + $menu = h3($header) . $menu; + } + + + return div( + div( + ul($menu, "class='doc-nav-list'"), + '', + "class='wedocs-sidebar wedocs-hide-mobile'" + ) . div( + $content, + '', + "class='wedocs-single-content'" + ), + '', + "class='wedocs-single-wrap'" + ); + } + static function getDocs(array $data, array $headers) { $content = ''; diff --git a/classes/APIgoatFetchAPI.php b/classes/APIgoatFetchAPI.php index b42c5dc..c967d54 100644 --- a/classes/APIgoatFetchAPI.php +++ b/classes/APIgoatFetchAPI.php @@ -68,6 +68,31 @@ class APIgoatFetchAPI } } + public function fetchExamples() + { + $clientOptions = $this->clientOptions; + $clientOptions['query'] = [ + "query" => [ + "select" => [ + "name", ["description", "text"], ["example_category.name", "category_name"], "json" + ], + "join" => ["example_category"] + ] + ]; + + //$clientOptions['debug'] = true; + $response = $this->client->get('Example', $clientOptions); + + $body = json_decode($response->getBody()->getContents(), true); + //$body['debug'][] = $clientOptions['query']; + if ($response->getStatusCode() == 200) { + return $body; + } else { + $body = json_decode($response->getBody(), true); + return $body; + } + } + private function saveCredentials() { if (!empty($this->jwt_pubkey)) { diff --git a/classes/APIgoatListExamples.php b/classes/APIgoatListExamples.php new file mode 100644 index 0000000..1a95f6d --- /dev/null +++ b/classes/APIgoatListExamples.php @@ -0,0 +1,25 @@ +fetchExamples(); + if (isset($Behaviors['debug'])) { + echo "
" . preprint($Behaviors['debug']) . "
"; + } + + if (isset($Behaviors['messages'])) { + echo "
" . preprint($Behaviors['messages']) . "
"; + } + if ($Behaviors['data']) { + $table = APIgoatDoc::getExamples($Behaviors['data'], ['Code' => 'Examples']); + + return $content . div($table, '', "class='site-main'"); + } else { + return $content . "
Error" . preprint($Behaviors); + } + } +} diff --git a/classes/APIgoatListModifiers.php b/classes/APIgoatListModifiers.php index 737d5b1..2604dae 100644 --- a/classes/APIgoatListModifiers.php +++ b/classes/APIgoatListModifiers.php @@ -15,7 +15,7 @@ class APIgoatListModifiers echo "
" . preprint($Behaviors['messages']) . "
"; }*/ if ($Behaviors['data']) { - $table = APIgoatDoc::getDocs($Behaviors['data'], ['Code' => 'Parameters']); + $table = APIgoatDoc::getDocs($Behaviors['data'], ['Code' => 'Modifiers']); return $content . div($table, '', "class='site-main'"); } else { diff --git a/includes/class-apigoat_doc.php b/includes/class-apigoat_doc.php index bcc7fca..b96a48a 100644 --- a/includes/class-apigoat_doc.php +++ b/includes/class-apigoat_doc.php @@ -125,6 +125,7 @@ class apigoat_doc require_once plugin_dir_path(dirname(__FILE__)) . 'public/class-apigoat_doc-public.php'; require plugin_dir_path(dirname(__FILE__)) . 'classes/APIgoatListModifiers.php'; + require plugin_dir_path(dirname(__FILE__)) . 'classes/APIgoatListExamples.php'; $this->loader = new apigoat_doc_Loader(); }