<?php
/**
 * The Base
 * The base class for all the classes
 *
 * @package Risen
 */

defined( 'WPINC' ) || exit;

/**
 * Base Class
 */
class Risen_Base {

	/**
	 * Hooks a function on to a specific action
	 *
	 * @param string   $tag             The name of the action to which the $function_to_add is hooked.
	 * @param callable $function_to_add The name of the function you wish to be called.
	 * @param int      $priority        Optional. Used to specify the order in which the functions
	 *                                  associated with a particular action are executed. Default 10.
	 *                                  Lower numbers correspond with earlier execution,
	 *                                  and functions with the same priority are executed
	 *                                  in the order in which they were added to the action.
	 * @param int      $accepted_args   Optional. The number of arguments the function accepts. Default 1.
	 * @return true Will always return true.
	 */
	protected function add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
		return add_action( $tag, [ $this, $function_to_add ], $priority, $accepted_args );
	}

	/**
	 * Hook a function or method to a specific filter action
	 *
	 * @param string   $tag             The name of the filter to hook the $function_to_add callback to.
	 * @param callable $function_to_add The callback to be run when the filter is applied.
	 * @param int      $priority        Optional. Used to specify the order in which the functions
	 *                                  associated with a particular action are executed. Default 10.
	 *                                  Lower numbers correspond with earlier execution,
	 *                                  and functions with the same priority are executed
	 *                                  in the order in which they were added to the action.
	 * @param int      $accepted_args   Optional. The number of arguments the function accepts. Default 1.
	 * @return true
	 */
	protected function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
		return add_filter( $tag, [ $this, $function_to_add ], $priority, $accepted_args );
	}

	/**
	 * Removes a function from a specified action hook
	 *
	 * @param string   $tag                The action hook to which the function to be removed is hooked.
	 * @param callable $function_to_remove The name of the function which should be removed.
	 * @param int      $priority           Optional. The priority of the function. Default 10.
	 * @return bool Whether the function is removed.
	 */
	protected function remove_action( $tag, $function_to_remove, $priority = 10 ) {
		return remove_action( $tag, [ $this, $function_to_remove ], $priority );
	}

	/**
	 * Removes a function from a specified filter hook
	 *
	 * @param string   $tag                The filter hook to which the function to be removed is hooked.
	 * @param callable $function_to_remove The name of the function which should be removed.
	 * @param int      $priority           Optional. The priority of the function. Default 10.
	 * @return bool    Whether the function existed before it was removed.
	 */
	protected function remove_filter( $tag, $function_to_remove, $priority = 10 ) {
		return remove_filter( $tag, [ $this, $function_to_remove ], $priority );
	}

	/**
	 * Inject config into class
	 *
	 * @param array $config Array of configuration.
	 */
	protected function config( $config = array() ) {
		if ( empty( $config ) ) {
			return;
		}

		foreach ( $config as $key => $value ) {
			$this->$key = $value;
		}
	}

	/**
	 * Is current page equals this
	 *
	 * @return boolean
	 */
	protected function is_current_page() {
		$page = isset( $_GET['page'] ) && ! empty( $_GET['page'] ) ? $_GET['page'] : false;
		return $page === $this->id;
	}

	/**
	 * Get path for includes directory.
	 *
	 * @return string
	 */
	public static function includes_path() {
		return get_parent_theme_file_path( 'includes/' );
	}

	/**
	 * Get uri for includes directory.
	 *
	 * @return string
	 */
	public static function includes_uri() {
		return get_parent_theme_file_uri( 'includes/' );
	}

	/**
	 * Get path for admin directory.
	 *
	 * @return string
	 */
	public static function admin_path() {
		return get_parent_theme_file_path( 'includes/admin/' );
	}

	/**
	 * Get uri for admin directory.
	 *
	 * @param  string $file File to access.
	 * @return string
	 */
	public static function admin_uri( $file = '' ) {
		return get_parent_theme_file_uri( 'includes/admin/' . $file );
	}
}

if ( ! function_exists( 'risen_action' ) ) :

	/**
	 * Do action with risen as prefix
	 */
	function risen_action() {

		$args = func_get_args();

		if ( ! isset( $args[0] ) || empty( $args[0] ) ) {
			return;
		}

		$action = 'risen_' . $args[0];
		unset( $args[0] );

		$args = array_merge( array(), $args );

		do_action_ref_array( $action, $args );
	}

endif;

if ( ! function_exists( 'risen_filter' ) ) :

	/**
	 * Apply filter with risen as prefix
	 */
	function risen_filter() {

		$args = func_get_args();

		if ( ! isset( $args[0] ) || empty( $args[0] ) ) {
			return;
		}

		$action = 'risen_' . $args[0];
		unset( $args[0] );

		$args = array_merge( array(), $args );

		return apply_filters_ref_array( $action, $args );
	}

endif;
