diff --git a/wp-content/plugins/favoritar-posts/favoritar-posts.php b/wp-content/plugins/favoritar-posts/favoritar-posts.php new file mode 100644 index 00000000..e2abce1b --- /dev/null +++ b/wp-content/plugins/favoritar-posts/favoritar-posts.php @@ -0,0 +1,19 @@ +repository = new Favoritos_Repository($wpdb); + $this->rest_controller = new Favoritos_REST_Controller($this->repository); + + add_action('rest_api_init', [$this->rest_controller, 'register_routes']); + } + + public static function init() + { + if (null === self::$instance) { + self::$instance = new self(); + } + } + + public static function activate() + { + global $wpdb; + $repository = new Favoritos_Repository($wpdb); + $repository->create_table(); + } + + public static function uninstall() + { + global $wpdb; + $repository = new Favoritos_Repository($wpdb); + $repository->drop_table(); + } +} diff --git a/wp-content/plugins/favoritar-posts/includes/class-favoritos-repository.php b/wp-content/plugins/favoritar-posts/includes/class-favoritos-repository.php new file mode 100644 index 00000000..975e5883 --- /dev/null +++ b/wp-content/plugins/favoritar-posts/includes/class-favoritos-repository.php @@ -0,0 +1,92 @@ +wpdb = $wpdb; + $this->table = $this->wpdb->prefix . 'user_favorites'; + } + + public function create_table() + { + $charset_collate = $this->wpdb->get_charset_collate(); + $sql = "CREATE TABLE {$this->table} ( + id bigint(20) unsigned NOT NULL AUTO_INCREMENT, + user_id bigint(20) unsigned NOT NULL, + post_id bigint(20) unsigned NOT NULL, + created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (id), + UNIQUE KEY user_post (user_id, post_id), + KEY post_id (post_id) + ) {$charset_collate};"; + + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; + dbDelta($sql); + } + + public function drop_table() + { + $this->wpdb->query("DROP TABLE IF EXISTS {$this->table}"); + } + + public function toggle($user_id, $post_id) + { + $exists = (bool) $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT id FROM {$this->table} WHERE user_id = %d AND post_id = %d", + $user_id, + $post_id + ) + ); + + if ($exists) { + $deleted = $this->wpdb->delete( + $this->table, + ['user_id' => $user_id, 'post_id' => $post_id], + ['%d', '%d'] + ); + + if (false === $deleted) { + return new WP_Error('db_delete_error', 'Nao foi possivel remover o favorito.'); + } + + return 'removido'; + } + + $inserted = $this->wpdb->insert( + $this->table, + [ + 'user_id' => $user_id, + 'post_id' => $post_id, + 'created_at' => current_time('mysql'), + ], + ['%d', '%d', '%s'] + ); + + if (false === $inserted) { + return new WP_Error('db_insert_error', 'Nao foi possivel favoritar o post.'); + } + + return 'adicionado'; + } + + public function get_user_favorite_ids($user_id) + { + $results = $this->wpdb->get_col( + $this->wpdb->prepare( + "SELECT post_id FROM {$this->table} WHERE user_id = %d ORDER BY created_at DESC", + $user_id + ) + ); + + return array_map('intval', $results); + } +} diff --git a/wp-content/plugins/favoritar-posts/includes/class-favoritos-rest-controller.php b/wp-content/plugins/favoritar-posts/includes/class-favoritos-rest-controller.php new file mode 100644 index 00000000..2768d0df --- /dev/null +++ b/wp-content/plugins/favoritar-posts/includes/class-favoritos-rest-controller.php @@ -0,0 +1,101 @@ +repository = $repository; + } + + public function register_routes() + { + register_rest_route($this->namespace, '/toggle/(?P\d+)', [ + 'methods' => 'POST', + 'callback' => [$this, 'handle_toggle'], + 'permission_callback' => [$this, 'check_permissions'], + 'args' => [ + 'post_id' => [ + 'validate_callback' => function ($param) { + return is_numeric($param) && $param > 0; + }, + ], + ], + ]); + + register_rest_route($this->namespace, '/list', [ + 'methods' => 'GET', + 'callback' => [$this, 'handle_list'], + 'permission_callback' => [$this, 'check_permissions'], + ]); + } + + public function check_permissions() + { + return is_user_logged_in(); + } + + public function handle_toggle($request) + { + $user_id = get_current_user_id(); + $post_id = (int) $request['post_id']; + + $post = get_post($post_id); + if (!$post || 'publish' !== $post->post_status) { + return new WP_Error('invalid_post', 'Post invalido.', ['status' => 400]); + } + + $status = $this->repository->toggle($user_id, $post_id); + if ($status instanceof WP_Error) { + return $status; + } + + $favorites = $this->format_posts( + $this->repository->get_user_favorite_ids($user_id) + ); + + return rest_ensure_response([ + 'status' => $status, + 'favoritos' => $favorites, + ]); + } + + public function handle_list($request) + { + $user_id = get_current_user_id(); + $favorites = $this->format_posts( + $this->repository->get_user_favorite_ids($user_id) + ); + + return rest_ensure_response($favorites); + } + + private function format_posts(array $ids) + { + if (empty($ids)) { + return []; + } + + $posts = get_posts([ + 'post__in' => $ids, + 'post_type' => 'post', + 'post_status' => 'publish', + 'orderby' => 'post__in', + 'posts_per_page' => count($ids), + ]); + + return array_map(function ($post) { + return [ + 'ID' => $post->ID, + 'title' => get_the_title($post), + 'link' => get_permalink($post), + ]; + }, $posts); + } +}