<?php
/**
 * Demo importer related functions
 *
 * @package Meridian One
 * @since 1.0
 */

/**
 * Table Of Contents
 *
 * meridian_one_importer_scripts ( Enqueue scripts and styles )
 * meridian_one_importer_display ( Display importer )
 * meridian_one_add_widgets_to_sidebar ( Add widgets to sidebars )
 * meridian_one_add_media_to_library ( Add images to media library )
 */
	
// include the AJAX
include get_parent_theme_file_path() . '/inc/importer/ajax.php';

/**
 * Enqueue scripts and styles
 *
 * @since 1.0
 */
function meridian_one_importer_scripts( $hook ) {

	// if getting started page
	if ( $hook == 'appearance_page_meridian-one-getting-started' ) {

		// theme
		$theme = wp_get_theme();

		// styles
		wp_enqueue_style( 'meridian-one-importer-style', get_parent_theme_file_uri() . '/inc/importer/css/main.css', array(), $theme->get( 'Version' ) );

		// scripts
		wp_enqueue_script( 'meridian-one-importer-js', get_parent_theme_file_uri() . '/inc/importer/js/main.js', array( 'jquery' ), $theme->get( 'Version' ) );			
		wp_localize_script( 'meridian-one-importer-js', 'MeridianOneImporterAjax', array( 'ajaxurl' => esc_url( admin_url( 'admin-ajax.php' ) ) ) );

	}

} add_action( 'admin_enqueue_scripts', 'meridian_one_importer_scripts' );

/**
 * Display importer
 *
 * @since 1.0
 */
function meridian_one_importer_display() {

	?>

	<div class="meridian-one-importer">
		
		<div class="meridian-one-importer-inner">

			<?php if ( defined( 'MERIDIAN_ONE_FEATURES_VERSION' ) ) : ?>

				<div class="meridian-one-importer-row">
					<div class="meridian-one-importer-progress">
						<div class="meridian-one-importer-progress-item" data-meridian-one-func-name="install-nav-menus"><span><?php esc_html_e( 'Setting up navigation menus&hellip;', 'meridian-one' ); ?></span> <strong><?php esc_html_e( 'done', 'meridian-one' ); ?></strong></div>
						<div class="meridian-one-importer-progress-item" data-meridian-one-func-name="install-home-page"><span><?php esc_html_e( 'Setting up home page&hellip;', 'meridian-one' ); ?></span> <strong><?php esc_html_e( 'done', 'meridian-one' ); ?></strong></div>
						<div class="meridian-one-importer-progress-item" data-meridian-one-func-name="install-customizer"><span><?php esc_html_e( 'Setting up customizer values&hellip;', 'meridian-one' ); ?></span> <strong><?php esc_html_e( 'done', 'meridian-one' ); ?></strong></div>
						<div class="meridian-one-importer-progress-item" data-meridian-one-func-name="install-widgets"><span><?php esc_html_e( 'Setting up homepage widgets&hellip;', 'meridian-one' ); ?></span> <strong><?php esc_html_e( 'done', 'meridian-one' ); ?></strong></div>
					</div><!-- .meridian-one-importer-progress -->
				</div><!-- .meridian-one-importer-row -->

				<div class="meridian-one-importer-all" style="clear:both;">
					<div class="meridian-one-importer-button-all">
						<a href="#" class="button button-primary meridian-one-importer-all-hook"><?php esc_html_e( 'OK. Import demo content.', 'meridian-one' ); ?></a>
					</div><!-- .meridian-one-importer-button -->
				</div><!-- .meridian-one-importer-all -->

			<?php else : ?>

				<?php esc_html_e( 'Plugin "Meridian One Features" must be activated first.', 'meridian-one' ); ?>

			<?php endif; ?>

		</div><!-- .meridian-one-importer-inner -->

	</div>

	<?php

}

/**
 * Add widgets to sidebars
 * 
 * @since 1.0
 */
function meridian_one_add_widgets_to_sidebar( $add_to_sidebar = array(), $ignore_sidebar_with_content = true ){

	if ( is_user_logged_in() && current_user_can( 'manage_options' ) ) {

		// stop if no widgets supplied
		if ( empty( $add_to_sidebar ) ) { return; }

		// get current widgets
		$sidebar_options = get_option('sidebars_widgets');

		// loop through sidebars
		foreach ( $add_to_sidebar as $sidebar_id => $widgets ) {

			// do not add if sidebar has widgets
			if ( ! empty( $sidebar_options[$sidebar_id] ) && $ignore_sidebar_with_content) { continue; }

			// loop through new widgets in sidebar
			foreach ( $widgets as $index => $widget ) {

				$widget_id_base = $widget['id_base'];
				$widget_instance  = $widget['instance'];

				$widget_instances = get_option('widget_'.$widget_id_base);

				if( ! is_array( $widget_instances ) ) {
					$widget_instances = array();
				}

				$count = count($widget_instances) + 1;

				$sidebar_options[$sidebar_id][] = $widget_id_base.'-'.$count;

				$widget_instances[$count] = $widget_instance;

				// save options
				update_option('widget_'.$widget_id_base,$widget_instances);

			} 

		} 

		// save options
		update_option('sidebars_widgets',$sidebar_options);  

	}

}

/**
 * Add images to media library
 *
 * @since 1.0
 */
function meridian_one_add_media_to_library( $url = false ) {

	if ( is_user_logged_in() && current_user_can( 'manage_options' ) ) {

		// stop if no URL supplied
		if ( ! $url ) return;

		// download image
		$tmp = download_url( $url );
		$post_id = 0;
		$desc = 'Placeholder';
		$file_array = array();

		// Set variables for storage
		// fix file filename for query strings
		preg_match('/[^\?]+\.(jpg|jpe|jpeg|gif|png)/i', $url, $matches);
		$file_array['name'] = 'placeholder.jpg';
		$file_array['tmp_name'] = $tmp;

		// If error storing temporarily, unlink
		if ( is_wp_error( $tmp ) ) {
			unlink($file_array['tmp_name']);
			$file_array['tmp_name'] = '';
		}

		// do the validation and storage stuff
		$id = media_handle_sideload( $file_array, $post_id, $desc );

		// If error storing permanently, unlink
		if ( is_wp_error($id) ) {
			unlink($file_array['tmp_name']);
			return false;
		}

		// return ID
		return $id;

	}

} 