From 002dfe61079b464bb1270badf64c9a08836e09db Mon Sep 17 00:00:00 2001 From: Jamerson Wesley Date: Sun, 22 Mar 2026 16:23:31 -0300 Subject: [PATCH] Finalizado --- apiki_favoritos/favoritos.js | 23 +++ apiki_favoritos/wp_favoritos.php | 244 +++++++++++++++++++++++++++++++ 2 files changed, 267 insertions(+) create mode 100644 apiki_favoritos/favoritos.js create mode 100644 apiki_favoritos/wp_favoritos.php diff --git a/apiki_favoritos/favoritos.js b/apiki_favoritos/favoritos.js new file mode 100644 index 00000000..3b562cea --- /dev/null +++ b/apiki_favoritos/favoritos.js @@ -0,0 +1,23 @@ +jQuery(document).ready(function($){ + $('.favorito-btn').click(function(){ + const btn = $(this); + const post_id = btn.data('post'); + const action = btn.text().includes('Desfavoritar') ? 'desfavoritar' : 'favoritar'; + + $.ajax({ + url: wpFavoritos.url + '/' + action, + method: 'POST', + data: { post_id }, + beforeSend: function(xhr){ + xhr.setRequestHeader('X-WP-Nonce', wpFavoritos.nonce); + }, + success: function(res){ + if(res.favorited){ + btn.text(' Desfavoritar'); + } else { + btn.text(' Favoritar'); + } + } + }); + }); +}); \ No newline at end of file diff --git a/apiki_favoritos/wp_favoritos.php b/apiki_favoritos/wp_favoritos.php new file mode 100644 index 00000000..06465cb6 --- /dev/null +++ b/apiki_favoritos/wp_favoritos.php @@ -0,0 +1,244 @@ +table = $wpdb->prefix . 'favoritos'; + + // Criar tabela ao ativar + register_activation_hook(__FILE__, [$this, 'install']); + + // Rotas REST API + add_action('rest_api_init', [$this, 'routes']); + + // Adiciona botão nos posts + add_filter('the_content', [$this, 'add_button']); + add_action('wp_enqueue_scripts', [$this, 'scripts']); + + // Dashboard do usuário + add_action('admin_menu', [$this, 'dashboard_menu']); + } + + // Criar tabela no banco + public function install() { + global $wpdb; + + $charset = $wpdb->get_charset_collate(); + + $sql = "CREATE TABLE {$this->table} ( + id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, + user_id BIGINT UNSIGNED NOT NULL, + post_id BIGINT UNSIGNED NOT NULL, + created_at DATETIME DEFAULT CURRENT_TIMESTAMP, + UNIQUE KEY unique_user_post (user_id, post_id) + ) $charset;"; + + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; + dbDelta($sql); + } + + // Rotas REST API + public function routes() { + register_rest_route('wp-favoritos/v1', '/favoritar', [ + 'methods' => 'POST', + 'callback' => [$this, 'favoritar'], + 'permission_callback' => function() { return is_user_logged_in(); } + ]); + + register_rest_route('wp-favoritos/v1', '/desfavoritar', [ + 'methods' => 'POST', + 'callback' => [$this, 'desfavoritar'], + 'permission_callback' => function() { return is_user_logged_in(); } + ]); + + register_rest_route('wp-favoritos/v1', '/status', [ + 'methods' => 'GET', + 'callback' => [$this, 'status'], + 'permission_callback' => function() { return is_user_logged_in(); } + ]); + + register_rest_route('wp-favoritos/v1', '/list', [ + 'methods' => 'GET', + 'callback' => [$this, 'listar'], + 'permission_callback' => function() { return is_user_logged_in(); } + ]); + } + + // Favoritar + public function favoritar($request) { + global $wpdb; + $user_id = get_current_user_id(); + $post_id = isset($request['post_id']) ? intval($request['post_id']) : 0; + + if (!$user_id || !$post_id) { + return new WP_Error('invalid_data', 'Usuário ou post inválido', ['status' => 400]); + } + + $exists = $wpdb->get_var($wpdb->prepare( + "SELECT id FROM {$this->table} WHERE user_id=%d AND post_id=%d", + $user_id, $post_id + )); + + if ($exists) return ['favorited' => true, 'message' => 'Já favoritado']; + + $wpdb->insert($this->table, [ + 'user_id' => $user_id, + 'post_id' => $post_id + ]); + + return ['favorited' => true, 'message' => 'Post favoritado!']; + } + + // Desfavoritar + public function desfavoritar($request) { + global $wpdb; + $user_id = get_current_user_id(); + $post_id = isset($request['post_id']) ? intval($request['post_id']) : 0; + + if (!$user_id || !$post_id) { + return new WP_Error('invalid_data', 'Usuário ou post inválido', ['status' => 400]); + } + + $wpdb->delete($this->table, [ + 'user_id' => $user_id, + 'post_id' => $post_id + ]); + + return ['favorited' => false, 'message' => 'Post desfavoritado!']; + } + + // Status + public function status($request) { + global $wpdb; + $user_id = get_current_user_id(); + $post_id = isset($request['post_id']) ? intval($request['post_id']) : 0; + + if (!$user_id || !$post_id) { + return new WP_Error('invalid_data', 'Usuário ou post inválido', ['status' => 400]); + } + + $exists = $wpdb->get_var($wpdb->prepare( + "SELECT id FROM {$this->table} WHERE user_id=%d AND post_id=%d", + $user_id, $post_id + )); + + return ['favorited' => $exists ? true : false]; + } + + // Listar favoritos + public function listar($request = null) { + global $wpdb; + $user_id = get_current_user_id(); + + $results = $wpdb->get_results($wpdb->prepare(" + SELECT f.post_id, p.post_title, p.post_date + FROM {$this->table} f + LEFT JOIN {$wpdb->posts} p ON p.ID = f.post_id + WHERE f.user_id = %d + ORDER BY f.created_at DESC + ", $user_id)); + + return $results; + } + + // Botão automático nos posts + public function add_button($content) { + if (!is_single()) return $content; + + global $post, $wpdb; + $user_id = get_current_user_id(); + $post_id = $post->ID; + + if (!$user_id) return $content . '

