40000)return null; $plan=json_decode($value,false,16); if(json_last_error()!==JSON_ERROR_NONE || !is_object($plan))return null; $clean=array(); foreach(array('meta_title','meta_description','focus_keyword','optimized_intro') as $key) { $text=property_exists($plan,$key)?$plan->$key:''; if(!is_string($text))return null; $clean[$key]=in_array($key,array('meta_description','optimized_intro'),true)?sanitize_textarea_field($text):sanitize_text_field($text); } if($clean['focus_keyword']==='')return null; foreach(array('secondary_keywords','long_tail_keywords','recommendations') as $key) { $items=property_exists($plan,$key)?$plan->$key:array(); if(!is_array($items))return null; $clean[$key]=array(); foreach($items as $item) { if(!is_string($item))return null; if(count($clean[$key])<10)$clean[$key][]=sanitize_text_field(substr($item,0,1000)); } } $headings=property_exists($plan,'headings')?$plan->headings:array(); if(!is_array($headings))return null; $clean['headings']=array(); foreach($headings as $heading) { if(!is_object($heading) || !is_string($heading->level??null) || !in_array($heading->level,array('H1','H2','H3'),true) || !is_string($heading->text??null))return null; if(count($clean['headings'])<15)$clean['headings'][]=array('level'=>sanitize_text_field($heading->level),'text'=>sanitize_text_field($heading->text)); } return $clean; } class AI4Ranks_Editor_AI { public static function init() { add_action('wp_ajax_ai4ranks_optimize',array(__CLASS__,'request')); add_action('wp_ajax_ai4ranks_generate_field',array(__CLASS__,'field_request')); add_action('save_post',array(__CLASS__,'save'),20); } public static function list_action($id) { if(!current_user_can('edit_post',$id)) {echo '—';return;} if(!get_option('seoai_site_token','')) {echo 'Connect AI4Ranks first';return;} if(!in_array(get_post_type($id),array('post','page','product'),true)) {echo '—';return;} $url=add_query_arg(array('ai4ranks_run'=>'1','ai4ranks_run_nonce'=>wp_create_nonce('ai4ranks_run_'.$id)),get_edit_post_link($id,'raw')); echo 'AI Optimize '; } public static function field_request() { if(!is_string($_REQUEST['nonce']??null))wp_send_json_error(array('message'=>'Invalid request.'),403); check_ajax_referer('ai4ranks_optimize','nonce'); $id=is_string($_POST['post_id']??null)?absint($_POST['post_id']):0; if(!$id || !current_user_can('edit_post',$id))wp_send_json_error(array('message'=>'Not allowed'),403); $token=get_option('seoai_site_token','');if(!$token)wp_send_json_error(array('message'=>'Connect AI4Ranks first.'),401); $type=get_post_type($id);$field=is_string($_POST['field']??null)?sanitize_key(wp_unslash($_POST['field'])):''; if(!in_array($type,array('post','page','product'),true)||!in_array($field,array('title','content','excerpt','meta_title','meta_description','focus_keyword','keywords','social_title','social_description'),true))wp_send_json_error(array('message'=>'Unsupported field'),400); $body=array('site_token'=>$token,'field'=>$field,'post_type'=>$type); foreach(array('title','content','current') as $key){if(isset($_POST[$key])&&!is_string($_POST[$key]))wp_send_json_error(array('message'=>'Content is too large.'),400);$body[$key]=sanitize_textarea_field(wp_unslash($_POST[$key]??''));if(strlen($body[$key])>100000)wp_send_json_error(array('message'=>'Content is too large.'),400);} $r=wp_remote_post(SEOAI_PLATFORM_URL.'/api/plugin/generate-field',array('headers'=>array('Content-Type'=>'application/json'),'body'=>wp_json_encode($body),'timeout'=>90,'sslverify'=>true)); if(is_wp_error($r))wp_send_json_error(array('message'=>'Could not reach AI4Ranks. Try again.'),502); $data=json_decode(wp_remote_retrieve_body($r),true); if(wp_remote_retrieve_response_code($r)!==200 || !is_array($data) || !is_string($data['text']??null))wp_send_json_error(array('message'=>is_string($data['detail']??null)?sanitize_textarea_field($data['detail']):'Generation failed.'),400); wp_send_json_success(array('text'=>sanitize_textarea_field($data['text']))); } public static function request() { if(!is_string($_REQUEST['nonce']??null))wp_send_json_error(array('message'=>'Invalid request.'),403); check_ajax_referer('ai4ranks_optimize','nonce'); $id=is_string($_POST['post_id']??null)?absint($_POST['post_id']):0; if(!$id || !current_user_can('edit_post',$id))wp_send_json_error(array('message'=>'Not allowed'),403); $token=get_option('seoai_site_token',''); if(!$token)wp_send_json_error(array('message'=>'Connect your website first.'),401); $content=is_string($_POST['content']??null)?wp_kses_post(wp_unslash($_POST['content'])):''; if(strlen($content)>100000 || strlen(trim(wp_strip_all_tags($content)))<30)wp_send_json_error(array('message'=>'Add at least 30 characters of content. The editor limit is 100,000 bytes.'),400); $body=array('site_token'=>$token,'content'=>$content,'post_title'=>is_string($_POST['title']??null)?sanitize_text_field(wp_unslash($_POST['title'])):'','post_url'=>get_permalink($id),'meta_title'=>is_string($_POST['meta_title']??null)?sanitize_text_field(wp_unslash($_POST['meta_title'])):'','meta_description'=>is_string($_POST['meta_description']??null)?sanitize_textarea_field(wp_unslash($_POST['meta_description'])):''); $response=wp_remote_post(SEOAI_PLATFORM_URL.'/api/plugin/optimize',array('headers'=>array('Content-Type'=>'application/json'),'body'=>wp_json_encode($body),'timeout'=>90,'sslverify'=>true)); if(is_wp_error($response))wp_send_json_error(array('message'=>'AI4Ranks could not be reached. No changes were applied.'),502); $data=json_decode(wp_remote_retrieve_body($response),true); if(wp_remote_retrieve_response_code($response)!==200 || empty($data['proposal']))wp_send_json_error(array('message'=>isset($data['detail']) && is_string($data['detail']) ? $data['detail'] : 'Optimization failed. No changes were applied.'),400); wp_send_json_success($data); } public static function render($post) { if(!get_option('seoai_site_token',''))return; wp_enqueue_script('ai4ranks-editor-ai',plugins_url('../assets/editor-ai.js',__FILE__),array(),(string)filemtime(__DIR__.'/../assets/editor-ai.js'),true); wp_enqueue_script('ai4ranks-field-ai',plugins_url('../assets/field-ai.js',__FILE__),array('ai4ranks-editor-ai'),(string)filemtime(__DIR__.'/../assets/field-ai.js'),true); wp_nonce_field('ai4ranks_editor_plan','ai4ranks_editor_plan_nonce'); $autorun=isset($_GET['ai4ranks_run']) && $_GET['ai4ranks_run']==='1' && is_string($_GET['ai4ranks_run_nonce']??null) && current_user_can('edit_post',$post->ID) && wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['ai4ranks_run_nonce'])),'ai4ranks_run_'.$post->ID); $plan=get_post_meta($post->ID,'_ai4ranks_optimization_plan',true); ?>

AI optimization workspace

Analyze the current editor content and generate metadata, keyword groups, a heading outline and content improvements in one request. Uses your account's AI connection and usage limits.

Apply prepares the SEO fields and keyword plan. Save or update this post to persist them. Heading and opening suggestions can be used when editing your content.