*/ namespace RankMath\Admin; use RankMath\Helper; use RankMath\Helpers\Str; use RankMath\Helpers\Param; use RankMath\Runner; use RankMath\Traits\Hooker; use RankMath\Admin\Database\Database; defined( 'ABSPATH' ) || exit; /** * Post_Columns class. */ class Post_Columns implements Runner { use Hooker; /** * SEO data. * * @var array */ private $data = []; /** * Register hooks. */ public function hooks() { $this->action( 'admin_init', 'init' ); } /** * Intialize. */ public function init() { if ( ! Helper::has_cap( 'onpage_general' ) ) { return; } $this->register_post_columns(); $this->register_media_columns(); // Column Content. $this->filter( 'rank_math_title', 'get_column_title', 5 ); $this->filter( 'rank_math_description', 'get_column_description', 5 ); $this->filter( 'rank_math_seo_details', 'get_column_seo_details', 5 ); } /** * Register post column hooks. */ private function register_post_columns() { $post_types = Helper::get_allowed_post_types(); foreach ( $post_types as $post_type ) { $this->filter( 'edd_download_columns', 'add_columns', 11 ); $this->filter( "manage_{$post_type}_posts_columns", 'add_columns', 11 ); $this->action( "manage_{$post_type}_posts_custom_column", 'columns_contents', 11, 2 ); $this->filter( "manage_edit-{$post_type}_sortable_columns", 'sortable_columns', 11 ); // Also make them hidden by default. $user_id = get_current_user_id(); $columns_hidden = (array) get_user_meta( $user_id, "manageedit-{$post_type}columnshidden", true ); $maybe_hidden = get_user_meta( $user_id, "manageedit-{$post_type}columnshidden_default", true ); // Continue if default is already set. if ( $maybe_hidden ) { continue; } // Set it to hidden by default. $columns_hidden = array_unique( array_merge( $columns_hidden, [ 'rank_math_title', 'rank_math_description' ] ) ); update_user_meta( $user_id, "manageedit-{$post_type}columnshidden", $columns_hidden ); update_user_meta( $user_id, "manageedit-{$post_type}columnshidden_default", '1' ); } } /** * Register media column hooks. */ private function register_media_columns() { if ( ! Helper::get_settings( 'titles.pt_attachment_bulk_editing' ) ) { return; } $this->filter( 'manage_media_columns', 'add_media_columns', 11 ); $this->action( 'manage_media_custom_column', 'media_contents', 11, 2 ); } /** * Add new columns for SEO title, description and focus keywords. * * @param array $columns Array of column names. * * @return array */ public function add_columns( $columns ) { global $post_type; $current_pt = $post_type; if ( ! $post_type && 'inline-save' === Param::post( 'action' ) ) { $post_id = Param::post( 'post_ID', 0, FILTER_VALIDATE_INT ); $current_pt = get_post_type( $post_id ); } $columns['rank_math_seo_details'] = esc_html__( 'SEO Details', 'rank-math' ); if ( Helper::get_settings( 'titles.pt_' . $current_pt . '_bulk_editing', true ) ) { $columns['rank_math_title'] = esc_html__( 'SEO Title', 'rank-math' ); $columns['rank_math_description'] = esc_html__( 'SEO Desc', 'rank-math' ); } return $columns; } /** * Make the SEO Score column sortable. * * @param array $columns Array of column names. * * @return array */ public function sortable_columns( $columns ) { $columns['rank_math_seo_details'] = 'rank_math_seo_score'; return $columns; } /** * Add new columns for Media Alt & Title. * * @param array $columns Array of column names. * * @return array */ public function add_media_columns( $columns ) { $columns['rank_math_image_title'] = esc_html__( 'Title', 'rank-math' ); $columns['rank_math_image_alt'] = esc_html__( 'Alternative Text', 'rank-math' ); return $columns; } /** * Add content for custom column. * * @param string $column_name The name of the column to display. * @param int $post_id The current post ID. */ public function columns_contents( $column_name, $post_id ) { if ( Str::starts_with( 'rank_math', $column_name ) ) { do_action( $column_name, $post_id ); } } /** * Add content for title column. * * @param int $post_id The current post ID. */ public function get_column_title( $post_id ) { $title = ! empty( $this->data[ $post_id ]['rank_math_title'] ) ? $this->data[ $post_id ]['rank_math_title'] : ''; if ( ! $title ) { $post_type = get_post_type( $post_id ); $title = Helper::get_settings( "titles.pt_{$post_type}_title" ); } ?>
data[ $post_id ]['rank_math_description'] ) ? $this->data[ $post_id ]['rank_math_description'] : ''; if ( ! $description ) { $post_type = get_post_type( $post_id ); $description = has_excerpt( $post_id ) ? '%excerpt%' : Helper::get_settings( "titles.pt_{$post_type}_description" ); } ?> data ) ) { $this->get_seo_data(); } $data = isset( $this->data[ $post_id ] ) ? $this->data[ $post_id ] : []; if ( ! self::is_post_indexable( $post_id ) ) { echo 'N/A'; echo '' . esc_html__( 'No Index', 'rank-math' ) . ''; $this->do_action( 'post/column/seo_details', $post_id, $data, $this->data ); return; } $keyword = ! empty( $data['rank_math_focus_keyword'] ) ? $data['rank_math_focus_keyword'] : ''; $keyword = explode( ',', $keyword )[0]; $is_pillar = ! empty( $data['rank_math_pillar_content'] ) && 'on' === $data['rank_math_pillar_content'] ? true : false; $score = empty( $keyword ) ? false : $this->get_seo_score( $data ); $class = ! $score ? 'no-score' : $this->get_seo_score_class( $score ); $score = $score ? $score . ' / 100' : 'N/A'; ?>