<?php

// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) {
    exit;
}

// Rank Math SEO Plugin
// https://wordpress.org/plugins/seo-by-rank-math/

if ( !class_exists( 'RankMath' ) ) {
    return;
}

if ( ! class_exists( 'PWBE_RankMath' ) ) :

final class PWBE_RankMath {

    function __construct() {
        add_filter( 'pwbe_product_columns', array( $this, 'pwbe_product_columns' ) );
    }

    function pwbe_product_columns( $columns ) {
        $new_columns = array();

        $new_columns[] = array(
            'name' => 'SEO Title',
            'type' => 'text',
            'table' => 'meta',
            'field' => 'rank_math_title',
            'readonly' => 'false',
            'visibility' => 'both',
            'sortable' => 'true',
            'views' => array( 'all', 'standard' )
        );

        $new_columns[] = array(
            'name' => 'SEO Focus Keywords',
            'type' => 'text',
            'table' => 'meta',
            'field' => 'rank_math_focus_keyword',
            'readonly' => 'false',
            'visibility' => 'both',
            'sortable' => 'true',
            'views' => array( 'all', 'standard' )
        );

        $new_columns[] = array(
            'name' => 'SEO Description',
            'type' => 'text',
            'table' => 'meta',
            'field' => 'rank_math_description',
            'readonly' => 'false',
            'visibility' => 'both',
            'sortable' => 'true',
            'views' => array( 'all', 'standard' )
        );

        if ( !empty( $new_columns ) ) {
            $start_index = 1;
            foreach ( $columns as $index => $column ) {
                // Insert the new columns after this column.
                if ( $column['field'] === '_sku' ) {
                    $start_index = $index + 1;
                    break;
                }
            }

            array_splice( $columns, $start_index, 0, $new_columns );
        }

        return $columns;
    }
}

new PWBE_RankMath();

endif;
