$label ) {
if ( $key === 'date' ) {
$new['awbseo_score'] = '
SEO';
}
$new[ $key ] = $label;
}
return $new;
}
public static function maybe_add_cpt_column( $post_type, $post_type_object ) {
if ( $post_type_object->show_ui && ! in_array( $post_type, [ 'post', 'page' ], true ) ) {
add_filter( "manage_{$post_type}_posts_columns", [ __CLASS__, 'add_column' ] );
// manage_posts_custom_column (registered in init()) fires for all post types
// including custom ones — no need to re-register render_column here, which
// would cause it to run twice (double badge) on CPT list tables.
add_filter( "manage_edit-{$post_type}_sortable_columns", [ __CLASS__, 'sortable_columns' ] );
}
}
/* ── Column renderer ───────────────────────────────────────────────────── */
public static function render_column( $column, $post_id ) {
if ( $column !== 'awbseo_score' ) {
return;
}
$keyword = AWBSEO_Meta_Manager::get( $post_id, 'focus_keyword' );
$has_title = (bool) AWBSEO_Meta_Manager::get( $post_id, 'title' );
$has_desc = (bool) AWBSEO_Meta_Manager::get( $post_id, 'description' );
// Read cached score — fast, no analysis on list view.
$score = get_post_meta( $post_id, self::META_SCORE, true );
$issues = get_post_meta( $post_id, self::META_ISSUES, true );
$analyzed = $score !== ''; // empty string = never saved, not the same as 0
if ( ! $analyzed ) {
// Not yet cached — show a prompt to save/analyze.
echo '
—
' . esc_html( $keyword ?: 'No keyword' ) . '
Save to score
';
return;
}
$score = (int) $score;
$issues = (int) $issues;
// Colour tier.
if ( ! $keyword ) {
$tier_class = 'awb-score-grey';
$tier_label = 'No keyword';
} elseif ( $score >= 70 ) {
$tier_class = 'awb-score-green';
$tier_label = 'Good';
} elseif ( $score >= 40 ) {
$tier_class = 'awb-score-orange';
$tier_label = 'Improve';
} else {
$tier_class = 'awb-score-red';
$tier_label = 'Poor';
}
$edit_url = get_edit_post_link( $post_id );
echo '';
// Score circle.
echo '
';
echo esc_html( $keyword ? $score : '—' );
echo '';
// Meta info block.
echo '
';
// Focus keyword.
if ( $keyword ) {
echo '' . esc_html( mb_strlen( $keyword ) > 22 ? mb_substr( $keyword, 0, 22 ) . '…' : $keyword ) . '';
} else {
echo 'No keyword';
}
// Mini status pills.
echo '';
echo self::pill( 'T', $has_title, 'Custom SEO title' . ( $has_title ? ' set' : ' missing' ) );
echo self::pill( 'D', $has_desc, 'Meta description' . ( $has_desc ? ' set' : ' missing' ) );
echo self::pill( 'K', (bool) $keyword, 'Focus keyword' . ( $keyword ? ' set' : ' missing' ) );
if ( $issues > 0 ) {
echo '' . (int) $issues . ' ⚠';
}
echo ''; // pills
echo '
'; // meta
echo '
'; // wrap
}
private static function pill( $label, $ok, $title ) {
$class = $ok ? 'awb-pill awb-pill-ok' : 'awb-pill awb-pill-miss';
return '' . esc_html( $label ) . '';
}
/* ── Score caching ─────────────────────────────────────────────────────── */
public static function cache_score( $post_id, $post ) {
// Skip autosaves, revisions, and non-public post types.
if (
( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ||
wp_is_post_revision( $post_id ) ||
! in_array( $post->post_status, [ 'publish', 'draft', 'pending', 'future' ], true )
) {
return;
}
// Run the full analyzer — it's only called on explicit save, not on list view.
$result = AWBSEO_On_Page_Analyzer::analyze( $post_id );
$score = (int) ( $result['score'] ?? 0 );
$issues = count( array_filter(
$result['checks'] ?? [],
fn( $c ) => in_array( $c['status'], [ 'warn', 'fail' ], true )
) );
update_post_meta( $post_id, self::META_SCORE, $score );
update_post_meta( $post_id, self::META_ISSUES, $issues );
}
/* ── Sortable columns ──────────────────────────────────────────────────── */
public static function sortable_columns( $cols ) {
$cols['awbseo_score'] = 'awbseo_score';
return $cols;
}
public static function sort_by_score( $query ) {
if ( ! is_admin() || ! $query->is_main_query() ) {
return;
}
if ( $query->get( 'orderby' ) === 'awbseo_score' ) {
$query->set( 'meta_key', self::META_SCORE );
$query->set( 'orderby', 'meta_value_num' );
}
}
/* ── Bulk action: Calculate SEO Scores ────────────────────────────────── */
public static function register_bulk_action( $actions ) {
$actions['awbseo_calc_scores'] = __( '⚡ Calculate SEO Scores', 'awb-seo' );
return $actions;
}
/**
* Process the bulk action.
*
* WordPress calls this filter after verifying the nonce and iterating $post_ids.
* We run the analyzer on each selected post and redirect back with a count notice.
*
* @param string $redirect_url Current redirect URL (with existing query args).
* @param string $action The bulk action key.
* @param int[] $post_ids Selected post IDs.
* @return string Updated redirect URL.
*/
public static function handle_bulk_action( $redirect_url, $action, $post_ids ) {
if ( $action !== 'awbseo_calc_scores' ) {
return $redirect_url;
}
$processed = 0;
foreach ( $post_ids as $post_id ) {
$post = get_post( $post_id );
if ( ! $post ) {
continue;
}
// Reuse the same analysis path used on save_post.
$result = AWBSEO_On_Page_Analyzer::analyze( $post_id );
$score = (int) ( $result['score'] ?? 0 );
$issues = count( array_filter(
$result['checks'] ?? [],
fn( $c ) => in_array( $c['status'], [ 'warn', 'fail' ], true )
) );
update_post_meta( $post_id, self::META_SCORE, $score );
update_post_meta( $post_id, self::META_ISSUES, $issues );
$processed++;
}
// Pass count back through a query arg so the notice can display it.
return add_query_arg( 'awbseo_scored', $processed, $redirect_url );
}
/** Admin notice shown after the bulk action redirects back. */
public static function bulk_action_notice() {
if ( ! isset( $_REQUEST['awbseo_scored'] ) ) {
return;
}
$count = (int) $_REQUEST['awbseo_scored'];
printf(
'',
sprintf(
/* translators: %d = number of posts scored */
esc_html( _n( 'SEO score calculated for %d post.', 'SEO scores calculated for %d posts.', $count, 'awb-seo' ) ),
$count
)
);
}
}