Skip to content

finaliza o plugin #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
version: "3.9"
services:
db:
container_name: fav-posts-db
image: mariadb:10.6.4-focal
command: '--default-authentication-plugin=mysql_native_password'
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=wordpress
- MYSQL_DATABASE=wordpress
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=wordpress
ports:
- 3306:3306
wp:
image: wordpress:6-php8.0
ports:
- 80:80
restart: always
volumes:
- wp_data:/var/www/html
- ${PWD}:/var/www/html/wp-content/plugins/fav-posts
environment:
- WORDPRESS_DB_HOST=db
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=wordpress
- WORDPRESS_DB_NAME=wordpress
depends_on:
- db
wpcli:
container_name: fav-posts-cli
image: wordpress:cli
user: 1000:1000
command: tail -f /dev/null
volumes:
- wp_data:/var/www/html
- ${PWD}:/var/www/html/wp-content/plugins/fav-posts
environment:
- WORDPRESS_DB_HOST=db
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=wordpress
- WORDPRESS_DB_NAME=wordpress

depends_on:
- db
volumes:
db_data:
wp_data:



115 changes: 115 additions & 0 deletions fav-posts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* Plugin Name: Posts Favoritos
* Description: Adicione Posts como favoritos
* Version: 1.0
* Author: Seu Nome
* License: GPLv2 or later
*/

defined('ABSPATH') || exit;

/**
* [Description PostsFavoritos]
*/
class PostsFavoritos {
/**
* @var string
*/
private $table_name = 'fav_posts';

/**
* @var string
*/
private $table_name_prefixed = '';
/**
* @var string
*/
public $endpoint = 'fav_posts';
public function __construct() {
global $wpdb;
$this->table_name_prefixed = $wpdb->prefix . $this->table_name;

// inclui classe para registrar rotas da rest api
require_once(plugin_dir_path(__FILE__) . 'inc/fav-posts-api-class.php');

new PostsFavoritosEndpoints( $this );
register_activation_hook(__FILE__, array($this, 'activate'));
}

// Método de ativação do plugin
public function activate() {
global $wpdb;

$collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS {$this->table_name_prefixed} (
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),
FOREIGN KEY (user_id) REFERENCES {$wpdb->users} (ID),
FOREIGN KEY (post_id) REFERENCES {$wpdb->posts} (ID)
) {$collate};";

require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
/**
* @param int $post_id
* @param int $user_id
* verifica se um post id foi favoritado pelo user id
* @return boolean
*/
public function is_post_favorito(int $post_id, int $user_id) {
global $wpdb;

$query = $wpdb->prepare(
"SELECT COUNT(*) FROM {$this->table_name_prefixed} WHERE post_id = %d AND user_id = %d",
$post_id,
$user_id
);
$count = (int) $wpdb->get_var($query);

return ! empty( $count );
}

/**
* @param int $post_id
*
* @return boolean
*/
public function add_favorito(int $post_id, int $user_id) {
global $wpdb;

$data = array(
'user_id' => $user_id,
'post_id' => $post_id
);
return ! empty( $wpdb->insert($this->table_name_prefixed, $data) );
}


/**
* @param int $post_id
* @param int $user_id
*
* @return boolean
*/
public function remove_favorito(int $post_id, int $user_id) {
if (!$this->is_post_favorito($user_id, $post_id)) {
return false;
}

global $wpdb;

$where = array(
'user_id' => $user_id,
'post_id' => $post_id
);

return ! empty( $wpdb->delete($this->table_name_prefixed, $where) );
}

}
// Inicia o plugin
$posts_favoritos = new PostsFavoritos();
140 changes: 140 additions & 0 deletions inc/fav-posts-api-class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
/**
* [Description PostsFavoritosEndpoints]
*/
class PostsFavoritosEndpoints extends WP_REST_Controller
{
private $posts_favoritos_plugin;

public function __construct($posts_favoritos_plugin)
{
$this->posts_favoritos_plugin = $posts_favoritos_plugin;
add_action('rest_api_init', array($this, 'register_endpoints'));
add_filter('wp_is_application_passwords_available', '__return_true');
}

/**
* registra as rotas
* @return void
*/
public function register_endpoints()
{
register_rest_route(
$this->posts_favoritos_plugin->endpoint,
'/add',
array(
'methods' => 'POST',
'permission_callback' => 'is_user_logged_in',
'callback' => array($this, 'add_favorito'),
'args' => array(
'post_id' => array(
'type' => 'integer',
'description' => 'Id do Post',
'required' => true,
)
)
)
);

register_rest_route(
$this->posts_favoritos_plugin->endpoint,
'/remove',
array(
'methods' => 'POST',
'permission_callback' => 'is_user_logged_in',
'callback' => array($this, 'remove_favorito'),
'args' => array(
'post_id' => array(
'type' => 'integer',
'description' => 'Id do Post',
'required' => true,
)
)
)
);
}

/**
* @param object $request
*
* @return [type]
*/
public function add_favorito($request)
{
$current_user_id = get_current_user_id();
$post_id = (int) $request->get_param('post_id');
if (empty($post_id)) {
return new WP_Error(
'rest_invalid_param',
'Parametro post_id invalido',
array('status' => 400)
);
}
if ($this->posts_favoritos_plugin->is_post_favorito($post_id, $current_user_id)) {
return new WP_Error(
'rest_invalid_param',
'Post id já adicionado aos favoritos',
array('status' => 400)
);
}
if ($this->posts_favoritos_plugin->add_favorito($post_id, $current_user_id)) {
$response = rest_ensure_response(
array(
'message' => 'Adicionado com sucesso',
'data' => array(
'post_id' => $post_id,
'user_id' => $current_user_id
)
)
);
$response->set_status(200);
return $response;
}

return new WP_Error(
'rest_invalid_param',
'Verifique os parametros e tente novamente',
array('status' => 400)
);
}

// Método para remover um post dos favoritos
public function remove_favorito($request)
{
$post_id = $request->get_param('post_id');
$current_user_id = get_current_user_id();
$post_id = (int) $request->get_param('post_id');
if (empty($post_id)) {
return new WP_Error(
'rest_invalid_param',
'Parametro post_id invalido',
array('status' => 400)
);
}
if (!$this->posts_favoritos_plugin->is_post_favorito($post_id, $current_user_id)) {
return new WP_Error(
'rest_invalid_param',
'Post id não adicionado aos favoritos',
array('status' => 400)
);
}
if ($this->posts_favoritos_plugin->remove_favorito($post_id, $current_user_id)) {
$response = rest_ensure_response(
array(
'message' => 'Removido com sucesso',
'data' => array(
'post_id' => $post_id,
'user_id' => $current_user_id
)
)
);
$response->set_status(200);
return $response;
}
return new WP_Error(
'rest_invalid_param',
'Verifique os parametros e tente novamente',
array('status' => 400)
);
}
}