400 ) ); } $post = get_post( $post_id ); if ( ! $post ) { return new WP_Error( 'schema_ai_post_not_found', __( 'Post not found.', 'ai-schema-gen' ), array( 'status' => 404 ) ); } return true; } /** * Prepare a raw database schema row for API response. * * @param object $schema Database row from the schemas table. * @return array Formatted schema data. */ protected function prepare_schema_response( $schema ) { return array( 'id' => (int) $schema->id, 'post_id' => (int) $schema->post_id, 'schema_type' => $schema->schema_type, 'schema_data' => json_decode( $schema->schema_data, true ), 'is_auto_generated' => (bool) $schema->is_auto_generated, 'confidence_score' => $schema->confidence_score ? (float) $schema->confidence_score : null, 'validation_status' => $schema->validation_status, 'validation_errors' => $schema->validation_errors ? json_decode( $schema->validation_errors, true ) : null, 'last_validated' => $schema->last_validated, 'created_at' => $schema->created_at, 'updated_at' => $schema->updated_at, ); } /** * Check whether the current user can edit the given post. * * @param int $post_id The WordPress post ID. * @return true|WP_Error True when allowed, WP_Error otherwise. */ protected function check_edit_permission( $post_id ) { if ( ! is_user_logged_in() ) { return new WP_Error( 'schema_ai_not_logged_in', __( 'You must be logged in.', 'ai-schema-gen' ), array( 'status' => 401 ) ); } if ( ! current_user_can( 'edit_post', $post_id ) ) { return new WP_Error( 'schema_ai_forbidden', __( 'You do not have permission to edit this post.', 'ai-schema-gen' ), array( 'status' => 403 ) ); } return true; } /** * Get the current user's subscription plan. * * @return string Plan identifier: 'free', 'pro', or 'premium'. */ protected function get_current_user_plan() { $user_id = get_current_user_id(); if ( ! $user_id ) { return 'free'; } $plan = get_user_meta( $user_id, 'schema_ai_plan', true ); return $plan ? $plan : 'free'; } /** * Return a standardised error response. * * @param string $code Machine-readable error code. * @param string $message Human-readable message. * @param int $status HTTP status code. * @param array $data Optional extra data merged into the error. * @return WP_Error */ protected function error_response( $code, $message, $status = 400, $data = array() ) { $data['status'] = $status; return new WP_Error( $code, $message, $data ); } /** * Return a standardised success response. * * @param mixed $data Response payload. * @param int $status HTTP status code. * @return WP_REST_Response */ protected function success_response( $data, $status = 200 ) { return new WP_REST_Response( $data, $status ); } }