PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / app / Http / Controllers / Reports / RetentionSnapshotController.php

RetentionSnapshotController.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Http/Controllers/Reports/RetentionSnapshotController.php

112 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Http\Controllers\Reports;
4
5 use FluentCart\App\Services\Report\RetentionSnapshotService;
6 use FluentCart\Framework\Http\Request\Request;
7 use FluentCart\Framework\Support\Arr;
8 use FluentCart\App\Http\Controllers\Controller;
9
10 class RetentionSnapshotController extends Controller
11 {
12 /**
13 * Generate retention snapshots via Action Scheduler (background job)
14 */
15 public function generate(Request $request)
16 {
17 $productId = $request->get('product_id');
18 if ($productId) {
19 $productId = (int) $productId;
20 }
21
22 // Check if Action Scheduler is available
23 if (!function_exists('as_enqueue_async_action')) {
24 // Fallback: run synchronously if Action Scheduler not available
25 $service = new RetentionSnapshotService();
26 $result = $service->generate($productId, null);
27
28 return [
29 'success' => Arr::get($result, 'success'),
30 'message' => Arr::get($result, 'message'),
31 'stats' => Arr::get($result, 'stats', []),
32 'mode' => 'synchronous',
33 ];
34 }
35
36 // Use timestamp as tracking ID - create it ONCE
37 $trackingId = time();
38
39 // Queue the job via Action Scheduler
40 // Note: Action Scheduler passes array values as separate arguments to the callback
41 as_schedule_single_action(
42 time(),
43 'fluent_cart_generate_retention_snapshots',
44 [$productId, $trackingId], // Will be passed as generateSnapshots($productId, $trackingId)
45 'fluent-cart-snapshots'
46 );
47
48 // Store job start time
49 update_option('fluent_cart_snapshot_job_' . $trackingId, [
50 'status' => 'pending',
51 'started_at' => current_time('mysql'),
52 'product_id' => $productId,
53 ]);
54
55 return [
56 'success' => true,
57 'message' => __('Snapshot generation queued', 'fluent-cart'),
58 'job_id' => $trackingId,
59 'mode' => 'background',
60 ];
61 }
62
63 /**
64 * Check status of a snapshot generation job
65 */
66 public function checkStatus(Request $request)
67 {
68 $jobId = $request->get('params.job_id');
69
70 if (!$jobId) {
71 return [
72 'success' => false,
73 'message' => __('Job ID required', 'fluent-cart'),
74 ];
75 }
76
77 $jobData = get_option('fluent_cart_snapshot_job_' . $jobId);
78
79 if (!$jobData) {
80 return [
81 'success' => false,
82 'message' => __('Job not found', 'fluent-cart'),
83 'job_id' => $jobId,
84 ];
85 }
86
87 // If job data shows completed or failed, return that status
88 $jobStatus = Arr::get($jobData, 'status');
89 if ($jobStatus && \in_array($jobStatus, ['completed', 'failed'])) {
90 return [
91 'success' => true,
92 'status' => $jobStatus,
93 'message' => Arr::get($jobData, 'message', \sprintf(
94 /* translators: %1$s: job status (completed or failed) */
95 __('Job %1$s', 'fluent-cart'),
96 $jobStatus
97 )),
98 'stats' => Arr::get($jobData, 'stats', []),
99 'data' => $jobData,
100 ];
101 }
102
103 // Otherwise, it's still running
104 return [
105 'success' => true,
106 'status' => 'running',
107 'message' => __('Job is still running', 'fluent-cart'),
108 'data' => $jobData,
109 ];
110 }
111 }
112