<?php
/**
 * Main plugin coordinator.
 *
 * @package GustavoPortfolioCore
 */

defined( 'ABSPATH' ) || exit;

/**
 * Coordinates the plugin modules.
 */
final class GSO_Portfolio_Core {
	/**
	 * Whether hooks have already been registered.
	 *
	 * @var bool
	 */
	private static $initialized = false;

	/**
	 * Register plugin hooks.
	 *
	 * @return void
	 */
	public static function init() {
		if ( self::$initialized ) {
			return;
		}

		self::$initialized = true;

		add_action( 'plugins_loaded', array( __CLASS__, 'load_textdomain' ) );

		GSO_Projects::init();
		GSO_Portfolio_Settings::init();
		GSO_Seeder::init();

		add_action( 'widgets_init', array( __CLASS__, 'register_widgets' ) );
	}

	/**
	 * Load translations.
	 *
	 * @return void
	 */
	public static function load_textdomain() {
		load_plugin_textdomain(
			'gustavoo-portfolio-core',
			false,
			dirname( plugin_basename( GSO_PORTFOLIO_CORE_FILE ) ) . '/languages'
		);
	}

	/**
	 * Register bundled widgets.
	 *
	 * @return void
	 */
	public static function register_widgets() {
		if ( class_exists( 'Gustavao_Portfolio_Newsletter_Widget' ) ) {
			return;
		}

		register_widget( 'GSO_Newsletter_Widget' );
	}

	/**
	 * Run activation tasks.
	 *
	 * @return void
	 */
	public static function activate() {
		GSO_Projects::register_content_types();
		GSO_Portfolio_Settings::ensure_defaults();
		GSO_Seeder::activate();

		flush_rewrite_rules();
	}

	/**
	 * Refresh rewrite rules without deleting content or settings.
	 *
	 * @return void
	 */
	public static function deactivate() {
		flush_rewrite_rules();
	}
}
