'1','post_types'=>[],'separator'=>'—','home_title'=>'','home_description'=>'','home_keywords'=>'','title_template'=>'%title% %sep% %sitename%','archive_title_template'=>'%term% %sep% %sitename%','default_schema'=>'WebPage','breadcrumbs'=>'1','keywords'=>'1']; } private static function type_defaults($type) { return ['show_in_search'=>'1','title'=>'%title% %sep% %sitename%','description'=>'','social_image_id'=>0,'social_image_url'=>'','social_title'=>'%title%','social_description'=>'','page_type'=>'WebPage','article_type'=>$type==='post'?'Article':'None','controls'=>'1','custom_fields'=>'']; } public static function settings() { $saved=get_option(self::OPTION,[]); $s=wp_parse_args(is_array($saved)?$saved:[],self::defaults()); if(empty($s['post_types'])) $s['post_types']=array_values(get_post_types(['public'=>true],'names')); foreach(get_post_types(['public'=>true],'names') as $type)if($type!=='attachment'){ $s['types'][$type]=wp_parse_args($s['types'][$type]??[],self::type_defaults($type)); $s['types'][$type]['controls']='1'; } return $s; } public static function register_settings() { register_setting('ai_seo_cro_seo',self::OPTION,['sanitize_callback'=>[__CLASS__,'sanitize_settings']]); } public static function sanitize_settings($v) { $v=is_array($v)?$v:[]; $types=get_post_types(['public'=>true],'names'); $clean=[];foreach($types as $type){if($type==='attachment')continue;$r=(array)($v['types'][$type]??[]);$clean[$type]=['show_in_search'=>empty($r['show_in_search'])?'0':'1','title'=>sanitize_text_field($r['title']??''),'description'=>sanitize_textarea_field($r['description']??''),'social_image_id'=>absint($r['social_image_id']??0),'social_image_url'=>esc_url_raw($r['social_image_url']??''),'social_title'=>sanitize_text_field($r['social_title']??''),'social_description'=>sanitize_textarea_field($r['social_description']??''),'page_type'=>in_array($r['page_type']??'',['WebPage','ItemPage','AboutPage','ContactPage'],true)?$r['page_type']:'WebPage','article_type'=>in_array($r['article_type']??'',['None','Article','BlogPosting','NewsArticle'],true)?$r['article_type']:'None','controls'=>'1','custom_fields'=>sanitize_text_field($r['custom_fields']??'')];} return ['enabled'=>empty($v['enabled'])?'0':'1','post_types'=>array_values(array_intersect(array_map('sanitize_key',$v['post_types']??[]),$types)),'separator'=>sanitize_text_field($v['separator']??'—'),'home_title'=>sanitize_text_field($v['home_title']??''),'home_description'=>sanitize_textarea_field($v['home_description']??''),'home_keywords'=>sanitize_text_field($v['home_keywords']??''),'title_template'=>sanitize_text_field($v['title_template']??''),'archive_title_template'=>sanitize_text_field($v['archive_title_template']??''),'default_schema'=>in_array($v['default_schema']??'',['WebPage','Article','BlogPosting'],true)?$v['default_schema']:'WebPage','breadcrumbs'=>empty($v['breadcrumbs'])?'0':'1','keywords'=>empty($v['keywords'])?'0':'1','types'=>$clean]; } public static function menu() { add_submenu_page('360-seo-dashboard','SEO Settings','SEO Settings','manage_options','360-seo-seo-settings',[__CLASS__,'settings_page']); } public static function plugin_links($links) { array_unshift($links,'SEO Settings');return $links; } public static function media_assets($hook) { if(in_array($hook,['post.php','post-new.php','360-seo_page_360-seo-seo-settings'],true))wp_enqueue_media();if($hook==='360-seo_page_360-seo-seo-settings')wp_enqueue_script('360-seo-seo-settings',AI_SEO_CRO_URL.'assets/js/seo-settings.js',['jquery'],AI_SEO_CRO_VERSION,true); } private static function field($label,$key,$s,$textarea=false) { echo ''; } public static function settings_page() { if(!current_user_can('manage_options'))return; $s=self::settings(); echo '
'; echo '
Search appearance

SEO Settings

Control how your site and content appear in search results and on social networks.

'; echo '
'; settings_fields('ai_seo_cro_seo'); echo '

Site defaults

Set fallback metadata used across your website.

'; self::field('Homepage title','home_title',$s); self::field('Homepage description','home_description',$s,true); self::field('Homepage keywords','home_keywords',$s); self::field('Title template','title_template',$s); self::field('Archive template','archive_title_template',$s); self::field('Title separator','separator',$s); echo '
Available variables%title%%term%%sitename%%sep%
'; echo '

SEO features

Core features are enabled site-wide.

'; foreach(['enabled'=>['Managed metadata','Output optimized titles and metadata.'],'breadcrumbs'=>['Breadcrumb schema','Add structured breadcrumb data to supported pages.'],'keywords'=>['Meta keywords','Allow focus keywords in page metadata.']] as $k=>$feature){ echo ''; } echo '
'; echo '
Content types

Search and social appearance

Customize defaults for each public post type.

'; self::type_sections($s); echo '
Save your changes to apply them across the site.'; submit_button('Save SEO settings','primary','submit',false); echo '
'; } private static function type_sections($s) { foreach(get_post_types(['public'=>true],'objects') as $o){ if($o->name==='attachment')continue; $d=$s['types'][$o->name];$b=self::OPTION.'[types]['.$o->name.']'; echo '

'.esc_html($o->labels->name).'

Defaults for '.esc_html(strtolower($o->labels->name)).'.

'; echo '

Search appearance

'; self::type_field('SEO title template',$b,'title',$d['title']);self::type_field('Meta description',$b,'description',$d['description'],true); echo '
';self::type_social_fields($b,$d);echo '
'; echo '
';self::type_schema_fields($b,$d);echo '
'; } } private static function type_social_fields($b,$d) { echo '

Social appearance

Set the default image and copy used when content is shared.

'.($d['social_image_url']?'':'No image selected').'
Recommended: 1200 × 675px
'; self::type_field('Social title',$b,'social_title',$d['social_title']);self::type_field('Social description',$b,'social_description',$d['social_description'],true); } private static function type_schema_fields($b,$d) { echo '

Schema and analysis

SEO controls and assessments enabledAvailable in the editor for this content type.
'; self::type_field('Custom fields for analysis',$b,'custom_fields',$d['custom_fields']); } public static function meta_boxes() { $s=self::settings(); foreach(get_post_types(['public'=>true],'names') as $type){ if($type==='attachment'||($s['types'][$type]['controls']??'1')!=='1')continue; add_meta_box('360-seo-meta','AI SEO Settings',[__CLASS__,'meta_box'],$type,'normal','high',[ '__block_editor_compatible_meta_box'=>true, ]); } } private static function type_field($label,$base,$key,$value,$textarea=false) { echo ''; } private static function preview_url($post) { if(function_exists('get_sample_permalink')){$sample=get_sample_permalink($post->ID,$post->post_title,$post->post_name);if(is_array($sample)){$slug=$sample[1]?:sanitize_title($post->post_title);return str_replace(['%postname%','%pagename%'],$slug,$sample[0]);}} return get_permalink($post); } private static function site_icon() { $url=get_site_icon_url(64);if($url)return $url;$logo=(int)get_theme_mod('custom_logo');return $logo?(string)wp_get_attachment_image_url($logo,'thumbnail'):''; } private static function preview_description($value) { $value=trim(wp_strip_all_tags((string)$value));if(stripos($value,'Excerpts are optional hand-crafted summaries')!==false)return '';return $value; } public static function post_data($id) { $v=get_post_meta($id,'_ai_seo_cro_meta',true); return wp_parse_args(is_array($v)?$v:[],['title'=>'','description'=>'','keyword'=>'','synonyms'=>'','related'=>'','canonical'=>'','schema'=>'Default','noindex'=>'0','nofollow'=>'0','robots_advanced'=>'','breadcrumb_title'=>'','cornerstone'=>'0','social_image_id'=>0,'social_image_url'=>'']); } private static function meta_field($label,$key,$d,$textarea=false) { echo ''; } public static function meta_box($post) { $d=self::post_data($post->ID); if($d['title']==='')$d['title']=$post->post_title; $score=self::score($post,$d);$preview_url=self::preview_url($post);$description=self::preview_description($d['description']);$icon=self::site_icon(); include AI_SEO_CRO_PATH.'views/seo-editor-view.php'; } public static function meta_box_legacy($post) { wp_nonce_field(self::NONCE,self::NONCE);$d=self::post_data($post->ID);$score=self::score($post,$d);$preview_url=self::preview_url($post);$icon=self::site_icon();echo '
SEO score'.esc_html($score).'%

Search result preview

'.($icon?'seo-serp-site':'').''.esc_html(get_bloginfo('name')).''.esc_html(wp_parse_url(home_url('/'),PHP_URL_HOST)).'
'.esc_html($preview_url).''.esc_html($d['title']?:$post->post_title).'

- '.esc_html($d['description']).'

'; self::social_panel($post,$d);self::meta_field('SEO title','title',$d); self::meta_field('Meta description','description',$d,true); self::meta_field('Focus keyphrase','keyword',$d); self::meta_field('Synonyms','synonyms',$d); self::meta_field('Related keyphrase','related',$d); echo '';self::analysis_panels($d);echo '
'; } private static function social_panel($post,$d) { $image=$d['social_image_url'];if($image===''&&has_post_thumbnail($post))$image=get_the_post_thumbnail_url($post,'large'); echo '

Social media appearance

Determine how this post should look when shared on Facebook, X, Instagram, WhatsApp, Threads, LinkedIn, Slack, and more.

Social share preview

'.($image!==''?'':'No image selected').'
'.esc_html($d['title']?:$post->post_title).'

'.esc_html($d['description']).'

'.esc_html(wp_parse_url(home_url('/'),PHP_URL_HOST)).'

'; } private static function analysis_panels($d) { echo '

Enter a focus keyphrase to calculate the SEO score.

'; } public static function save_post($id) { if(!isset($_POST[self::NONCE])||!wp_verify_nonce(sanitize_text_field(wp_unslash($_POST[self::NONCE])),self::NONCE)||!current_user_can('edit_post',$id)||wp_is_post_autosave($id)||wp_is_post_revision($id))return; $r=map_deep((array)wp_unslash($_POST['ai_seo_cro_meta']??[]),'sanitize_textarea_field');$r['title']=mb_substr((string)($r['title']??''),0,60);$r['description']=mb_substr((string)($r['description']??''),0,160); $schemas=['Default','WebPage','Article','BlogPosting','FAQPage','Product','LocalBusiness','JobPosting','VideoObject']; update_post_meta($id,'_ai_seo_cro_meta',['title'=>sanitize_text_field($r['title']??''),'description'=>sanitize_textarea_field($r['description']??''),'keyword'=>sanitize_text_field($r['keyword']??''),'synonyms'=>sanitize_text_field($r['synonyms']??''),'related'=>sanitize_text_field($r['related']??''),'canonical'=>esc_url_raw($r['canonical']??''),'schema'=>in_array($r['schema']??'',$schemas,true)?$r['schema']:'Default','noindex'=>empty($r['noindex'])?'0':'1','nofollow'=>empty($r['nofollow'])?'0':'1','robots_advanced'=>sanitize_text_field($r['robots_advanced']??''),'breadcrumb_title'=>sanitize_text_field($r['breadcrumb_title']??''),'cornerstone'=>empty($r['cornerstone'])?'0':'1','social_image_id'=>absint($r['social_image_id']??0),'social_image_url'=>esc_url_raw($r['social_image_url']??'')]); } public static function score($post,$d) { $score=0; $title=$d['title']?:$post->post_title; $desc=$d['description']?:wp_trim_words($post->post_content,25,''); $key=mb_strtolower(trim($d['keyword'])); $body=mb_strtolower(wp_strip_all_tags($post->post_content)); if(mb_strlen($title)>=30&&mb_strlen($title)<=60)$score+=20; if(mb_strlen($desc)>=120&&mb_strlen($desc)<=160)$score+=20; if($key!==''){$score+=15;if(str_contains(mb_strtolower($title),$key))$score+=15;if(str_contains(mb_strtolower($desc),$key))$score+=10;if(str_contains($body,$key))$score+=10;} if(has_post_thumbnail($post))$score+=5;if(preg_match('/post_content))$score+=5;return min(100,$score); } // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Bounded cached lookup of posts with SEO metadata for duplicate keyword detection. public static function editor_assets($hook) { if(!in_array($hook,['post.php','post-new.php'],true))return;global $post;$used=[];$ids=get_posts(['post_type'=>'any','post_status'=>'any','posts_per_page'=>101,'fields'=>'ids','meta_key'=>'_ai_seo_cro_meta']);foreach($ids as $id){if($post && (int)$id===(int)$post->ID)continue;$key=self::post_data($id)['keyword'];if($key!=='')$used[]=mb_strtolower($key);}wp_enqueue_style('360-seo-admin-css',AI_SEO_CRO_URL.'assets/css/admin.css',[],AI_SEO_CRO_VERSION);wp_enqueue_script('360-seo-seo-editor',AI_SEO_CRO_URL.'assets/js/seo-editor.js',[],AI_SEO_CRO_VERSION,true);wp_localize_script('360-seo-seo-editor','AI_SEO_EDITOR',['siteTitle'=>get_bloginfo('name'),'separator'=>self::settings()['separator'],'homeUrl'=>home_url('/'),'usedKeywords'=>array_values(array_unique($used))]); } public static function filter_title($title) { if(is_admin()||self::settings()['enabled']!=='1')return $title; $custom=''; if(is_singular())$custom=self::post_data(get_queried_object_id())['title']; elseif(is_front_page()||is_home())$custom=self::settings()['home_title']; elseif(is_category()||is_tag()||is_tax())$custom=(string)get_term_meta(get_queried_object_id(),'_ai_seo_cro_title',true); if($custom===''){if(is_singular()){$type=get_post_type();$custom=self::settings()['types'][$type]['title']??self::settings()['title_template'];}elseif(is_archive())$custom=self::settings()['archive_title_template'];} return $custom!==''?self::variables($custom):$title; } public static function current_description() { if(is_singular()){$v=self::post_data(get_queried_object_id())['description'];return $v!==''?$v:(self::settings()['types'][get_post_type()]['description']??'');}if(is_front_page()||is_home())return self::settings()['home_description'];if(is_category()||is_tag()||is_tax())return(string)get_term_meta(get_queried_object_id(),'_ai_seo_cro_description',true);return ''; } public static function current_canonical() { return is_singular()?self::post_data(get_queried_object_id())['canonical']:''; } public static function current_social_image() { if(!is_singular())return '';$d=self::post_data(get_queried_object_id());if(absint($d['social_image_id'])){$url=wp_get_attachment_image_url(absint($d['social_image_id']),'full');if($url)return esc_url_raw($url);}return esc_url_raw($d['social_image_url']?:self::settings()['types'][get_post_type()]['social_image_url']??''); } public static function current_social_title() { if(!is_singular())return '';return self::variables(self::settings()['types'][get_post_type()]['social_title']??''); } public static function current_social_description() { if(!is_singular())return '';return self::variables(self::settings()['types'][get_post_type()]['social_description']??''); } public static function schema_type() { if(!is_singular())return '';$v=self::post_data(get_queried_object_id())['schema'];if($v!=='Default')return $v;$d=self::settings()['types'][get_post_type()]??[];return ($d['article_type']??'None')!=='None'?$d['article_type']:($d['page_type']??self::settings()['default_schema']); } private static function variables($v) { $category='';if(is_singular('post')){$c=get_the_category();if($c)$category=$c[0]->name;}return strtr($v,['%title%'=>is_singular()?get_the_title():'','%term%'=>is_archive()?wp_strip_all_tags(get_the_archive_title()):'','%sitename%'=>get_bloginfo('name'),'%sep%'=>self::settings()['separator'],'%primary_category%'=>$category]); } public static function filter_robots($robots) { if(is_singular()&&(self::settings()['types'][get_post_type()]['show_in_search']??'1')==='0'){unset($robots['index']);$robots['noindex']=true;}return $robots; } public static function sitemap_args($args,$type) { if((self::settings()['types'][$type]['show_in_search']??'1')==='0')$args['post__in']=[0];return $args; } public static function output_extra_meta() { if(self::settings()['enabled']!=='1')return; $keywords=''; if(is_singular())$keywords=self::post_data(get_queried_object_id())['keyword'];elseif(is_front_page()||is_home())$keywords=self::settings()['home_keywords'];elseif(is_archive())$keywords=(string)get_term_meta(get_queried_object_id(),'_ai_seo_cro_keyword',true); if($keywords!==''&&self::settings()['keywords']==='1')echo ''; if(is_singular()){$d=self::post_data(get_queried_object_id());$robots=[];if($d['noindex']==='1')$robots[]='noindex';if($d['nofollow']==='1')$robots[]='nofollow';if($d['robots_advanced']!=='')$robots[]=sanitize_text_field($d['robots_advanced']);if($robots)echo '';} } public static function breadcrumbs() { $items=[['Home',home_url('/')]];if(is_singular()){$p=get_queried_object();if($p&&$p->post_type==='post'){$c=get_the_category($p->ID);if($c)$items[]=[$c[0]->name,get_category_link($c[0])];}if($p&&$p->post_parent)$items[]=[get_the_title($p->post_parent),get_permalink($p->post_parent)];$d=self::post_data($p->ID);$items[]=[$d['breadcrumb_title']?:get_the_title($p),get_permalink($p)];}elseif(is_archive())$items[]=[wp_strip_all_tags(get_the_archive_title()),''];return $items; } public static function breadcrumb_shortcode() { if(self::settings()['breadcrumbs']!=='1')return '';$items=self::breadcrumbs();$out=[];foreach($items as $i=>$v)$out[]=$v[1]&&$i'.esc_html($v[0]).'':''.esc_html($v[0]).'';return ''; } public static function output_breadcrumb_schema() { if(self::settings()['breadcrumbs']!=='1'||is_front_page())return;$list=[];foreach(self::breadcrumbs() as $i=>$v)$list[]=['@type'=>'ListItem','position'=>$i+1,'name'=>$v[0],'item'=>$v[1]];echo ''; } public static function term_hooks() { foreach(get_taxonomies(['public'=>true],'names') as $tax){add_action($tax.'_add_form_fields',[__CLASS__,'term_add_fields']);add_action($tax.'_edit_form_fields',[__CLASS__,'term_edit_fields']);add_action('created_'.$tax,[__CLASS__,'save_term']);add_action('edited_'.$tax,[__CLASS__,'save_term']);} } public static function term_add_fields() { wp_nonce_field('ai_seo_cro_term','ai_seo_cro_term_nonce');echo '
'; } public static function term_edit_fields($term) { wp_nonce_field('ai_seo_cro_term','ai_seo_cro_term_nonce');foreach(['title'=>'SEO title','description'=>'Meta description','keyword'=>'Focus keywords'] as $key=>$label){$v=get_term_meta($term->term_id,'_ai_seo_cro_'.$key,true);echo ''.esc_html($label).''.($key==='description'?'':'').'';} } public static function save_term($id) { if(!isset($_POST['ai_seo_cro_term_nonce'])||!wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['ai_seo_cro_term_nonce'])),'ai_seo_cro_term'))return;foreach(['title','description','keyword'] as $k)update_term_meta($id,'_ai_seo_cro_'.$k,sanitize_textarea_field(wp_unslash($_POST['ai_seo_cro_'.$k]??''))); } } AI_SEO_CRO_SEO_Manager::init();