<?php
/**
 * Admin: Payouts Screen Options
 *
 * @package    AffiliateWP
 * @subpackage Admin/Payouts
 * @copyright  Copyright (c) 2016, Sandhills Development, LLC
 * @license    http://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since      1.9
 */

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

require_once AFFILIATEWP_PLUGIN_DIR . 'includes/admin/payouts/class-list-table.php';

/**
 * Adds per-page screen option to the Payouts list table.
 *
 * @since 1.9
 */
function affwp_payouts_screen_options() {

	$screen = affwp_get_current_screen();

	if ( $screen !== 'affiliate-wp-payouts' ) {
		return;
	}

	if ( isset( $_GET['payout_id'] ) ) {

		add_screen_option(
			'per_page',
			array(
				'label'   => __( 'Number of referrals per page:', 'affiliate-wp' ),
				'option'  => 'affwp_edit_referrals_per_page',
				'default' => 30,
			)
		);

	} else {

		add_screen_option(
			'per_page',
			array(
				'label'   => __( 'Number of payouts per page:', 'affiliate-wp' ),
				'option'  => 'affwp_edit_payouts_per_page',
				'default' => 30,
			)
		);

	}

	/*
	 * Instantiate the list table to make the columns array available to screen options.
	 *
	 * If the 'view_payout' action is set, don't instantiate. Instantiating in sub-views
	 * creates conflicts in the screen option column controls if another list table is being
	 * displayed.
	 */
	if ( empty( $_REQUEST['action'] )
	     || ( ! empty( $_REQUEST['action'] ) && 'view_payout' !== $_REQUEST['action'] )
	) {
		new AffWP_Payouts_Table;
	}

	// The Actions column holds the per-row controls, not data; don't let it be hidden.
	affwp_lock_actions_column( 'affiliate-wp-payouts' );

	/**
	 * Fires at the end of the Payouts screen options callback.
	 *
	 * @since 1.9
	 *
	 * @param \WP_Screen $screen Current screen.
	 */
	do_action( 'affwp_payouts_screen_options', $screen );

}

/**
 * Renders per-page screen option value for the Payouts list table.
 *
 * @since 1.9
 *
 * @param bool|int $status Screen option value.
 * @param string   $option The option name.
 * @param int      $value  The number of rows to use.
 * @return bool|int Screen option value.
 */
function affwp_payouts_set_screen_option( $status, $option, $value ) {

	if ( 'affwp_edit_payouts_per_page' === $option ) {
		update_user_meta( get_current_user_id(), $option, $value );

		return $value;
	}

	return $status;

}
add_filter( 'set-screen-option', 'affwp_payouts_set_screen_option', 10, 3 );

/**
 * Default hidden columns on the Payouts list table.
 *
 * The `owner` / "Generated By" column is low-signal for the scan case
 * (most stores have one admin, so 95%+ of rows share the same value)
 * and the data is already surfaced in the payout drawer. Default-hide
 * it so new users get a tighter list, while keeping the Screen Options
 * toggle in place for admins who genuinely want the column.
 *
 * Runs only when the current user has no explicit column preferences
 * saved (WP core falls back to this filter when `manage{screen}columnshidden`
 * user meta is not set). Existing users' preferences are untouched.
 *
 * @since 2.33.0
 *
 * @param string[]   $hidden Default hidden columns.
 * @param \WP_Screen $screen Current screen.
 * @return string[] Updated default hidden columns.
 */
function affwp_payouts_default_hidden_columns( $hidden, $screen ) {
	if ( empty( $screen->id ) || false === strpos( $screen->id, 'affiliate-wp-payouts' ) ) {
		return $hidden;
	}

	$hidden   = (array) $hidden;
	$hidden[] = 'owner';

	return array_values( array_unique( $hidden ) );
}
add_filter( 'default_hidden_columns', 'affwp_payouts_default_hidden_columns', 10, 2 );
