Data Flow Explanation
- Form Input (HTML):
- The shortcode
You do not have permission to generate posts.
renders a form with an input field for the issuer:html<input type=”text” id=”grok_topic” name=”grok_topic” value=”Enter the Issuer” class=”regular-text” required> - Users enter the issuer name (e.g., “City of New York”) into this field, which has the ID grok_topic.
- The shortcode
- JavaScript (grok-ai-deal-post-frontend.js):
- When the “Generate Deal Post” button is clicked, the JavaScript in js/grok-ai-deal-post-frontend.js captures the input value:
javascriptconst topic = topicInput.value.trim();
- Here, topicInput is the DOM element document.getElementById(‘grok_topic’).
- The script validates that the input isn’t empty or the default value (“Enter the Issuer”):
javascriptif (!topic || topic === grokAiFrontendSettings.defaultTopic) {messageDiv.innerHTML = ‘<div class=”error”><p>’ + grokAiFrontendSettings.errorMessage + ‘</p></div>’;return;}
- The issuer name (topic) is sent to the server via an AJAX request:
javascriptconst formData = new FormData();formData.append(‘action’, ‘grok_generate_deal_post’);formData.append(‘grok_topic’, topic);formData.append(‘nonce’, grokAiFrontendSettings.nonce);fetch(grokAiFrontendSettings.ajaxurl, {method: ‘POST’,body: formData})
- When the “Generate Deal Post” button is clicked, the JavaScript in js/grok-ai-deal-post-frontend.js captures the input value:
- AJAX Handler (PHP):
- The AJAX request targets the grok_generate_deal_post action, handled by the grok_generate_deal_post_ajax function in grok-ai-post-generator.php:
phpfunction grok_generate_deal_post_ajax() {check_ajax_referer(‘grok_ai_deal_post_nonce’, ‘nonce’);if (!current_user_can(‘publish_posts’)) {wp_send_json_error(array(‘message’ => __(‘Insufficient permissions.’, ‘grok-ai-post-generator’)));}$topic = isset($_POST[‘grok_topic’]) ? sanitize_text_field($_POST[‘grok_topic’]) : ”;if (empty($topic) || $topic === __(‘Enter the Issuer’, ‘grok-ai-post-generator’)) {wp_send_json_error(array(‘message’ => __(‘Please enter a valid issuer.’, ‘grok-ai-post-generator’)));}$result = grok_ai_generate_post($topic);if (is_wp_error($result)) {wp_send_json_error(array(‘message’ => esc_html($result->get_error_message())));}wp_send_json_success($result);}
- The issuer name is retrieved from $_POST[‘grok_topic’], sanitized using sanitize_text_field(), and stored in $topic.
- The AJAX request targets the grok_generate_deal_post action, handled by the grok_generate_deal_post_ajax function in grok-ai-post-generator.php:
- Post Generation (grok_ai_generate_post):
- The $topic value is passed to the grok_ai_generate_post function:
php$result = grok_ai_generate_post($topic);
- Inside grok_ai_generate_post, the issuer name is sanitized again and used to replace {issuer} in the default deal prompt:
php$issuer = sanitize_text_field($topic);$default_deal_prompt = “Generate a professional, detailed financial status and summary report for {issuer}, tailored for a financial desk or investor newsletter. …”;$prompt = empty($custom_prompt) ? $default_deal_prompt : wp_kses_post($custom_prompt);$prompt = str_replace(‘{issuer}’, $issuer, $prompt);
- The prompt, with {issuer} replaced (e.g., “City of New York”), is sent to the xAI API via grok_ai_generate_content:
php$generated_content = grok_ai_generate_content($prompt, $api_key);
- The $topic value is passed to the grok_ai_generate_post function:
- API Call (grok_ai_generate_content):
- The grok_ai_generate_content function sends the prompt to the xAI API:
php$response = wp_remote_post(‘https://api.x.ai/v1/completions’, array(‘headers’ => array(‘Authorization’ => ‘Bearer ‘ . $api_key,‘Content-Type’ => ‘application/json’,),‘body’ => json_encode(array(‘model’ => ‘grok-3’,‘prompt’ => $prompt,‘max_tokens’ => 4000,‘temperature’ => 0.8,)),‘timeout’ => 180,‘redirection’ => 5,‘httpversion’ => ‘1.1’,));
- The API processes the prompt with the issuer name and returns the generated content, which is then used to create a WordPress post.
- The grok_ai_generate_content function sends the prompt to the xAI API:
- Post Creation and URL Return:
- The grok_ai_generate_post function creates a post with the API response, using the issuer name as the post title and the generated content (converted from Markdown to HTML via Parsedown) as the post content:
php$post_data = array(‘post_title’ => $issuer,‘post_content’ => $html_content,‘post_status’ => $post_status,‘post_type’ => ‘post’,‘post_author’ => get_current_user_id(),‘post_category’ => [$category_id],‘meta_input’ => [‘_thumbnail_id’ => $featuredImageId,],);$post_id = wp_insert_post($post_data);
- The function returns an array with the post ID, post URL, and edit URL:
phpreturn array(‘post_id’ => $post_id,‘post_url’ => get_permalink($post_id),‘edit_url’ => get_edit_post_link($post_id),);
- The AJAX handler sends this array back to the JavaScript, which populates the grok_post_url input field with the post URL:
javascriptif (data.success) {postUrlInput.value = data.data.post_url;}
- The grok_ai_generate_post function creates a post with the API response, using the issuer name as the post title and the generated content (converted from Markdown to HTML via Parsedown) as the post content:
Summary
The {issuer} placeholder gets its data from the <input id=”grok_topic”> field in the shortcode’s form. The flow is:
- User Input: User enters the issuer name (e.g., “City of New York”) in the form.
- JavaScript: Captures the input value (topicInput.value) and sends it via AJAX as grok_topic.
- PHP (AJAX Handler): Retrieves $_POST[‘grok_topic’], sanitizes it, and passes it to grok_ai_generate_post.
- PHP (Post Generation): Sanitizes the issuer name again, replaces {issuer} in the default deal prompt, and sends the prompt to the xAI API.
- API: Generates content based on the prompt with the issuer name.
- Post Creation: Creates a post and returns its URL, which JavaScript displays in the grok_post_url input field.