<?php 
/**
 * WP-Admin functions.
 *
 * @package Sunset
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}


add_action('admin_enqueue_scripts', 'sunset_admin_scripts');
/**
 * Admin scripts and styles
 */
function sunset_admin_scripts() {
	
	if ( ! did_action( 'wp_enqueue_media' ) ) {
		wp_enqueue_media();
	}

    wp_enqueue_script( 'sunset-admin', get_template_directory_uri() . '/assets/js/admin.js', array( 'jquery' ), null, true );
}

add_action( 'wp_nav_menu_item_custom_fields', 'sunset_add_media_to_menu_item', 10, 5 );
/**
 * Add media button for uploading custom image for each menu item
 *
 * @param int      $item_id Menu item ID.
 * @param WP_Post  $item    Menu item data object.
 * @param int      $depth   Depth of menu item. Used for padding.
 * @param stdClass $args    An object of menu item arguments.
 * @param int      $id      Nav menu ID.
 */

function sunset_add_media_to_menu_item ($item_id, $item, $depth, $args, $id) {
    $image_url = get_post_meta($item_id, '_sunset_menu_image_url', true) ?: '';
    echo '<div class="menu-item-actions description-wide">';
    echo '<p>';
	echo '<button type="button" class="sunset-upload-menu-image-button button" id="sunset-upload-menu-image-button-'.$item_id.'">'.__("Upload menu image", 'my-sunset').'</button>';
    echo '<input type="hidden" class="sunset-menu-image-url" name="sunset-menu-image-url['.$item_id.']" value="'.$image_url.'" />';
    echo '</p>';
    echo '<div><img '.($image_url ? '' : 'style="display: none;"').' src="'.$image_url.'" alt width="200" height="auto" /></div>';
    echo '<span class="plugins"><a '.($image_url ? '' : 'style="display: none;"').' class="delete sunset-delete-menu-image-button" href="#">'.__("Delete menu image", 'my-sunset').'</a></span>';
    echo '</div>';
}

add_action('wp_update_nav_menu_item', 'sunset_update_menu_meta', 10, 2);
/**
 * Update menu item meta (save menu item's image url)
 */
function sunset_update_menu_meta($menu_id, $menu_item_db_id) {
	if ( isset($_POST['sunset-menu-image-url'][$menu_item_db_id]) ) {
        update_post_meta($menu_item_db_id, '_sunset_menu_image_url', $_POST['sunset-menu-image-url'][$menu_item_db_id]);
    }	
}

add_action('admin_notices', 'sunset_no_plugin_admin_notice');
/**
 * Add notice to wp-admin if no sunset-core plugin enabled
 */
function sunset_no_plugin_admin_notice() {
	if ( !function_exists( 'sunset_core_block_assets' ) ){
        $plugin_url = "https://wordpress.org/plugins/sunset-core/";
		echo '<div data-dismissible="disable-done-notice-forever" class="notice notice-warning is-dismissible">
			<p>'.sprintf(__('Please install and enable <a href="%1$s" target="_blank">sunset-core plugin</a> to use 100&#37; of this theme power','my-sunset'), $plugin_url).'&nbsp;💪🚀</p>
		</div>';
	}
    if ( !class_exists( 'OCDI_Plugin' ) ){
        $plugin_url = "https://wordpress.org/plugins/one-click-demo-import/";
        echo '<div data-dismissible="disable-done-notice-forever" class="notice notice-warning is-dismissible">
            <p>'.sprintf(__('If you want to install demo content - please install <a href="%1$s" target="_blank">One Click Demo Import</a> plugin.','my-sunset'), $plugin_url).'</p>
        </div>';
    }
    echo '<div data-dismissible="disable-done-notice-forever" class="notice notice-warning is-dismissible">
        <p>'.sprintf(__('Theme Documentation is available <a href="%1$s" target="_blank">here</a>.','my-sunset'), 'https://sunset.bogdan.kyiv.ua/blog/theme-documentation/').'</p>
    </div>';
}