From 41046919dee274c07ff5ba9481d067017ccff8dc Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 8 Nov 2024 09:17:13 -0300 Subject: [PATCH 01/19] create wp-fav-posts.php --- src/wp-fav-posts.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/wp-fav-posts.php diff --git a/src/wp-fav-posts.php b/src/wp-fav-posts.php new file mode 100644 index 00000000..347e3cf3 --- /dev/null +++ b/src/wp-fav-posts.php @@ -0,0 +1,24 @@ + Date: Fri, 8 Nov 2024 09:17:42 -0300 Subject: [PATCH 02/19] create .gitattributes --- .gitattributes | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..6cb9b25a --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* eof=lf \ No newline at end of file From f7522dd85253f5a06f794980b5461d15e5fb1a0e Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 8 Nov 2024 09:18:02 -0300 Subject: [PATCH 03/19] create hook_callbacks.php --- src/hook_callbacks.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/hook_callbacks.php diff --git a/src/hook_callbacks.php b/src/hook_callbacks.php new file mode 100644 index 00000000..c3dd7f31 --- /dev/null +++ b/src/hook_callbacks.php @@ -0,0 +1,24 @@ +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); +} \ No newline at end of file From 709d1bc8b0fdc8393494a67b951412f63eb588b2 Mon Sep 17 00:00:00 2001 From: Raphael Date: Fri, 8 Nov 2024 14:38:02 -0300 Subject: [PATCH 04/19] update wp-fav-posts.php: create table on init --- src/wp-fav-posts.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/wp-fav-posts.php b/src/wp-fav-posts.php index 347e3cf3..632e875c 100644 --- a/src/wp-fav-posts.php +++ b/src/wp-fav-posts.php @@ -12,11 +12,15 @@ define("PLUGIN_DIR", plugin_dir_path(__FILE__)); +require_once(PLUGIN_DIR . "src/hook_callbacks.php"); +require_once(PLUGIN_DIR . "src/routes.php"); + + class WP_Fav_Posts { public function __construct() { - - } + add_action("init", "wpfav_create_table"); + } } From e4778afbf822cda597df38ad15c6e07cb047801f Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 9 Nov 2024 08:55:13 -0300 Subject: [PATCH 05/19] update hook_callbacks.php: add wpfav_is_authed() --- src/hook_callbacks.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/hook_callbacks.php b/src/hook_callbacks.php index c3dd7f31..bd5571a0 100644 --- a/src/hook_callbacks.php +++ b/src/hook_callbacks.php @@ -21,4 +21,10 @@ function wpfav_create_table() { ) $collation;"; dbDelta($sql); +} + + + +function wpfav_is_authed() { + return is_user_logged_in(); } \ No newline at end of file From 1e925725ebfc8ae1a98986bc6e4668cd567e4b03 Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 9 Nov 2024 09:14:27 -0300 Subject: [PATCH 06/19] create db_utils.php --- src/db_utils.php | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/db_utils.php diff --git a/src/db_utils.php b/src/db_utils.php new file mode 100644 index 00000000..00a60f38 --- /dev/null +++ b/src/db_utils.php @@ -0,0 +1,61 @@ +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)); + } +} \ No newline at end of file From 88bc127aa9bfded9439cd3fd881e0a937a9b91dd Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 9 Nov 2024 10:01:31 -0300 Subject: [PATCH 07/19] create routes.php --- src/routes.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/routes.php diff --git a/src/routes.php b/src/routes.php new file mode 100644 index 00000000..acbff0a7 --- /dev/null +++ b/src/routes.php @@ -0,0 +1,30 @@ +has_param("id")) { + wp_send_json_error("missing_id", "Missing post id", ["status" => 400]); + return; + } + + $id = $request->get_param("id"); + $user_id = get_current_user_id(); + + if (!get_post($id)) { + wp_send_json_error("post_not_found", "Post not found", ["status" => 404]); + return; + } + + Database::addBookmark($user_id, $id); + + wp_send_json_success(); +} \ No newline at end of file From d5958c2d2bf6dbf65cb788be2d52f2fe39bdc07f Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 9 Nov 2024 10:02:23 -0300 Subject: [PATCH 08/19] update hook_callbacks.php: remove wpfav_create_table() --- src/hook_callbacks.php | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/src/hook_callbacks.php b/src/hook_callbacks.php index bd5571a0..5689cf7b 100644 --- a/src/hook_callbacks.php +++ b/src/hook_callbacks.php @@ -2,27 +2,6 @@ if (!defined("ABSPATH")) exit; -require_once(ABSPATH . "wp-admin/includes/upgrade.php"); - - - -function wpfav_create_table() { - 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); -} - function wpfav_is_authed() { From 4c35da16388d3db9c376177dbeb0d8fd165f7d47 Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 9 Nov 2024 10:03:09 -0300 Subject: [PATCH 09/19] update wp-fav-posts.php: add on_init() --- src/wp-fav-posts.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/wp-fav-posts.php b/src/wp-fav-posts.php index 632e875c..07ef9cbc 100644 --- a/src/wp-fav-posts.php +++ b/src/wp-fav-posts.php @@ -14,15 +14,28 @@ require_once(PLUGIN_DIR . "src/hook_callbacks.php"); require_once(PLUGIN_DIR . "src/routes.php"); +require_once(PLUGIN_DIR . "src/db_utils.php"); class WP_Fav_Posts { public function __construct() { - add_action("init", "wpfav_create_table"); + add_action("init", "on_init"); + } + + + public function on_init() { + Database::createTable(); + + register_rest_route("wpfav/v1", "/add-bookmark/(?P\d+)", [ + "methods" => "POST", + "callback" => "wpfav_add_bookmark", + "permission_callback" => "wpfav_is_authed" + ]); } } new WP_Fav_Posts(); + From 1eb35795339b6508ffcdaa3f18d771c75e6c1291 Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 9 Nov 2024 10:04:10 -0300 Subject: [PATCH 10/19] update wp-fav-posts.php: rename plugin class --- src/wp-fav-posts.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-fav-posts.php b/src/wp-fav-posts.php index 07ef9cbc..f52cde89 100644 --- a/src/wp-fav-posts.php +++ b/src/wp-fav-posts.php @@ -18,7 +18,7 @@ -class WP_Fav_Posts { +class WPFavPosts { public function __construct() { add_action("init", "on_init"); } @@ -37,5 +37,5 @@ public function on_init() { -new WP_Fav_Posts(); +new WPFavPosts(); From cd585d02399b4803a79a51b59ce705d88af8a15a Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 9 Nov 2024 10:06:32 -0300 Subject: [PATCH 11/19] add remove bookmark route --- src/routes.php | 21 +++++++++++++++++++++ src/wp-fav-posts.php | 5 +++++ 2 files changed, 26 insertions(+) diff --git a/src/routes.php b/src/routes.php index acbff0a7..da9d2700 100644 --- a/src/routes.php +++ b/src/routes.php @@ -26,5 +26,26 @@ function wpfav_add_bookmark($request) { Database::addBookmark($user_id, $id); + wp_send_json_success(); +} + + + +function wpfav_remove_bookmark($request) { + if (!$request->has_param("id")) { + wp_send_json_error("missing_id", "Missing post id", ["status" => 400]); + return; + } + + $id = $request->get_param("id"); + $user_id = get_current_user_id(); + + if (!get_post($id)) { + wp_send_json_error("post_not_found", "Post not found", ["status" => 404]); + return; + } + + Database::removeBookmark($user_id, $id); + wp_send_json_success(); } \ No newline at end of file diff --git a/src/wp-fav-posts.php b/src/wp-fav-posts.php index f52cde89..11d7ce06 100644 --- a/src/wp-fav-posts.php +++ b/src/wp-fav-posts.php @@ -32,6 +32,11 @@ public function on_init() { "callback" => "wpfav_add_bookmark", "permission_callback" => "wpfav_is_authed" ]); + register_rest_route("wpfav/v1", "/remove-bookmark/(?P\d+)", [ + "methods" => "POST", + "callback" => "wpfav_remove_bookmark", + "permission_callback" => "wpfav_is_authed" + ]); } } From 7253a05b643f77b5cc27a58242cbe04e125556dc Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 9 Nov 2024 10:10:04 -0300 Subject: [PATCH 12/19] add "is bookmarked" route --- src/routes.php | 21 +++++++++++++++++++++ src/wp-fav-posts.php | 5 +++++ 2 files changed, 26 insertions(+) diff --git a/src/routes.php b/src/routes.php index da9d2700..d6946350 100644 --- a/src/routes.php +++ b/src/routes.php @@ -48,4 +48,25 @@ function wpfav_remove_bookmark($request) { Database::removeBookmark($user_id, $id); wp_send_json_success(); +} + + + +function wpfav_is_bookmarked($request) { + if (!$request->has_param("id")) { + wp_send_json_error("missing_id", "Missing post id", ["status" => 400]); + return; + } + + $id = $request->get_param("id"); + $user_id = get_current_user_id(); + + if (!get_post($id)) { + wp_send_json_error("post_not_found", "Post not found", ["status" => 404]); + return; + } + + $is_bookmarked = Database::isBookmarked($user_id, $id); + + wp_send_json_success([ "is_bookmarked" => $is_bookmarked ]); } \ No newline at end of file diff --git a/src/wp-fav-posts.php b/src/wp-fav-posts.php index 11d7ce06..08cf1547 100644 --- a/src/wp-fav-posts.php +++ b/src/wp-fav-posts.php @@ -37,6 +37,11 @@ public function on_init() { "callback" => "wpfav_remove_bookmark", "permission_callback" => "wpfav_is_authed" ]); + register_rest_route("wpfav/v1", "/is-bookmarked/(?P\d+)", [ + "methods" => "GET", + "callback" => "wpfav_is_bookmarked", + "permission_callback" => "wpfav_is_authed" + ]); } } From 4fe5d090de9184451e19037bd2085122dbdf7863 Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 9 Nov 2024 10:14:51 -0300 Subject: [PATCH 13/19] add list bookmarks route --- src/routes.php | 10 ++++++++++ src/wp-fav-posts.php | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/src/routes.php b/src/routes.php index d6946350..f3d1a6c1 100644 --- a/src/routes.php +++ b/src/routes.php @@ -69,4 +69,14 @@ function wpfav_is_bookmarked($request) { $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 ]); } \ No newline at end of file diff --git a/src/wp-fav-posts.php b/src/wp-fav-posts.php index 08cf1547..2cf3b5e2 100644 --- a/src/wp-fav-posts.php +++ b/src/wp-fav-posts.php @@ -42,6 +42,11 @@ public function on_init() { "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" + ]); } } From 5cb476818a41fe742c2799a01509263dc3421fe3 Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 9 Nov 2024 10:22:26 -0300 Subject: [PATCH 14/19] update routes.php: remove unnecessary requires and defines --- src/routes.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/routes.php b/src/routes.php index f3d1a6c1..6e8d9a00 100644 --- a/src/routes.php +++ b/src/routes.php @@ -2,10 +2,7 @@ if (!defined("ABSPATH")) exit; -define("PLUGIN_DIR", plugin_dir_path(__FILE__)); - -require_once(PLUGIN_DIR . "src/routes.php"); require_once(PLUGIN_DIR . "src/db_utils.php"); From 43114f182e79244a7a4fb58cd6e5a45d89ded303 Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 9 Nov 2024 10:22:48 -0300 Subject: [PATCH 15/19] move wp-fav-posts.php to the root folder --- src/wp-fav-posts.php => wp-fav-posts.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/wp-fav-posts.php => wp-fav-posts.php (100%) diff --git a/src/wp-fav-posts.php b/wp-fav-posts.php similarity index 100% rename from src/wp-fav-posts.php rename to wp-fav-posts.php From 9ae05f2cfa705fdaffc231e5f73a7450f6c7e1d2 Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 9 Nov 2024 10:26:03 -0300 Subject: [PATCH 16/19] update wp-fav-posts.php: fix missing $this on hook callback --- wp-fav-posts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-fav-posts.php b/wp-fav-posts.php index 2cf3b5e2..e14b02d9 100644 --- a/wp-fav-posts.php +++ b/wp-fav-posts.php @@ -20,7 +20,7 @@ class WPFavPosts { public function __construct() { - add_action("init", "on_init"); + add_action("init", [$this, "on_init"]); } From 447cacd469a4bafbf99820b2d3c5a30f6fae6b6c Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 9 Nov 2024 10:28:33 -0300 Subject: [PATCH 17/19] update db_utils.php: fix missing path check and requires --- src/db_utils.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/db_utils.php b/src/db_utils.php index 00a60f38..3511a8cd 100644 --- a/src/db_utils.php +++ b/src/db_utils.php @@ -1,5 +1,10 @@ Date: Sat, 9 Nov 2024 15:03:48 -0300 Subject: [PATCH 18/19] update routes.php: fix error responses --- src/routes.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/routes.php b/src/routes.php index 6e8d9a00..2c3c75d9 100644 --- a/src/routes.php +++ b/src/routes.php @@ -9,7 +9,7 @@ function wpfav_add_bookmark($request) { if (!$request->has_param("id")) { - wp_send_json_error("missing_id", "Missing post id", ["status" => 400]); + wp_send_json_error([ "error" => "Missing post id" ], 400); return; } @@ -17,7 +17,7 @@ function wpfav_add_bookmark($request) { $user_id = get_current_user_id(); if (!get_post($id)) { - wp_send_json_error("post_not_found", "Post not found", ["status" => 404]); + wp_send_json_error([ "error" => "Post not found" ], 404); return; } @@ -30,7 +30,7 @@ function wpfav_add_bookmark($request) { function wpfav_remove_bookmark($request) { if (!$request->has_param("id")) { - wp_send_json_error("missing_id", "Missing post id", ["status" => 400]); + wp_send_json_error([ "error" => "Missing post id" ], 400); return; } @@ -38,7 +38,7 @@ function wpfav_remove_bookmark($request) { $user_id = get_current_user_id(); if (!get_post($id)) { - wp_send_json_error("post_not_found", "Post not found", ["status" => 404]); + wp_send_json_error([ "error" => "Post not found" ], 404); return; } @@ -51,7 +51,7 @@ function wpfav_remove_bookmark($request) { function wpfav_is_bookmarked($request) { if (!$request->has_param("id")) { - wp_send_json_error("missing_id", "Missing post id", ["status" => 400]); + wp_send_json_error([ "error" => "Missing post id" ], 400); return; } @@ -59,7 +59,7 @@ function wpfav_is_bookmarked($request) { $user_id = get_current_user_id(); if (!get_post($id)) { - wp_send_json_error("post_not_found", "Post not found", ["status" => 404]); + wp_send_json_error([ "error" => "Post not found" ], 404); return; } From 1a7cea1b7e29619b8f3196b0982ac6175386ad39 Mon Sep 17 00:00:00 2001 From: Raphael Date: Sat, 9 Nov 2024 15:08:01 -0300 Subject: [PATCH 19/19] update .gitattributes: fix typo --- .gitattributes | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitattributes b/.gitattributes index 6cb9b25a..44b4224b 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -* eof=lf \ No newline at end of file +* eol=lf \ No newline at end of file