Skip to content

Alan Pardini SantAna #49

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 2 commits 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
90 changes: 90 additions & 0 deletions Plugin Favoritar Posts.postman_collection.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
64 changes: 64 additions & 0 deletions classes/CreateTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Arquivo da classe CreateTable
*
* PHP version 7.4
*
* @category CreateTable
* @package WPPluginFavoritarPosts
* @author Alan Pardini Sant Ana <[email protected]>
* @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 <[email protected]>
* @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();
116 changes: 116 additions & 0 deletions classes/LoginNaWPRestAPI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* Arquivo da classe LoginNaWPRestAPI
*
* PHP version 7.4
*
* @category LoginNaWPRestAPI
* @package WPPluginFavoritarPosts
* @author Alan Pardini Sant Ana <[email protected]>
* @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 <[email protected]>
* @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();
85 changes: 85 additions & 0 deletions classes/WPRestApiDesfavPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Arquivo da classe WPRestApiDesfavPost
*
* PHP version 7.4
*
* @category WPRestApiDesfavPost
* @package WPPluginFavoritarPosts
* @author Alan Pardini Sant Ana <[email protected]>
* @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 <[email protected]>
* @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();
Loading