<?php
/**
#_________________________________________________ PLUGIN
Plugin Name: RW Elephant Rental Inventory
Plugin URI: https://www.rwelephant.com/
Description: Gallery displays R.W. Elephant rental inventory on your website.
Version: 2.4.0
Author: R.W. Elephant
Author URI: https://www.rwelephant.com/
Text Domain: rw-elephant-rental-inventory
Domain Path: languages
License: GPL2

#_________________________________________________ LICENSE
Copyright 2012-18 R.W. Elephant (email : info@rwelephant.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

#_________________________________________________ CONSTANTS
*/

if ( ! defined( 'ABSPATH' ) ) {

	exit;

}

if ( ! class_exists( 'RWEGallery' ) ) {

	final class RWEGallery {

		/**
		 * Minimum PHP version
		 *
		 * @var string
		 */
		private $php_min_version = '5.6.0';

		/**
		 * Plugin assets URL
		 *
		 * @var string
		 */
		public static $assets_url;

		/**
		 * Plugin Data
		 *
		 * @var array
		 */
		public static $plugin_data;

		public function __construct( $cur_php_version = PHP_VERSION ) {

			self::$assets_url = plugin_dir_url( __FILE__ ) . 'lib/assets/';

			self::$plugin_data = $this->get_plugin_data();

			require_once __DIR__ . '/constants.php';
			require_once __DIR__ . '/lib/helpers.php';
			require_once __DIR__ . '/lib/blocks/wishlist/index.php';
			require_once __DIR__ . '/lib/blocks/gallery/index.php';
			require_once __DIR__ . '/lib/classes/autoload.php';

			if ( defined( 'WP_CLI' ) && WP_CLI ) {

				\WP_CLI::add_command( 'rwe', '\RWEG\CLI' );

				return;

			}

			add_action( 'plugins_loaded', [ $this, 'i18n' ] );

			add_action( 'wp_enqueue_scripts', [ $this, 'register_assets' ] );
			// Register admin assets so they are available for walkthrough and other admin features.
			add_action( 'admin_enqueue_scripts', [ $this, 'register_admin_assets' ] );

			if ( version_compare( $cur_php_version, $this->php_min_version, '<' ) ) {

				new RWEG\Notice(
					'error',
					sprintf(
						/* translators: 1. The minimum support PHP version for this plugin. */
						__( 'RW Elephant Rental Inventory requires PHP version %s or higher. Please contact your system administrator.', 'rw-elephant-rental-inventory' ),
						esc_html( $this->php_min_version )
					),
					true
				);

			}

		}

		/**
		 * Load languages
		 *
		 * @action plugins_loaded
		 */
		public function i18n() {

			load_plugin_textdomain( 'rw-elephant-rental-inventory', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );

		}

		/**
		 * Get the plugin data from the plugin header in this file
		 *
		 * @return array Data from the plugin header.
		 *
		 * @since 1.0.0
		 */
		public function get_plugin_data() {

			if ( ! function_exists( 'get_plugins' ) ) {

				require_once( ABSPATH . 'wp-admin/includes/plugin.php' );

			}

			$plugin_data = get_plugin_data( __FILE__ );

			$plugin_data['base'] = plugin_basename( __FILE__ );

			return $plugin_data;

		}

		/**
		 * Register plugin assets
		 *
		 * @since 2.3.10
		 *
		 * @action wp_enqueue_scripts
		 */
		public function register_assets() {
			$suffix = SCRIPT_DEBUG ? '' : '.min';
			$rtl    = is_rtl() ? '-rtl' : '';

			// Register frontend styles
			wp_register_style( 'rwe-gallery', RW_ELEPHANT_RENTAL_INVENTORY_URL  . "lib/assets/css/rw-elephant{$rtl}{$suffix}.css", [ rwe_get_last_style() ], RW_ELEPHANT_RENTAL_INVENTORY_VERSION, 'all' );
			wp_register_style( 'rwe-pikaday', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/css/pikaday{$suffix}.css", [ 'rwe-gallery' ], '2.0.0', 'all' );
			wp_register_style( 'rwe-slick', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/css/slick{$suffix}.css", [], '1.8.1', 'all' );
			wp_register_style( 'rwe-slick-lightbox', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/css/slick-lightbox{$suffix}.css", [ 'rwe-slick' ], '0.2.12', 'all' );

			// Register frontend scripts
			wp_register_script( 'rwe-gallery', RW_ELEPHANT_RENTAL_INVENTORY_URL  . "lib/assets/js/rw-elephant{$suffix}.js", [ 'jquery', 'rwe-slick' ], RW_ELEPHANT_RENTAL_INVENTORY_VERSION, true );
			wp_register_script( 'rwe-moment', RW_ELEPHANT_RENTAL_INVENTORY_URL . 'lib/assets/js/moment.min.js', [ 'jquery' ], '2.21.0', true );
			wp_register_script( 'rwe-pikaday', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/js/pikaday{$suffix}.js", [ 'rwe-moment' ], '1.6.1', true );
			wp_register_script( 'rwe-slick', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/js/slick{$suffix}.js", [ 'jquery' ], '1.8.1', true );
			wp_register_script( 'rwe-slick-lightbox', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/js/slick-lightbox{$suffix}.js", [ 'rwe-slick' ], '0.2.12', true );
			wp_register_script( 'rwe-wishlist', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/js/rw-elephant-wishlist{$suffix}.js", [ 'rwe-pikaday' ], RW_ELEPHANT_RENTAL_INVENTORY_VERSION, true );

			// Register backend styles
			wp_register_style( 'rwe-featherlight', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/css/featherlight{$suffix}.css", [], '1.7.9', 'all' );
			wp_register_style( 'rwe-admin', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/css/rw-elephant-admin{$rtl}{$suffix}.css", [ 'rwe-featherlight' ], RW_ELEPHANT_RENTAL_INVENTORY_VERSION, 'all' );

			// Register backend scripts
			wp_register_script( 'rwe-gallery-block-editor', RW_ELEPHANT_RENTAL_INVENTORY_URL . 'lib/blocks/gallery/gallery-block.js', [ 'wp-blocks', 'wp-element', 'wp-components', 'wp-api' ], filemtime( plugin_dir_path( __FILE__ ) . 'lib/blocks/gallery/gallery-block.js' ) );
			wp_register_script( 'rwe-wishlist-block-editor', RW_ELEPHANT_RENTAL_INVENTORY_URL . 'lib/blocks/wishlist/wishlist-block.js', [ 'wp-blocks', 'wp-element', 'wp-components', 'wp-api' ], filemtime( plugin_dir_path( __FILE__ ) . 'lib/blocks/wishlist/wishlist-block.js' ) );
			wp_register_script( 'rwe-settings', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/js/rw-elephant-settings{$suffix}.js", [ 'jquery-ui-sortable', 'wp-color-picker', 'rwe-featherlight' ], false, true );
			wp_register_script( 'rwe-featherlight', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/js/featherlight{$suffix}.js", [], '1.7.9', false );
			wp_register_script( 'rwe-walkthrough', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/js/rw-elephant-walkthrough{$suffix}.js", [ 'rwe-featherlight' ], RW_ELEPHANT_RENTAL_INVENTORY_VERSION, false );
		}

		/**
		 * Register admin plugin assets (for walkthrough and admin pages)
		 */
		public function register_admin_assets() {
			$suffix = SCRIPT_DEBUG ? '' : '.min';
			$rtl    = is_rtl() ? '-rtl' : '';

			// Register backend styles
			wp_register_style( 'rwe-featherlight', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/css/featherlight{$suffix}.css", [], '1.7.9', 'all' );
			wp_register_style( 'rwe-admin', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/css/rw-elephant-admin{$rtl}{$suffix}.css", [ 'rwe-featherlight' ], RW_ELEPHANT_RENTAL_INVENTORY_VERSION, 'all' );

			// Register backend scripts
			wp_register_script( 'rwe-featherlight', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/js/featherlight{$suffix}.js", [], '1.7.9', false );
			wp_register_script( 'rwe-walkthrough', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/js/rw-elephant-walkthrough{$suffix}.js", [ 'rwe-featherlight' ], RW_ELEPHANT_RENTAL_INVENTORY_VERSION, false );
			wp_register_script( 'rwe-settings', RW_ELEPHANT_RENTAL_INVENTORY_URL . "lib/assets/js/rw-elephant-settings{$suffix}.js", [ 'jquery-ui-sortable', 'wp-color-picker', 'rwe-featherlight' ], false, true );
		}

	}

	new RWEGallery();

}
