<?php
/**
 * Registers legacy extensions that have been merged into EDD (Pro).
 *
 * @package     EDD\Pro\Admin\Extensions
 * @copyright   Copyright (c) 2023, Sandhills Development, LLC
 * @license     https://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       3.5.0
 */

namespace EDD\Pro\Admin\Extensions;

/**
 * Class Legacy
 *
 * @since 3.5.0
 */
class Legacy extends \EDD\Admin\Extensions\Legacy {

	/**
	 * Deactivates the legacy extension.
	 *
	 * @since 3.1.1
	 * @return void
	 */
	public function manage_legacy_extensions() {
		remove_filter( 'plugin_action_links_easy-digital-downloads/easy-digital-downloads.php', 'edd_plugin_action_links' );
		add_filter( 'plugin_action_links_easy-digital-downloads-pro/easy-digital-downloads.php', 'edd_plugin_action_links', 10, 2 );
		parent::manage_legacy_extensions();
	}

	/**
	 * Removes the activation link from the plugins table.
	 *
	 * @since 3.1.1
	 * @param array  $links       The plugin action links.
	 * @param string $plugin_file The plugin file.
	 * @return array
	 */
	public function update_plugin_links( $links, $plugin_file ) {
		$plugin_file = explode( '/', $plugin_file );
		$slug        = reset( $plugin_file );
		if ( ! array_key_exists( $slug, $this->get_pro_extensions() ) ) {
			return parent::update_plugin_links( $links, $plugin_file );
		}

		$links['activate'] = __( 'Inactive &mdash; Part of EDD (Pro)', 'easy-digital-downloads' );
		if ( 'easy-digital-downloads' === $slug ) {
			$links['activate'] = __( 'Inactive &mdash; You\'ve got EDD (Pro)!', 'easy-digital-downloads' );
		}

		return $links;
	}

	/**
	 * Gets the array of extensions which have been merged into EDD.
	 *
	 * @since 3.1.1
	 *
	 * @return array
	 */
	protected function get_extensions() {
		return array_merge( parent::get_extensions(), $this->get_pro_extensions() );
	}

	/**
	 * Whether the plugin should be deactivated.
	 *
	 * @since 3.1.1
	 * @param string $basename The plugin basename.
	 * @return bool
	 */
	protected static function should_deactivate( $basename ) {
		return 'easy-digital-downloads/easy-digital-downloads.php' === $basename ? false : parent::should_deactivate( $basename );
	}

	/**
	 * Gets extensions replaced by pro functionality.
	 *
	 * @since 3.1.1
	 * @return array
	 */
	private function get_pro_extensions() {
		return array(
			'edd-duplicate-downloads' => array(
				'notification-id' => 'dd-legacy-notice',
				'name'            => 'Duplicate Downloads',
				'basename'        => 'edd-duplicate-downloads/easy-digital-downloads-duplicate-downloads.php',
				'option'          => 'edd_duplicate_downloads_license_active',
			),
			'easy-digital-downloads'  => array(
				'basename' => 'easy-digital-downloads/easy-digital-downloads.php',
			),
			'edd-invoices'            => array(
				'notification-id' => 'invoices-legacy-pro',
				'name'            => 'Invoices',
				'basename'        => 'edd-invoices/edd-invoices.php',
			),
			'edd-campaign-tracker'    => array(
				'notification-id' => 'campaign-tracker-legacy-notice',
				'name'            => 'Campaign Tracker',
				'basename'        => 'edd-campaign-tracker/edd-campaign-tracker.php',
				'on_deactivate'   => array( $this, 'deactivate_campaign_tracker' ),
			),
		);
	}

	/**
	 * Updates the campaign tracker option when Campaign Tracker is deactivated.
	 *
	 * This enables the campaign_tracker setting option when the standalone
	 * plugin was previously active, ensuring continuity for existing users.
	 *
	 * @since 3.6.3
	 * @return void
	 */
	public function deactivate_campaign_tracker() {
		// If the setting is already enabled, do nothing.
		if ( edd_get_option( 'campaign_tracker', false ) ) {
			return;
		}

		// Enable campaign tracking since the standalone plugin was active.
		edd_update_option( 'campaign_tracker', true );
	}
}
