plugin = ai_seo_article_generator(); $this->init_hooks(); } private function init_hooks() { add_filter('ai_seo_article_generator_generated_content', array($this, 'fix_content_structure'), 5, 2); add_filter('ai_seo_article_generator_content_prompt', array($this, 'enhance_content_prompt_for_html'), 20, 2); } /** * Fix content structure issues */ public function fix_content_structure($content, $data) { // Check if content lacks HTML structure if ($this->content_lacks_html($content)) { $content = $this->add_html_structure($content, $data); } // Fix truncation issues if ($this->is_content_truncated($content, $data)) { $content = $this->complete_truncated_content($content, $data); } // Ensure proper paragraph structure $content = $this->fix_paragraph_structure($content); // Add missing HTML elements $content = $this->add_missing_html_elements($content); return $content; } /** * Enhance content prompt to emphasize HTML structure */ public function enhance_content_prompt_for_html($prompt, $data) { $html_emphasis = "\n\n馃毃 讚专讬砖转 HTML 讞讜讘讛:\n"; $html_emphasis .= "- 讛砖转诪砖 专拽 讘转讙讬讜转 HTML 转拽讬谞讜转:
,
转讜讻谉 讛驻住拽讛 讛专讗砖讜谞讛 注诐 讟拽住讟 讘讜诇讟.
\n"; $html_emphasis .= "" . trim($section['content']) . "
\n\n"; break; case 'list': $structured_content .= $this->convert_to_html_list($section['content']) . "\n\n"; break; default: $structured_content .= "" . trim($section['content']) . "
\n\n"; } } } return $structured_content; } /** * Identify content sections */ private function identify_content_sections($content) { $sections = array(); $lines = explode("\n", $content); foreach ($lines as $line) { $line = trim($line); if (empty($line)) continue; $section = array(); // Check if it's a heading if ($this->is_heading_line($line)) { $section['type'] = 'heading'; $section['content'] = $this->clean_heading($line); } // Check if it's a list item elseif ($this->is_list_item($line)) { $section['type'] = 'list'; $section['content'] = $line; } // Regular paragraph else { $section['type'] = 'paragraph'; $section['content'] = $line; } $sections[] = $section; } // Merge consecutive list items $sections = $this->merge_list_items($sections); return $sections; } /** * Check if line is a heading */ private function is_heading_line($line) { // Check for common heading patterns $heading_patterns = array( '/^#{1,6}\s+/', // Markdown headers '/^[讗-转].{10,80}[:?]$/', // Hebrew questions/statements '/^[A-Z].{10,80}[:?]$/', // English questions/statements '/^\d+\.\s*[讗-转].{5,60}$/', // Numbered sections (Hebrew) '/^\d+\.\s*[A-Z].{5,60}$/', // Numbered sections (English) ); foreach ($heading_patterns as $pattern) { if (preg_match($pattern, $line)) { return true; } } return false; } /** * Check if line is a list item */ private function is_list_item($line) { $list_patterns = array( '/^[-*鈥\s+/', // Bullet points '/^\d+\.\s+/', // Numbered lists '/^[讗-转]\.\s+/', // Hebrew numbered lists ); foreach ($list_patterns as $pattern) { if (preg_match($pattern, $line)) { return true; } } return false; } /** * Clean heading text */ private function clean_heading($line) { // Remove markdown symbols $cleaned = preg_replace('/^#{1,6}\s*/', '', $line); // Remove leading numbers $cleaned = preg_replace('/^\d+\.\s*/', '', $cleaned); return trim($cleaned); } /** * Determine heading level */ private function determine_heading_level($content) { // First heading should be H2 (WordPress post title is H1) static $heading_count = 0; $heading_count++; if ($heading_count <= 3) { return 2; // Main sections } else { return 3; // Subsections } } /** * Convert text to HTML list */ private function convert_to_html_list($content) { $items = explode("\n", $content); $html = "诇住讬讻讜诐, 讛谞讜砖讗 讛讝讛 诪爪讬讙 讛讝讚诪谞讜讬讜转 专讘讜转 讜诪讙讜讜谞讜转. 讞砖讜讘 诇讛讘讬谉 讗转 讻诇 讛驻专讟讬诐 讜诇讛转讗讬诐 讗转 讛驻转专讜谉 诇爪专讻讬诐 讛住驻爪讬驻讬讬诐 砖诇 讻诇 诪拽专讛.
\n"; $completion .= "讛讛诪诇爪讛 讛讬讗 诇讛转讬讬注抓 注诐 诪讜诪讞讬诐 讘转讞讜诐 讜诇讘爪注 讘讚讬拽讜转 诪拽讬驻讜转 诇驻谞讬 拽讘诇转 讛讞诇讟讜转 诪砖诪注讜转讬讜转. 讝讛 讬讘讟讬讞 转讜爪讗讜转 讗讜驻讟讬诪诇讬讜转 讜诪讝注讜专 住讬讻讜谞讬诐.
"; } else { $completion = "\n\nIn summary, this topic presents numerous opportunities and considerations. It's important to understand all the details and tailor the solution to specific needs of each case.
\n"; $completion .= "The recommendation is to consult with experts in the field and conduct comprehensive assessments before making significant decisions. This ensures optimal results and risk minimization.
"; } $content .= $completion; } return $content; } /** * Fix paragraph structure */ private function fix_paragraph_structure($content) { // Split content into blocks $blocks = explode("\n\n", $content); $fixed_content = ''; foreach ($blocks as $block) { $block = trim($block); if (empty($block)) continue; // Skip if already has HTML structure if (preg_match('/^<[h|p|ul|ol|div]/', $block)) { $fixed_content .= $block . "\n\n"; continue; } // Wrap in paragraph tags if (!preg_match('/^', $block)) { $fixed_content .= "" . $block . "
\n\n"; } else { $fixed_content .= $block . "\n\n"; } } return $fixed_content; } /** * Add missing HTML elements */ private function add_missing_html_elements($content) { // Ensure we have at least one H2 if (!preg_match('/转讜讻谉 诪讘讜讗 诇谞讜砖讗.
\n\n" . $content; } // Ensure we have at least one list if (!preg_match('/<(ul|ol)/', $content)) { $list = ".*?<\/p>)/s', '$1' . "\n\n" . $list, $content, 1); } return $content; } }