🔒 Faça login para favoritar

'; + + $favorited = $wpdb->get_var($wpdb->prepare( + "SELECT id FROM {$this->table} WHERE user_id=%d AND post_id=%d", + $user_id, $post_id + )) ? true : false; + + $label = $favorited ? ' Desfavoritar' : ' Favoritar'; + + $html = "
+ +
"; + + return $html . $content; + } + + // JS + public function scripts() { + wp_enqueue_script( + 'favoritos-js', + plugin_dir_url(__FILE__) . 'favoritos.js', + ['jquery'], + '1.0', + true + ); + + wp_localize_script('favoritos-js', 'wpFavoritos', [ + 'url' => rest_url('wp-favoritos/v1'), + 'nonce' => wp_create_nonce('wp_rest') + ]); + } + + // Menu do dashboard + public function dashboard_menu() { + add_menu_page( + 'Meus Favoritos', + 'Meus Favoritos', + 'read', + 'meus-favoritos', + [$this, 'dashboard_page'], + 'dashicons-heart', + 30 + ); + } + + // Página do dashboard + public function dashboard_page() { + $favoritos = $this->listar(); + + echo '
'; + echo '

Meus Favoritos

'; + + if (!$favoritos) { + echo '

Você ainda não favoritou nenhum post.

'; + } else { + echo ''; + echo ''; + echo ''; + foreach ($favoritos as $fav) { + $edit_link = get_edit_post_link($fav->post_id); + $view_link = get_permalink($fav->post_id); + echo ''; + echo ""; + echo ""; + echo ""; + echo ""; + echo ''; + } + echo ''; + echo '
IDTítuloDataAções
{$fav->post_id}{$fav->post_title}{$fav->post_date} + Ver | + Editar +
'; + } + + echo '
'; + } +} + +new WPFavoritosCompleto(); \ No newline at end of file