Skip to content

Commit d482f8a

Browse files
committed
Add first plugin version
1 parent 491a881 commit d482f8a

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

api-favorites.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Q2A API - plugin to Question2Answer
4+
* @author Arkadiusz Waluk <arkadiusz@waluk.pl>
5+
*/
6+
7+
class api_favorites
8+
{
9+
public function match_request($request)
10+
{
11+
return $request === API_URL.'favorites';
12+
}
13+
14+
public function process_request()
15+
{
16+
$user_id = qa_get_logged_in_userid();
17+
if (empty($user_id)) {
18+
return_json_response(['error' => 'User not logged']);
19+
}
20+
21+
$db_questions = qa_db_select_with_pending(qa_db_user_favorite_qs_selectspec($user_id));
22+
$questions = array_column($db_questions, 'postid');
23+
24+
$db_users = qa_db_select_with_pending(qa_db_user_favorite_users_selectspec($user_id));
25+
$users = array_column($db_users, 'handle');
26+
27+
$db_tags = qa_db_select_with_pending(qa_db_user_favorite_tags_selectspec($user_id));
28+
$tags = array_column($db_tags, 'word');
29+
30+
$db_categories = qa_db_select_with_pending(qa_db_user_favorite_categories_selectspec($user_id));
31+
$categories = array_column($db_categories, 'title');
32+
33+
return_json_response([
34+
'questions' => $questions,
35+
'users' => $users,
36+
'tags' => $tags,
37+
'categories' => $categories,
38+
]);
39+
}
40+
}

api-functions.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Q2A API - plugin to Question2Answer
4+
* @author Arkadiusz Waluk <arkadiusz@waluk.pl>
5+
*/
6+
7+
define('API_URL', 'api/');
8+
9+
function return_json_response($data, $code = 200)
10+
{
11+
header('Content-Type: application/json');
12+
http_response_code($code);
13+
14+
echo json_encode($data);
15+
exit();
16+
}

metadata.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "Q2A API",
3+
"uri": "",
4+
"description": "Simple API serving data from Q2A in JSON",
5+
"version": "1.0",
6+
"date": "2017-09-01",
7+
"author": "Arkadiusz Waluk",
8+
"author_uri": "http://waluk.pl",
9+
"license": "MIT",
10+
"min_q2a": "1.5",
11+
"update_uri": ""
12+
}

qa-plugin.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Q2A API - plugin to Question2Answer
4+
* @author Arkadiusz Waluk <arkadiusz@waluk.pl>
5+
*/
6+
7+
/*
8+
Plugin Name: Q2A API
9+
Plugin URI:
10+
Plugin Description: Simple API serving data from Q2A in JSON
11+
Plugin Version: 1.0
12+
Plugin Date: 2017-09-01
13+
Plugin Author: Arkadiusz Waluk
14+
Plugin Author URI: https://waluk.pl
15+
Plugin License: MIT
16+
Plugin Minimum Question2Answer Version: 1.5
17+
Plugin Update Check URI:
18+
*/
19+
20+
if (!defined('QA_VERSION')) {
21+
header('Location: ../../');
22+
exit;
23+
}
24+
25+
qa_register_plugin_module('page', 'api-favorites.php', 'api_favorites', 'API user favorites');
26+
27+
require_once 'api-functions.php';

0 commit comments

Comments
 (0)