Skip to content

Conversation

@bordoni
Copy link
Owner

@bordoni bordoni commented May 30, 2025

📋 Summary

This PR introduces a comprehensive REST API infrastructure for FakerPress, designed to replace the existing AJAX-based functionality with modern REST endpoints. The implementation follows FakerPress's established service provider pattern and WordPress coding standards.

🚀 What's New

Core Infrastructure

  • REST Controller Service Provider - Main controller extending Service_Provider pattern
  • Interface & Abstract Classes - Standardized contracts for all REST endpoints
  • OpenAPI 3.0 Documentation - Complete API documentation with schema generation
  • Documentation Endpoint - Self-documenting API accessible at /wp-json/fakerpress/v1/docs

API Namespace

All endpoints are available under: /wp-json/fakerpress/v1/

🛡️ Security Features

  • WordPress Authentication - Cookie and nonce-based auth
  • Capability Checking - Granular permission control per endpoint
  • Input Validation - Automatic parameter validation and sanitization
  • Error Handling - Standardized error responses

📖 Documentation

OpenAPI 3.0 Specification

  • Complete API documentation with schemas
  • Request/response examples
  • Security scheme definitions
  • Compatible with Swagger UI, Postman, etc.

Available Endpoints

  • GET /docs/openapi - Raw OpenAPI specification

🔄 Migration Path

This infrastructure prepares for migrating existing AJAX endpoints:

  1. Current AJAX Actions (in Ajax.php):

    • fakerpress.module_generate
    • fakerpress.select2-WP_Query
    • fakerpress.search_authors
    • fakerpress.select2-search_terms
  2. Future REST Endpoints:

    • POST /posts/generate
    • POST /users/generate
    • POST /terms/generate
    • POST /comments/generate
    • POST /attachments/generate
    • POST /meta/generate

🧪 Testing

The infrastructure includes:

  • Parameter validation and sanitization
  • Standardized response formats
  • OpenAPI schema validation
  • WordPress REST API compatibility

📋 Next Steps

  1. Create individual endpoint classes for each module
  2. Migrate AJAX functionality to REST endpoints
  3. Update frontend to consume REST API
  4. Maintain backward compatibility during transition

🔍 Code Quality

  • ✅ WordPress Coding Standards compliance
  • ✅ Proper docblock documentation
  • ✅ Short array syntax for PHP
  • ✅ Early return patterns for conditionals
  • ✅ ES6 syntax ready for JavaScript integration

This PR establishes the foundation for FakerPress's REST API migration. The infrastructure is complete and ready for individual module endpoint implementation.

@bordoni bordoni self-assigned this May 30, 2025
bordoni added 6 commits May 30, 2025 01:24
- Refactor module.js to ES6 syntax with arrow functions, const/let, template literals
- Replace WordPress AJAX with REST API endpoints for all module generation
- Add comprehensive form data parsing for complex nested structures (fakerpress[taxonomy][0][qty][max])
- Implement smart numeric conversion (integers, floats, negatives) while preserving dates
- Add proper internationalization support for both PHP and JavaScript
- Fix REST API authentication with proper wp_rest nonce handling
- Add data-endpoint attributes to all module forms (posts, users, terms, comments)
- Enhance error handling with styled notices and detailed error messages
- Restore links functionality to display generated item edit links
- Filter empty form values to clean up API requests
- Add wp-i18n dependency and script translations setup
- Update Assets.php to localize REST API data for JavaScript

Breaking changes:
- Module generation now uses REST endpoints instead of admin-ajax.php
- Form data structure optimized for REST API consumption
- Requires wp-api-request and wp-i18n JavaScript dependencies

Endpoints:
- POST /wp-json/fakerpress/v1/posts/generate
- POST /wp-json/fakerpress/v1/users/generate
- POST /wp-json/fakerpress/v1/terms/generate
- POST /wp-json/fakerpress/v1/comments/generate
…n, and OpenAPI schema generation

- Add Handles_Batching trait for consistent batch processing across endpoints
- Enhance Abstract_Endpoint with robust parameter validation and sanitization
- Improve OpenAPI schema generation with proper endpoint args conversion
- Refactor all endpoint classes (Posts, Comments, Terms, Users) to use batching trait
- Add proper type hints and nullable permission handling
- Fix Attachment module to include required WordPress admin functions for REST context
- Streamline Documentation endpoint with cleaner OpenAPI spec delivery
- Update Controller to use centralized OpenAPI base specification
- Improve JavaScript module with enhanced error handling and ES6 patterns

