true ), 'names' ) as $t ) {
if ( ! in_array( $t, array( 'attachment', 'revision', 'nav_menu_item' ), true ) ) {
$out[] = $t;
}
}
return $out;
}
private static function hierarchical_taxonomies( $post_type ) {
$canonical_taxes = array( 'product_cat', 'category' );
$out = array();
foreach ( get_object_taxonomies( $post_type, 'objects' ) as $tx ) {
if ( in_array( $tx->name, $canonical_taxes, true ) && ! empty( $tx->public ) && ! empty( $tx->hierarchical ) ) {
$out[ $tx->name ] = $tx;
}
}
return $out;
}
public static function add_box() {
foreach ( self::post_types() as $pt ) {
add_meta_box( self::BOX_ID, __( 'Rooster One SEO', 'ai-eshop-optimizer' ), array( __CLASS__, 'render' ), $pt, 'normal', 'high' );
}
}
public static function place_under_product_data( $order ) {
if ( ! is_array( $order ) || empty( $order['normal'] ) ) {
return $order;
}
foreach ( $order as $ids ) {
if ( in_array( self::BOX_ID, array_map( 'trim', explode( ',', (string) $ids ) ), true ) ) {
return $order;
}
}
$ids = explode( ',', (string) $order['normal'] );
$at = array_search( 'woocommerce-product-data', $ids, true );
if ( false === $at ) {
return $order;
}
array_splice( $ids, $at + 1, 0, array( self::BOX_ID ) );
$order['normal'] = implode( ',', $ids );
return $order;
}
public static function render( $post ) {
$type = (string) $post->post_type;
$row = AIEO_SEO_Store::get( $type, (int) $post->ID );
$row = is_array( $row ) ? $row : array();
$primary = array();
if ( ! empty( $row['primary_term'] ) ) {
$decoded = json_decode( (string) $row['primary_term'], true );
if ( is_array( $decoded ) ) {
$primary = $decoded;
}
}
wp_nonce_field( self::NONCE, self::NONCE );
echo '
';
self::render_score_panel( $type, (int) $post->ID );
foreach ( self::hierarchical_taxonomies( $type ) as $tax => $tx_obj ) {
$assigned = get_the_terms( $post, $tax );
if ( empty( $assigned ) || is_wp_error( $assigned ) ) {
continue;
}
$current = isset( $primary[ $tax ] ) ? (int) $primary[ $tax ] : 0;
$field = 'aieo_seo_primary_' . $tax;
echo '
';
echo esc_html( sprintf( __( 'Canonical %s', 'ai-eshop-optimizer' ), $tx_obj->labels->singular_name ) );
echo ' ';
echo '';
echo '' . esc_html__( '— First assigned (default) —', 'ai-eshop-optimizer' ) . ' ';
foreach ( $assigned as $term ) {
echo 'term_id, false ) . '>' . esc_html( $term->name ) . ' ';
}
echo '
';
}
echo '
' . esc_html__( 'Focus keyphrase', 'ai-eshop-optimizer' ) . ' ';
echo '
';
echo '
' . esc_html__( 'SEO title', 'ai-eshop-optimizer' ) . ' ';
echo '
';
echo '
' . esc_html__( 'Meta description', 'ai-eshop-optimizer' ) . ' ';
echo '
';
$kw_existing = '';
if ( class_exists( 'AIEO_SEO_Semantic_Cues' ) ) {
$kw_existing = implode( ', ', AIEO_SEO_Semantic_Cues::keywords_from_json( (string) ( $row['keyphrases'] ?? '' ) ) );
}
echo '
' . esc_html__( 'Keywords', 'ai-eshop-optimizer' ) . ' ';
echo ' ';
echo '' . esc_html__( 'Comma-separated. Feeds the AI description, structured data and internal linking — not the dead tag (Google ignores it).', 'ai-eshop-optimizer' ) . '
';
if ( class_exists( 'AIEO_SEO_AI_Assist' ) && AIEO_SEO_AI_Assist::available() ) {
echo '
' . esc_html__( 'Suggest with AI', 'ai-eshop-optimizer' ) . '
';
}
self::render_ours_panel( $post );
echo '
' . esc_html__( 'Canonical URL override', 'ai-eshop-optimizer' ) . ' ';
echo ' ';
echo '' . esc_html__( 'Max 300 characters, so it stays consistent with the product feeds / core vitals. (Longer canonicals still render in the page tag but are not propagated to product_url.)', 'ai-eshop-optimizer' ) . '
';
$aieo_noindex = ! empty( $row['robots_noindex'] ) && isset( $row['robots_default'] ) && ! (int) $row['robots_default'];
echo '
' . esc_html__( 'No-index this page', 'ai-eshop-optimizer' ) . ' ';
echo '
';
if ( class_exists( 'AIEO_SEO_Sitemap_Rules' ) ) {
echo '
' . esc_html__( 'In the sitemap', 'ai-eshop-optimizer' ) . ' ';
echo \Rooster\Admin\Ui::select(
'aieo_seo_sitemap',
array(
'' => __( 'Automatic: the sitemap rules decide', 'ai-eshop-optimizer' ),
'always' => __( 'Always', 'ai-eshop-optimizer' ),
'never' => __( 'Never', 'ai-eshop-optimizer' ),
),
AIEO_SEO_Sitemap_Rules::item_mode( 'post', (int) $post->ID )
);
echo '
';
}
self::render_ours_js();
self::render_ai_js();
echo '
';
}
private static function render_ours_js() {
?>
$title, 'detail' => $detail, 'id' => $id ) );
}
private static function render_score_panel( $type, $id ) {
if ( ! class_exists( 'AIEO_SEO_Score' ) || ! AIEO_SEO_Score::enabled() ) {
return;
}
$r = AIEO_SEO_Score::compute( $type, $id );
$score = (int) $r['score'];
$tone = $score >= 80 ? 'success' : ( $score >= 50 ? 'warning' : 'destructive' );
$title = sprintf( __( 'SEO health: %d / 100', 'ai-eshop-optimizer' ), $score );
$text = '';
if ( 'product' === $type ) {
$text = esc_html( sprintf( __( 'demand %d', 'ai-eshop-optimizer' ), (int) $r['demand'] ) );
}
$kinds = array(
'fail' => array( 'is-fail', 'dashicons-dismiss', __( 'Problem', 'ai-eshop-optimizer' ) ),
'warn' => array( 'is-warn', 'dashicons-warning', __( 'Needs attention', 'ai-eshop-optimizer' ) ),
'pass' => array( 'is-pass', 'dashicons-yes-alt', __( 'OK', 'ai-eshop-optimizer' ) ),
);
$rows = array( 'fail' => '', 'warn' => '', 'pass' => '' );
$issues = 0;
foreach ( (array) $r['breakdown'] as $c ) {
$status = isset( $kinds[ $c['status'] ?? '' ] ) ? (string) $c['status'] : 'warn';
$issues += 'pass' === $status ? 0 : 1;
$hint = (string) ( $c['hint'] ?? '' );
$kind = $kinds[ $status ];
$rows[ $status ] .= ''
. ' '
. '' . esc_html( sprintf( __( '%s:', 'ai-eshop-optimizer' ), $kind[2] ) ) . ' '
. esc_html( (string) $c['label'] )
. ( '' !== $hint ? ' — ' . esc_html( $hint ) . ' ' : '' )
. ' ';
}
if ( 0 === $issues ) {
$text = esc_html__( 'All checks pass.', 'ai-eshop-optimizer' ) . ( '' !== $text ? ' ' . $text : '' );
}
$list = implode( '', $rows );
$detail = '' !== $list ? '' : '';
echo self::notice( $text, $tone, $title, $detail, 'aieo-seo-score' );
}
private static function render_ours_panel( $post ) {
if ( ! class_exists( 'AIEO_SEO_AI_Assist' ) ) {
$f = defined( 'AIEO_DIR' ) ? AIEO_DIR . 'includes/seo/class-aieo-seo-ai-assist.php' : '';
if ( $f && is_file( $f ) ) {
require_once $f;
}
}
if ( ! class_exists( 'AIEO_SEO_AI_Assist' ) || ! method_exists( 'AIEO_SEO_AI_Assist', 'cues' ) ) {
return;
}
$c = AIEO_SEO_AI_Assist::cues( $post );
if ( empty( $c['keywords'] ) && '' === (string) $c['description'] ) {
return;
}
$detail = '';
if ( ! empty( $c['keywords'] ) ) {
$kw_csv = implode( ', ', $c['keywords'] );
$labels = array();
foreach ( $c['keywords'] as $kw ) {
$is_gsc = false;
foreach ( $c['gsc_queries'] as $g ) {
if ( $g['query'] === $kw ) { $is_gsc = true; break; }
}
$labels[] = $is_gsc
? '' . esc_html( $kw ) . ' '
. '' . esc_html__( 'Real Google query for this page', 'ai-eshop-optimizer' ) . ' '
: esc_html( $kw );
}
$detail .= '' . esc_html__( 'Keywords', 'ai-eshop-optimizer' ) . '
' . implode( ', ', $labels );
$detail .= '
' . esc_html__( 'Use these keywords', 'ai-eshop-optimizer' ) . '
';
}
if ( '' !== (string) $c['description'] ) {
$detail .= '' . esc_html__( 'Description draft', 'ai-eshop-optimizer' ) . '
';
$detail .= '' . esc_html( $c['description'] ) . '
';
$detail .= '' . esc_html__( 'Use this description', 'ai-eshop-optimizer' ) . '
';
}
echo ''
. '
' . esc_html__( 'Suggested by Rooster (from your index + Search Console — no AI cost)', 'ai-eshop-optimizer' ) . '
'
. $detail . '
';
}
private static function render_ai_js() {
if ( ! class_exists( 'AIEO_SEO_AI_Assist' ) || ! AIEO_SEO_AI_Assist::available() ) {
return;
}
?>
= 20 ) {
break;
}
}
return implode( ', ', $out );
}
public static function save( $post_id, $post ) {
if ( ! isset( $_POST[ self::NONCE ] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST[ self::NONCE ] ) ), self::NONCE ) ) {
return;
}
if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
$type = (string) $post->post_type;
$post_id = (int) $post_id;
foreach ( self::hierarchical_taxonomies( $type ) as $tax => $tx_obj ) {
$field = 'aieo_seo_primary_' . $tax;
if ( ! isset( $_POST[ $field ] ) ) {
continue;
}
$term_id = (int) $_POST[ $field ];
if ( $term_id > 0 && ! has_term( $term_id, $tax, $post_id ) ) {
$term_id = 0;
}
AIEO_SEO_Store::set_primary_term( $type, $post_id, $tax, $term_id );
}
$fields = array(
'title' => isset( $_POST['aieo_seo_title'] ) ? sanitize_text_field( wp_unslash( $_POST['aieo_seo_title'] ) ) : '',
'description' => isset( $_POST['aieo_seo_description'] ) ? sanitize_textarea_field( wp_unslash( $_POST['aieo_seo_description'] ) ) : '',
'focus_keyphrase' => isset( $_POST['aieo_seo_keyphrase'] ) ? sanitize_text_field( wp_unslash( $_POST['aieo_seo_keyphrase'] ) ) : '',
'canonical_override' => isset( $_POST['aieo_seo_canonical'] ) ? esc_url_raw( wp_unslash( $_POST['aieo_seo_canonical'] ) ) : '',
'robots_noindex' => ! empty( $_POST['aieo_seo_noindex'] ) ? 1 : 0,
);
if ( '' !== $fields['focus_keyphrase'] ) {
$fields['focus_keyphrase_source'] = 'manual';
}
if ( isset( $_POST['aieo_seo_noindex_shown'] ) && (int) $_POST['aieo_seo_noindex_shown'] === $fields['robots_noindex'] ) {
unset( $fields['robots_noindex'] );
} elseif ( 1 === $fields['robots_noindex'] ) {
$fields['robots_default'] = 0;
}
if ( isset( $_POST['aieo_seo_keywords'] ) && class_exists( 'AIEO_SEO_Semantic_Cues' ) ) {
$kw_csv = self::clean_keywords( wp_unslash( $_POST['aieo_seo_keywords'] ) );
$kw_list = '' === $kw_csv ? array() : array_map( 'trim', explode( ',', $kw_csv ) );
$existing = (array) AIEO_SEO_Store::get( $type, $post_id );
$fields['keyphrases'] = AIEO_SEO_Semantic_Cues::keywords_to_json(
(string) ( $existing['keyphrases'] ?? '' ),
$kw_list
);
}
AIEO_SEO_Store::save( $type, $post_id, $fields );
if ( isset( $_POST['aieo_seo_sitemap'] ) && class_exists( 'AIEO_SEO_Sitemap_Rules' ) ) {
$mode = sanitize_key( wp_unslash( $_POST['aieo_seo_sitemap'] ) );
if ( in_array( $mode, array( 'always', 'never' ), true ) ) {
update_post_meta( $post_id, AIEO_SEO_Sitemap_Rules::META, $mode );
} else {
delete_post_meta( $post_id, AIEO_SEO_Sitemap_Rules::META );
}
}
}
}