<?php
/**
 * Activation Plugn notices for the theme.
 *
 * @since 1.0.0
 * @author CodeManas
 * @copyright 2019 CodeManas. All Rights Reserved !
 */

require_once CODEMANAS_THEME_DIR . 'inc/core/class-tgm-plugin-activation.php';

add_action( 'tgmpa_register', 'octane_register_required_plugins' );

//If PRO VERSION exists remove notice
function octane_check_if_pro_plugin_exists() {
	$plugins = array(
		// This is an example of how to include a plugin from the WordPress Plugin Repository.
		array(
			'name'     => __( 'One Click Demo Importer', 'octane' ),
			'slug'     => 'one-click-demo-import',
			'required' => false,
		),
		array(
			'name'     => __( 'Breadcrumb NavXT', 'octane' ),
			'slug'     => 'breadcrumb-navxt',
			'required' => false,
		),
		array(
			'name'     => __( 'Contact Form 7', 'octane' ),
			'slug'     => 'contact-form-7',
			'required' => false,
		),
		array(
			'name'     => __( 'Mailpoet', 'octane' ),
			'slug'     => 'mailpoet',
			'required' => false,
		),
		array(
			'name'     => __( 'WooCommerce', 'octane' ),
			'slug'     => 'woocommerce',
			'required' => false,
		),
	);

	return $plugins;
}

/**
 * Register the required plugins for this theme.
 */
function octane_register_required_plugins() {

	$plugins = octane_check_if_pro_plugin_exists();

	$config = array(
		'id'           => 'octane',
		'default_path' => CODEMANAS_THEME_DIR . 'assets/plugins/',
		'menu'         => 'tgmpa-install-plugins',
		'parent_slug'  => 'octane-theme',
		'capability'   => 'edit_theme_options',
		'has_notices'  => true,
		'dismissable'  => true,
		'dismiss_msg'  => '',
		'is_automatic' => false,
	);

	tgmpa( $plugins, $config );
}

if ( ! function_exists( 'octane_after_import' ) ) :
	/**
	 * Import theme options after import of content widgets and plugins are completed.
	 *
	 * @param $selected_import
	 */
	function octane_after_import( $selected_import ) {

		$site_options = array(
			'nav_menu_locations'                   => 'primary-menu',
			'show_on_front'                        => 'page',
			'page_on_front'                        => 'Homepage',
			'page_for_posts'                       => 'Blog',
		);

		octane_import_theme_options( $site_options );
	}

	add_action( 'pt-ocdi/after_import', 'octane_after_import' );
endif;

/**
 * Set imported options to theme
 *
 * @param array $options
 */
function octane_import_theme_options( $options = array() ) {
	if ( ! isset( $options ) ) {
		return;
	}

	foreach ( $options as $option_name => $option_value ) {

		if ( ! empty( $option_value ) ) {

			switch ( $option_name ) {

				case 'page_for_posts':
				case 'page_on_front':
					$page = get_page_by_title( $option_value );
					if ( is_object( $page ) ) {
						update_option( $option_name, $page->ID );
					}
					break;

				// nav menu locations.
				case 'nav_menu_locations':
					$menu_locations = array();

					// Update menu locations.
					if ( isset( $option_value ) ) {

						$term = get_term_by( 'slug', $option_value, 'nav_menu' );
						if ( is_object( $term ) ) {
							$menu_locations[ $option_value ] = $term->term_id;
						}

						set_theme_mod( 'nav_menu_locations', $menu_locations );
					}
					break;

				default:
					update_option( $option_name, $option_value );
					break;
			}
		}
	}
}