<?php
/**
 * Admin order-status UI restrictions.
 *
 * @package WOSR
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class WOSR_Functions {
	public function __construct() {
		add_filter( 'wc_order_statuses', array( $this, 'filter_order_statuses' ), 999999 );
		add_filter( 'woocommerce_admin_order_preview_actions', array( $this, 'filter_preview_actions' ), 999999, 2 );
		add_filter( 'woocommerce_admin_order_actions', array( $this, 'filter_order_status_actions' ), 999999, 2 );
		add_filter( 'views_woocommerce_page_wc-orders', array( $this, 'restrict_statuses_overview' ) );
		add_filter( 'views_edit-shop_order', array( $this, 'restrict_statuses_overview' ) );
	}

	public function filter_order_statuses( $statuses ) {
		if ( ! is_admin() ) {
			return $statuses;
		}
		$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
		if ( ! $screen || ! in_array( $screen->id, array( 'shop_order', 'woocommerce_page_wc-orders' ), true ) ) {
			return $statuses;
		}
		$order_id = 0;
		if ( isset( $_GET['id'] ) && ! is_array( $_GET['id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only screen context.
			$order_id = absint( wp_unslash( $_GET['id'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		} elseif ( isset( $_GET['post'] ) && ! is_array( $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only screen context.
			$order_id = absint( wp_unslash( $_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		}
		$order = $order_id ? wc_get_order( $order_id ) : false;
		if ( ! $order ) {
			return $statuses;
		}
		$allowed = WOSR_Permissions::get_allowed_statuses( $order, wp_get_current_user() );
		if ( null === $allowed ) {
			return $statuses;
		}
		$current = 'wc-' . $order->get_status();
		foreach ( array_keys( $statuses ) as $status ) {
			if ( $status !== $current && ! in_array( $status, $allowed, true ) ) {
				unset( $statuses[ $status ] );
			}
		}
		return $statuses;
	}

	public function filter_preview_actions( $actions, $order ) {
		return $this->filter_actions( $actions, $order );
	}

	public function filter_order_status_actions( $actions, $order ) {
		return $this->filter_actions( $actions, $order );
	}

	private function filter_actions( $actions, $order ) {
		if ( ! $order instanceof WC_Order ) {
			return $actions;
		}
		foreach ( $actions as $key => $action ) {
			if ( isset( $action['actions'] ) && is_array( $action['actions'] ) ) {
				$actions[ $key ]['actions'] = $this->filter_actions( $action['actions'], $order );
				continue;
			}
			$target = WOSR_Permissions::get_action_target_status( $key, is_array( $action ) ? $action : array() );
			if ( $target && ! WOSR_Permissions::status_transition_is_allowed( $order, $target, wp_get_current_user() ) ) {
				unset( $actions[ $key ] );
			}
		}
		return $actions;
	}

	public function restrict_statuses_overview( $views ) {
		$allowed = WOSR_Permissions::get_overview_statuses( wp_get_current_user() );
		if ( null === $allowed ) {
			return $views;
		}
		foreach ( array_keys( $views ) as $status ) {
			if ( 'all' !== $status && ! in_array( sanitize_key( $status ), $allowed, true ) ) {
				unset( $views[ $status ] );
			}
		}
		return $views;
	}
}

new WOSR_Functions();
