<?php

class Rover_IDX_THIRDPARTY {

	private Rover_IDX $rover_idx;

	function __construct(Rover_IDX $rover_idx)
		{
		$this->rover_idx					= $rover_idx;

#		if ( !function_exists('is_plugin_active') ) {
#			include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
#			}

		}

	public function no_cache()
		{
		#	Tell caching plugins to back off
		define('DONOTCACHEPAGE', true);		#	W3TC, WP Super Cache, WP Rocket, others
		define('DONOTCACHEOBJECT', true);	#	Object cache
		define('DONOTCACHEDB', true);		#	DB cache

		#	LiteSpeed Cache
		do_action('litespeed_control_set_nocache', 'Rover Control Panel');

		#	WP Rocket
		add_filter('do_rocket_generate_caching_files', '__return_false');

		#	WP Optimize
		add_filter('wpo_can_cache_page', '__return_false');

		#	Batcache WordPress page caching
		if (function_exists( 'batcache_cancel' ))
			batcache_cancel();
		}

	public function integrate()
		{
		#	Make sure third party SEO plugins know about our sitemap(s)

		$wp_settings						= get_option(roverOPTIONS::SEO);
		$domain								= get_site_url();
		$domain_len							= strlen($domain);

		foreach ($this->rover_idx->all_selected_regions as $one_region => $region_slugs)
			{
			if (isset($wp_settings[$one_region]))
				{
				#	Yoast SEO

				if ( defined( 'WPSEO_VERSION' ) ) {

					global $wpseo_sitemaps;
					if (isset($wpseo_sitemaps) && !empty($wpseo_sitemaps)) {

						$wp_settings[$one_region]['url'];

						$wpseo_sitemaps->register_sitemap("CUSTOM_SITEMAP", "CUSTOM_SITEMAP_sitemap_generate");
						}
					}

					#	All in One SEO

				if ( defined( 'AIOSEO_PHP_VERSION_DIR' ) ) {

#					https://aioseo.com/docs/aioseo_sitemap_indexes/
#
#					add_filter( 'aioseo_sitemap_indexes', 'aioseo_add_sitemap_index' );
#					function aioseo_add_sitemap_index( $indexes ) {
#						$indexes[] = [
#							'loc'     => 'https://somedomain.com/custom-sitemap.xml',
#							'lastmod' => aioseo()->helpers->dateTimeToIso8601( '2021-09-08 12:02' ),
#							'count'   => 1000
#							];
#						return $indexes;
#						}

					}

				#	Rank Math SEO

				if ( defined( 'RANK_MATH_VERSION' ) ) {

#					https://rankmath.com/kb/custom-sitemaps/
#
#					add_filter('rank_math/sitemap/providers', function( $external_providers ) {
#						$external_providers['custom'] = new \RankMath\Sitemap\Providers\Custom();
#						return $external_providers;
#						});

					}

				#	The SEO Framework

				if ( defined( 'THE_SEO_FRAMEWORK_PLUGIN_BASE_FILE' ) ) {

#					https://kb.theseoframework.com/kb/filter-reference-for-the-seo-framework/#sitemap-related
#
#					add_filter( 'the_seo_framework_sitemap_additional_urls', function( $custom_urls = []) {
#
#						$custom_urls[ 'https://example.com/somepage' ] = [ 'lastmod' => '2021-12-16 10:58' ];
#
#						return $custom_urls;
#						} );
					}
				}
			}
		}

