@@ -40,6 +40,69 @@ class SiteGenService {
40
40
public function __construct () {
41
41
$ this ->input_data = ReduxStateService::get ( 'input ' );
42
42
$ this ->sitegen_data = ReduxStateService::get ( 'sitegen ' );
43
+
44
+ // Register AJAX handler for immediate async processing
45
+ add_action ( 'wp_ajax_sitegen_process_images_async ' , array ( $ this , 'handle_ajax_image_processing ' ) );
46
+ add_action ( 'wp_ajax_nopriv_sitegen_process_images_async ' , array ( $ this , 'handle_ajax_image_processing ' ) );
47
+ }
48
+
49
+ /**
50
+ * Handle AJAX request for immediate async image processing.
51
+ */
52
+ public function handle_ajax_image_processing () {
53
+ // Verify nonce for security
54
+ if ( ! wp_verify_nonce ( $ _POST ['nonce ' ] ?? '' , 'sitegen_async_images ' ) ) {
55
+ wp_die ( 'Security check failed ' );
56
+ }
57
+
58
+ $ post_id = intval ( $ _POST ['post_id ' ] ?? 0 );
59
+ $ content = sanitize_textarea_field ( $ _POST ['content ' ] ?? '' );
60
+
61
+ if ( ! $ post_id || ! $ content ) {
62
+ wp_die ( 'Invalid parameters ' );
63
+ }
64
+
65
+ // Process images
66
+ $ updated_content = self ::sideload_images_and_replace_grammar ( $ content , $ post_id );
67
+ if ( $ updated_content !== $ content ) {
68
+ wp_update_post (
69
+ array (
70
+ 'ID ' => $ post_id ,
71
+ 'post_content ' => $ updated_content ,
72
+ )
73
+ );
74
+ }
75
+
76
+ wp_die ( 'Success ' );
77
+ }
78
+
79
+
80
+
81
+ /**
82
+ * Process homepage images immediately in background (alternative to cron).
83
+ * This method dispatches an async request that doesn't block the main request.
84
+ *
85
+ * @param int $post_id The post ID to process images for.
86
+ * @param string $content The content containing images.
87
+ */
88
+ public function process_homepage_images_immediate_async ( $ post_id , $ content ) {
89
+ // Use wp_remote_post to make an async request to the same site
90
+ $ admin_url = admin_url ( 'admin-ajax.php ' );
91
+ $ nonce = wp_create_nonce ( 'sitegen_async_images ' );
92
+
93
+ wp_remote_post (
94
+ $ admin_url ,
95
+ array (
96
+ 'timeout ' => 0.01 , // Very short timeout to not block
97
+ 'blocking ' => false , // Non-blocking request
98
+ 'body ' => array (
99
+ 'action ' => 'sitegen_process_images_async ' ,
100
+ 'post_id ' => $ post_id ,
101
+ 'content ' => $ content ,
102
+ 'nonce ' => $ nonce ,
103
+ ),
104
+ )
105
+ );
43
106
}
44
107
45
108
/**
@@ -80,16 +143,8 @@ public function publish_homepage( string $selected_sitegen_homepage ) {
80
143
);
81
144
}
82
145
83
- // Process images by extracting URLs from img tags in the content and update the post
84
- $ updated_content = self ::sideload_images_and_replace_grammar ( $ content , $ post_id );
85
- if ( $ updated_content !== $ content ) {
86
- wp_update_post (
87
- array (
88
- 'ID ' => $ post_id ,
89
- 'post_content ' => $ updated_content ,
90
- )
91
- );
92
- }
146
+ // Process images immediately in background (non-blocking)
147
+ $ this ->process_homepage_images_immediate_async ( $ post_id , $ content );
93
148
94
149
// Add the homepage to the site navigation.
95
150
$ this ->add_page_to_navigation ( $ post_id , $ title , get_permalink ( $ post_id ) );
0 commit comments