<?php
/**
 * Compatibility functions for third party plugins.
 *
 * @since 2.0.0
 * @package GeoDirectory
 */

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


class GeoDir_Compatibility {

	/**
	 * Initiate the compatibility class.
	 */
	public static function init() {

		/*######################################################
		Yoast (WP SEO)
		######################################################*/
		add_filter( 'option_wpseo_taxonomy_meta', array(__CLASS__,'wpseo_taxonomy_meta'), 10, 2 );

		/*######################################################
		Disqus (comments system) :: If Disqus plugin is active, do some fixes to show on blogs but no on GD post types
		######################################################*/
		if ( function_exists( 'dsq_can_replace' ) ) {
			remove_filter( 'comments_template', array(__CLASS__,'dsq_comments_template') );
			add_filter( 'comments_template', array(__CLASS__,'dsq_comments_template'), 100 );
			add_filter( 'pre_option_disqus_active', array(__CLASS__,'disqus_active'), 10, 1 );
		}

		/*######################################################
		JetPack :: Disable JetPack comments on GD post types.
		######################################################*/
		add_action( 'plugins_loaded', array(__CLASS__,'jetpack') );

		/*######################################################
		Ninja Forms :: Add our own form tags.
		######################################################*/
		add_action( 'ninja_forms_loaded', array(__CLASS__,'ninja_forms'));

		/*######################################################
		Primer (theme) :: Fix single page title.
		######################################################*/
		add_filter('primer_the_page_title',array(__CLASS__,'primer_title'));



		// after_setup_theme checks
		add_action( 'after_setup_theme', array(__CLASS__,'for_later_checks') );

	}

	/**
	 * Add our own tags to ninja forms.
	 */
	public static function ninja_forms(){
		require_once( GEODIRECTORY_PLUGIN_DIR . 'includes/widgets/class-geodir-widget-ninja-forms.php' );
		Ninja_Forms()->merge_tags[ 'geodirectory' ] = new GeoDir_Ninja_Forms_MergeTags();
	}

	/**
	 * Theme single template compatibility check.
	 *
	 * @param string $theme
	 *
	 * @return mixed|string
	 */
	public static function theme_single_template($theme = ''){


		if(!$theme){
			$theme = get_template();
		}

		$themes = array(
			'twentyseventeen'   => 'single.php',
		);

		return isset($themes[$theme]) ? $themes[$theme] : '';
	}

	/**
	 * Fix details page title, primer theme breaks it.
	 */
	public static function for_later_checks(){
		/*######################################################
		Boss. (BuddyBoss)
		######################################################*/
		if(class_exists('BuddyBoss_Theme')){
			add_action( 'wp_enqueue_scripts', array(__CLASS__,'buddyboss'), 100 );
			add_filter( 'body_class', array(__CLASS__,'buddyboss_body_class') );
		}
	}

	public static function primer_title($title){

		if(geodir_is_page('single')){
			$title = get_the_title();
		}

		return $title;
	}


	
	/**
	 * Add category meta image in Yoast SEO taxonomy data.
	 *
	 * @since 1.6.9
	 *
	 * @global object $wp_query WordPress Query object.
	 *
	 * @param array $value Taxonomy meta value.
	 * @param string $option Option name.
	 *
	 * @return mixed The taxonomy option value.
	 */
	public static function wpseo_taxonomy_meta( $value, $option = '' ) {
		global $wp_query;

		if ( ! empty( $value ) && ( is_category() || is_tax() ) ) {
			$term = $wp_query->get_queried_object();

			if ( ! empty( $term->term_id ) && ! empty( $term->taxonomy ) && isset( $value[ $term->taxonomy ][ $term->term_id ] ) && geodir_is_gd_taxonomy( $term->taxonomy ) ) {
				$image = geodir_get_cat_image( $term->term_id, true );

				if ( ! empty( $image ) ) {
					$value[ $term->taxonomy ][ $term->term_id ]['wpseo_twitter-image']   = $image;
					$value[ $term->taxonomy ][ $term->term_id ]['wpseo_opengraph-image'] = $image;
				}
			}
		}

		return $value;
	}



	
	/**
	 * Disable Disqus plugin on the fly when visiting GeoDirectory post types.
	 *
	 * @since 1.5.0
	 * @package GeoDirectory
	 *
	 * @param string $disqus_active Hook called before DB call for option so this is empty.
	 *
	 * @return string `1` if active `0` if disabled.
	 */
	public static function disqus_active( $disqus_active ) {
		global $post;
		$all_postypes = geodir_get_posttypes();
	
		if ( isset( $post->post_type ) && is_array( $all_postypes ) && in_array( $post->post_type, $all_postypes ) ) {
			$disqus_active = '0';
		}
	
		return $disqus_active;
	}
	
	

	/**
	 * Disable JetPack comments on GD post types.
	 *
	 * @since 1.6.21
	 */
	public static function jetpack() {
		//only run if jetpack installed
		if ( defined( 'JETPACK__VERSION' ) ) {
			$post_types = geodir_get_posttypes();
			foreach ( $post_types as $post_type ) {
				add_filter( 'jetpack_comment_form_enabled_for_' . $post_type, '__return_false' );
			}
		}
	}


	/*######################################################
		Boss. (BuddyBoss)
	######################################################*/

	/*
	 * Disable their select boxes as they break ours.
	 */
	public static function buddyboss() {
		wp_dequeue_script( 'selectboxes' );
	}

	/**
	 * Add `page` body class on GD pages to help with padding.
	 *
	 * @param $classes
	 *
	 * @return array
	 */
	public static function buddyboss_body_class($classes){

		if ( geodir_is_geodir_page() ) {
			$classes[] = 'page';
		}

		return $classes;
	}

}