<?php

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

// Rank Math SEO Pro Plugin
// https://rankmath.com

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

use RankMath\Helper;

if ( ! class_exists( 'PWBE_RankMathPro' ) ) :

final class PWBE_RankMathPro {

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

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

        $gtin  = Helper::get_settings( 'general.gtin', 'gtin8' );
        $label = 'isbn' === $gtin ? esc_html__( 'ISBN', 'rank-math-pro' ) : ( 'mpn' === $gtin ? esc_html__( 'MPN', 'rank-math-pro' ) : esc_html__( 'GTIN', 'rank-math-pro' ) );

        $new_columns[] = array(
            'name' => $label,
            'type' => 'text',
            'table' => 'meta',
            'field' => '_rank_math_gtin_code',
            '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_RankMathPro();

endif;