This commit significantly improves the REST API's reliability, consistency, and documentation
while maintaining backward compatibility and following WordPress coding standards.
…r handling and type safety

- Update all module classes (Comment, Post, Term, User) to use consistent parse_request signatures
- Replace string error returns with WP_Error objects for better error handling
- Add proper type hints (int $qty, array $request) to parse_request methods
- Remove deprecated quantity calculation logic and legacy request handling
- Improve docblock documentation with proper parameter descriptions
- Update REST endpoints to handle WP_Error responses instead of string errors
- Remove unnecessary imports and clean up namespace usage
- Standardize error messages and codes across all modules

This refactoring improves type safety, error handling consistency, and prepares
the modules for better integration with the REST API architecture.
@bordoni bordoni requested a review from Copilot June 9, 2025 18:53
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR establishes a REST API infrastructure for FakerPress using the service provider pattern, migrating AJAX actions to modern endpoints and adding OpenAPI support.

  • Introduce a central REST\Controller to register module endpoints under /wp-json/fakerpress/v1/
  • Implement individual endpoint classes (Posts, Users, Terms, Comments) with batching support and standardized request/response schemas
  • Add OpenAPI 3.0 documentation generation, update admin templates and assets to consume the new API

Reviewed Changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/templates/pages/posts.php Add data-endpoint="posts" attribute to the form
src/templates/pages/comments.php Add data-endpoint="comments" attribute to the form
src/FakerPress/REST/Traits/Handles_Batching.php New trait implementing batching logic for endpoints
src/FakerPress/REST/Controller.php Service provider that loads and registers all endpoints
src/FakerPress/REST/OpenAPI.php Utilities to build OpenAPI specification
src/FakerPress/REST/Endpoints/* Individual endpoint classes for Posts, Users, Terms, Comments, Documentation
src/FakerPress/REST/README.md Documentation for the REST API architecture and usage
src/FakerPress/Plugin.php Bind and register the REST controller and endpoint implementations
src/FakerPress/Assets.php Enqueue scripts with REST localization and i18n support
Comments suppressed due to low confidence (1)

src/FakerPress/REST/Traits/Handles_Batching.php:129

  • [nitpick] The batching success message uses cumulative item counts instead of batch number (e.g. 'Batch 20 of 100'). Consider renaming placeholders or computing the batch index to avoid misleading output.
__( '(Batch %d of %d total)', 'fakerpress' ),

bordoni and others added 12 commits June 9, 2025 16:08
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Add Attachments REST endpoint with batching support
- Create Attachment_View admin interface class
- Implement complete attachment generation in parse_request
- Add image providers (Placehold.co, Lorem Picsum)
- Fix dropdown issues and JavaScript errors
- Ensure PNG format for placehold.co to avoid SVG issues
- Add size control via dimensions or aspect ratio
- Support alt text, captions, and descriptions generation

Resolves attachment generation returning 0 items and SVG format issues
- Document version 0.9.0 with all REST API features
- Add REST API section to feature descriptions
- Include attachment generation documentation
- Update stable tag to 0.9.0
- Remove module_generate method from Ajax class
- Remove associated action hook
- Clean up unused imports (Factory, View_Factory)
- Module generation now handled exclusively through REST API
- Use specific Lorem Picsum image IDs instead of random parameter
- Fetch image metadata from Lorem Picsum API to get author names
- Include "Photo by [Author] on Unsplash" attribution in alt text
- Update readme with new feature documentation

This ensures proper attribution for photographers whose work is used
- Add placeholder() alias method to match expected Faker format
- Fix WP_Attachment provider to use correct method names
- Ensure both placeholder and lorempicsum providers work correctly

Fixes "Unknown format 'placeholder'" exception
- Always add attribution for both Lorem Picsum and Placehold.co images
- Remove conditional that prevented attribution when no caption was generated
- Ensure every attachment has proper source attribution
- Update documentation to reflect universal attribution

All generated images now include attribution regardless of caption settings
- Removed redundant changelog line about attachment generation returning 0 items
- Cleaned up changelog to maintain clarity and conciseness
- Add get_menu_icon_svg() method to Menu.php with FakerPress SVG icon
- Use the actual fakerpress.svg from resources/img for consistency
- Remove font icon CSS file and registration from Assets.php
- Clear out font.pcss to remove custom font usage
- Update changelog to document the change

This modernizes the admin menu icon using native SVG support in WordPress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants