From 62fd409c818be05d450282bd941e91d7df1236f1 Mon Sep 17 00:00:00 2001 From: dalton-macedo Date: Wed, 6 Nov 2024 09:24:45 -0300 Subject: [PATCH 1/2] Create favorite-posts.php Arquivo principal do plugin favoritar posts --- favorite-posts.php | 105 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 favorite-posts.php diff --git a/favorite-posts.php b/favorite-posts.php new file mode 100644 index 00000000..a51994ad --- /dev/null +++ b/favorite-posts.php @@ -0,0 +1,105 @@ +prefix . 'favorite_posts'; + $charsetCollate = $wpdb->get_charset_collate(); + + $sql = "CREATE TABLE $tableName ( + 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) + ) $charsetCollate;"; + + require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); + dbDelta($sql); + } + + // Registra rotas na WP REST API + public function registerApiRoutes() { + register_rest_route('favorite-posts/v1', '/toggle', [ + 'methods' => 'POST', + 'callback' => [$this, 'handleToggleFavorite'], + 'permission_callback' => function () { + return is_user_logged_in(); + } + ]); + } + + // Cria e executa a lógica de favoritar/desfavoritar + public function handleToggleFavorite(WP_REST_Request $request) { + $userId = get_current_user_id(); + $postId = $request->get_param('post_id'); + + if (!get_post($postId)) { + return new WP_Error('invalid_post', 'Post inválido.', ['status' => 404]); + } + + global $wpdb; + $tableName = $wpdb->prefix . 'favorite_posts'; + + // Verifica se o post já está favoritado + $favoriteExists = $wpdb->get_var($wpdb->prepare( + "SELECT id FROM $tableName WHERE user_id = %d AND post_id = %d", + $userId, $postId + )); + + if ($favoriteExists) { + // Remove o favorito + $wpdb->delete($tableName, ['id' => $favoriteExists], ['%d']); + return ['status' => 'unfavorited']; + } else { + // Adiciona o favorito + $wpdb->insert($tableName, [ + 'user_id' => $userId, + 'post_id' => $postId + ], ['%d', '%d']); + return ['status' => 'favorited']; + } + } + + // Adiciona os scripts necessários + public function enqueueScripts() { + if (is_single()) { + wp_enqueue_script('favorite-posts-script', plugins_url('/favorite-posts.js', __FILE__), ['jquery'], null, true); + wp_localize_script('favorite-posts-script', 'favoritePostsData', [ + 'nonce' => wp_create_nonce('wp_rest'), + 'apiUrl' => rest_url('favorite-posts/v1/toggle') + ]); + } + } + + // Exibir o botão de favoritar + public function exibirBotaoFavoritar($atts) { + if (!is_user_logged_in()) { + return '

Você precisa estar logado para favoritar posts.

'; + } + + global $post; + $postId = $post->ID; + return ''; + } +} + +new FavoritarPostsPlugin(); From 02dc6381f7a5b7da6e6dd516de817e3025d2568a Mon Sep 17 00:00:00 2001 From: dalton-macedo Date: Wed, 6 Nov 2024 09:26:31 -0300 Subject: [PATCH 2/2] Create favorite-posts.js Arquivo js para executar o botao favoritar/desfavoritar --- favorite-posts.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 favorite-posts.js diff --git a/favorite-posts.js b/favorite-posts.js new file mode 100644 index 00000000..4d82eb66 --- /dev/null +++ b/favorite-posts.js @@ -0,0 +1,27 @@ +jQuery(document).ready(function($) { + $('.favorite-button').on('click', function() { + var button = $(this); + var postId = button.data('post-id'); + + $.ajax({ + method: 'POST', + url: favoritePostsData.apiUrl, + beforeSend: function(xhr) { + xhr.setRequestHeader('X-WP-Nonce', favoritePostsData.nonce); + }, + data: { + post_id: postId + }, + success: function(response) { + if (response.status === 'favorited') { + button.text('Desfavoritar'); + } else if (response.status === 'unfavorited') { + button.text('Favoritar'); + } + }, + error: function() { + alert('Ocorreu um erro. Tente novamente.'); + } + }); + }); +});