<?php
/**
 * Class UltimaKit_Module_Add_Id_Column_In_Admin_Tables
 *
 * @since 1.0.0
 * @package    UltimaKit
 */

/**
 * Class UltimaKit_Module_Add_Id_Column_In_Admin_Tables
 *
 * This class provides methods to control the display of dashboard widgets based on user preferences.
 * It allows users to selectively hide certain dashboard widgets to streamline their WordPress dashboard
 * experience and improve usability.
 *
 * @since 1.0.0
 */
class UltimaKit_Module_Add_Id_Column_In_Admin_Tables extends UltimaKit_Module_Manager {
	/**
	 *
	 * @var string
	 */
	protected $ID = 'ultimakit_module_add_id_column_in_admin_tables';

	/**
	 * The name of the module.
	 *
	 * @var string
	 */
	protected $name;

	/**
	 * A brief description of what the module does.
	 *
	 * @var string
	 */
	protected $description;

	/**
	 * The pricing plan associated with the module.
	 *
	 * @var string
	 */
	protected $plan = 'free';

	/**
	 * The category of functionality the module falls under.
	 *
	 * @var string
	 */
	protected $category = 'Admin Interface';

	/**
	 * The type of module, indicating its platform or use case.
	 *
	 * @var string
	 */
	protected $type = 'WordPress';

	/**
	 * Flag indicating whether the module is active.
	 *
	 * @var bool
	 */
	protected $is_active;

	/**
	 * URL providing more detailed information about the module.
	 *
	 * @var string
	 */
	protected $read_more_link = 'add-id-column-in-admin-tables-in-wordpress';

	/**
	 * The settings associated with the module, if any.
	 *
	 * @var array
	 */
	protected $settings;

	/**
	 *
	 * Initializes the module with default values for properties and prepares
	 * any necessary setup or hooks into WordPress. This may include setting
	 * initial values, registering hooks, or preparing resources needed for
	 * the module to function properly within WordPress.
	 */
	public function __construct() {
		$this->name        = __( 'Add ID column in admin tables', 'ultimakit-for-wp' );
		$this->description = __( 'Display the post id in the admin in its own column for convenience.', 'ultimakit-for-wp' );
		$this->is_active   = $this->isModuleActive( $this->ID );
		$this->settings    = 'no';
		$this->initializeModule();
	}

	/**
	 * Initializes the specific module within the application.
	 *
	 * This function is responsible for performing the initial setup required to get the module
	 * up and running. This includes registering hooks and filters, enqueing styles and scripts,
	 * and any other preliminary setup tasks that need to be performed before the module can
	 * start functioning as expected.
	 *
	 * It's typically called during the plugin or theme's initialization phase, ensuring that
	 * all module dependencies are loaded and ready for use.
	 *
	 * @return void
	 */
	protected function initializeModule() {
		if ( $this->is_active ) {

			add_action( 'admin_head', array( $this, 'ultimakit_set_column_width' ) );
			add_action(
				'admin_init',
				function () {
					// Get all public post types
					$post_types = get_post_types( array(), 'names' );

					foreach ( $post_types as $post_type ) {
						// Add new column to the posts list
						add_filter( "manage_{$post_type}_posts_columns", array( $this, 'ultimakit_add_post_id_column' ) );

						// Fill the new column with the post ID
						add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'ultimakit_show_post_id_column_data' ), 10, 2 );
					}
				}
			);
		}
	}

	public function ultimakit_set_column_width() {
		echo '<style>
			.column-ultimakit_post_id { width: 90px; }
		</style>';
	}

	public function ultimakit_add_post_id_column( $columns ) {
		// Create a new array with the custom ID column at the beginning
		$new_columns = array( 'ultimakit_post_id' => __( 'ID', 'ultimakit-for-wp' ) );

		// Merge the new array with the existing columns
		$columns = array_merge( $new_columns, $columns );

		return $columns;
	}

	public function ultimakit_show_post_id_column_data( $column, $post_id ) {
		if ( 'ultimakit_post_id' === $column ) {
			echo '<code>' . absint( $post_id ) . '</code>';
		}
	}
}
