-
Notifications
You must be signed in to change notification settings - Fork 82
Eduardo Lopes - Conclusão do desafio #80
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
elopes-sv
wants to merge
2
commits into
Apiki:master
Choose a base branch
from
elopes-sv:eduardo-lopes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
| /** | ||
| * Plugin Name: Apiki Favorites | ||
| * Description: Favorite and unfavorite posts for authenticated users via the WordPress REST API. | ||
| * Version: 1.0.0 | ||
| * Requires PHP: 5.6 | ||
| * Author: Apiki Challenge | ||
| */ | ||
|
|
||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| exit; | ||
| } | ||
|
|
||
| define( 'APIKI_FAVORITES_VERSION', '1.0.0' ); | ||
| define( 'APIKI_FAVORITES_PLUGIN_FILE', __FILE__ ); | ||
| define( 'APIKI_FAVORITES_PLUGIN_PATH', plugin_dir_path( __FILE__ ) ); | ||
|
|
||
| require_once APIKI_FAVORITES_PLUGIN_PATH . 'includes/class-activator.php'; | ||
| require_once APIKI_FAVORITES_PLUGIN_PATH . 'includes/class-favorites-repository.php'; | ||
| require_once APIKI_FAVORITES_PLUGIN_PATH . 'includes/class-favorites-rest-controller.php'; | ||
| require_once APIKI_FAVORITES_PLUGIN_PATH . 'includes/class-plugin.php'; | ||
|
|
||
| register_activation_hook( __FILE__, array( 'Apiki_Favorites_Activator', 'activate' ) ); | ||
|
|
||
| function apiki_favorites_run_plugin() { | ||
| $plugin = new Apiki_Favorites_Plugin(); | ||
| $plugin->run(); | ||
| } | ||
|
|
||
| apiki_favorites_run_plugin(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| exit; | ||
| } | ||
|
|
||
| class Apiki_Favorites_Activator { | ||
|
|
||
| public static function activate() { | ||
| global $wpdb; | ||
|
|
||
| $table_name = $wpdb->prefix . 'apiki_favorites'; | ||
| $charset_collate = $wpdb->get_charset_collate(); | ||
| $sql = "CREATE TABLE {$table_name} ( | ||
| 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), | ||
| UNIQUE KEY user_post (user_id, post_id) | ||
| ) {$charset_collate};"; | ||
|
|
||
| require_once ABSPATH . 'wp-admin/includes/upgrade.php'; | ||
|
|
||
| dbDelta( $sql ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| <?php | ||
|
|
||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| exit; | ||
| } | ||
|
|
||
| class Apiki_Favorites_Repository { | ||
|
|
||
| protected $wpdb; | ||
|
|
||
| protected $table_name; | ||
|
|
||
| public function __construct( $database = null ) { | ||
| global $wpdb; | ||
|
|
||
| $this->wpdb = $database ? $database : $wpdb; | ||
| $this->table_name = $this->wpdb->prefix . 'apiki_favorites'; | ||
| } | ||
|
|
||
| public function exists( $user_id, $post_id ) { | ||
| $favorite_id = $this->wpdb->get_var( | ||
| $this->wpdb->prepare( | ||
| "SELECT id FROM {$this->table_name} WHERE user_id = %d AND post_id = %d LIMIT 1", | ||
| $user_id, | ||
| $post_id | ||
| ) | ||
| ); | ||
|
|
||
| return ! empty( $favorite_id ); | ||
| } | ||
|
|
||
| public function insert( $user_id, $post_id ) { | ||
| return $this->wpdb->insert( | ||
| $this->table_name, | ||
| array( | ||
| 'user_id' => $user_id, | ||
| 'post_id' => $post_id, | ||
| ), | ||
| array( | ||
| '%d', | ||
| '%d', | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| public function delete( $user_id, $post_id ) { | ||
| return $this->wpdb->delete( | ||
| $this->table_name, | ||
| array( | ||
| 'user_id' => $user_id, | ||
| 'post_id' => $post_id, | ||
| ), | ||
| array( | ||
| '%d', | ||
| '%d', | ||
| ) | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| <?php | ||
|
|
||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| exit; | ||
| } | ||
|
|
||
| class Apiki_Favorites_REST_Controller { | ||
|
|
||
| protected $namespace = 'apiki/v1'; | ||
|
|
||
| protected $repository; | ||
|
|
||
| public function __construct( Apiki_Favorites_Repository $repository ) { | ||
| $this->repository = $repository; | ||
| } | ||
|
|
||
| public function register_routes() { | ||
| register_rest_route( | ||
| $this->namespace, | ||
| '/posts/(?P<post_id>\d+)/favorite', | ||
| array( | ||
| array( | ||
| 'methods' => WP_REST_Server::CREATABLE, | ||
| 'callback' => array( $this, 'favorite' ), | ||
| 'permission_callback' => array( $this, 'permission_check' ), | ||
| ), | ||
| array( | ||
| 'methods' => WP_REST_Server::DELETABLE, | ||
| 'callback' => array( $this, 'unfavorite' ), | ||
| 'permission_callback' => array( $this, 'permission_check' ), | ||
| ), | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| public function permission_check( $request ) { | ||
| if ( ! is_user_logged_in() ) { | ||
| return new WP_Error( | ||
| 'apiki_favorites_forbidden', | ||
| 'Authentication required.', | ||
| array( 'status' => 401 ) | ||
| ); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public function favorite( WP_REST_Request $request ) { | ||
| $post_id = (int) $request['post_id']; | ||
| $post = $this->get_valid_post( $post_id ); | ||
| $user_id = get_current_user_id(); | ||
|
|
||
| if ( is_wp_error( $post ) ) { | ||
| return $post; | ||
| } | ||
|
|
||
| if ( $this->repository->exists( $user_id, $post_id ) ) { | ||
| return new WP_Error( | ||
| 'apiki_favorites_conflict', | ||
| 'Post already favorited.', | ||
| array( 'status' => 409 ) | ||
| ); | ||
| } | ||
|
|
||
| $insert_result = $this->repository->insert( $user_id, $post_id ); | ||
|
|
||
| if ( false === $insert_result ) { | ||
| if ( $this->repository->exists( $user_id, $post_id ) ) { | ||
| return new WP_Error( | ||
| 'apiki_favorites_conflict', | ||
| 'Post already favorited.', | ||
| array( 'status' => 409 ) | ||
| ); | ||
| } | ||
|
|
||
| return new WP_Error( | ||
| 'apiki_favorites_insert_error', | ||
| 'Could not favorite post.', | ||
| array( 'status' => 500 ) | ||
| ); | ||
| } | ||
|
|
||
| return new WP_REST_Response( | ||
| array( | ||
| 'post_id' => $post_id, | ||
| 'favorited' => true, | ||
| ), | ||
| 201 | ||
| ); | ||
| } | ||
|
|
||
| public function unfavorite( WP_REST_Request $request ) { | ||
| $post_id = (int) $request['post_id']; | ||
| $post = $this->get_valid_post( $post_id ); | ||
| $user_id = get_current_user_id(); | ||
|
|
||
| if ( is_wp_error( $post ) ) { | ||
| return $post; | ||
| } | ||
|
|
||
| if ( ! $this->repository->exists( $user_id, $post_id ) ) { | ||
| return new WP_Error( | ||
| 'apiki_favorites_not_found', | ||
| 'Favorite not found.', | ||
| array( 'status' => 404 ) | ||
| ); | ||
| } | ||
|
|
||
| $delete_result = $this->repository->delete( $user_id, $post_id ); | ||
|
|
||
| if ( 0 === $delete_result ) { | ||
| return new WP_Error( | ||
| 'apiki_favorites_not_found', | ||
| 'Favorite not found.', | ||
| array( 'status' => 404 ) | ||
| ); | ||
| } | ||
|
|
||
| if ( false === $delete_result ) { | ||
| return new WP_Error( | ||
| 'apiki_favorites_delete_error', | ||
| 'Could not unfavorite post.', | ||
| array( 'status' => 500 ) | ||
| ); | ||
| } | ||
|
|
||
| return new WP_REST_Response( | ||
| array( | ||
| 'post_id' => $post_id, | ||
| 'favorited' => false, | ||
| ), | ||
| 200 | ||
| ); | ||
| } | ||
|
|
||
| protected function get_valid_post( $post_id ) { | ||
| $post = get_post( $post_id ); | ||
|
|
||
| if ( ! $post || 'post' !== $post->post_type ) { | ||
| return new WP_Error( | ||
| 'apiki_favorites_post_not_found', | ||
| 'Post not found.', | ||
| array( 'status' => 404 ) | ||
| ); | ||
| } | ||
|
|
||
| return $post; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| exit; | ||
| } | ||
|
|
||
| class Apiki_Favorites_Plugin { | ||
|
|
||
| protected $repository; | ||
|
|
||
| protected $rest_controller; | ||
|
|
||
| public function __construct() { | ||
| $this->repository = new Apiki_Favorites_Repository(); | ||
| $this->rest_controller = new Apiki_Favorites_REST_Controller( $this->repository ); | ||
| } | ||
|
|
||
| public function run() { | ||
| add_action( 'rest_api_init', array( $this->rest_controller, 'register_routes' ) ); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get_valid_postonly checks thatget_postreturns aposttype, butget_postcan return private/draft posts even when the current user is not allowed to read them. In practice, any authenticated user can favorite/unfavorite non-public posts and infer their existence from the response code (404vs201/409/200). Add a permission/status check (for examplecurrent_user_can( 'read_post', $post_id )or equivalent visibility logic) before treating the post as valid.Useful? React with 👍 / 👎.