<?php
if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly

class Rivax_bbpress {

	public static $_instance;

	/**
	 * Instance
	 *
	 * Ensures only one instance of the class is loaded or can be loaded.
	 */
	public static function get_instance() {

		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self();
		}
		return self::$_instance;

	}


	function __construct() {

		if(! class_exists( 'bbPress' )) {
			return;
		}

		add_filter( 'template_include', [$this, 'single_template'] );
		add_filter( 'bbp_default_styles', [$this, 'optimize_load_styles'] );
		add_filter( 'bbp_default_scripts', [$this, 'optimize_load_styles'] );
		add_filter( 'bbp_no_breadcrumb', [$this, 'disable_breadcrumb'] );
	}


	public function single_template( $template ) {
		if ( function_exists( 'is_bbpress' ) && is_bbpress() ) {
			$bbpress_template = locate_template( 'single-bbpress.php' );
			if ( ! empty( $bbpress_template ) ) {
				return $bbpress_template;
			}
		}
		return $template;
	}


	public function optimize_load_styles ( $defaults ) {
		if(rivax_get_option('disable-bbpress-assets')) {
			if ( function_exists( 'is_bbpress' ) && !is_bbpress() ) {
				return [];
			}
		}
		return $defaults;
	}

	public function disable_breadcrumb () {
		if(rivax_get_option('disable-bbpress-breadcrumb')) {
			return true;
		}
		return false;
	}


}

Rivax_bbpress::get_instance();