From fd5975d75a35bc757985a58ba096b2151cb9f095 Mon Sep 17 00:00:00 2001 From: Clayton Santos Cordeiro Date: Mon, 15 May 2023 10:02:55 -0300 Subject: [PATCH] Add files via upload --- pluginFavorito/pluginFavorito.php | 134 ++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 pluginFavorito/pluginFavorito.php diff --git a/pluginFavorito/pluginFavorito.php b/pluginFavorito/pluginFavorito.php new file mode 100644 index 0000000..102be3a --- /dev/null +++ b/pluginFavorito/pluginFavorito.php @@ -0,0 +1,134 @@ +prefix . 'favoritos'; + + // Verifica se a tabela já existe a tabela no banco de dados + + if ($wpdb->get_var("SHOW TABLES LIKE '$post_favoritos'") != $post_favoritos) { + + // Cria a tabela no banco de dados + + $charset_collate = $wpdb->get_charset_collate(); + $sql = "CREATE TABLE $post_favoritos ( + id INT(11) NOT NULL AUTO_INCREMENT, + post_id INT(11) NOT NULL, + user_id INT(11) NOT NULL, + created_at DATETIME NOT NULL, + updated_at DATETIME, + deleted_at DATETIME + PRIMARY KEY (id) + ) $charset_collate;"; + + require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); + dbDelta($sql); + } +} +register_activation_hook(__FILE__, 'criar_tabela_favoritos'); + +// Adicionando as rotas da API REST + +function adicionar_rotas_api() { + register_rest_route('meu-plugin-favoritos/v1', '/favoritos/(?P\d+)', array( + 'methods' => 'POST', + 'callback' => 'adicionar_favorito', + 'permission_callback' => function () { + return is_user_logged_in(); + }, + )); + + register_rest_route('meu-plugin-favoritos/v1', '/favoritos/(?P\d+)', array( + 'methods' => 'DELETE', + 'callback' => 'remover_favorito', + 'permission_callback' => function () { + return is_user_logged_in(); + }, + )); +} + +add_action('rest_api_init', 'adicionar_rotas_api'); + +// Função para adicionar um favorito + +function adicionar_favorito($request) { + $user_id = get_current_user_id(); + $post_id = $request['id']; + $created_at = current_time('mysql'); + + global $wpdb; + $post_favoritos = $wpdb->prefix . 'favoritos'; + + // Verifica se o favorito já existe + $favorito_existe = $wpdb->get_var($wpdb->prepare("SELECT id FROM $post_favoritos WHERE post_id = %d AND user_id = %d", $post_id, $user_id)); + + if ($favorito_existe) { + return new WP_Error('favorito_ja_existente', 'Este post já foi favoritado.', array('status' => 400)); + } + + // Insere o favorito na tabela com a data de criação + $wpdb->insert( + $post_favoritos, + array( + 'post_id' => $post_id, + 'user_id' => $user_id, + 'created_at' => $created_at + ), + array( + '%d', + '%d', + '%s' + ) + ); + + return array('message' => 'Post favoritado com sucesso!'); +} + +// Função para remover dos favoritos + +function remover_favorito($request) { + $user_id = get_current_user_id(); + $post_id = $request['id']; + $deleted_at = current_time('mysql'); + + global $wpdb; + $post_favoritos = $wpdb->prefix . 'favoritos'; + + // Verifica se o favorito existe + $favorito_existe = $wpdb->get_var($wpdb->prepare("SELECT id FROM $post_favoritos WHERE post_id = %d AND user_id = %d", $post_id, $user_id)); + + if (!$favorito_existe) { + return new WP_Error('favorito_nao_encontrado', 'O post não está favoritado.', array('status' => 400)); + } + + // Atualiza a coluna de data de exclusão para registrar a data atual + $wpdb->update( + $post_favoritos, + array( + 'deleted_at' => $deleted_at + ), + array( + 'post_id' => $post_id, + 'user_id' => $user_id + ), + array( + '%s' + ), + array( + '%d', + '%d' + ) + ); + + return array('message' => 'Post removido dos favoritos com sucesso!'); +} \ No newline at end of file