diff --git a/favorite-posts/favorite-posts.php b/favorite-posts/favorite-posts.php new file mode 100644 index 00000000..985f6961 --- /dev/null +++ b/favorite-posts/favorite-posts.php @@ -0,0 +1,16 @@ +prefix . 'favorite_posts'; + + $exists = $wpdb->get_var($wpdb->prepare( + "SELECT id FROM $table_name WHERE user_id = %d AND post_id = %d", + $user_id, $post_id + )); + + if ($exists) { + $wpdb->delete($table_name, ['id' => $exists]); + return ['status' => 'removed']; + } else { + $wpdb->insert($table_name, ['user_id' => $user_id, 'post_id' => $post_id]); + return ['status' => 'added']; + } +} diff --git a/favorite-posts/includes/install.php b/favorite-posts/includes/install.php new file mode 100644 index 00000000..9ffcc391 --- /dev/null +++ b/favorite-posts/includes/install.php @@ -0,0 +1,20 @@ +prefix . 'favorite_posts'; + $charset_collate = $wpdb->get_charset_collate(); + + $sql = "CREATE TABLE IF NOT EXISTS $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 (user_id, post_id) + ) $charset_collate;"; + + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; + dbDelta($sql); +} diff --git a/favorite-posts/includes/routes.php b/favorite-posts/includes/routes.php new file mode 100644 index 00000000..217c0afa --- /dev/null +++ b/favorite-posts/includes/routes.php @@ -0,0 +1,11 @@ +\d+)', [ + 'methods' => 'POST', + 'callback' => 'fp_toggle_favorite', + 'permission_callback' => function () { + return is_user_logged_in(); + } + ]); +});