<?php

namespace WP_science\ThirdParty\Hostings;

use WpeCommon;

/**
 * Compatibility class for WP Engine.
 *
 * @since 3.6.1
 */
class WPEngine extends AbstractNoCacheHost {
	/**
	 * Array of events this subscriber wants to listen to.
	 *
	 * @since 3.6.1
	 *
	 * @return array
	 */
	public static function get_subscribed_events() {
		return [
			'science_varnish_field_settings'           => 'varnish_addon_title',
			'science_display_input_varnish_auto_purge' => 'return_false',
			'science_cache_mandatory_cookies'          => [ 'return_empty_array', PHP_INT_MAX ],
			'admin_init'                              => 'run_science_bot_after_wpengine',
			'science_set_wp_cache_constant'            => 'return_false',
			'do_science_generate_caching_files'        => 'return_false',
			'after_science_clean_domain'               => 'clean_wpengine',
			'science_buffer'                           => [ 'add_footprint', 50 ],
			'science_disable_htaccess'                 => 'return_true',
			'science_generate_advanced_cache_file'     => 'return_false',
		];
	}

	/**
	 * Changes the text on the Varnish one-click block.
	 *
	 * @since 3.6.1
	 *
	 * @param array $settings Field settings data.
	 *
	 * @return array modified field settings data.
	 */
	public function varnish_addon_title( $settings ) {
		$settings['varnish_auto_purge']['title'] = sprintf(
			// Translators: %s = Hosting name.
			__( 'Your site is hosted on %s, we have enabled Varnish auto-purge for compatibility.', 'science' ),
			'WP Engine'
		);

		return $settings;
	}

	/**
	 * Run WP science preload bot after purged the Varnish cache via WP Engine Hosting.
	 *
	 * @since 3.6.1
	 */
	public function run_science_bot_after_wpengine() {
		if ( ! wpe_param( 'purge-all' ) ) {
			return;
		}

		if ( ! science_has_constant( 'PWP_NAME' ) ) {
			return;
		}

		if ( ! check_admin_referer( science_get_constant( 'PWP_NAME' ) . '-config' ) ) {
			return;
		}

		if ( ! current_user_can( 'science_preload_cache' ) ) {
			return;
		}

		// Preload cache.
		run_science_bot();
		run_science_sitemap_preload();
	}

	/**
	 * Call the cache server to purge the cache with WP Engine hosting.
	 *
	 * @since 3.6.1
	 */
	public function clean_wpengine() {
		if ( method_exists( 'WpeCommon', 'purge_memcached' ) ) {
			WpeCommon::purge_memcached();
		}

		if ( method_exists( 'WpeCommon', 'purge_varnish_cache' ) ) {
			WpeCommon::purge_varnish_cache();
		}
	}

	/**
	 * Add WP science footprint on Buffer.
	 *
	 * @since 3.6.1
	 *
	 * @param string $buffer HTML content.
	 *
	 * @return string HTML with WP science footprint.
	 */
	public function add_footprint( $buffer ) {
		if ( ! preg_match( '/<\/html>/i', $buffer ) ) {
			return $buffer;
		}

		$footprint  = science_get_constant( 'WP_science_WHITE_LABEL_FOOTPRINT' )
			? "\n" . '<!-- Optimized for great performance'
			: "\n" . '<!-- This website is like a science, isn\'t it? Performance optimized by ' . science_get_constant( 'WP_science_PLUGIN_NAME' ) . '. Learn more: https://wordpress.me';
		$footprint .= ' -->';

		return $buffer . $footprint;
	}
}
