<?php

namespace GemBoards\utils\traits;

trait Template {

	public static function template_path() {
		return apply_filters( 'gemboards/template_path', 'gemboards/' );
	}

	public static function plugin_path() {
		return apply_filters( 'gemboards/plugin_path', GEMBOARDS_ROOT_DIR_PATH );
	}

	public static function get_template_part( $slug, $name = '' ) {
		$template = false;
		if ( $name ) {
			$template = GEMBOARDS_TEMPLATE_DEBUG_MODE ? '' : locate_template(
				array(
					"$slug-$name.php",
					self::template_path() . "$slug-$name.php",
				)
			);

			if ( ! $template ) {
				$fallback = self::plugin_path() . "/templates/$slug-$name.php";
				$template = file_exists( $fallback ) ? $fallback : '';
			}
		}

		if ( ! $template ) {
			// If a template file doesn't exist, look in your-theme/slug.php and your-theme/gemboards/slug.php.
			$template = GEMBOARDS_TEMPLATE_DEBUG_MODE ? '' : locate_template(
				array(
					"$slug.php",
					self::template_path() . "$slug.php",
				)
			);
		}
		// Allow 3rd party plugins to filter a template file from their plugin.
		$template = apply_filters( 'gemboards/get_template_part', $template, $slug, $name );
		if ( $template ) {
			load_template( $template, false );
		}
	}

	public static function locate_template( $template_name, $template_path = '', $default_path = '' ) {
		if ( ! $template_path ) {
			$template_path = self::template_path();
		}

		if ( ! $default_path ) {
			$default_path = self::plugin_path() . 'templates/';
		}

		if ( empty( $template ) ) {
			$template = locate_template(
				array(
					trailingslashit( $template_path ) . $template_name,
					$template_name,
				)
			);
		}

		// Get default template/.
		if ( ! $template || GEMBOARDS_TEMPLATE_DEBUG_MODE ) {
			if ( empty( $cs_template ) ) {
				$template = $default_path . $template_name;
			} else {
				$template = $default_path . $cs_template;
			}
		}

		// Return what we found.
		return apply_filters( 'gemboards/locate_template', $template, $template_name, $template_path );
	}

	public static function get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
		$template = self::locate_template( $template_name, $template_path, $default_path );

		// Allow 3rd party plugin filter template file from their plugin.
		$filter_template = apply_filters( 'gemboards/get_template', $template, $template_name, $args, $template_path, $default_path );

		if ( $filter_template !== $template ) {
			if ( ! file_exists( $filter_template ) ) {
				/* translators: %s template */
				_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( '%s does not exist.', 'gemboards' ), '<code>' . esc_html( $filter_template ) . '</code>' ), '6.9.0' );

				return;
			}
			$template = $filter_template;
		}

		$action_args = array(
			'template_name' => $template_name,
			'template_path' => $template_path,
			'located'       => $template,
			'args'          => $args,
		);

		if ( ! empty( $args ) && is_array( $args ) ) {
			if ( isset( $args['action_args'] ) ) {
				_doing_it_wrong(
					__FUNCTION__,
					esc_html__( 'action_args should not be overwritten when calling gemboards/get_template.', 'gemboards' ),
					'1.0.0'
				);
				unset( $args['action_args'] );
			}

			extract( $args ); // @codingStandardsIgnoreLine
		}

		do_action( 'gemboards/before_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] );

		include $action_args['located'];

		do_action( 'gemboards/after_template_part', $action_args['template_name'], $action_args['template_path'], $action_args['located'], $action_args['args'] );
	}
}
