'POST', 'callback' => [ $this, 'analyze' ], 'permission_callback' => [ $this, 'checkPermission' ], 'args' => [ 'post_id' => [ 'required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint', 'validate_callback' => 'rest_validate_request_arg', ], 'title' => [ 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg', ], 'description' => [ 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg', ], 'slug' => [ 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg', ], 'content' => [ 'type' => 'string', 'default' => '', 'sanitize_callback' => 'wp_kses_post', 'validate_callback' => 'rest_validate_request_arg', ], 'keywords' => [ 'type' => 'array', 'default' => [], 'items' => [ 'type' => 'string' ], // WordPress only runs the top level sanitize_callback and never the one on an // item schema, so the keyword elements are cleaned by this callback. 'sanitize_callback' => [ self::class, 'sanitizeKeywords' ], 'validate_callback' => 'rest_validate_request_arg', ], ], ] ); } /** * Sanitize a keyword list element by element. * * The framework normalisation the default sanitizer would have applied is * kept by rest_sanitize_array (a scalar becomes a list, keys are * reindexed), then each scalar element is passed through * sanitize_text_field. A non scalar element is left untouched so the * route's type validation rejects it instead of PHP casting it to the * literal string "Array". * * @param mixed $value Raw keyword list. * @return array The result. */ public static function sanitizeKeywords( mixed $value ): array { return array_map( static fn ( mixed $keyword ): mixed => is_scalar( $keyword ) ? sanitize_text_field( (string) $keyword ) : $keyword, rest_sanitize_array( $value ) ); } /** * Check permission for the requested post. * * @param WP_REST_Request $request Request. * @return bool|WP_Error The result. */ public function checkPermission( WP_REST_Request $request ): bool|WP_Error { $postId = absint( $request->get_param( 'post_id' ) ); if ( $postId > 0 && current_user_can( 'edit_post', $postId ) ) { return true; } return new WP_Error( 'rest_forbidden', __( 'Sorry, you are not allowed to analyse this content.', 'rankkernel' ), [ 'status' => 403 ] ); } /** * Analyse the supplied content. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error The result. */ public function analyze( WP_REST_Request $request ): WP_REST_Response|WP_Error { $postId = absint( $request->get_param( 'post_id' ) ); $post = get_post( $postId ); if ( ! $post instanceof WP_Post ) { return new WP_Error( 'rankkernel_unknown_post', __( 'That post could not be found.', 'rankkernel' ), [ 'status' => 404 ] ); } $keywords = $request->get_param( 'keywords' ); $keywords = is_array( $keywords ) ? array_values( array_map( 'strval', array_filter( $keywords, 'is_scalar' ) ) ) : []; $analyzer = $this->analyzer ?? new Analyzer(); $result = $analyzer->analyze( [ 'html' => (string) $request->get_param( 'content' ), 'title' => (string) $request->get_param( 'title' ), 'description' => (string) $request->get_param( 'description' ), 'slug' => (string) $request->get_param( 'slug' ), 'keywords' => $keywords, 'site_url' => home_url( '/' ), 'featured_alt' => AnalysisScore::featuredAlt( $postId ), 'used_keywords' => $this->usedKeywords( $keywords, $postId ), ] ); return new WP_REST_Response( $result, 200 ); } /** * Titles of other posts that already target the primary keyword. * * Null means the check does not apply and an empty list means the keyword is * free. Only the primary keyword is looked up, because the warning is about * the term the page is built around. * * @param string[] $keywords Keywords. * @param int $postId Post being edited. * @return string[]|null The result. */ private function usedKeywords( array $keywords, int $postId ): ?array { if ( [] === $keywords ) { return null; } $index = $this->index ?? new KeywordIndex(); return $index->usedElsewhere( $keywords[0], $postId ); } }