<?php
/**
 * Core plugin functionality.
 *
 * @package StrategyBlocks
 */

namespace StrategyBlocks\Core;

use StrategyBlocks\ModuleInitialization;

/**
 * Default setup routine
 *
 * @return void
 */
function setup() {
	$n = function ( $function ) {
		return __NAMESPACE__ . "\\$function";
	};

	add_action( 'init', $n( 'i18n' ) );
	add_action( 'init', $n( 'init' ), apply_filters( 'strategy_blocks_init_priority', 4 ) );

	do_action( 'strategy_blocks_loaded' );
}

/**
 * Registers the default textdomain.
 *
 * @return void
 */
function i18n() {
	$locale = apply_filters( 'plugin_locale', get_locale(), 'strategy-blocks' );
	load_textdomain( 'strategy-blocks', WP_LANG_DIR . '/strategy-blocks/strategy-blocks-' . $locale . '.mo' );
	load_plugin_textdomain( 'strategy-blocks', false, plugin_basename( STRATEGY_BLOCKS_PATH ) . '/languages/' );
}

/**
 * Initializes the plugin and fires an action other plugins can hook into.
 *
 * @return void
 */
function init() {
	do_action( 'strategy_blocks_before_init' );

	// If the composer.json isn't found, trigger a warning.
	if ( ! file_exists( STRATEGY_BLOCKS_PATH . 'composer.json' ) ) {
		add_action(
			'admin_notices',
			function () {
				$class = 'notice notice-error';
				/* translators: %s: the path to the plugin */
				$message = sprintf( __( 'The composer.json file was not found within %s. No classes will be loaded.', 'strategy-blocks' ), STRATEGY_BLOCKS_PATH );

				printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
			}
		);
		return;
	}

	ModuleInitialization::instance()->init_classes();
	do_action( 'strategy_blocks_init' );
}

/**
 * Activate the plugin
 *
 * @return void
 */
function activate() {
	// First load the init scripts in case any rewrite functionality is being loaded
	init();
	flush_rewrite_rules();
	add_option( 'strategy_blocks_repo', 'Strategyllcweb/strategy-blocks' );
	add_option( 'strategy_blocks_branch', 'master' );
}

/**
 * Deactivate the plugin
 *
 * Uninstall routines should be in uninstall.php
 *
 * @return void
 */
function deactivate() {
	delete_option( 'strategy_blocks_repo' );
	delete_option( 'strategy_blocks_branch' );
}
