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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* eol=lf
66 changes: 66 additions & 0 deletions src/db_utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

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

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');


class Database {
public static function createTable() {
global $wpdb;

$table = $wpdb->prefix . "user_bookmarks";

$collation = $wpdb->get_charset_collate();

$sql = "CREATE TABLE $table (
id BIGINT(20) NOT NULL AUTO_INCREMENT,
user_id BIGINT(20) NOT NULL,
post_id BIGINT(20) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY user_bookmark (user_id, post_id)
) $collation;";

dbDelta($sql);
}



public static function addBookmark($user_id, $post_id) {
global $wpdb;

$table = $wpdb->prefix . "user_bookmarks";

$wpdb->replace($table, ["user_id" => $user_id, "post_id" => $post_id]);
}



public static function removeBookmark($user_id, $post_id) {
global $wpdb;

$table = $wpdb->prefix . "user_bookmarks";

$wpdb->delete($table, ["user_id" => $user_id, "post_id" => $post_id]);
}



public static function isBookmarked($user_id, $post_id) {
global $wpdb;

$table = $wpdb->prefix . "user_bookmarks";

return ($wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table WHERE user_id = %d AND post_id = %d", $user_id, $post_id)) > 0);
}



public static function getBookmarks($user_id) {
global $wpdb;

$table = $wpdb->prefix . "user_bookmarks";

return $wpdb->get_results($wpdb->prepare("SELECT post_id FROM $table WHERE user_id = %d", $user_id));
}
}
9 changes: 9 additions & 0 deletions src/hook_callbacks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

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



function wpfav_is_authed() {
return is_user_logged_in();
}
79 changes: 79 additions & 0 deletions src/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

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


require_once(PLUGIN_DIR . "src/db_utils.php");



function wpfav_add_bookmark($request) {
if (!$request->has_param("id")) {
wp_send_json_error([ "error" => "Missing post id" ], 400);
return;
}

$id = $request->get_param("id");
$user_id = get_current_user_id();

if (!get_post($id)) {
wp_send_json_error([ "error" => "Post not found" ], 404);
return;
}

Database::addBookmark($user_id, $id);

wp_send_json_success();
}



function wpfav_remove_bookmark($request) {
if (!$request->has_param("id")) {
wp_send_json_error([ "error" => "Missing post id" ], 400);
return;
}

$id = $request->get_param("id");
$user_id = get_current_user_id();

if (!get_post($id)) {
wp_send_json_error([ "error" => "Post not found" ], 404);
return;
}

Database::removeBookmark($user_id, $id);

wp_send_json_success();
}



function wpfav_is_bookmarked($request) {
if (!$request->has_param("id")) {
wp_send_json_error([ "error" => "Missing post id" ], 400);
return;
}

$id = $request->get_param("id");
$user_id = get_current_user_id();

if (!get_post($id)) {
wp_send_json_error([ "error" => "Post not found" ], 404);
return;
}

$is_bookmarked = Database::isBookmarked($user_id, $id);

wp_send_json_success([ "is_bookmarked" => $is_bookmarked ]);
}



function wpfav_get_bookmarks($request) {
$user_id = get_current_user_id();

$bookmarks = Database::getBookmarks($user_id);

wp_send_json_success([ "bookmarks" => $bookmarks ]);
}
56 changes: 56 additions & 0 deletions wp-fav-posts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/*
Plugin Name: WP Fav Posts
Description: Plugin to bookmark posts using WP REST API
Version: 1.0
Author: Raphael Ceccato Pauli
*/


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

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


require_once(PLUGIN_DIR . "src/hook_callbacks.php");
require_once(PLUGIN_DIR . "src/routes.php");
require_once(PLUGIN_DIR . "src/db_utils.php");



class WPFavPosts {
public function __construct() {
add_action("init", [$this, "on_init"]);
}


public function on_init() {
Database::createTable();

register_rest_route("wpfav/v1", "/add-bookmark/(?P<id>\d+)", [
"methods" => "POST",
"callback" => "wpfav_add_bookmark",
"permission_callback" => "wpfav_is_authed"
]);
register_rest_route("wpfav/v1", "/remove-bookmark/(?P<id>\d+)", [
"methods" => "POST",
"callback" => "wpfav_remove_bookmark",
"permission_callback" => "wpfav_is_authed"
]);
register_rest_route("wpfav/v1", "/is-bookmarked/(?P<id>\d+)", [
"methods" => "GET",
"callback" => "wpfav_is_bookmarked",
"permission_callback" => "wpfav_is_authed"
]);
register_rest_route("wpfav/v1", "/get-bookmarks", [
"methods" => "GET",
"callback" => "wpfav_get_bookmarks",
"permission_callback" => "wpfav_is_authed"
]);
}
}



new WPFavPosts();