<?php

class PeepSo3_Compatibility_Auto_Updates {

	private static $instance;

	public static function get_instance()
	{
		return isset(self::$instance) ? self::$instance : self::$instance = new self;
	}

	private function __construct() {

		// We intentionally do NOT hide the auto-updates link (see c1baf5b8 / PeepSo#4141).
		// The link only appears for a PeepSo plugin that currently has a pending update
		// (WP only shows the toggle for "update-supported" plugins). It is harmless: the
		// two site-option filters below strip PeepSo plugins from `auto_update_plugins` on
		// both read and write, so auto-updates can never actually run. Hiding the link left
		// an empty/broken-looking column, so return '' is left disabled on purpose.
		add_filter( 'plugin_auto_update_setting_html', function ( $html, $plugin_file, $plugin_data ) {

			if ( stristr( $plugin_file, 'peepso' ) ) {
				//return ''; // intentionally disabled — see note above
			}

			return $html;

		}, PHP_INT_MAX, 4 );

		// Remove any PeepSo plugins when the auto_update_plugins value is written
		// Overrides enabling via bulk actions
		add_filter( 'pre_update_site_option_auto_update_plugins', function ( $value ) {
			return self::remove_peepso($value);
		}, PHP_INT_MAX );

		// Remove any PeepSo plugins when the auto_update_plugins value is accessed
		// Overrides enabling when PeepSo is inactive
		add_filter('site_option_auto_update_plugins', function( $value ) {
			return self::remove_peepso( $value );
		}, PHP_INT_MAX);
	}

	private function remove_peepso($value) {

		if ( is_array( $value ) && count( $value ) ) {
			foreach ( $value as $k => $v ) {
				if ( stristr( $v, 'peepso' ) ) {
					unset( $value[ $k ] );
				}
			}
		}

		return $value;
	}
}

if(!defined('PEEPSO_DISABLE_COMPATIBILITY_AUTO_UPDATES')) {
    PeepSo3_Compatibility_Auto_Updates::get_instance();
}