Action Scheduler Based Reporting Engine

Overview

The Action Scheduler based reporting engine provides an asynchronous framework for generating large reports in WordPress. It breaks down report generation into smaller batches to prevent timeouts and provides progress tracking.

Core Components

1. JC_AS_Report_Controller (Abstract Base)

  • Base controller class for all Action Scheduler reports
  • Handles asynchronous execution
  • Manages report statuses and progress tracking
  • Provides AJAX endpoints for control and monitoring
  • Supports concurrent/non-concurrent report execution

Key Methods:

  • trigger(): Starts report generation
  • process_step(): Processes individual batches
  • can_start(): Checks if new report can begin
  • report_progress(): Returns report status via AJAX
  • stop_processing(): Halts report generation
  • delete_report(): Removes report data

2. JC_Async_Report (Data Model)

  • Represents individual report instances
  • Stores report metadata:
    • Title
    • Created date/time
    • Status
    • Progress metrics
    • Context data
    • Download information
  • Handles persistence via WooCommerce data store

3. JC_Async_Report_Data_Store

  • Manages persistence of report data
  • Handles CRUD operations
  • Provides query methods for report retrieval

Implementation Pattern

  1. Report Definition
    • Extend JC_AS_Report_Controller
    • Define report type and processing logic
    • Implement required abstract methods
  2. Data Model
    • Use JC_Async_Report for state management
    • Store report-specific context and results

Key Features

  1. Progress Tracking
    • Real-time status updates
    • Step counting and completion percentage
    • Processing time metrics
  2. Error Handling
    • Graceful failure management
    • Status logging
    • Recovery options
  3. Concurrency Control
    • Optional concurrent report execution
    • Report queuing
    • Resource management
  4. File Management
    • Secure file generation
    • Organized storage structure
    • Download link generation

Database Structure

Tables created and managed:

jc_async_reports
- id
- title
- date_created
- created_by
- report_type
- status
- context (JSON)
- steps_processed
- total_steps
- downloads (JSON)

Usage Example

class JC_General_Redemption_Controller extends JC_AS_Report_Controller {
    protected $report_type = 'general-redemption';
    protected $action = 'general-redemption';
    protected $base_action = 'general_async_redemptions';
    protected $supports_concurrent = false;
    
    protected function setup_context(JC_Async_Report &$report, $context = []) {
        $file = $this->create_file($context);
        $report->set_downloads(json_encode(['filename' => $file['name'], 'fileurl' => $file['url']]));
        $report->set_context(json_encode($context));
    }
    
    protected function get_total_steps(JC_Async_Report &$report) : int {
        // Calculate total processing steps
        $data_store = WC_Data_Store::load('journal_premium_entitlement');
        $context = json_decode($report->get_context(), true);
        return intval($data_store->get_total_for($context['entitlement_number']) / $this->per_step);
    }
}

// Usage
$controller = JC_General_Redemption_Controller::get_instance();

Security Features

  1. Capability Checks
    • Requires ‘customer_screens’ capability
    • AJAX nonce verification
    • File access controls
  2. Data Validation
    • Input sanitization
    • Context validation
    • Status verification

Integration Points

  1. WordPress Hooks
    • Action scheduler integration
    • WooCommerce data store system
    • AJAX handlers
  2. Template System
    • Progress display templates
    • Report list views
    • Download management