<?php
namespace LWS\Adminpanel\EditList;
if( !defined( 'ABSPATH' ) ) exit();

/** Allows you to add filter input or link to the editlist.
 * It is up to you to apply in your EditListSource::read implementation (looking at $_POST or $_GET...)
 * This class provide only a way to display it in rightfull place.
 * Do not insert your <input>, if any, in a <form>. It will be created on-the-fly.
 *
 * You can extends this class and overload input function
 * or provide a CALLABLE to return your html code. */
class Filter
{
	protected $_callback = null;
	protected $_content  = null;
	protected $_class    = '';

	/** The filter inputs.
	 *	@return string with the form content.
	 * @note use class lws-input-enter-submit on a <input>
	 * to allow validation by pressing enter key without submit button. */
	function input($above=true)
	{
		if( !is_null($this->_callback) && is_callable($this->_callback) )
			return call_user_func($this->_callback, $above);
		else if( !is_null($this->_content) && is_string($this->_content) )
			return $this->_content;
		else
			return "";
	}

	/** The filter will be defined by a callback function
	 *  @param $callable a php CALLABLE which will provide the html code.
	 * @return Filter instance */
	static function callback($callable, $class='')
	{
		$inst = new Filter($class);
		$inst->_callback = $callable;
		return $inst;
	}

	/** The filter is provided as is. Good for simple static html code.
	 * @return Filter instance */
	static function content($html, $class='')
	{
		$inst = new Filter($class);
		$inst->_content = $html;
		return $inst;
	}

	/** provided for convenience.
	 * @return string build a url to apply filter with given arguments.
	 * @param $getArgs is an array of (variable_name => value),
	 * this should be read as $_GET in your custom EditListSource::read implementation.
	 * @note cannot be used with EditListFilter::content since we must be at display step to know data. */
	static function url($getArgs=array())
	{
		if( isset($_REQUEST['page']) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			$getArgs['page'] = \sanitize_text_field(wp_unslash($_REQUEST['page'])); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		if( isset($_REQUEST['tab']) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			$getArgs['tab'] = \sanitize_text_field(wp_unslash($_REQUEST['tab'])); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		return add_query_arg($getArgs, admin_url('/admin.php'));
	}

	function __construct($class='')
	{
		$this->_callback = null;
		$this->_content = null;
		$this->_class = $class;
	}

	function cssClass()
	{
		$c = "lws-editlist-filter";
		if( !empty($this->_class) )
			$c .= (' ' . $this->_class);
		return $c;
	}

}
