engine = $engine; } /** * Registers Plugin service hooks. * * @since 1.0.0 * * @return array> */ public function get_hooks(): array { return array( array( 'rest_api_init', array( $this, 'register_routes' ) ), ); } /** * Registers all REST routes. * * @since 1.0.0 * @hook rest_api_init * * @return void */ public function register_routes(): void { register_rest_route( self::NAMESPACE, '/suggestions', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_suggestions' ), 'permission_callback' => array( $this, 'suggestions_permission' ), 'args' => array( 'post_id' => array( 'required' => true, 'type' => 'integer', 'minimum' => 1, 'sanitize_callback' => 'absint', 'description' => __( 'The post ID to get suggestions for.', 'postlinkly' ), ), 'limit' => array( 'required' => false, 'type' => 'integer', 'minimum' => 1, 'maximum' => 100, 'default' => 10, 'sanitize_callback' => 'absint', 'description' => __( 'Maximum number of suggestions to return.', 'postlinkly' ), ), ), ) ); } /** * Permission callback for the suggestions endpoint. * * @since 1.0.0 * * @param \WP_REST_Request $request The REST request. * * @return bool|\WP_Error True when authorised. */ public function suggestions_permission( \WP_REST_Request $request ): bool|\WP_Error { $post_id = (int) $request->get_param( 'post_id' ); if ( ! is_user_logged_in() ) { return new \WP_Error( 'rest_not_logged_in', __( 'You must be logged in to access suggestions.', 'postlinkly' ), array( 'status' => 401 ) ); } if ( ! current_user_can( 'edit_post', $post_id ) ) { return new \WP_Error( 'rest_forbidden', __( 'You do not have permission to access suggestions for this post.', 'postlinkly' ), array( 'status' => 403 ) ); } return true; } /** * Handles GET /postlinkly/v1/suggestions. * * @since 1.0.0 * * @param \WP_REST_Request $request The REST request. * * @return \WP_REST_Response|\WP_Error */ public function get_suggestions( \WP_REST_Request $request ): \WP_REST_Response|\WP_Error { $post_id = (int) $request->get_param( 'post_id' ); $limit = (int) $request->get_param( 'limit' ); $post = get_post( $post_id ); if ( null === $post ) { return new \WP_Error( 'rest_post_not_found', __( 'Post not found.', 'postlinkly' ), array( 'status' => 404 ) ); } $source = new PostModel( (int) $post->ID, PostModel::TYPE_POST, (string) $post->post_title, (string) $post->post_status ); $content = (string) $post->post_content; $suggestions = $this->engine->get_suggestions( $source, $content, $limit ); return new \WP_REST_Response( array( 'post_id' => $post_id, 'suggestions' => array_map( array( $this, 'serialise' ), $suggestions ), ), 200 ); } /** * Serialises a SuggestionModel for the REST response. * * @since 1.0.0 * * @param SuggestionModel $s Suggestion to serialise. * * @return array */ private function serialise( SuggestionModel $s ): array { return array( 'target_id' => $s->target->id, 'target_type' => $s->target->type, 'target_title' => $s->target->title, 'anchor' => $s->anchor, 'sentence' => $s->original_sentence, 'total_score' => round( $s->total_score, 4 ), ); } }