Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions apiki-favorites/apiki-favorites.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Plugin Name: Apiki Favorites
* Description: Um plugin para permitir que usuários logados favoritem posts.
* Version: 1.0.0
* Author: MateusBreno
* License: GPL2
*/

declare(strict_types=1);

if (!defined("ABSPATH")) {
exit;
}

define("APIKI_FAVORITES_PATH", plugin_dir_path(__FILE__));

require_once APIKI_FAVORITES_PATH . "includes/class-apiki-favorites-db.php";
require_once APIKI_FAVORITES_PATH . "includes/class-apiki-favorites-rest-controller.php";

class Apiki_Favorites
{
public function __construct()
{
register_activation_hook(__FILE__, [$this, "activate"]);
register_deactivation_hook(__FILE__, [$this, "deactivate"]);

add_action("plugins_loaded", [$this, "init_plugin"]);
}

public function activate(): void
{
Apiki_Favorites_DB::create_favorites_table();
}

public function deactivate(): void
{
Apiki_Favorites_DB::drop_favorites_table();
}

public function init_plugin(): void
{
new Apiki_Favorites_REST_Controller();
}
}

new Apiki_Favorites();
36 changes: 36 additions & 0 deletions apiki-favorites/includes/class-apiki-favorites-db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

if (!defined("ABSPATH")) {
exit;
}

class Apiki_Favorites_DB
{
public static function create_favorites_table(): void
{
global $wpdb;
$table_name = $wpdb->prefix . "apiki_favorites";

$charset_collate = $wpdb->get_charset_collate();

$sql = "CREATE TABLE $table_name (
id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
user_id bigint(20) UNSIGNED NOT NULL,
post_id bigint(20) UNSIGNED NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY user_post_id (user_id, post_id)
) $charset_collate;";

require_once ABSPATH . "wp-admin/includes/upgrade.php";
dbDelta($sql);
}

public static function drop_favorites_table(): void
{
global $wpdb;
$table_name = $wpdb->prefix . "apiki_favorites";
$wpdb->query("DROP TABLE IF EXISTS $table_name");
}
}
131 changes: 131 additions & 0 deletions apiki-favorites/includes/class-apiki-favorites-rest-controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

declare(strict_types=1);

if (!defined("ABSPATH")) {
exit;
}

class Apiki_Favorites_REST_Controller
{
private string $namespace = "apiki/v1";
private string $base = "favorites";

public function __construct()
{
add_action("rest_api_init", [$this, "register_routes"]);
}

public function register_routes(): void
{
register_rest_route(
$this->namespace,
"/" . $this->base . "/(?P<post_id>\d+)",
[
[
"methods" => WP_REST_Server::CREATABLE,
"callback" => [$this, "add_favorite"],
"permission_callback" => [$this, "permission_check"],
"args" => [
"post_id" => [
"validate_callback" => function ($param, $request, $key) {
return is_numeric($param);
},
],
],
],
[
"methods" => WP_REST_Server::DELETABLE,
"callback" => [$this, "remove_favorite"],
"permission_callback" => [$this, "permission_check"],
"args" => [
"post_id" => [
"validate_callback" => function ($param, $request, $key) {
return is_numeric($param);
},
],
],
],
]
);
}

public function permission_check(WP_REST_Request $request): bool
{
return is_user_logged_in();
}

public function add_favorite(WP_REST_Request $request): WP_REST_Response
{
global $wpdb;
$user_id = get_current_user_id();
$post_id = (int) $request["post_id"];

if (!get_post($post_id)) {
return new WP_REST_Response(
["message" => "Post not found."],
404
);
}

$table_name = $wpdb->prefix . "apiki_favorites";

$data = [
"user_id" => $user_id,
"post_id" => $post_id,
];

$format = ["%d", "%d"];

$inserted = $wpdb->insert($table_name, $data, $format);

if ($inserted === false) {
return new WP_REST_Response(
["message" => "Error adding favorite or already favorited."],
400
);
}

return new WP_REST_Response(
["message" => "Post favorited successfully."],
200
);
}

public function remove_favorite(WP_REST_Request $request): WP_REST_Response
{
global $wpdb;
$user_id = get_current_user_id();
$post_id = (int) $request["post_id"];

$table_name = $wpdb->prefix . "apiki_favorites";

$deleted = $wpdb->delete(
$table_name,
[
"user_id" => $user_id,
"post_id" => $post_id,
],
["%d", "%d"]
);

if ($deleted === false) {
return new WP_REST_Response(
["message" => "Error removing favorite."],
400
);
}

if ($deleted === 0) {
return new WP_REST_Response(
["message" => "Favorite not found to remove."],
404
);
}

return new WP_REST_Response(
["message" => "Post unfavorited successfully."],
200
);
}
}