<?php

/*
  Plugin Name: Hangit
  Plugin URI: http://www.velvary.com.au/vanilla-beans/wordpress/hangit
  Description: Addon for products and posts that would benefit from their featured image being dragged and resized onto background images
Version: 3.1.0
  Author: vsmash
  Author URI: http://www.velvary.com.au
  License: GPLv2
  Text Domain: hangit
  Domain Path: /languages
 */

// If this file is called directly, abort.
if (!defined('WPINC')) {
    die;
}

if (!defined('VBEANHANGIT_PLUGIN_DIR')) {
    define('VBEANHANGIT_PLUGIN_DIR', \plugin_dir_path(__FILE__));
}
if (!defined('VBEANHANGIT_PLUGIN_URL')) {
    define('VBEANHANGIT_PLUGIN_URL', \plugin_dir_url(__FILE__));
}
if (!defined('VBEANHANGIT_PLUGIN_FILE')) {
    define('VBEANHANGIT_PLUGIN_FILE', __FILE__);
}
if (!defined('VBEANHANGIT_PLUGIN_VERSION')) {
define('VBEANHANGIT_PLUGIN_VERSION','3.1.0');
}

if (!defined('VBEANHANGIT_BACKGROUNDS')) {
    define('VBEANHANGIT_BACKGROUNDS', 'louvre.jpg|The Louvre|Inside the Louvre in Paris,greenbedroom.jpg|Apartment Room|Apartment Room background for hangit plugin,living-room-wall-colors-photos.jpg|Living Room|Orange Living Room,at-t-stadium.jpg|AT&T Stadium|AT&T Stadium - Arlington Texas,whiteloungeroom.jpg|White Loungeroom setting|Background example for hangit,Mexican_highway.jpg|Mexican Highway|Sign on a Mexican Highway');
}

$dbug = (\site_url() == 'https://vanillabeans.test' || \site_url() == 'http://vanillabeans.test');

if (!defined('VBEANHANGIT_DBUG')) {
    define('VBEANHANGIT_DBUG', $dbug);
}

if(!function_exists('vbean_hangit_splitbackgrounds')){
    function vbean_hangit_splitbackgrounds() {
        $rooms = array();
        $roomstring = VBEANHANGIT_BACKGROUNDS;
        $roomarray = explode(",", $roomstring);
        foreach ($roomarray as $room) {
            $roomdetails = explode("|", $room);
            if (count($roomdetails) > 0) {
                $rooms[] = $roomdetails[0];  // Just get the filename
            }
        }
        return $rooms;
    }
}


/* ===========================================
  Define Includes
  =========================================== */
$includes = array(
    'functions.php'
);

$frontend_includes = array(
    'wall-placement.php'
);


$adminincludes = array(
    'settings.php'
);

/* ===========================================
  Load Includes
  =========================================== */
// Common
foreach ($includes as $include) {
    require_once( dirname(__FILE__) . '/inc/' . $include );
}
if (\is_admin()) {
    //load admin part
    foreach ($adminincludes as $admininclude) {
        require_once( \dirname(__FILE__) . '/inc/admin/' . $admininclude );
    }
} else {
    //load front part
    foreach ($frontend_includes as $include) {
        require_once( \dirname(__FILE__) . '/inc/' . $include );
    }
}

/**
 * Load plugin text domain for translations
 */
function vbean_hangit_load_textdomain() {
    \load_plugin_textdomain('hangit', false, dirname(\plugin_basename(__FILE__)) . '/languages');
}
\add_action('plugins_loaded', 'vbean_hangit_load_textdomain');

// Enqueue scripts and styles
function vbean_hangit_enqueue_scripts() {
    // CSS
    \wp_enqueue_style('hangit-style', VBEANHANGIT_PLUGIN_URL . 'assets/showroom.css', array(), VBEANHANGIT_PLUGIN_VERSION);

    // JavaScript
    \wp_enqueue_script('jquery'); // We still need basic jQuery
    \wp_enqueue_script('html2canvas', VBEANHANGIT_PLUGIN_URL . 'assets/html2canvas.min.js', array('jquery'), '1.4.1', true);
    \wp_enqueue_script('hangit-vanilla-drag', VBEANHANGIT_PLUGIN_URL . 'assets/vanilla-drag.js', array('jquery'), VBEANHANGIT_PLUGIN_VERSION, true);
    \wp_enqueue_script('hangit-frame-controls', VBEANHANGIT_PLUGIN_URL . 'assets/frame-controls.js', array('jquery'), VBEANHANGIT_PLUGIN_VERSION, true);
    \wp_enqueue_script('hangit-local-background', VBEANHANGIT_PLUGIN_URL . 'assets/local-background.js', array('jquery'), VBEANHANGIT_PLUGIN_VERSION, true);
    \wp_enqueue_script('hangit-print', VBEANHANGIT_PLUGIN_URL . 'assets/print.js', array('jquery', 'html2canvas'), VBEANHANGIT_PLUGIN_VERSION, true);

    // Debug script if needed
    \wp_enqueue_script('hangit_dbug_vanillabean', VBEANHANGIT_PLUGIN_URL . 'assets/clog.js', array('jquery'), VBEANHANGIT_PLUGIN_VERSION);
}
\add_action('wp_enqueue_scripts', 'vbean_hangit_enqueue_scripts');

\add_action('admin_menu', 'vbean_hangit_create_menu');







if (!function_exists('vbean_hangit_create_menu')) {






    function vbean_hangit_create_menu() {


        if (empty($GLOBALS['admin_page_hooks']['vanillabeans-settings'])) {
            //create new top-level menu
            \add_menu_page('Vanilla Beans', 'Vanilla Beans', 'administrator', 'vanillabeans-settings', 'VanillaBeans\LiveSettings', VBEANHANGIT_PLUGIN_URL . 'vicon.png', 4);
        }



        $vbean_hook = \add_submenu_page('vanillabeans-settings', 'Hangit', 'Hangit', 'administrator', 'hangit-settings', 'VanillaBeans\hangit\SettingsPage');


        //call register settings function
        \add_action('admin_init', 'VanillaBeans\hangit\RegisterSettings');
    }

}

if(!function_exists('hangit_add_settings_link')){
function hangit_add_settings_link($links) {
    $settings_link = '<a href="admin.php?page=hangit-settings">' . \__("Settings", 'hangit') . '</a>';
    \array_push($links, $settings_link);
    return $links;
}
}
$plugin = \plugin_basename(__FILE__);
\add_filter("plugin_action_links_$plugin", 'hangit_add_settings_link');
