'', // If empty, will try to extract from content or use topic 'post_status' => 'draft', // draft, publish, future 'category_id' => null, // null = use default category 'tags' => '', // Comma-separated tags 'author_id' => null // null = current user ); } /** * Execute the step * * @param AIGH_Pipeline_Context $context * @return AIGH_Pipeline_Context|WP_Error */ public function execute($context) { $this->log_start($context); // Get content $content = $this->get_from_context($context, 'content'); if (empty($content)) { $this->log_end($context, false); return $this->create_error( 'empty_content', 'No content available to save' ); } // Determine title $title = $this->determine_title($context); if (empty($title)) { $this->log_end($context, false); return $this->create_error( 'empty_title', 'Could not determine a title for the post' ); } // Prepare post data $post_data = array( 'post_title' => $title, 'post_content' => wp_kses_post($content), 'post_status' => $this->get_config_value('post_status', 'draft'), 'post_author' => $this->get_config_value('author_id') ?: get_current_user_id(), 'post_type' => 'post' ); // Set category $category_id = $this->get_config_value('category_id'); if (!$category_id) { $category_id = get_option('aigh_default_category', 1); } $post_data['post_category'] = array($category_id); // Create the post $post_id = wp_insert_post($post_data, true); if (is_wp_error($post_id)) { $this->log_end($context, false); return $this->create_error( 'post_creation_failed', 'Failed to create post: ' . $post_id->get_error_message() ); } // Set tags if provided $tags = $this->get_config_value('tags', ''); if (empty($tags)) { // Try to get from context (might be set by research or planning steps) $tags = $this->get_from_context($context, 'keywords', ''); } if (!empty($tags)) { $tag_array = array_map('trim', explode(',', $tags)); wp_set_post_tags($post_id, $tag_array, false); } // Add post meta to track AI generation update_post_meta($post_id, '_aigh_generated', 1); update_post_meta($post_id, '_aigh_generated_date', current_time('mysql')); // Store execution ID if available $execution_id = $context->get_metadata('execution_id'); if ($execution_id) { update_post_meta($post_id, '_aigh_execution_id', $execution_id); } // Store pipeline info if available $pipeline_id = $context->get_metadata('pipeline_id'); if ($pipeline_id) { update_post_meta($post_id, '_aigh_pipeline_id', $pipeline_id); } // Get edit URL $edit_url = get_edit_post_link($post_id, 'raw'); // Store results in context $this->set_in_context($context, 'post_id', $post_id); $this->set_in_context($context, 'edit_url', $edit_url); $this->set_in_context($context, 'post_title', $title); $this->set_in_context($context, 'saved_at', current_time('mysql')); $this->log_end($context, true); return $context; } /** * Determine the title for the post * * @param AIGH_Pipeline_Context $context * @return string */ private function determine_title($context) { // First, check if title is in config $title = $this->get_config_value('title', ''); if (!empty($title)) { return sanitize_text_field($title); } // Second, check if title is in context (might be set by earlier step) $title = $this->get_from_context($context, 'title', ''); if (!empty($title)) { return sanitize_text_field($title); } // Third, try to extract from content (look for first H1 or H2) $content = $this->get_from_context($context, 'content', ''); if (!empty($content)) { // Try H1 first if (preg_match('/]*>(.*?)<\/h1>/is', $content, $matches)) { return sanitize_text_field(wp_strip_all_tags($matches[1])); } // Try H2 if (preg_match('/]*>(.*?)<\/h2>/is', $content, $matches)) { return sanitize_text_field(wp_strip_all_tags($matches[1])); } } // Fourth, use topic as fallback $topic = $this->get_from_context($context, 'topic', ''); if (!empty($topic)) { // Truncate if too long $title = substr($topic, 0, 100); if (strlen($topic) > 100) { $title .= '...'; } return sanitize_text_field($title); } // Last resort return 'AI Generated Article - ' . gmdate('Y-m-d H:i'); } /** * Get configuration schema for UI rendering * * @return array */ public function get_config_schema() { return array( 'title' => array( 'type' => 'text', 'label' => 'Post Title', 'description' => 'Leave empty to auto-detect from content or topic', 'required' => false ), 'post_status' => array( 'type' => 'select', 'label' => 'Post Status', 'description' => 'What status should the post have?', 'options' => array( 'draft' => 'Draft', 'publish' => 'Publish Immediately', 'pending' => 'Pending Review' ), 'default' => 'draft' ), 'tags' => array( 'type' => 'text', 'label' => 'Tags', 'description' => 'Comma-separated tags (optional)', 'required' => false ) ); } }