diff --git a/Plugin Favoritar Posts.postman_collection.json b/Plugin Favoritar Posts.postman_collection.json new file mode 100644 index 0000000..315962a --- /dev/null +++ b/Plugin Favoritar Posts.postman_collection.json @@ -0,0 +1,90 @@ +{ + "info": { + "_postman_id": "d0aa2c70-b123-4836-8484-701ee99c72af", + "name": "Plugin Favoritar Posts", + "description": "Usando a WP Rest API favorite, desfavorite e liste os posts favoritados.", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "7736609", + "_collection_link": "https://grey-space-336270.postman.co/workspace/Desafio-APIKI~0befd4d9-768c-4b58-b81d-11b2d65b7a80/collection/7736609-d0aa2c70-b123-4836-8484-701ee99c72af?action=share&source=collection_link&creator=7736609" + }, + "item": [ + { + "name": "Listar Posts", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{URL}}/wp-json/plugin-fav-post/listar-posts", + "host": [ + "{{URL}}" + ], + "path": [ + "wp-json", + "plugin-fav-post", + "listar-posts" + ] + } + }, + "response": [] + }, + { + "name": "Favoritar Post", + "request": { + "method": "GET", + "header": [] + }, + "response": [] + }, + { + "name": "Desfavoritar Post", + "request": { + "method": "GET", + "header": [] + }, + "response": [] + } + ], + "auth": { + "type": "basic", + "basic": [ + { + "key": "password", + "value": "admin", + "type": "string" + }, + { + "key": "username", + "value": "admin", + "type": "string" + } + ] + }, + "event": [ + { + "listen": "prerequest", + "script": { + "type": "text/javascript", + "packages": {}, + "exec": [ + "" + ] + } + }, + { + "listen": "test", + "script": { + "type": "text/javascript", + "packages": {}, + "exec": [ + "" + ] + } + } + ], + "variable": [ + { + "key": "URL", + "value": "https://wp-plugin-favoritar-posts.ddev.site" + } + ] +} \ No newline at end of file diff --git a/classes/CreateTable.php b/classes/CreateTable.php new file mode 100644 index 0000000..012b921 --- /dev/null +++ b/classes/CreateTable.php @@ -0,0 +1,64 @@ + + * @license http://opensource.org/licenses/MIT MIT + * @link https://www.linkedin.com/in/alanpardinisantana/ + */ + +namespace App; + +/** + * Classe de criação da nova tabela no banco de dados + * + * PHP version 7.4 + * + * @category Class + * @package CreateTable + * @author Alan Pardini Sant Ana + * @license http://opensource.org/licenses/MIT MIT + * @link https://www.linkedin.com/in/alanpardinisantana/ + */ +class CreateTable +{ + + /** + * Construct + */ + public function __construct() + { + /** + * Hooks + */ + register_activation_hook( MAINDIR . '/favoritar-posts.php', [ $this, 'create_new_table' ] ); + } + + /** + * Função de criação da nova tabela + * + * @return null Não retorna nada + */ + public function create_new_table() { + global $wpdb; + + $table_name = $wpdb->prefix . 'favoritar_posts'; + $charset_collate = $wpdb->get_charset_collate(); + + $sql = "CREATE TABLE $table_name ( + id mediumint(9) NOT NULL AUTO_INCREMENT, + user_id mediumint(9) NOT NULL, + post_id mediumint(9) NOT NULL, + PRIMARY KEY (id) + ) $charset_collate;"; + + $wpdb->query( $sql ); + } + +} + +$CreateTable = new CreateTable(); diff --git a/classes/LoginNaWPRestAPI.php b/classes/LoginNaWPRestAPI.php new file mode 100644 index 0000000..6431a23 --- /dev/null +++ b/classes/LoginNaWPRestAPI.php @@ -0,0 +1,116 @@ + + * @license http://opensource.org/licenses/MIT MIT + * @link https://www.linkedin.com/in/alanpardinisantana/ + */ + +namespace App; + +/** + * Login na WP Rest API + * + * PHP version 7.4 + * + * @category Class + * @package LoginNaWPRestAPI + * @author Alan Pardini Sant Ana + * @license http://opensource.org/licenses/MIT MIT + * @link https://www.linkedin.com/in/alanpardinisantana/ + */ +class LoginNaWPRestAPI +{ + + /** + * Construct + */ + public function __construct() + { + /** + * Hooks + */ + add_filter( 'rest_authentication_errors', [ $this, 'rest_auth' ] ); + add_filter( 'determine_current_user', [ $this, 'json_basic_auth_handler' ], 20 ); + } + + /** + * Função de retorno de não logado + * + * @return null Não retorna nada + */ + public function rest_auth( $result ) { + // If a previous authentication check was applied, + // pass that result along without modification. + if ( true === $result || is_wp_error( $result ) ) { + return $result; + } + + // No authentication has been performed yet. + // Return an error if user is not logged in. + if ( ! is_user_logged_in() ) { + return new \WP_Error( + 'rest_not_logged_in', + __( 'You are not currently logged in.' ), + array( 'status' => 401 ) + ); + } + + // Our custom authentication check should have no effect + // on logged-in requests + return $result; + } + + /** + * Função de determinação do usuário logado + * + * @return null Não retorna nada + */ + public function json_basic_auth_handler( $user ) { + global $wp_json_basic_auth_error; + + $wp_json_basic_auth_error = null; + + // Don't authenticate twice + if ( ! empty( $user ) ) { + return $user; + } + + // Check that we're trying to authenticate + if ( !isset( $_SERVER['PHP_AUTH_USER'] ) ) { + return $user; + } + + $username = $_SERVER['PHP_AUTH_USER']; + $password = $_SERVER['PHP_AUTH_PW']; + + /** + * In multi-site, wp_authenticate_spam_check filter is run on authentication. This filter calls + * get_currentuserinfo which in turn calls the determine_current_user filter. This leads to infinite + * recursion and a stack overflow unless the current function is removed from the determine_current_user + * filter during authentication. + */ + remove_filter( 'determine_current_user', [ $this, 'json_basic_auth_handler' ], 20 ); + + $user = wp_authenticate( $username, $password ); + + add_filter( 'determine_current_user', [ $this, 'json_basic_auth_handler' ], 20 ); + + if ( is_wp_error( $user ) ) { + $wp_json_basic_auth_error = $user; + return null; + } + + $wp_json_basic_auth_error = true; + + return $user->ID; + } + +} + +$LoginNaWPRestAPI = new LoginNaWPRestAPI(); diff --git a/classes/WPRestApiDesfavPost.php b/classes/WPRestApiDesfavPost.php new file mode 100644 index 0000000..70ac210 --- /dev/null +++ b/classes/WPRestApiDesfavPost.php @@ -0,0 +1,85 @@ + + * @license http://opensource.org/licenses/MIT MIT + * @link https://www.linkedin.com/in/alanpardinisantana/ + */ + +namespace App; + +/** + * Classe de desfavoritar o post na WP Rest API + * + * PHP version 7.4 + * + * @category Class + * @package WPRestApiDesfavPost + * @author Alan Pardini Sant Ana + * @license http://opensource.org/licenses/MIT MIT + * @link https://www.linkedin.com/in/alanpardinisantana/ + */ +class WPRestApiDesfavPost +{ + + /** + * Construct + */ + public function __construct() + { + /** + * Hooks + */ + add_action( 'rest_api_init', [ $this, 'create_custon_endpoint' ] ); + } + + /** + * Função de criação do endpoint + * + * @return null Não retorna nada + */ + public function create_custon_endpoint(){ + register_rest_route( + 'plugin-fav-post', + '/desfavoritar-post', + array( + 'methods' => 'POST', + 'callback' => [ $this, 'desfav_post' ], + ) + ); + } + + /** + * Função de desfavoritar e retornar na WP Rest API + * + * @return string Retorna sucesso ou falha + */ + public function desfav_post() { + $get_post = get_post( $_POST[ 'post_id' ] ); + if( $get_post ) { + + global $wpdb; + + $table_name = $wpdb->prefix . 'favoritar_posts'; + $user_ID = get_current_user_id(); + + $wpdb->delete($table_name, array( + "post_id" => $_POST[ 'post_id' ], + "user_id" => $user_ID, + )); + + return __( 'Post defavoritado com sucesso!' ); + + } else { + return __( 'Post ID não existe.' ); + } + } + +} + +$WPRestApiDesfavPost = new WPRestApiDesfavPost(); diff --git a/classes/WPRestApiFavPost.php b/classes/WPRestApiFavPost.php new file mode 100644 index 0000000..5622ca2 --- /dev/null +++ b/classes/WPRestApiFavPost.php @@ -0,0 +1,85 @@ + + * @license http://opensource.org/licenses/MIT MIT + * @link https://www.linkedin.com/in/alanpardinisantana/ + */ + +namespace App; + +/** + * Classe de favoritar o post na WP Rest API + * + * PHP version 7.4 + * + * @category Class + * @package WPRestApiFavPost + * @author Alan Pardini Sant Ana + * @license http://opensource.org/licenses/MIT MIT + * @link https://www.linkedin.com/in/alanpardinisantana/ + */ +class WPRestApiFavPost +{ + + /** + * Construct + */ + public function __construct() + { + /** + * Hooks + */ + add_action( 'rest_api_init', [ $this, 'create_custon_endpoint' ] ); + } + + /** + * Função de criação do endpoint + * + * @return null Não retorna nada + */ + public function create_custon_endpoint(){ + register_rest_route( + 'plugin-fav-post', + '/favoritar-post', + array( + 'methods' => 'POST', + 'callback' => [ $this, 'fav_post' ], + ) + ); + } + + /** + * Função de favoritar e retornar na WP Rest API + * + * @return string Retorna sucesso ou falha + */ + public function fav_post() { + $get_post = get_post( $_POST[ 'post_id' ] ); + if( $get_post ) { + + global $wpdb; + + $table_name = $wpdb->prefix . 'favoritar_posts'; + $user_ID = get_current_user_id(); + + $wpdb->insert($table_name, array( + "post_id" => $_POST[ 'post_id' ], + "user_id" => $user_ID, + )); + + return __( 'Post favoritado com sucesso!' ); + + } else { + return __( 'Post ID não existe.' ); + } + } + +} + +$WPRestApiFavPost = new WPRestApiFavPost(); diff --git a/classes/WPRestApiGetFavPosts.php b/classes/WPRestApiGetFavPosts.php new file mode 100644 index 0000000..1b57e3c --- /dev/null +++ b/classes/WPRestApiGetFavPosts.php @@ -0,0 +1,90 @@ + + * @license http://opensource.org/licenses/MIT MIT + * @link https://www.linkedin.com/in/alanpardinisantana/ + */ + +namespace App; + +/** + * Classe de listar posts favoritos + * + * PHP version 7.4 + * + * @category Class + * @package WPRestApiGetFavPosts + * @author Alan Pardini Sant Ana + * @license http://opensource.org/licenses/MIT MIT + * @link https://www.linkedin.com/in/alanpardinisantana/ + */ +class WPRestApiGetFavPosts +{ + + /** + * Construct + */ + public function __construct() + { + /** + * Hooks + */ + add_action( 'rest_api_init', [ $this, 'create_custon_endpoint' ] ); + } + + /** + * Função de criação do endpoint + * + * @return null Não retorna nada + */ + public function create_custon_endpoint(){ + register_rest_route( + 'plugin-fav-post', + '/listar-posts', + array( + 'methods' => 'GET', + 'callback' => [ $this, 'get_fav_posts' ], + ) + ); + } + + /** + * Função para listar os posts na WP Rest API + * + * @return array Retorna os posts + */ + public function get_fav_posts() { + global $wpdb; + + $user_ID = get_current_user_id(); + $table_name = $wpdb->prefix . 'favoritar_posts'; + $query = $wpdb->get_results( "SELECT * FROM $table_name WHERE user_id = $user_ID" ); + + if( !$query ) { + return __( 'Nenhum post favoritado.' ); + } + + $post_ids = []; + foreach ( $query as $value ) { + $post_ids[] = $value->post_id; + } + + $args = array( + 'post_type' => array( 'post' ), + 'orderby' => 'DESC', + 'post__in' => $post_ids + ); + + $posts = get_posts( $args ); + return $posts; + } + +} + +$WPRestApiGetFavPosts = new WPRestApiGetFavPosts(); diff --git a/favoritar-posts.php b/favoritar-posts.php new file mode 100644 index 0000000..109b1e6 --- /dev/null +++ b/favoritar-posts.php @@ -0,0 +1,23 @@ +