	public function filters()
		{
		#	Elementor
		if (class_exists( '\Elementor\Plugin' )) {
			add_filter( 'the_content', function( $content ) {
					remove_filter( 'the_content', [ \Elementor\Plugin::instance()->frontend, 'apply_builder_in_content' ], 9 );
					return $content;
				}, 8 );
			}

		#	Yoast SEO

		#############################################################
		#	I M P O R T A N T
		#
		#	Do not use is_plugin_active() on the frontend
		#	That function is not loaded unless plugin.php is included
		#############################################################


		#	https://developer.yoast.com/customization/apis/metadata-api/#generic-presenters

		#	Yoast SEO

		if ( defined( 'WPSEO_VERSION' ) ) {

			add_action( 'template_redirect', [$this, 'remove_wpseo'] );

#			add_filter( 'wpseo_title', '__return_false' );		#	Will stop Yoast SEO from changing the titles.
#			add_filter( 'wpseo_title', '__return_empty_string' );
#			add_filter( 'wpseo_metadesc', '__return_false' );
#
#			add_filter( 'wpseo_opengraph_title', '__return_false' );
#			add_filter( 'wpseo_opengraph_desc', '__return_false' );
#			add_filter( 'wpseo_opengraph_image', '__return_false' );
#			add_filter( 'wpseo_opengraph_url', '__return_false' );
#			add_filter( 'wpseo_canonical', '__return_false' );
#
#			add_filter( 'wpseo_og_article_published_time', '__return_false' );
#			add_filter( 'wpseo_og_article_modified_time', '__return_false' );
#			add_filter( 'wpseo_og_og_updated_time', '__return_false' );
#
#			add_filter( 'wpseo_json_ld_output', '__return_false' );

#			add_filter( 'wpseo_schema_webpage', array($this, 'wpseo_schema_webpage') );
			}

		#	All in One SEO


		#	All in One SEO
		#	https://aioseo.com/docs/aioseo_disable/

		if ( defined( 'AIOSEO_PHP_VERSION_DIR' ) ) {

			add_filter( 'aioseo_disable', function( $details ) {
				return true;
				});

			add_filter( 'aioseo_disable_title_rewrites', function( $disabled ) {
				return true;
				}, $priority = 10);

#			Is this used to notify AIOSEO of our sitemaps?
#			add_filter( 'aioseo_sitemap_indexes', function( $indexes ) {
#
#				$indexes[] = [
#					'loc'     => 'https://somedomain.com/custom-sitemap.xml',
#					'lastmod' => aioseo()->helpers->dateTimeToIso8601( '2021-09-08 12:02' ),
#					'count'   => 1000
#					];
#				return $indexes;
#				});
			}

		#	Rank Math SEO
		#	https://rankmath.com/kb/filters-hooks-api-developer/

		if ( defined( 'RANK_MATH_VERSION' ) ) {

			remove_all_actions( 'rank_math/head' );
			remove_all_actions( 'rank_math/opengraph/facebook' );
			remove_all_actions( 'rank_math/opengraph/twitter' );
			remove_all_actions( 'rank_math/frontend/robots' );

			add_filter('rank_math/frontend/title', '__return_false');
			add_filter('rank_math/frontend/robots', function( $robots ) {

				$robots["index"]			= 'index';

				return $robots;
				});

			add_filter( 'rank_math/redirection/redirect', function ( $do_redirect ) {

				if ( is_404() ) {
					return false;		# prevent redirect
					}

				return $do_redirect;
				});
			}

		#	The SEO Framework

		if ( defined( 'THE_SEO_FRAMEWORK_PLUGIN_BASE_FILE' ) ) {

			#	https://theseoframework.com/docs/api/filters/

			add_filter( 'the_seo_framework_title_from_custom_field', '__return_false', 10, 2 );
			add_filter( 'the_seo_framework_title_from_generation', '__return_false', 10, 2 );

			add_filter( 'the_seo_framework_custom_field_description', '__return_false', 10, 2 );
			add_filter( 'the_seo_framework_generated_description', '__return_false', 10, 2 );


			add_filter( 'the_seo_framework_ogtitle_output', '__return_false' );
			add_filter( 'the_seo_framework_ogdescription_output', '__return_false' );
			add_filter( 'the_seo_framework_ogurl_output', '__return_false' );
			add_filter( 'the_seo_framework_rel_canonical_output', '__return_false', 10, 2 );

			add_filter( 'the_seo_framework_image_details', function( $details ) {
				#	Do not allow SEO Framework to select the og:images
				return array();
				});

			add_filter( 'the_seo_framework_modifiedtime_output', '__return_false' );
			}

		#	SEO Press
		#	https://www.seopress.org/support/hooks/

		if ( defined( 'SEOPRESS_VERSION' ) ) {

			add_action( 'wp_head', 'rover_sp_remove_my_action', 0 );
			function rover_sp_remove_my_action(){
				remove_action( 'wp_head', 'seopress_social_website_option', 1 );//priority matter
				}

			add_filter( 'seopress_titles_canonical', '__return_false' );
			add_filter( 'seopress_titles_desc', '__return_false' );

			add_filter( 'seopress_social_og_title', '__return_false' );
			add_filter( 'seopress_social_og_desc', '__return_false' );
			add_filter( 'seopress_social_og_thumb', '__return_false' );
			add_filter( 'seopress_social_og_url', '__return_false' );
			}

		#	Slim SEO

		if ( defined( 'SLIM_SEO_VERSION' ) ) {
			add_filter( 'slim_seo_canonical_url', '__return_false' );
			}


		#	Squirrly SEO
		#	https://www.seopress.org/support/hooks/

		if ( defined( 'SQ_VERSION' ) ) {

			add_filter( 'sq_title', function( $desc ) {			return null;		}, 11);

			add_filter( 'sq_canonical', function( $desc ) {		return null;		}, 11);

			add_filter( 'sq_description', function( $desc ) {	return null;		}, 11);

			add_filter( 'sq_open_graph', function( $og ) {		return array();		}, 11);

			add_filter( 'sq_twitter_card', function( $tw ) {	return array();		}, 11);
			}

		#	Contact Form 7
		if ( defined( 'WPCF7_VERSION' ) ) {

#			add_filter( 'parse_request', '__return_false' );

			remove_action( 'wpcf7_init', 'action_wpcf7_init', 10, 0 );

			remove_action( 'wpcf7_init', 'wpcf7_add_form_tag_acceptance' );
			remove_action( 'wpcf7_init', 'wpcf7_add_form_tag_checkbox' );
			remove_action( 'wpcf7_init', 'wpcf7_add_form_tag_count' );
			remove_action( 'wpcf7_init', 'wpcf7_add_form_tag_date' );
			remove_action( 'wpcf7_init', 'wpcf7_add_form_tag_file' );
			remove_action( 'wpcf7_init', 'wpcf7_add_form_tag_hidden' );
			remove_action( 'wpcf7_init', 'wpcf7_add_form_tag_number' );
			remove_action( 'wpcf7_init', 'wpcf7_add_form_tag_quiz' );
			remove_action( 'wpcf7_init', 'wpcf7_add_form_tag_captcha' );

			remove_action( 'wpcf7_init', 'wpcf7_add_form_tag_response' );
			remove_action( 'wpcf7_init', 'wpcf7_add_form_tag_select' );
			remove_action( 'wpcf7_init', 'wpcf7_add_form_tag_submit' );
			remove_action( 'wpcf7_init', 'wpcf7_add_form_tag_text' );
			remove_action( 'wpcf7_init', 'wpcf7_add_form_tag_textarea' );
			remove_action( 'wpcf7_init', 'wpcf7_sendinblue_register_service' );
			remove_action( 'wpcf7_init', 'wpcf7_recaptcha_register_service' );
			remove_action( 'wpcf7_init', 'wpcf7_recaptcha_add_form_tag_recaptcha' );
			remove_action('wpcf7_init', 'wpcf7_stripe_register_service');
			remove_action( 'init', 'wpcf7_init', 9 );

			remove_action( 'plugins_loaded', 'wpcf7', 10, 0 );

			add_filter( 'wpcf7_form_action_url', '__return_false' );
			}

		#	Jetpack

		if ( defined( 'JETPACK__VERSION' ) ) {
			add_filter( 'jetpack_enable_open_graph', '__return_false' );
			add_filter( 'jetpack_photon_skip_for_url', function( $skip, $image_url ) {
				if (strpos($image_url, 'roveridx.com') !== false) {
					#	Tell Jetpack / wp.com to NOT replace roveridx.com image urls
					#	They try to force the wrong width / height
					#	https://i0.wp.com/cdn-all.roveridx.com/rover-images-pillarnine/A2271858/12182025062830_300_0.webp?resize=100%2C25&ssl=1

					return true; // Skip CDN for this exact URL
					}
				return $skip;
				}, 10, 2 );
			}

		#	WooCommerce
		if ( defined( 'WC_VERSION' ) ) {
			if ( class_exists( 'WooCommerce', false ) ) {
				remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10);
				remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);
				}
			}

		#	Genesis themes

		if ( function_exists( 'genesis_grid_loop' ) )		{
			remove_action( 'wp_head', 'genesis_robots_meta');
			remove_action( 'wp_head', 'genesis_canonical', 5);
			remove_action( 'genesis_meta','genesis_robots_meta' );
			remove_action( 'genesis_after_post_content', 'genesis_post_meta' );
			}

		#	x theme

		if ( function_exists( 'x_get_content_layout' ) )	{
			remove_filter( 'the_content', 'sharing_display', (19 + 1) );
			add_filter( 'sharing_show', '__return_false', 9999 );
			}

		}

	public function remove_wpseo()
		{
		$front_end					= YoastSEO()->classes->get( Yoast\WP\SEO\Integrations\Front_End_Integration::class );

		remove_action( 'wpseo_head', [ $front_end, 'present_head' ], -9999 );
		}
	}

?>